blob: 45ec26a63c63968c5e3d107b6e1de5176624281b [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -05001/*
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
18package afrouter
19
20import (
sslobodr392ebd52019-01-18 12:41:49 -050021 "errors"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040022 "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"
sslobodr1d1e50b2019-03-14 09:17:40 -040028 "regexp"
sslobodr392ebd52019-01-18 12:41:49 -050029 "strconv"
sslobodr392ebd52019-01-18 12:41:49 -050030)
31
sslobodr1d1e50b2019-03-14 09:17:40 -040032const (
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040033 PKG_MTHD_PKG int = 1
sslobodr1d1e50b2019-03-14 09:17:40 -040034 PKG_MTHD_MTHD int = 2
35)
36
sslobodr392ebd52019-01-18 12:41:49 -050037type AffinityRouter struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040038 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
sslobodr392ebd52019-01-18 12:41:49 -050045 nbBindingMthdMap map[string]byte
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040046 bkndClstr *backendCluster
47 affinity map[string]*backend
48 curBknd **backend
sslobodr392ebd52019-01-18 12:41:49 -050049}
50
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040051func newAffinityRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
sslobodr392ebd52019-01-18 12:41:49 -050052 var err error = nil
53 var rtrn_err bool = false
sslobodr1d1e50b2019-03-14 09:17:40 -040054 var pkg_re *regexp.Regexp = regexp.MustCompile(`^(\.[^.]+\.)(.+)$`)
sslobodr392ebd52019-01-18 12:41:49 -050055 // 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 Hagerman0ab4cb22019-04-24 13:13:35 -040083 bptr = nil
sslobodr392ebd52019-01-18 12:41:49 -050084 dr := AffinityRouter{
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040085 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,
sslobodr392ebd52019-01-18 12:41:49 -050091 //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 Hagerman0ab4cb22019-04-24 13:13:35 -040097 log.Error("An association must be specified")
sslobodr392ebd52019-01-18 12:41:49 -050098 } else {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040099 log.Errorf("The association '%s' is not valid", config.Association)
sslobodr392ebd52019-01-18 12:41:49 -0500100 }
101 rtrn_err = true
102 }
103
sslobodr392ebd52019-01-18 12:41:49 -0500104 // This has already been validated bfore this function
105 // is called so just use it.
sslobodr5f0b5a32019-01-24 07:45:19 -0500106 for idx := range rTypeNames {
sslobodr392ebd52019-01-18 12:41:49 -0500107 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400115 fb, err := ioutil.ReadFile(config.ProtoFile)
sslobodr392ebd52019-01-18 12:41:49 -0500116 if err != nil {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400117 log.Errorf("Could not open proto file '%s'", config.ProtoFile)
sslobodr392ebd52019-01-18 12:41:49 -0500118 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
sslobodr392ebd52019-01-18 12:41:49 -0500126 // Build the routing structure based on the loaded protobuf
127 // descriptor file and the config information.
128 type key struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400129 mthd string
sslobodr392ebd52019-01-18 12:41:49 -0500130 field string
131 }
132 var msgs map[key]byte = make(map[key]byte)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400133 for _, f := range dr.protoDescriptor.File {
sslobodr392ebd52019-01-18 12:41:49 -0500134 // Build a temporary map of message types by name.
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400135 for _, m := range f.MessageType {
136 for _, fld := range m.Field {
sslobodr392ebd52019-01-18 12:41:49 -0500137 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400143 for _, f := range dr.protoDescriptor.File {
sslobodr392ebd52019-01-18 12:41:49 -0500144 if *f.Package == rconf.ProtoPackage {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400145 for _, s := range f.Service {
sslobodr392ebd52019-01-18 12:41:49 -0500146 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400151 for _, m := range s.Method {
sslobodr392ebd52019-01-18 12:41:49 -0500152 // Find the input type in the messages and extract the
153 // field number and save it for future reference.
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400154 log.Debugf("Processing method '%s'", *m.Name)
sslobodr392ebd52019-01-18 12:41:49 -0500155 // Determine if this is a method we're supposed to be processing.
156 if needMethod(*m.Name, config) == true {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400157 log.Debugf("Enabling method '%s'", *m.Name)
sslobodr1d1e50b2019-03-14 09:17:40 -0400158 pkg_methd := pkg_re.FindStringSubmatch(*m.InputType)
159 if pkg_methd == nil {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400160 log.Errorf("Regular expression didn't match input type '%s'", *m.InputType)
sslobodr1d1e50b2019-03-14 09:17:40 -0400161 rtrn_err = true
162 }
sslobodr392ebd52019-01-18 12:41:49 -0500163 // The input type has the package name prepended to it. Remove it.
sslobodr1d1e50b2019-03-14 09:17:40 -0400164 //in := (*m.InputType)[len(rconf.ProtoPackage)+2:]
165 in := pkg_methd[PKG_MTHD_MTHD]
sslobodr392ebd52019-01-18 12:41:49 -0500166 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400169 *m.Name, config.RouteField, in)
sslobodr392ebd52019-01-18 12:41:49 -0500170 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400175 log.Debugf("Enabling southbound method '%s'", *m.Name)
sslobodr392ebd52019-01-18 12:41:49 -0500176 // 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400181 *m.Name, config.RouteField, out)
sslobodr392ebd52019-01-18 12:41:49 -0500182 rtrn_err = true
183 }
184 }
185 }
186 }
187 }
188 }
189 }
190
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400191 // Create the backend cluster or link to an existing one
sslobodr392ebd52019-01-18 12:41:49 -0500192 ok := true
193 if dr.bkndClstr, ok = bClusters[config.backendCluster.Name]; ok == false {
sslobodrcd37bc52019-01-24 11:47:16 -0500194 if dr.bkndClstr, err = newBackendCluster(config.backendCluster); err != nil {
sslobodr392ebd52019-01-18 12:41:49 -0500195 log.Errorf("Could not create a backend for router %s", config.Name)
196 rtrn_err = true
197 }
198 }
199
200 if rtrn_err {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400201 return dr, errors.New(fmt.Sprintf("Failed to create a new router '%s'", dr.name))
sslobodr392ebd52019-01-18 12:41:49 -0500202 }
203
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400204 return dr, nil
sslobodr392ebd52019-01-18 12:41:49 -0500205}
206
207func needSbMethod(mthd string, conf *RouteConfig) bool {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400208 for _, m := range conf.NbBindingMethods {
sslobodr392ebd52019-01-18 12:41:49 -0500209 if mthd == m {
210 return true
211 }
212 }
213 return false
214}
215
216func needMethod(mthd string, conf *RouteConfig) bool {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400217 for _, m := range conf.Methods {
sslobodr392ebd52019-01-18 12:41:49 -0500218 if mthd == m {
219 return true
220 }
221 }
222 return false
223}
224
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400225func (r AffinityRouter) Service() string {
sslobodr392ebd52019-01-18 12:41:49 -0500226 return r.grpcService
227}
228
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400229func (r AffinityRouter) Name() string {
sslobodr392ebd52019-01-18 12:41:49 -0500230 return r.name
231}
232
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400233func (r AffinityRouter) skipField(data *[]byte, idx *int) error {
234 switch (*data)[*idx] & 3 {
235 case 0: // Varint
sslobodr392ebd52019-01-18 12:41:49 -0500236 (*idx)++
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400237 for (*data)[*idx] >= 128 {
sslobodr392ebd52019-01-18 12:41:49 -0500238 (*idx)++
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400239 }
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
sslobodr392ebd52019-01-18 12:41:49 -0500251 }
252 return nil
253}
254
255func (r AffinityRouter) decodeProtoField(payload []byte, fieldId byte) (string, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400256 idx := 0
sslobodr392ebd52019-01-18 12:41:49 -0500257 b := proto.NewBuffer([]byte{})
sslobodr1d1e50b2019-03-14 09:17:40 -0400258 //b.DebugPrint("The Buffer", payload)
sslobodr392ebd52019-01-18 12:41:49 -0500259 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400267 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
sslobodr392ebd52019-01-18 12:41:49 -0500292 }
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
301func (r AffinityRouter) Route(sel interface{}) *backend {
302 switch sl := sel.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400303 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!!")
sslobodr392ebd52019-01-18 12:41:49 -0500326 var err error
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400327 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)
sslobodr392ebd52019-01-18 12:41:49 -0500331 return *r.curBknd
332 } else {
333 sl.err = err
334 return nil
335 }
336 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400337 }
338 default:
339 log.Errorf("Internal: invalid data type in Route call %v", sel)
340 return nil
sslobodr392ebd52019-01-18 12:41:49 -0500341 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400342 log.Errorf("Bad lookup in affinity map %v", r.affinity)
sslobodr392ebd52019-01-18 12:41:49 -0500343 return nil
344}
345
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400346func (ar AffinityRouter) GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error) {
347 return "", "", nil
sslobodr392ebd52019-01-18 12:41:49 -0500348}
349
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400350func (ar AffinityRouter) BackendCluster(mthd string, metaKey string) (*backendCluster, error) {
sslobodr392ebd52019-01-18 12:41:49 -0500351 return ar.bkndClstr, nil
352}
353
354func (ar AffinityRouter) FindBackendCluster(beName string) *backendCluster {
355 if beName == ar.bkndClstr.name {
356 return ar.bkndClstr
357 }
358 return nil
359}
360
361func (r AffinityRouter) ReplyHandler(sel interface{}) error {
362 switch sl := sel.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400363 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")
sslobodr392ebd52019-01-18 12:41:49 -0500375 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400376 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
sslobodr392ebd52019-01-18 12:41:49 -0500381 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400382 }
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
sslobodr392ebd52019-01-18 12:41:49 -0500388 }
389}
390
391func (ar AffinityRouter) setAffinity(key string, be *backend) error {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400392 if be2, ok := ar.affinity[key]; ok == false {
sslobodr392ebd52019-01-18 12:41:49 -0500393 ar.affinity[key] = be
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400394 log.Debugf("New affinity set to backend %s for key %s", be.name, key)
sslobodr392ebd52019-01-18 12:41:49 -0500395 } 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 Hagerman0ab4cb22019-04-24 13:13:35 -0400397 key, be.name, ar.affinity[key].name, ar.name))
sslobodr392ebd52019-01-18 12:41:49 -0500398 log.Error(err)
399 return err
400 }
401 return nil
402}