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