|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package ttljob |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "sync/atomic" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/sql" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/sql/physicalplan" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +// NOTE: This test is for functions in ttljob.go. We already have |
| 22 | +// ttljob_test.go, but that is part of the ttljob_test package. This test is |
| 23 | +// specifically part of the ttljob package to access non-exported functions and |
| 24 | +// structs. Hence, the name '_internal_' in the file to signify that it accesses |
| 25 | +// internal functions. |
| 26 | + |
| 27 | +func TestReplanDecider(t *testing.T) { |
| 28 | + defer leaktest.AfterTest(t)() |
| 29 | + defer log.Scope(t).Close(t) |
| 30 | + |
| 31 | + testCases := []struct { |
| 32 | + desc string |
| 33 | + beforeNodes []base.SQLInstanceID |
| 34 | + afterNodes []base.SQLInstanceID |
| 35 | + threshold float64 |
| 36 | + expectReplan bool |
| 37 | + }{ |
| 38 | + { |
| 39 | + desc: "nodes don't change", |
| 40 | + beforeNodes: []base.SQLInstanceID{1, 2, 3}, |
| 41 | + afterNodes: []base.SQLInstanceID{1, 2, 3}, |
| 42 | + threshold: 0.1, |
| 43 | + expectReplan: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + desc: "one node is shutdown", |
| 47 | + beforeNodes: []base.SQLInstanceID{1, 2, 3}, |
| 48 | + afterNodes: []base.SQLInstanceID{1, 3}, |
| 49 | + threshold: 0.1, |
| 50 | + expectReplan: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + desc: "one node is brought online", |
| 54 | + beforeNodes: []base.SQLInstanceID{1, 2, 3}, |
| 55 | + afterNodes: []base.SQLInstanceID{1, 2, 3, 4}, |
| 56 | + threshold: 0.1, |
| 57 | + expectReplan: false, |
| 58 | + }, |
| 59 | + { |
| 60 | + desc: "one node is replaced", |
| 61 | + beforeNodes: []base.SQLInstanceID{1, 2, 3}, |
| 62 | + afterNodes: []base.SQLInstanceID{1, 2, 4}, |
| 63 | + threshold: 0.1, |
| 64 | + expectReplan: true, |
| 65 | + }, |
| 66 | + { |
| 67 | + desc: "multiple nodes shutdown", |
| 68 | + beforeNodes: []base.SQLInstanceID{1, 2, 3, 4, 5}, |
| 69 | + afterNodes: []base.SQLInstanceID{1, 3}, |
| 70 | + threshold: 0.1, |
| 71 | + expectReplan: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + desc: "all nodes replaced", |
| 75 | + beforeNodes: []base.SQLInstanceID{1, 2, 3}, |
| 76 | + afterNodes: []base.SQLInstanceID{4, 5, 6}, |
| 77 | + threshold: 0.1, |
| 78 | + expectReplan: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + desc: "threshold boundary: exactly at threshold", |
| 82 | + beforeNodes: []base.SQLInstanceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, |
| 83 | + afterNodes: []base.SQLInstanceID{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 84 | + threshold: 0.1, |
| 85 | + expectReplan: false, |
| 86 | + }, |
| 87 | + { |
| 88 | + desc: "threshold boundary: just above threshold", |
| 89 | + beforeNodes: []base.SQLInstanceID{1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 90 | + afterNodes: []base.SQLInstanceID{1, 2, 3, 4, 5, 6, 7, 8}, |
| 91 | + threshold: 0.1, |
| 92 | + expectReplan: true, |
| 93 | + }, |
| 94 | + { |
| 95 | + desc: "threshold disabled", |
| 96 | + beforeNodes: []base.SQLInstanceID{1, 2, 3}, |
| 97 | + afterNodes: []base.SQLInstanceID{1, 2}, |
| 98 | + threshold: 0.0, |
| 99 | + expectReplan: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + desc: "large scale: many nodes lost", |
| 103 | + beforeNodes: []base.SQLInstanceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, |
| 104 | + afterNodes: []base.SQLInstanceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, |
| 105 | + threshold: 0.1, |
| 106 | + expectReplan: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + desc: "mixed scenario: nodes added and removed", |
| 110 | + beforeNodes: []base.SQLInstanceID{1, 2, 3, 4, 5}, |
| 111 | + afterNodes: []base.SQLInstanceID{1, 3, 5, 6, 7, 8}, |
| 112 | + threshold: 0.1, |
| 113 | + expectReplan: true, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, testCase := range testCases { |
| 118 | + t.Run(testCase.desc, func(t *testing.T) { |
| 119 | + // Create atomic counter and set stability window to 1 for immediate replan (current behavior) |
| 120 | + consecutiveReplanDecisions := &atomic.Int64{} |
| 121 | + decider := replanDecider(consecutiveReplanDecisions, func() int64 { return 1 }, func() float64 { return testCase.threshold }) |
| 122 | + ctx := context.Background() |
| 123 | + oldPlan := &sql.PhysicalPlan{} |
| 124 | + oldPlan.PhysicalInfrastructure = &physicalplan.PhysicalInfrastructure{Processors: nil} |
| 125 | + for _, nodeID := range testCase.beforeNodes { |
| 126 | + oldPlan.Processors = append(oldPlan.Processors, physicalplan.Processor{SQLInstanceID: nodeID}) |
| 127 | + } |
| 128 | + newPlan := &sql.PhysicalPlan{} |
| 129 | + newPlan.PhysicalInfrastructure = &physicalplan.PhysicalInfrastructure{Processors: nil} |
| 130 | + for _, nodeID := range testCase.afterNodes { |
| 131 | + newPlan.Processors = append(newPlan.Processors, physicalplan.Processor{SQLInstanceID: nodeID}) |
| 132 | + } |
| 133 | + replan := decider(ctx, oldPlan, newPlan) |
| 134 | + require.Equal(t, testCase.expectReplan, replan) |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestReplanDeciderStabilityWindow(t *testing.T) { |
| 140 | + defer leaktest.AfterTest(t)() |
| 141 | + defer log.Scope(t).Close(t) |
| 142 | + |
| 143 | + testCases := []struct { |
| 144 | + desc string |
| 145 | + stabilityWindow int64 |
| 146 | + threshold float64 |
| 147 | + planChanges [][]base.SQLInstanceID // sequence of plan changes |
| 148 | + expectedReplans []bool // expected replan decision for each change |
| 149 | + }{ |
| 150 | + { |
| 151 | + desc: "stability window 1 - immediate replan", |
| 152 | + stabilityWindow: 1, |
| 153 | + threshold: 0.1, |
| 154 | + planChanges: [][]base.SQLInstanceID{{2, 3}, {2, 4}, {3, 4}}, |
| 155 | + expectedReplans: []bool{true, true, true}, |
| 156 | + }, |
| 157 | + { |
| 158 | + desc: "stability window 2 - requires consecutive decisions", |
| 159 | + stabilityWindow: 2, |
| 160 | + threshold: 0.1, |
| 161 | + planChanges: [][]base.SQLInstanceID{{2, 3}, {2, 4}, {1, 2, 3}}, |
| 162 | + expectedReplans: []bool{false, true, false}, // first false, second true (meets window), third false (reset) |
| 163 | + }, |
| 164 | + { |
| 165 | + desc: "stability window 2 - interrupted sequence", |
| 166 | + stabilityWindow: 2, |
| 167 | + threshold: 0.1, |
| 168 | + planChanges: [][]base.SQLInstanceID{{2, 3}, {1, 2, 3}, {2, 4}, {3, 4}}, |
| 169 | + expectedReplans: []bool{false, false, false, true}, // interrupted, then consecutive |
| 170 | + }, |
| 171 | + { |
| 172 | + desc: "stability window 3 - three consecutive needed", |
| 173 | + stabilityWindow: 3, |
| 174 | + threshold: 0.1, |
| 175 | + planChanges: [][]base.SQLInstanceID{{2, 3}, {2, 4}, {3, 4}, {1, 2, 3}}, |
| 176 | + expectedReplans: []bool{false, false, true, false}, // third one triggers replan |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + for _, testCase := range testCases { |
| 181 | + t.Run(testCase.desc, func(t *testing.T) { |
| 182 | + consecutiveReplanDecisions := &atomic.Int64{} |
| 183 | + decider := replanDecider( |
| 184 | + consecutiveReplanDecisions, |
| 185 | + func() int64 { return testCase.stabilityWindow }, |
| 186 | + func() float64 { return testCase.threshold }, |
| 187 | + ) |
| 188 | + ctx := context.Background() |
| 189 | + |
| 190 | + // Use initial plan with nodes 1,2,3 |
| 191 | + initialPlan := &sql.PhysicalPlan{} |
| 192 | + initialPlan.PhysicalInfrastructure = &physicalplan.PhysicalInfrastructure{Processors: nil} |
| 193 | + for _, nodeID := range []base.SQLInstanceID{1, 2, 3} { |
| 194 | + initialPlan.Processors = append(initialPlan.Processors, physicalplan.Processor{SQLInstanceID: nodeID}) |
| 195 | + } |
| 196 | + |
| 197 | + for i, nodes := range testCase.planChanges { |
| 198 | + newPlan := &sql.PhysicalPlan{} |
| 199 | + newPlan.PhysicalInfrastructure = &physicalplan.PhysicalInfrastructure{Processors: nil} |
| 200 | + for _, nodeID := range nodes { |
| 201 | + newPlan.Processors = append(newPlan.Processors, physicalplan.Processor{SQLInstanceID: nodeID}) |
| 202 | + } |
| 203 | + |
| 204 | + replan := decider(ctx, initialPlan, newPlan) |
| 205 | + if replan != testCase.expectedReplans[i] { |
| 206 | + t.Errorf("step %d: expected replan=%v, got %v (consecutive count: %d)", i, testCase.expectedReplans[i], replan, consecutiveReplanDecisions.Load()) |
| 207 | + } |
| 208 | + |
| 209 | + // Update initial plan for next iteration to maintain state |
| 210 | + initialPlan = newPlan |
| 211 | + } |
| 212 | + }) |
| 213 | + } |
| 214 | +} |
0 commit comments