|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 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 | +package wrappers |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/arangodb/go-driver" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + timeout = time.Second * 60 |
| 32 | + interval = time.Second * 2 |
| 33 | +) |
| 34 | + |
| 35 | +type activeFailoverWrapper struct { |
| 36 | + testEnv |
| 37 | + driver.Connection |
| 38 | +} |
| 39 | + |
| 40 | +type testEnv interface { |
| 41 | + Error(message ...interface{}) |
| 42 | + Errorf(format string, args ...interface{}) |
| 43 | + Fatal(message ...interface{}) |
| 44 | + Fatalf(format string, args ...interface{}) |
| 45 | + Log(message ...interface{}) |
| 46 | + Logf(format string, args ...interface{}) |
| 47 | + Name() string |
| 48 | + FailNow() |
| 49 | +} |
| 50 | + |
| 51 | +// NewActiveFailoverWrapper is meant for use by tests generated by 'go test' to cover AF failing cases. |
| 52 | +// It wraps the given connection and retries requests that fail with an error indicating that there is no leader. |
| 53 | +func NewActiveFailoverWrapper(t testEnv, conn driver.Connection) driver.Connection { |
| 54 | + return &activeFailoverWrapper{t, conn} |
| 55 | +} |
| 56 | + |
| 57 | +// TODO: with Go 1.18 we can extract this method to utils using generics |
| 58 | +func (af *activeFailoverWrapper) Do(ctx context.Context, req driver.Request) (driver.Response, error) { |
| 59 | + timeoutT := time.NewTimer(timeout) |
| 60 | + defer timeoutT.Stop() |
| 61 | + |
| 62 | + intervalT := time.NewTicker(interval) |
| 63 | + defer intervalT.Stop() |
| 64 | + |
| 65 | + for { |
| 66 | + resp, err := af.Connection.Do(ctx, req) |
| 67 | + if err != nil { |
| 68 | + if driver.IsNoLeaderOrOngoing(err) { |
| 69 | + af.Logf("RETRYING (ERROR) - there is no Leader or Leader change is ongoing: %v", err) |
| 70 | + } else { |
| 71 | + return resp, err |
| 72 | + } |
| 73 | + } else { |
| 74 | + if errBody := resp.CheckStatus(); errBody != nil && driver.IsNoLeaderOrOngoing(errBody) { |
| 75 | + af.Logf("RETRYING (ERROR IN BODY CASE) - there is no Leader or Leader change is ongoing: %v", errBody) |
| 76 | + } else { |
| 77 | + return resp, err |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + select { |
| 82 | + case <-timeoutT.C: |
| 83 | + return nil, fmt.Errorf("activeFailoverWrapper function time out (waiting for the Leader)") |
| 84 | + case <-intervalT.C: |
| 85 | + continue |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments