Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [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 afrouter |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "github.com/golang/protobuf/proto" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "google.golang.org/grpc" |
| 25 | "regexp" |
| 26 | "strconv" |
| 27 | ) |
| 28 | |
| 29 | // TODO: Used in multiple routers, should move to common file |
| 30 | const ( |
| 31 | PKG_MTHD_PKG int = 1 |
| 32 | PKG_MTHD_MTHD int = 2 |
| 33 | ) |
| 34 | |
| 35 | type AffinityRouter struct { |
| 36 | name string |
| 37 | association associationType |
| 38 | routingField string |
| 39 | grpcService string |
| 40 | methodMap map[string]byte |
| 41 | nbBindingMethodMap map[string]byte |
| 42 | cluster *cluster |
| 43 | affinity map[string]*backend |
| 44 | currentBackend **backend |
| 45 | } |
| 46 | |
| 47 | func newAffinityRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) { |
| 48 | var err error = nil |
| 49 | var rtrn_err = false |
| 50 | var pkg_re = regexp.MustCompile(`^(\.[^.]+\.)(.+)$`) |
| 51 | // Validate the configuration |
| 52 | |
| 53 | // A name must exist |
| 54 | if config.Name == "" { |
| 55 | log.Error("A router 'name' must be specified") |
| 56 | rtrn_err = true |
| 57 | } |
| 58 | |
| 59 | if rconf.ProtoPackage == "" { |
| 60 | log.Error("A 'package' must be specified") |
| 61 | rtrn_err = true |
| 62 | } |
| 63 | |
| 64 | if rconf.ProtoService == "" { |
| 65 | log.Error("A 'service' must be specified") |
| 66 | rtrn_err = true |
| 67 | } |
| 68 | |
| 69 | //if config.RouteField == "" { |
| 70 | // log.Error("A 'routing_field' must be specified") |
| 71 | // rtrn_err = true |
| 72 | //} |
| 73 | |
| 74 | // TODO The overrieds section is currently not being used |
| 75 | // so the router will route all methods based on the |
| 76 | // routing_field. This needs to be added so that methods |
| 77 | // can have different routing fields. |
| 78 | var bptr *backend |
| 79 | bptr = nil |
| 80 | dr := AffinityRouter{ |
| 81 | name: config.Name, |
| 82 | grpcService: rconf.ProtoService, |
| 83 | affinity: make(map[string]*backend), |
| 84 | methodMap: make(map[string]byte), |
| 85 | nbBindingMethodMap: make(map[string]byte), |
| 86 | currentBackend: &bptr, |
| 87 | } |
| 88 | // An association must exist |
| 89 | dr.association = config.Association |
| 90 | if dr.association == AssociationUndefined { |
| 91 | log.Error("An association must be specified") |
| 92 | rtrn_err = true |
| 93 | } |
| 94 | |
| 95 | // Build the routing structure based on the loaded protobuf |
| 96 | // descriptor file and the config information. |
| 97 | type key struct { |
| 98 | method string |
| 99 | field string |
| 100 | } |
| 101 | var fieldNumberLookup = make(map[key]byte) |
| 102 | for _, f := range rconf.protoDescriptor.File { |
| 103 | // Build a temporary map of message types by name. |
| 104 | for _, m := range f.MessageType { |
| 105 | for _, fld := range m.Field { |
| 106 | log.Debugf("Processing message '%s', field '%s'", *m.Name, *fld.Name) |
| 107 | fieldNumberLookup[key{*m.Name, *fld.Name}] = byte(*fld.Number) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | for _, f := range rconf.protoDescriptor.File { |
| 112 | if *f.Package == rconf.ProtoPackage { |
| 113 | for _, s := range f.Service { |
| 114 | if *s.Name == rconf.ProtoService { |
| 115 | log.Debugf("Loading package data '%s' for service '%s' for router '%s'", *f.Package, *s.Name, dr.name) |
| 116 | // Now create a map keyed by method name with the value being the |
| 117 | // field number of the route selector. |
| 118 | var ok bool |
| 119 | for _, m := range s.Method { |
| 120 | // Find the input type in the messages and extract the |
| 121 | // field number and save it for future reference. |
| 122 | log.Debugf("Processing method '%s'", *m.Name) |
| 123 | // Determine if this is a method we're supposed to be processing. |
| 124 | if needMethod(*m.Name, config) { |
| 125 | log.Debugf("Enabling method '%s'", *m.Name) |
| 126 | pkg_methd := pkg_re.FindStringSubmatch(*m.InputType) |
| 127 | if pkg_methd == nil { |
| 128 | log.Errorf("Regular expression didn't match input type '%s'", *m.InputType) |
| 129 | rtrn_err = true |
| 130 | } |
| 131 | // The input type has the package name prepended to it. Remove it. |
| 132 | //in := (*m.InputType)[len(rconf.ProtoPackage)+2:] |
| 133 | in := pkg_methd[PKG_MTHD_MTHD] |
| 134 | dr.methodMap[*m.Name], ok = fieldNumberLookup[key{in, config.RouteField}] |
| 135 | if !ok { |
| 136 | log.Errorf("Method '%s' has no field named '%s' in it's parameter message '%s'", |
| 137 | *m.Name, config.RouteField, in) |
| 138 | rtrn_err = true |
| 139 | } |
| 140 | } |
| 141 | // The sb method is always included in the methods so we can check it here too. |
| 142 | if needNbBindingMethod(*m.Name, config) { |
| 143 | log.Debugf("Enabling southbound method '%s'", *m.Name) |
| 144 | // The output type has the package name prepended to it. Remove it. |
| 145 | out := (*m.OutputType)[len(rconf.ProtoPackage)+2:] |
| 146 | dr.nbBindingMethodMap[*m.Name], ok = fieldNumberLookup[key{out, config.RouteField}] |
| 147 | if !ok { |
| 148 | log.Errorf("Method '%s' has no field named '%s' in it's parameter message '%s'", |
| 149 | *m.Name, config.RouteField, out) |
| 150 | rtrn_err = true |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Create the backend cluster or link to an existing one |
| 160 | ok := true |
| 161 | if dr.cluster, ok = clusters[config.backendCluster.Name]; !ok { |
| 162 | if dr.cluster, err = newBackendCluster(config.backendCluster); err != nil { |
| 163 | log.Errorf("Could not create a backend for router %s", config.Name) |
| 164 | rtrn_err = true |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if rtrn_err { |
| 169 | return dr, errors.New(fmt.Sprintf("Failed to create a new router '%s'", dr.name)) |
| 170 | } |
| 171 | |
| 172 | return dr, nil |
| 173 | } |
| 174 | |
| 175 | func needNbBindingMethod(mthd string, conf *RouteConfig) bool { |
| 176 | for _, m := range conf.NbBindingMethods { |
| 177 | if mthd == m { |
| 178 | return true |
| 179 | } |
| 180 | } |
| 181 | return false |
| 182 | } |
| 183 | |
| 184 | // TODO: Used in multiple routers, should move to common file |
| 185 | func needMethod(mthd string, conf *RouteConfig) bool { |
| 186 | for _, m := range conf.Methods { |
| 187 | if mthd == m { |
| 188 | return true |
| 189 | } |
| 190 | } |
| 191 | return false |
| 192 | } |
| 193 | |
| 194 | func (ar AffinityRouter) Service() string { |
| 195 | return ar.grpcService |
| 196 | } |
| 197 | |
| 198 | func (ar AffinityRouter) Name() string { |
| 199 | return ar.name |
| 200 | } |
| 201 | |
| 202 | func (ar AffinityRouter) skipField(data *[]byte, idx *int) error { |
| 203 | switch (*data)[*idx] & 3 { |
| 204 | case 0: // Varint |
| 205 | // skip the field number/type |
| 206 | *idx++ |
| 207 | // if the msb is set, then more bytes to follow |
| 208 | for (*data)[*idx] >= 128 { |
| 209 | *idx++ |
| 210 | } |
| 211 | // the last byte doesn't have the msb set |
| 212 | *idx++ |
| 213 | case 1: // 64 bit |
| 214 | *idx += 9 |
| 215 | case 2: // Length delimited |
| 216 | *idx++ |
| 217 | b := proto.NewBuffer((*data)[*idx:]) |
| 218 | t, _ := b.DecodeVarint() |
| 219 | *idx += int(t) + 1 |
| 220 | case 3: // Deprecated |
| 221 | case 4: // Deprecated |
| 222 | case 5: // 32 bit |
| 223 | *idx += 5 |
| 224 | } |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | func (ar AffinityRouter) decodeProtoField(payload []byte, fieldId byte) (string, error) { |
| 229 | idx := 0 |
| 230 | b := proto.NewBuffer([]byte{}) |
| 231 | //b.DebugPrint("The Buffer", payload) |
| 232 | for { // Find the route selector field |
| 233 | log.Debugf("Decoding afinity value attributeNumber: %d from %v at index %d", fieldId, payload, idx) |
| 234 | log.Debugf("Attempting match with payload: %d, methodTable: %d", payload[idx], fieldId) |
| 235 | if payload[idx]>>3 == fieldId { |
| 236 | log.Debugf("Method match with payload: %d, methodTable: %d", payload[idx], fieldId) |
| 237 | // TODO: Consider supporting other selector types.... Way, way in the future |
| 238 | // ok, the future is now, support strings as well... ugh. |
| 239 | var selector string |
| 240 | switch payload[idx] & 3 { |
| 241 | case 0: // Integer |
| 242 | b.SetBuf(payload[idx+1:]) |
| 243 | v, e := b.DecodeVarint() |
| 244 | if e == nil { |
| 245 | log.Debugf("Decoded the ing field: %v", v) |
| 246 | selector = strconv.Itoa(int(v)) |
| 247 | } else { |
| 248 | log.Errorf("Failed to decode varint %v", e) |
| 249 | return "", e |
| 250 | } |
| 251 | case 2: // Length delimited AKA string |
| 252 | b.SetBuf(payload[idx+1:]) |
| 253 | v, e := b.DecodeStringBytes() |
| 254 | if e == nil { |
| 255 | log.Debugf("Decoded the string field: %v", v) |
| 256 | selector = v |
| 257 | } else { |
| 258 | log.Errorf("Failed to decode string %v", e) |
| 259 | return "", e |
| 260 | } |
| 261 | default: |
| 262 | err := errors.New(fmt.Sprintf("Only integer and string route selectors are permitted")) |
| 263 | log.Error(err) |
| 264 | return "", err |
| 265 | } |
| 266 | return selector, nil |
| 267 | } else if err := ar.skipField(&payload, &idx); err != nil { |
| 268 | log.Errorf("Parsing message failed %v", err) |
| 269 | return "", err |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func (ar AffinityRouter) Route(sel interface{}) (*backend, *connection) { |
| 275 | switch sl := sel.(type) { |
| 276 | case *requestFrame: |
| 277 | log.Debugf("Route called for requestFrame with method %s", sl.methodInfo.method) |
| 278 | // Check if this method should be affinity bound from the |
| 279 | // reply rather than the request. |
| 280 | if _, ok := ar.nbBindingMethodMap[sl.methodInfo.method]; ok { |
| 281 | var err error |
| 282 | log.Debugf("Method '%s' affinity binds on reply", sl.methodInfo.method) |
| 283 | // Just round robin route the southbound request |
| 284 | if *ar.currentBackend, err = ar.cluster.nextBackend(*ar.currentBackend, BackendSequenceRoundRobin); err == nil { |
| 285 | return *ar.currentBackend, nil |
| 286 | } else { |
| 287 | sl.err = err |
| 288 | return nil, nil |
| 289 | } |
| 290 | } |
| 291 | // Not a south affinity binding method, proceed with north affinity binding. |
| 292 | if selector, err := ar.decodeProtoField(sl.payload, ar.methodMap[sl.methodInfo.method]); err == nil { |
| 293 | log.Debugf("Establishing affinity for selector: %s", selector) |
| 294 | if rtrn, ok := ar.affinity[selector]; ok { |
| 295 | return rtrn, nil |
| 296 | } else { |
| 297 | // The selector isn't in the map, create a new affinity mapping |
| 298 | log.Debugf("MUST CREATE A NEW AFFINITY MAP ENTRY!!") |
| 299 | var err error |
| 300 | if *ar.currentBackend, err = ar.cluster.nextBackend(*ar.currentBackend, BackendSequenceRoundRobin); err == nil { |
| 301 | ar.setAffinity(selector, *ar.currentBackend) |
| 302 | //ar.affinity[selector] = *ar.currentBackend |
| 303 | //log.Debugf("New affinity set to backend %s",(*ar.currentBackend).name) |
| 304 | return *ar.currentBackend, nil |
| 305 | } else { |
| 306 | sl.err = err |
| 307 | return nil, nil |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | default: |
| 312 | log.Errorf("Internal: invalid data type in Route call %v", sel) |
| 313 | return nil, nil |
| 314 | } |
| 315 | log.Errorf("Bad lookup in affinity map %v", ar.affinity) |
| 316 | return nil, nil |
| 317 | } |
| 318 | |
| 319 | func (ar AffinityRouter) GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error) { |
| 320 | return "", "", nil |
| 321 | } |
| 322 | |
| 323 | func (ar AffinityRouter) IsStreaming(_ string) (bool, bool) { |
| 324 | panic("not implemented") |
| 325 | } |
| 326 | |
| 327 | func (ar AffinityRouter) BackendCluster(mthd string, metaKey string) (*cluster, error) { |
| 328 | return ar.cluster, nil |
| 329 | } |
| 330 | |
| 331 | func (ar AffinityRouter) FindBackendCluster(beName string) *cluster { |
| 332 | if beName == ar.cluster.name { |
| 333 | return ar.cluster |
| 334 | } |
| 335 | return nil |
| 336 | } |
| 337 | |
| 338 | func (ar AffinityRouter) ReplyHandler(sel interface{}) error { |
| 339 | switch sl := sel.(type) { |
| 340 | case *responseFrame: |
| 341 | log.Debugf("Reply handler called for responseFrame with method %s", sl.method) |
| 342 | // Determine if reply action is required. |
| 343 | if fld, ok := ar.nbBindingMethodMap[sl.method]; ok && len(sl.payload) > 0 { |
| 344 | // Extract the field value from the frame and |
| 345 | // and set affinity accordingly |
| 346 | if selector, err := ar.decodeProtoField(sl.payload, fld); err == nil { |
| 347 | log.Debug("Settign affinity on reply") |
| 348 | if ar.setAffinity(selector, sl.backend) != nil { |
| 349 | log.Error("Setting affinity on reply failed") |
| 350 | } |
| 351 | return nil |
| 352 | } else { |
| 353 | err := errors.New(fmt.Sprintf("Failed to decode reply field %d for method %s", fld, sl.method)) |
| 354 | log.Error(err) |
| 355 | return err |
| 356 | } |
| 357 | } |
| 358 | return nil |
| 359 | default: |
| 360 | err := errors.New(fmt.Sprintf("Internal: invalid data type in ReplyHander call %v", sl)) |
| 361 | log.Error(err) |
| 362 | return err |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | func (ar AffinityRouter) setAffinity(key string, be *backend) error { |
| 367 | if be2, ok := ar.affinity[key]; !ok { |
| 368 | ar.affinity[key] = be |
| 369 | log.Debugf("New affinity set to backend %s for key %s", be.name, key) |
| 370 | } else if be2 != be { |
| 371 | err := errors.New(fmt.Sprintf("Attempting multiple sets of affinity for key %s to backend %s from %s on router %s", |
| 372 | key, be.name, ar.affinity[key].name, ar.name)) |
| 373 | log.Error(err) |
| 374 | return err |
| 375 | } |
| 376 | return nil |
| 377 | } |