|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020-2023 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package connection |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "math/big" |
| 26 | + "sort" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/kkdai/maglev" |
| 30 | + "github.com/pkg/errors" |
| 31 | +) |
| 32 | + |
| 33 | +// RequestHashValueExtractor accepts request method and full request path and must return a value which will be used for hash calculation |
| 34 | +type RequestHashValueExtractor func(requestMethod, requestPath string) (string, error) |
| 35 | + |
| 36 | +// NewMaglevHashEndpoints returns Endpoint manager which runs round-robin |
| 37 | +func NewMaglevHashEndpoints(eps []string, extractor RequestHashValueExtractor) (Endpoint, error) { |
| 38 | + // order of endpoints affects hashing result |
| 39 | + sort.Strings(eps) |
| 40 | + |
| 41 | + // lookupSize must be equal or greater than len(eps) and it must be a prime number |
| 42 | + lookupSize := findNextPrime(uint64(len(eps))) |
| 43 | + |
| 44 | + table, err := maglev.NewMaglev(eps, lookupSize) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + return &maglevHashEndpoints{ |
| 50 | + extractor: extractor, |
| 51 | + endpoints: eps, |
| 52 | + maglevTable: table, |
| 53 | + }, nil |
| 54 | +} |
| 55 | + |
| 56 | +func findNextPrime(i uint64) uint64 { |
| 57 | + bigInt := big.NewInt(0).SetUint64(i) |
| 58 | + |
| 59 | + for { |
| 60 | + if bigInt.ProbablyPrime(1) { |
| 61 | + return bigInt.Uint64() |
| 62 | + } |
| 63 | + i++ |
| 64 | + bigInt.SetUint64(i) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +type maglevHashEndpoints struct { |
| 69 | + extractor RequestHashValueExtractor |
| 70 | + endpoints []string |
| 71 | + maglevTable *maglev.Maglev |
| 72 | +} |
| 73 | + |
| 74 | +func (e *maglevHashEndpoints) List() []string { |
| 75 | + return e.endpoints |
| 76 | +} |
| 77 | + |
| 78 | +func (e *maglevHashEndpoints) Get(providedEp, requestMethod, requestPath string) (string, error) { |
| 79 | + if len(e.endpoints) == 0 { |
| 80 | + return "", errors.New("no endpoints known") |
| 81 | + } |
| 82 | + |
| 83 | + for _, known := range e.endpoints { |
| 84 | + if known == providedEp { |
| 85 | + return known, nil |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + val, err := e.extractor(requestMethod, requestPath) |
| 90 | + if err != nil { |
| 91 | + return "", errors.WithMessagef(err, "could not extract value for method '%s' path '%s'", requestMethod, requestPath) |
| 92 | + } |
| 93 | + |
| 94 | + r, err := e.maglevTable.Get(val) |
| 95 | + if err != nil { |
| 96 | + return r, errors.WithMessage(err, "failed to lookup Maglev table") |
| 97 | + } |
| 98 | + |
| 99 | + return r, nil |
| 100 | +} |
| 101 | + |
| 102 | +var _ RequestHashValueExtractor = RequestDBNameValueExtractor |
| 103 | + |
| 104 | +// RequestDBNameValueExtractor might be used as RequestHashValueExtractor to use DB name from URL for hashing |
| 105 | +// It fallbacks to requestMethod+requestPath concatenation in case if path does not contain DB name |
| 106 | +func RequestDBNameValueExtractor(requestMethod, requestPath string) (string, error) { |
| 107 | + // most go-driver requests to ArangoDB are executed against `_db/<db-name>/xxxx/yyy/`) URL pattern |
| 108 | + // we can try to extract db-name to load-balance requests between endpoints |
| 109 | + |
| 110 | + parts := strings.Split(strings.Trim(strings.TrimSpace(requestPath), "/"), "/") |
| 111 | + if len(parts) >= 3 { |
| 112 | + if parts[0] == "_db" { |
| 113 | + return parts[1], nil |
| 114 | + } |
| 115 | + } |
| 116 | + return fmt.Sprintf("%s_%s", requestMethod, requestPath), nil |
| 117 | +} |
0 commit comments