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