sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 20 | "errors" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 21 | "regexp" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 22 | "strconv" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 23 | "time" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 24 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 26 | empty "github.com/golang/protobuf/ptypes/empty" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/common/log" |
| 28 | kafka "github.com/opencord/voltha-go/kafka" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 29 | pb "github.com/opencord/voltha-protos/go/afrouter" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 30 | cmn "github.com/opencord/voltha-protos/go/common" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 31 | ic "github.com/opencord/voltha-protos/go/inter_container" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 32 | vpb "github.com/opencord/voltha-protos/go/voltha" |
| 33 | "golang.org/x/net/context" |
| 34 | "google.golang.org/grpc" |
| 35 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 | "k8s.io/client-go/kubernetes" |
| 37 | "k8s.io/client-go/rest" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | type configConn struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 41 | Server string `json:"Server"` |
| 42 | Cluster string `json:"Cluster"` |
| 43 | Backend string `json:"Backend"` |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 44 | connections map[string]connection |
| 45 | } |
| 46 | |
| 47 | type connection struct { |
| 48 | Name string `json:"Connection"` |
| 49 | Addr string `json:"Addr"` |
| 50 | Port uint64 `json:"Port"` |
| 51 | } |
| 52 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 53 | type volthaPod struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 54 | name string |
| 55 | ipAddr string |
| 56 | node string |
| 57 | devIds map[string]struct{} |
| 58 | cluster string |
| 59 | backend string |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 60 | connection string |
| 61 | } |
| 62 | |
| 63 | type podTrack struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 64 | pod *volthaPod |
| 65 | dn bool |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 66 | } |
| 67 | |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 68 | var nPods int = 6 |
| 69 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 70 | // Topic is affinityRouter |
| 71 | // port: 9092 |
| 72 | |
| 73 | func newKafkaClient(clientType string, host string, port int, instanceID string) (kafka.Client, error) { |
| 74 | |
| 75 | log.Infow("kafka-client-type", log.Fields{"client": clientType}) |
| 76 | switch clientType { |
| 77 | case "sarama": |
| 78 | return kafka.NewSaramaClient( |
| 79 | kafka.Host(host), |
| 80 | kafka.Port(port), |
| 81 | kafka.ConsumerType(kafka.GroupCustomer), |
| 82 | kafka.ProducerReturnOnErrors(true), |
| 83 | kafka.ProducerReturnOnSuccess(true), |
| 84 | kafka.ProducerMaxRetries(6), |
| 85 | kafka.NumPartitions(3), |
| 86 | kafka.ConsumerGroupName(instanceID), |
| 87 | kafka.ConsumerGroupPrefix(instanceID), |
| 88 | kafka.AutoCreateTopic(false), |
| 89 | kafka.ProducerFlushFrequency(5), |
| 90 | kafka.ProducerRetryBackoff(time.Millisecond*30)), nil |
| 91 | } |
| 92 | return nil, errors.New("unsupported-client-type") |
| 93 | } |
| 94 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 95 | func k8sClientSet() *kubernetes.Clientset { |
| 96 | // creates the in-cluster config |
| 97 | config, err := rest.InClusterConfig() |
| 98 | if err != nil { |
| 99 | panic(err.Error()) |
| 100 | } |
| 101 | // creates the clientset |
| 102 | clientset, err := kubernetes.NewForConfig(config) |
| 103 | if err != nil { |
| 104 | panic(err.Error()) |
| 105 | } |
| 106 | |
| 107 | return clientset |
| 108 | } |
| 109 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 110 | func connect(addr string) (*grpc.ClientConn, error) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 111 | for ctr := 0; ctr < 100; ctr++ { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 112 | log.Debugf("Trying to connect to %s", addr) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 113 | conn, err := grpc.Dial(addr, grpc.WithInsecure()) |
| 114 | if err != nil { |
| 115 | log.Debugf("Attempt to connect failed, retrying %v:", err) |
| 116 | } else { |
| 117 | log.Debugf("Connection succeeded") |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 118 | return conn, err |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 119 | } |
| 120 | time.Sleep(10 * time.Second) |
| 121 | } |
| 122 | log.Debugf("Too many connection attempts, giving up!") |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 123 | return nil, errors.New("Timeout attempting to conect") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 126 | func getVolthaPods(cs *kubernetes.Clientset, coreFilter *regexp.Regexp) []*volthaPod { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 127 | var rtrn []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 128 | |
| 129 | pods, err := cs.CoreV1().Pods("").List(metav1.ListOptions{}) |
| 130 | if err != nil { |
| 131 | panic(err.Error()) |
| 132 | } |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 133 | //log.Debugf("There are a total of %d pods in the cluster\n", len(pods.Items)) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 134 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 135 | for _, v := range pods.Items { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 136 | if v.Namespace == "voltha" && coreFilter.MatchString(v.Name) { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 137 | log.Debugf("Namespace: %s, PodName: %s, PodIP: %s, Host: %s\n", v.Namespace, v.Name, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 138 | v.Status.PodIP, v.Spec.NodeName) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 139 | // Only add the pod if it has an IP address. If it doesn't then it likely crashed and |
| 140 | // and is still in the process of getting re-started. |
| 141 | if v.Status.PodIP != "" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 142 | rtrn = append(rtrn, &volthaPod{name: v.Name, ipAddr: v.Status.PodIP, node: v.Spec.NodeName, |
| 143 | devIds: make(map[string]struct{}), backend: "", connection: ""}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 144 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | return rtrn |
| 148 | } |
| 149 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 150 | func reconcilePodDeviceIds(pod *volthaPod, ids map[string]struct{}) bool { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 151 | var idList cmn.IDs |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 152 | for k, _ := range ids { |
| 153 | idList.Items = append(idList.Items, &cmn.ID{Id: k}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 154 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 155 | conn, err := connect(pod.ipAddr + ":50057") |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 156 | defer conn.Close() |
sslobodr | 6c1689c | 2019-01-24 07:31:15 -0500 | [diff] [blame] | 157 | if err != nil { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 158 | log.Debugf("Could not query devices from %s, could not connect", pod.name) |
| 159 | return false |
| 160 | } |
| 161 | client := vpb.NewVolthaServiceClient(conn) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 162 | _, err = client.ReconcileDevices(context.Background(), &idList) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 163 | if err != nil { |
| 164 | log.Error(err) |
| 165 | return false |
| 166 | } |
| 167 | |
| 168 | return true |
| 169 | } |
| 170 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 171 | func queryPodDeviceIds(pod *volthaPod) map[string]struct{} { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 172 | var rtrn map[string]struct{} = make(map[string]struct{}) |
| 173 | // Open a connection to the pod |
| 174 | // port 50057 |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 175 | conn, err := connect(pod.ipAddr + ":50057") |
sslobodr | 6c1689c | 2019-01-24 07:31:15 -0500 | [diff] [blame] | 176 | if err != nil { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 177 | log.Debugf("Could not query devices from %s, could not connect", pod.name) |
| 178 | return rtrn |
| 179 | } |
| 180 | defer conn.Close() |
| 181 | client := vpb.NewVolthaServiceClient(conn) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 182 | devs, err := client.ListDeviceIds(context.Background(), &empty.Empty{}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 183 | if err != nil { |
| 184 | log.Error(err) |
| 185 | return rtrn |
| 186 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 187 | for _, dv := range devs.Items { |
| 188 | rtrn[dv.Id] = struct{}{} |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return rtrn |
| 192 | } |
| 193 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 194 | func queryDeviceIds(pods []*volthaPod) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 195 | for pk, _ := range pods { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 196 | // Keep the old Id list if a new list is not returned |
| 197 | if idList := queryPodDeviceIds(pods[pk]); len(idList) != 0 { |
| 198 | pods[pk].devIds = idList |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 199 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 203 | func allEmpty(pods []*volthaPod) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 204 | for k, _ := range pods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 205 | if len(pods[k].devIds) != 0 { |
| 206 | return false |
| 207 | } |
| 208 | } |
| 209 | return true |
| 210 | } |
| 211 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 212 | func rmPod(pods []*volthaPod, idx int) []*volthaPod { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 213 | return append(pods[:idx], pods[idx+1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 216 | func groupIntersectingPods1(pods []*volthaPod, podCt int) ([][]*volthaPod, []*volthaPod) { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 217 | var rtrn [][]*volthaPod |
| 218 | var out []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 219 | |
| 220 | for { |
| 221 | if len(pods) == 0 { |
| 222 | break |
| 223 | } |
| 224 | if len(pods[0].devIds) == 0 { // Ignore pods with no devices |
| 225 | ////log.Debugf("%s empty pod", pd[k].pod.name) |
| 226 | out = append(out, pods[0]) |
| 227 | pods = rmPod(pods, 0) |
| 228 | continue |
| 229 | } |
| 230 | // Start a pod group with this pod |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 231 | var grp []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 232 | grp = append(grp, pods[0]) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 233 | pods = rmPod(pods, 0) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 234 | //log.Debugf("Creating new group %s", pd[k].pod.name) |
| 235 | // Find the peer pod based on device overlap |
| 236 | // It's ok if one isn't found, an empty one will be used instead |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 237 | for k, _ := range pods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 238 | if len(pods[k].devIds) == 0 { // Skip pods with no devices |
| 239 | //log.Debugf("%s empty pod", pd[k1].pod.name) |
| 240 | continue |
| 241 | } |
| 242 | if intersect(grp[0].devIds, pods[k].devIds) == true { |
| 243 | //log.Debugf("intersection found %s:%s", pd[k].pod.name, pd[k1].pod.name) |
| 244 | if grp[0].node == pods[k].node { |
| 245 | // This should never happen |
| 246 | log.Errorf("pods %s and %s intersect and are on the same server!! Not pairing", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 247 | grp[0].name, pods[k].name) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 248 | continue |
| 249 | } |
| 250 | grp = append(grp, pods[k]) |
| 251 | pods = rmPod(pods, k) |
| 252 | break |
| 253 | |
| 254 | } |
| 255 | } |
| 256 | rtrn = append(rtrn, grp) |
| 257 | //log.Debugf("Added group %s", grp[0].name) |
| 258 | // Check if the number of groups = half the pods, if so all groups are started. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 259 | if len(rtrn) == podCt>>1 { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 260 | // Append any remaining pods to out |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 261 | out = append(out, pods[0:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 262 | break |
| 263 | } |
| 264 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 265 | return rtrn, out |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 266 | } |
| 267 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 268 | func unallocPodCount(pd []*podTrack) int { |
| 269 | var rtrn int = 0 |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 270 | for _, v := range pd { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 271 | if v.dn == false { |
| 272 | rtrn++ |
| 273 | } |
| 274 | } |
| 275 | return rtrn |
| 276 | } |
| 277 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 278 | func sameNode(pod *volthaPod, grps [][]*volthaPod) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 279 | for _, v := range grps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 280 | if v[0].node == pod.node { |
| 281 | return true |
| 282 | } |
| 283 | if len(v) == 2 && v[1].node == pod.node { |
| 284 | return true |
| 285 | } |
| 286 | } |
| 287 | return false |
| 288 | } |
| 289 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 290 | func startRemainingGroups1(grps [][]*volthaPod, pods []*volthaPod, podCt int) ([][]*volthaPod, []*volthaPod) { |
| 291 | var grp []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 292 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 293 | for k, _ := range pods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 294 | if sameNode(pods[k], grps) { |
| 295 | continue |
| 296 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 297 | grp = []*volthaPod{} |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 298 | grp = append(grp, pods[k]) |
| 299 | pods = rmPod(pods, k) |
| 300 | grps = append(grps, grp) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 301 | if len(grps) == podCt>>1 { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 302 | break |
| 303 | } |
| 304 | } |
| 305 | return grps, pods |
| 306 | } |
| 307 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 308 | func hasSingleSecondNode(grp []*volthaPod) bool { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 309 | var srvrs map[string]struct{} = make(map[string]struct{}) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 310 | for k, _ := range grp { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 311 | if k == 0 { |
| 312 | continue // Ignore the first item |
| 313 | } |
| 314 | srvrs[grp[k].node] = struct{}{} |
| 315 | } |
| 316 | if len(srvrs) == 1 { |
| 317 | return true |
| 318 | } |
| 319 | return false |
| 320 | } |
| 321 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 322 | func addNode(grps [][]*volthaPod, idx *volthaPod, item *volthaPod) [][]*volthaPod { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 323 | for k, _ := range grps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 324 | if grps[k][0].name == idx.name { |
| 325 | grps[k] = append(grps[k], item) |
| 326 | return grps |
| 327 | } |
| 328 | } |
| 329 | // TODO: Error checking required here. |
| 330 | return grps |
| 331 | } |
| 332 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 333 | func removeNode(grps [][]*volthaPod, item *volthaPod) [][]*volthaPod { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 334 | for k, _ := range grps { |
| 335 | for k1, _ := range grps[k] { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 336 | if grps[k][k1].name == item.name { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 337 | grps[k] = append(grps[k][:k1], grps[k][k1+1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 338 | break |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | return grps |
| 343 | } |
| 344 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 345 | func groupRemainingPods1(grps [][]*volthaPod, pods []*volthaPod) [][]*volthaPod { |
| 346 | var lgrps [][]*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 347 | // All groups must be started when this function is called. |
| 348 | // Copy incomplete groups |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 349 | for k, _ := range grps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 350 | if len(grps[k]) != 2 { |
| 351 | lgrps = append(lgrps, grps[k]) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Add all pairing candidates to each started group. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 356 | for k, _ := range pods { |
| 357 | for k2, _ := range lgrps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 358 | if lgrps[k2][0].node != pods[k].node { |
| 359 | lgrps[k2] = append(lgrps[k2], pods[k]) |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | //TODO: If any member of lgrps doesn't have at least 2 |
| 365 | // nodes something is wrong. Check for that here |
| 366 | |
| 367 | for { |
| 368 | for { // Address groups with only a single server choice |
| 369 | var ssn bool = false |
| 370 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 371 | for k, _ := range lgrps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 372 | // Now if any of the groups only have a single |
| 373 | // node as the choice for the second member |
| 374 | // address that one first. |
| 375 | if hasSingleSecondNode(lgrps[k]) == true { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 376 | ssn = true |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 377 | // Add this pairing to the groups |
| 378 | grps = addNode(grps, lgrps[k][0], lgrps[k][1]) |
| 379 | // Since this node is now used, remove it from all |
| 380 | // remaining tenative groups |
| 381 | lgrps = removeNode(lgrps, lgrps[k][1]) |
| 382 | // Now remove this group completely since |
| 383 | // it's been addressed |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 384 | lgrps = append(lgrps[:k], lgrps[k+1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 385 | break |
| 386 | } |
| 387 | } |
| 388 | if ssn == false { |
| 389 | break |
| 390 | } |
| 391 | } |
| 392 | // Now adress one of the remaining groups |
| 393 | if len(lgrps) == 0 { |
| 394 | break // Nothing left to do, exit the loop |
| 395 | } |
| 396 | grps = addNode(grps, lgrps[0][0], lgrps[0][1]) |
| 397 | lgrps = removeNode(lgrps, lgrps[0][1]) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 398 | lgrps = append(lgrps[:0], lgrps[1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 399 | } |
| 400 | return grps |
| 401 | } |
| 402 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 403 | func groupPods1(pods []*volthaPod) [][]*volthaPod { |
| 404 | var rtrn [][]*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 405 | var podCt int = len(pods) |
| 406 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 407 | rtrn, pods = groupIntersectingPods1(pods, podCt) |
| 408 | // There are several outcomes here |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 409 | // 1) All pods have been paired and we're done |
| 410 | // 2) Some un-allocated pods remain |
| 411 | // 2.a) All groups have been started |
| 412 | // 2.b) Not all groups have been started |
| 413 | if len(pods) == 0 { |
| 414 | return rtrn |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 415 | } else if len(rtrn) == podCt>>1 { // All groupings started |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 416 | // Allocate the remaining (presumably empty) pods to the started groups |
| 417 | return groupRemainingPods1(rtrn, pods) |
| 418 | } else { // Some groupings started |
| 419 | // Start empty groups with remaining pods |
| 420 | // each grouping is on a different server then |
| 421 | // allocate remaining pods. |
| 422 | rtrn, pods = startRemainingGroups1(rtrn, pods, podCt) |
| 423 | return groupRemainingPods1(rtrn, pods) |
| 424 | } |
| 425 | } |
| 426 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 427 | func intersect(d1 map[string]struct{}, d2 map[string]struct{}) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 428 | for k, _ := range d1 { |
| 429 | if _, ok := d2[k]; ok == true { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 430 | return true |
| 431 | } |
| 432 | } |
| 433 | return false |
| 434 | } |
| 435 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 436 | func setConnection(client pb.ConfigurationClient, cluster string, backend string, connection string, addr string, port uint64) { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 437 | log.Debugf("Configuring backend %s : connection %s in cluster %s\n\n", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 438 | backend, connection, cluster) |
| 439 | cnf := &pb.Conn{Server: "grpc_command", Cluster: cluster, Backend: backend, |
| 440 | Connection: connection, Addr: addr, |
| 441 | Port: port} |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 442 | if res, err := client.SetConnection(context.Background(), cnf); err != nil { |
| 443 | log.Debugf("failed SetConnection RPC call: %s", err) |
| 444 | } else { |
| 445 | log.Debugf("Result: %v", res) |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | func setAffinity(client pb.ConfigurationClient, ids map[string]struct{}, backend string) { |
| 450 | log.Debugf("Configuring backend %s : affinities \n", backend) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 451 | aff := &pb.Affinity{Router: "vcore", Route: "dev_manager", Cluster: "vcore", Backend: backend} |
| 452 | for k, _ := range ids { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 453 | log.Debugf("Setting affinity for id %s", k) |
| 454 | aff.Id = k |
| 455 | if res, err := client.SetAffinity(context.Background(), aff); err != nil { |
| 456 | log.Debugf("failed affinity RPC call: %s", err) |
| 457 | } else { |
| 458 | log.Debugf("Result: %v", res) |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 463 | func getBackendForCore(coreId string, coreGroups [][]*volthaPod) string { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 464 | for _, v := range coreGroups { |
| 465 | for _, v2 := range v { |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 466 | if v2.name == coreId { |
| 467 | return v2.backend |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | log.Errorf("No backend found for core %s\n", coreId) |
| 472 | return "" |
| 473 | } |
| 474 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 475 | func monitorDiscovery(client pb.ConfigurationClient, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 476 | ch <-chan *ic.InterContainerMessage, |
| 477 | coreGroups [][]*volthaPod) { |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 478 | var id map[string]struct{} = make(map[string]struct{}) |
| 479 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 480 | select { |
| 481 | case msg := <-ch: |
| 482 | log.Debugf("Received a device discovery notification") |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 483 | device := &ic.DeviceDiscovered{} |
| 484 | if err := ptypes.UnmarshalAny(msg.Body, device); err != nil { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 485 | log.Errorf("Could not unmarshal received notification %v", msg) |
| 486 | } else { |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 487 | // Set the affinity of the discovered device. |
| 488 | if be := getBackendForCore(device.Id, coreGroups); be != "" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 489 | id[device.Id] = struct{}{} |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 490 | setAffinity(client, id, be) |
| 491 | } else { |
| 492 | log.Error("Cant use an empty string as a backend name") |
| 493 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 494 | } |
| 495 | break |
| 496 | } |
| 497 | } |
| 498 | |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 499 | func startDiscoveryMonitor(client pb.ConfigurationClient, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 500 | coreGroups [][]*volthaPod) error { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 501 | var ch <-chan *ic.InterContainerMessage |
| 502 | // Connect to kafka for discovery events |
| 503 | topic := &kafka.Topic{Name: "AffinityRouter"} |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 504 | kc, err := newKafkaClient("sarama", "kafka", 9092, "arouterd") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 505 | kc.Start() |
| 506 | |
| 507 | if ch, err = kc.Subscribe(topic); err != nil { |
| 508 | log.Error("Could not subscribe to the 'AffinityRouter' channel, discovery disabled") |
| 509 | return err |
| 510 | } |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 511 | go monitorDiscovery(client, ch, coreGroups) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 512 | return nil |
| 513 | } |
| 514 | |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 515 | // Determines which items in core groups |
| 516 | // have changed based on the list provided |
| 517 | // and returns a coreGroup with only the changed |
| 518 | // items and a pod list with the new items |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 519 | func getAddrDiffs(coreGroups [][]*volthaPod, rwPods []*volthaPod) ([][]*volthaPod, []*volthaPod) { |
| 520 | var nList []*volthaPod |
| 521 | var rtrn [][]*volthaPod = make([][]*volthaPod, nPods>>1) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 522 | var ipAddrs map[string]struct{} = make(map[string]struct{}) |
| 523 | |
| 524 | log.Debug("Get addr diffs") |
| 525 | |
| 526 | // Start with an empty array |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 527 | for k, _ := range rtrn { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 528 | rtrn[k] = make([]*volthaPod, 2) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // Build a list with only the new items |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 532 | for _, v := range rwPods { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 533 | if hasIpAddr(coreGroups, v.ipAddr) == false { |
| 534 | nList = append(nList, v) |
| 535 | } |
| 536 | ipAddrs[v.ipAddr] = struct{}{} // for the search below |
| 537 | } |
| 538 | |
| 539 | // Now build the coreGroups with only the changed items |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 540 | for k1, v1 := range coreGroups { |
| 541 | for k2, v2 := range v1 { |
| 542 | if _, ok := ipAddrs[v2.ipAddr]; ok == false { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 543 | rtrn[k1][k2] = v2 |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | return rtrn, nList |
| 548 | } |
| 549 | |
| 550 | // Figure out where best to put the new pods |
| 551 | // in the coreGroup array based on the old |
| 552 | // pods being replaced. The criteria is that |
| 553 | // the new pod be on the same server as the |
| 554 | // old pod was. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 555 | func reconcileAddrDiffs(coreGroupDiffs [][]*volthaPod, rwPodDiffs []*volthaPod) [][]*volthaPod { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 556 | var srvrs map[string][]*volthaPod = make(map[string][]*volthaPod) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 557 | |
| 558 | log.Debug("Reconciling diffs") |
| 559 | log.Debug("Building server list") |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 560 | for _, v := range rwPodDiffs { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 561 | log.Debugf("Adding %v to the server list", *v) |
| 562 | srvrs[v.node] = append(srvrs[v.node], v) |
| 563 | } |
| 564 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 565 | for k1, v1 := range coreGroupDiffs { |
| 566 | log.Debugf("k1:%v, v1:%v", k1, v1) |
| 567 | for k2, v2 := range v1 { |
| 568 | log.Debugf("k2:%v, v2:%v", k2, v2) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 569 | if v2 == nil { // Nothing to do here |
| 570 | continue |
| 571 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 572 | if _, ok := srvrs[v2.node]; ok == true { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 573 | coreGroupDiffs[k1][k2] = srvrs[v2.node][0] |
| 574 | if len(srvrs[v2.node]) > 1 { // remove one entry from the list |
| 575 | srvrs[v2.node] = append(srvrs[v2.node][:0], srvrs[v2.node][1:]...) |
| 576 | } else { // Delete the endtry from the map |
| 577 | delete(srvrs, v2.node) |
| 578 | } |
| 579 | } else { |
| 580 | log.Error("This should never happen, node appears to have changed names") |
| 581 | // attempt to limp along by keeping this old entry |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return coreGroupDiffs |
| 587 | } |
| 588 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 589 | func applyAddrDiffs(client pb.ConfigurationClient, coreList interface{}, nPods []*volthaPod) { |
| 590 | var newEntries [][]*volthaPod |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 591 | |
| 592 | log.Debug("Applying diffs") |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 593 | switch cores := coreList.(type) { |
| 594 | case [][]*volthaPod: |
| 595 | newEntries = reconcileAddrDiffs(getAddrDiffs(cores, nPods)) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 596 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 597 | // Now replace the information in coreGropus with the new |
| 598 | // entries and then reconcile the device ids on the core |
| 599 | // that's in the new entry with the device ids of it's |
| 600 | // active-active peer. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 601 | for k1, v1 := range cores { |
| 602 | for k2, v2 := range v1 { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 603 | if newEntries[k1][k2] != nil { |
| 604 | // TODO: Missing is the case where bothe the primary |
| 605 | // and the secondary core crash and come back. |
| 606 | // Pull the device ids from the active-active peer |
| 607 | ids := queryPodDeviceIds(cores[k1][k2^1]) |
| 608 | if len(ids) != 0 { |
| 609 | if reconcilePodDeviceIds(newEntries[k1][k2], ids) == false { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 610 | log.Errorf("Attempt to reconcile ids on pod %v failed", newEntries[k1][k2]) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 611 | } |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 612 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 613 | // Send the affininty router new connection information |
| 614 | setConnection(client, "vcore", v2.backend, v2.connection, newEntries[k1][k2].ipAddr, 50057) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 615 | // Copy the new entry information over |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 616 | cores[k1][k2].ipAddr = newEntries[k1][k2].ipAddr |
| 617 | cores[k1][k2].name = newEntries[k1][k2].name |
| 618 | cores[k1][k2].devIds = ids |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 619 | } |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 620 | } |
| 621 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 622 | case []*volthaPod: |
| 623 | var mia []*volthaPod |
| 624 | var found bool |
| 625 | // TODO: Break this using functions to simplify |
| 626 | // reading of the code. |
| 627 | // Find the core(s) that have changed addresses |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 628 | for k1, v1 := range cores { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 629 | found = false |
| 630 | for _, v2 := range nPods { |
| 631 | if v1.ipAddr == v2.ipAddr { |
| 632 | found = true |
| 633 | break |
| 634 | } |
| 635 | } |
| 636 | if found == false { |
| 637 | mia = append(mia, cores[k1]) |
| 638 | } |
| 639 | } |
| 640 | // Now plug in the new addresses and set the connection |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 641 | for _, v1 := range nPods { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 642 | found = false |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 643 | for _, v2 := range cores { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 644 | if v1.ipAddr == v2.ipAddr { |
| 645 | found = true |
| 646 | break |
| 647 | } |
| 648 | } |
| 649 | if found == true { |
| 650 | continue |
| 651 | } |
| 652 | mia[0].ipAddr = v1.ipAddr |
| 653 | mia[0].name = v1.name |
| 654 | setConnection(client, "ro_vcore", mia[0].backend, mia[0].connection, v1.ipAddr, 50057) |
| 655 | // Now get rid of the mia entry just processed |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 656 | mia = append(mia[:0], mia[1:]...) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 657 | } |
| 658 | default: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 659 | log.Error("Internal: Unexpected type in call to applyAddrDiffs") |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 663 | func updateDeviceIds(coreGroups [][]*volthaPod, rwPods []*volthaPod) { |
| 664 | var byName map[string]*volthaPod = make(map[string]*volthaPod) |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 665 | |
| 666 | // Convinience |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 667 | for _, v := range rwPods { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 668 | byName[v.name] = v |
| 669 | } |
| 670 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 671 | for k1, v1 := range coreGroups { |
| 672 | for k2, _ := range v1 { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 673 | coreGroups[k1][k2].devIds = byName[v1[k2].name].devIds |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 678 | func startCoreMonitor(client pb.ConfigurationClient, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 679 | clientset *kubernetes.Clientset, |
| 680 | rwCoreFltr *regexp.Regexp, |
| 681 | roCoreFltr *regexp.Regexp, |
| 682 | coreGroups [][]*volthaPod, |
| 683 | oRoPods []*volthaPod) error { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 684 | // Now that initial allocation has been completed, monitor the pods |
| 685 | // for IP changes |
| 686 | // The main loop needs to do the following: |
| 687 | // 1) Periodically query the pods and filter out |
| 688 | // the vcore ones |
| 689 | // 2) Validate that the pods running are the same |
| 690 | // as the previous check |
| 691 | // 3) Validate that the IP addresses are the same |
| 692 | // as the last check. |
| 693 | // If the pod name(s) ha(s/ve) changed then remove |
| 694 | // the unused pod names and add in the new pod names |
| 695 | // maintaining the cluster/backend information. |
| 696 | // If an IP address has changed (which shouldn't |
| 697 | // happen unless a pod is re-started) it should get |
| 698 | // caught by the pod name change. |
| 699 | for { |
| 700 | time.Sleep(10 * time.Second) // Wait a while |
| 701 | // Get the rw core list from k8s |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 702 | rwPods := getVolthaPods(clientset, rwCoreFltr) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 703 | queryDeviceIds(rwPods) |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 704 | updateDeviceIds(coreGroups, rwPods) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 705 | // If we didn't get 2n+1 pods then wait since |
| 706 | // something is down and will hopefully come |
| 707 | // back up at some point. |
| 708 | // TODO: remove the 6 pod hardcoding |
| 709 | if len(rwPods) != 6 { |
| 710 | continue |
| 711 | } |
| 712 | // We have all pods, check if any IP addresses |
| 713 | // have changed. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 714 | for _, v := range rwPods { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 715 | if hasIpAddr(coreGroups, v.ipAddr) == false { |
| 716 | log.Debug("Address has changed...") |
| 717 | applyAddrDiffs(client, coreGroups, rwPods) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 718 | break |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 719 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 720 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 721 | |
| 722 | roPods := getVolthaPods(clientset, roCoreFltr) |
| 723 | |
| 724 | if len(roPods) != 3 { |
| 725 | continue |
| 726 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 727 | for _, v := range roPods { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 728 | if hasIpAddr(oRoPods, v.ipAddr) == false { |
| 729 | applyAddrDiffs(client, oRoPods, roPods) |
| 730 | break |
| 731 | } |
| 732 | } |
| 733 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 734 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 735 | } |
| 736 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 737 | func hasIpAddr(coreList interface{}, ipAddr string) bool { |
| 738 | switch cores := coreList.(type) { |
| 739 | case []*volthaPod: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 740 | for _, v := range cores { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 741 | if v.ipAddr == ipAddr { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 742 | return true |
| 743 | } |
| 744 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 745 | case [][]*volthaPod: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 746 | for _, v1 := range cores { |
| 747 | for _, v2 := range v1 { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 748 | if v2.ipAddr == ipAddr { |
| 749 | return true |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | default: |
| 754 | log.Error("Internal: Unexpected type in call to hasIpAddr") |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 755 | } |
| 756 | return false |
| 757 | } |
| 758 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 759 | func main() { |
| 760 | // This is currently hard coded to a cluster with 3 servers |
| 761 | //var connections map[string]configConn = make(map[string]configConn) |
| 762 | //var rwCorePodsPrev map[string]rwPod = make(map[string]rwPod) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 763 | var err error |
| 764 | var conn *grpc.ClientConn |
| 765 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 766 | // Set up the regular expression to identify the voltha cores |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 767 | rwCoreFltr := regexp.MustCompile(`rw-core[0-9]-`) |
| 768 | roCoreFltr := regexp.MustCompile(`ro-core-`) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 769 | |
| 770 | // Set up logging |
| 771 | if _, err := log.SetDefaultLogger(log.JSON, 0, nil); err != nil { |
| 772 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 773 | } |
| 774 | |
| 775 | // Set up kubernetes api |
| 776 | clientset := k8sClientSet() |
| 777 | |
| 778 | // Connect to the affinity router and set up the client |
| 779 | conn, err = connect("localhost:55554") // This is a sidecar container so communicating over localhost |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 780 | defer conn.Close() |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 781 | if err != nil { |
| 782 | panic(err.Error()) |
| 783 | } |
| 784 | client := pb.NewConfigurationClient(conn) |
| 785 | |
| 786 | // Get the voltha rw-core podes |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 787 | rwPods := getVolthaPods(clientset, rwCoreFltr) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 788 | |
| 789 | // Fetch the devices held by each running core |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 790 | queryDeviceIds(rwPods) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 791 | |
| 792 | // For debugging... comment out l8r |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 793 | for _, v := range rwPods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 794 | log.Debugf("Pod list %v", *v) |
| 795 | } |
| 796 | |
| 797 | coreGroups := groupPods1(rwPods) |
| 798 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 799 | // Assign the groupings to the the backends and connections |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 800 | for k, _ := range coreGroups { |
| 801 | for k1, _ := range coreGroups[k] { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 802 | coreGroups[k][k1].cluster = "vcore" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 803 | coreGroups[k][k1].backend = "vcore" + strconv.Itoa(k+1) |
| 804 | coreGroups[k][k1].connection = "vcore" + strconv.Itoa(k+1) + strconv.Itoa(k1+1) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 805 | } |
| 806 | } |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 807 | log.Info("Core gouping completed") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 808 | |
| 809 | // TODO: Debugging code, comment out for production |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 810 | for k, v := range coreGroups { |
| 811 | for k2, v2 := range v { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 812 | log.Debugf("Core group %d,%d: %v", k, k2, v2) |
| 813 | } |
| 814 | } |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 815 | log.Info("Setting affinities") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 816 | // Now set the affinities for exising devices in the cores |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 817 | for _, v := range coreGroups { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 818 | setAffinity(client, v[0].devIds, v[0].backend) |
| 819 | setAffinity(client, v[1].devIds, v[1].backend) |
| 820 | } |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 821 | log.Info("Setting connections") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 822 | // Configure the backeds based on the calculated core groups |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 823 | for _, v := range coreGroups { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 824 | setConnection(client, "vcore", v[0].backend, v[0].connection, v[0].ipAddr, 50057) |
| 825 | setConnection(client, "vcore", v[1].backend, v[1].connection, v[1].ipAddr, 50057) |
| 826 | } |
| 827 | |
| 828 | // Process the read only pods |
| 829 | roPods := getVolthaPods(clientset, roCoreFltr) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 830 | for k, v := range roPods { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 831 | log.Debugf("Processing ro_pod %v", v) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 832 | vN := "ro_vcore" + strconv.Itoa(k+1) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 833 | log.Debugf("Setting connection %s, %s, %s", vN, vN+"1", v.ipAddr) |
| 834 | roPods[k].cluster = "ro_core" |
| 835 | roPods[k].backend = vN |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 836 | roPods[k].connection = vN + "1" |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 837 | setConnection(client, "ro_vcore", v.backend, v.connection, v.ipAddr, 50057) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 838 | } |
| 839 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 840 | log.Info("Starting discovery monitoring") |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 841 | startDiscoveryMonitor(client, coreGroups) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 842 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 843 | log.Info("Starting core monitoring") |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 844 | startCoreMonitor(client, clientset, rwCoreFltr, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 845 | roCoreFltr, coreGroups, roPods) // Never returns |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 846 | return |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 847 | } |