|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Uber Technologies, Inc. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +package client |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "fmt" |
| 30 | + "math/rand" |
| 31 | + "runtime" |
| 32 | + "strings" |
| 33 | + "time" |
| 34 | + |
| 35 | + "go.temporal.io/server/common/config" |
| 36 | + "go.temporal.io/server/common/persistence" |
| 37 | +) |
| 38 | + |
| 39 | +// NewTargetedDataStoreErrorGenerator returns a new instance of a data store error generator that will inject errors |
| 40 | +// into the persistence layer based on the provided configuration. |
| 41 | +func NewTargetedDataStoreErrorGenerator(cfg *config.FaultInjectionDataStoreConfig) ErrorGenerator { |
| 42 | + methods := make(map[string]ErrorGenerator, len(cfg.Methods)) |
| 43 | + for methodName, methodConfig := range cfg.Methods { |
| 44 | + var faultWeights []FaultWeight |
| 45 | + methodErrorRate := 0.0 |
| 46 | + for errorName, errorRate := range methodConfig.Errors { |
| 47 | + err := getErrorFromName(errorName) |
| 48 | + faultWeights = append(faultWeights, FaultWeight{ |
| 49 | + errFactory: func(data string) error { |
| 50 | + return err |
| 51 | + }, |
| 52 | + weight: errorRate, |
| 53 | + }) |
| 54 | + methodErrorRate += errorRate |
| 55 | + } |
| 56 | + errorGenerator := NewDefaultErrorGenerator(methodErrorRate, faultWeights) |
| 57 | + seed := methodConfig.Seed |
| 58 | + if seed == 0 { |
| 59 | + seed = time.Now().UnixNano() |
| 60 | + } |
| 61 | + errorGenerator.r = rand.New(rand.NewSource(seed)) |
| 62 | + methods[methodName] = errorGenerator |
| 63 | + } |
| 64 | + return &dataStoreErrorGenerator{MethodErrorGenerators: methods} |
| 65 | +} |
| 66 | + |
| 67 | +// dataStoreErrorGenerator is an implementation of ErrorGenerator that will inject errors into the persistence layer |
| 68 | +// using a per-method configuration. |
| 69 | +type dataStoreErrorGenerator struct { |
| 70 | + MethodErrorGenerators map[string]ErrorGenerator |
| 71 | +} |
| 72 | + |
| 73 | +// Generate returns an error from the configured error types and rates for this method. |
| 74 | +// This method infers the fault injection target's method name from the function name of the caller. |
| 75 | +// As a result, this method should only be called from the persistence layer. |
| 76 | +// This method will panic if the method name cannot be inferred. |
| 77 | +// If no errors are configured for the method, or if there are some errors configured for this method, |
| 78 | +// but no error is sampled, then this method returns nil. |
| 79 | +// When this method returns nil, this causes the persistence layer to use the real implementation. |
| 80 | +func (d *dataStoreErrorGenerator) Generate() error { |
| 81 | + pc, _, _, ok := runtime.Caller(1) |
| 82 | + if !ok { |
| 83 | + panic("failed to get caller info") |
| 84 | + } |
| 85 | + runtimeFunc := runtime.FuncForPC(pc) |
| 86 | + if runtimeFunc == nil { |
| 87 | + panic("failed to get runtime function") |
| 88 | + } |
| 89 | + parts := strings.Split(runtimeFunc.Name(), ".") |
| 90 | + methodName := parts[len(parts)-1] |
| 91 | + methodErrorGenerator, ok := d.MethodErrorGenerators[methodName] |
| 92 | + if !ok { |
| 93 | + return nil |
| 94 | + } |
| 95 | + err := methodErrorGenerator.Generate() |
| 96 | + return err |
| 97 | +} |
| 98 | + |
| 99 | +// getErrorFromName returns an error based on the provided name. If the name is not recognized, then this method will |
| 100 | +// panic. |
| 101 | +func getErrorFromName(name string) error { |
| 102 | + switch name { |
| 103 | + case "ShardOwnershipLostError": |
| 104 | + return &persistence.ShardOwnershipLostError{} |
| 105 | + case "DeadlineExceededError": |
| 106 | + return context.DeadlineExceeded |
| 107 | + default: |
| 108 | + panic(fmt.Sprintf("unknown error type: %v", name)) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// UpdateRate should not be called for the data store error generator since the rate is defined on a per-method basis. |
| 113 | +func (d *dataStoreErrorGenerator) UpdateRate(rate float64) { |
| 114 | + panic("UpdateRate not supported for data store error generators") |
| 115 | +} |
| 116 | + |
| 117 | +// UpdateWeights should not be called for the data store error generator since the weights are defined on a per-method |
| 118 | +// basis. |
| 119 | +func (d *dataStoreErrorGenerator) UpdateWeights(weights []FaultWeight) { |
| 120 | + panic("UpdateWeights not supported for data store error generators") |
| 121 | +} |
| 122 | + |
| 123 | +// Rate should not be called for the data store error generator since there is no global rate for the data store, only |
| 124 | +// per-method rates. |
| 125 | +func (d *dataStoreErrorGenerator) Rate() float64 { |
| 126 | + panic("Rate not supported for data store error generators") |
| 127 | +} |
0 commit comments