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