|
| 1 | +package ldmigration |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +// ExecutionOrder represents the various execution modes this SDK can operate |
| 6 | +// under while performing migration-assisted reads. |
| 7 | +type ExecutionOrder string |
| 8 | + |
| 9 | +const ( |
| 10 | + // Serial execution ensures the authoritative read will always complete execution before executing the |
| 11 | + // non-authoritative read. |
| 12 | + Serial ExecutionOrder = "serial" |
| 13 | + // Random execution randomly decides if the authoritative read should execute first or second. |
| 14 | + Random ExecutionOrder = "random" |
| 15 | + // Concurrent executes both reads in separate go routines, and waits until both calls have finished before |
| 16 | + // proceeding. |
| 17 | + Concurrent ExecutionOrder = "concurrent" |
| 18 | +) |
| 19 | + |
| 20 | +// Operation represents a type of migration operation; namely, read or write. |
| 21 | +type Operation string |
| 22 | + |
| 23 | +const ( |
| 24 | + // Read denotes a read-related migration operation. |
| 25 | + Read Operation = "read" |
| 26 | + // Write denotes a write-related migration operation. |
| 27 | + Write Operation = "write" |
| 28 | +) |
| 29 | + |
| 30 | +// ConsistencyCheck records the results of a consistency check and the ratio at |
| 31 | +// which the check was sampled. |
| 32 | +// |
| 33 | +// For example, a sampling ratio of 10 indicts this consistency check was |
| 34 | +// sampled approximately once every ten operations. |
| 35 | +type ConsistencyCheck struct { |
| 36 | + consistent bool |
| 37 | + samplingRatio int |
| 38 | +} |
| 39 | + |
| 40 | +// NewConsistencyCheck creates a new consistency check reflecting the provided values. |
| 41 | +func NewConsistencyCheck(wasConsistent bool, samplingRatio int) *ConsistencyCheck { |
| 42 | + return &ConsistencyCheck{ |
| 43 | + consistent: wasConsistent, |
| 44 | + samplingRatio: samplingRatio, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Consistent returns whether or not the check returned a consistent result. |
| 49 | +func (c ConsistencyCheck) Consistent() bool { |
| 50 | + return c.consistent |
| 51 | +} |
| 52 | + |
| 53 | +// SamplingRatio returns the 1 in x sampling ratio used to determine if the consistency check should be run. |
| 54 | +func (c ConsistencyCheck) SamplingRatio() int { |
| 55 | + return c.samplingRatio |
| 56 | +} |
| 57 | + |
| 58 | +// Origin represents the source of origin for a migration-related operation. |
| 59 | +type Origin string |
| 60 | + |
| 61 | +const ( |
| 62 | + // Old represents the technology source we are migrating away from. |
| 63 | + Old Origin = "old" |
| 64 | + // New represents the technology source we are migrating towards. |
| 65 | + New Origin = "new" |
| 66 | +) |
| 67 | + |
| 68 | +// Stage denotes one of six possible stages a technology migration could be a |
| 69 | +// part of, progressing through the following order. |
| 70 | +// |
| 71 | +// Off -> DualWrite -> Shadow -> Live -> RampDown -> Complete |
| 72 | +type Stage string |
| 73 | + |
| 74 | +const ( |
| 75 | + // Off - migration hasn't started, "old" is authoritative for reads and writes |
| 76 | + Off Stage = "off" |
| 77 | + |
| 78 | + // DualWrite - write to both "old" and "new", "old" is authoritative for reads |
| 79 | + DualWrite Stage = "dualwrite" |
| 80 | + |
| 81 | + // Shadow - both "new" and "old" versions run with a preference for "old" |
| 82 | + Shadow Stage = "shadow" |
| 83 | + |
| 84 | + // Live - both "new" and "old" versions run with a preference for "new" |
| 85 | + Live Stage = "live" |
| 86 | + |
| 87 | + // RampDown - only read from "new", write to "old" and "new" |
| 88 | + RampDown Stage = "rampdown" |
| 89 | + |
| 90 | + // Complete - migration is done |
| 91 | + Complete Stage = "complete" |
| 92 | +) |
| 93 | + |
| 94 | +// ParseStage parses a MigrationStage from a string, or returns an error if the stage is unrecognized. |
| 95 | +func ParseStage(val string) (Stage, error) { |
| 96 | + switch val { |
| 97 | + case "off": |
| 98 | + return Off, nil |
| 99 | + case "dualwrite": |
| 100 | + return DualWrite, nil |
| 101 | + case "shadow": |
| 102 | + return Shadow, nil |
| 103 | + case "live": |
| 104 | + return Live, nil |
| 105 | + case "rampdown": |
| 106 | + return RampDown, nil |
| 107 | + case "complete": |
| 108 | + return Complete, nil |
| 109 | + default: |
| 110 | + return Off, fmt.Errorf("invalid stage %s provided", val) |
| 111 | + } |
| 112 | +} |
0 commit comments