|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package autosharding |
| 20 | + |
| 21 | +import ( |
| 22 | + "bytes" |
| 23 | + "fmt" |
| 24 | + "slices" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/google/go-cmp/cmp" |
| 28 | +) |
| 29 | + |
| 30 | +func (s) TestSliceMap_Lookup(t *testing.T) { |
| 31 | + sm := &sliceMap{ |
| 32 | + slices: []sliceMapEntry{ |
| 33 | + {startKey: []byte("b"), endpoints: []int{1}}, |
| 34 | + {startKey: []byte("d"), endpoints: []int{2}}, |
| 35 | + {startKey: []byte("f"), endpoints: []int{3}}, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + key []byte |
| 42 | + want int |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "exact-match-first", |
| 46 | + key: []byte("b"), |
| 47 | + want: 0, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "exact-match-middle", |
| 51 | + key: []byte("d"), |
| 52 | + want: 1, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "exact-match-last", |
| 56 | + key: []byte("f"), |
| 57 | + want: 2, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "between-first-and-middle", |
| 61 | + key: []byte("c"), |
| 62 | + want: 0, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "between-middle-and-last", |
| 66 | + key: []byte("e"), |
| 67 | + want: 1, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "after-last", |
| 71 | + key: []byte("g"), |
| 72 | + want: 2, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tc := range tests { |
| 77 | + t.Run(tc.name, func(t *testing.T) { |
| 78 | + if got := sm.lookup(tc.key); got != tc.want { |
| 79 | + t.Errorf("lookup(%q) = %d, want %d", tc.key, got, tc.want) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (s) TestSliceMap_Lookup_Empty(t *testing.T) { |
| 86 | + sm := &sliceMap{} |
| 87 | + if got := sm.lookup([]byte("a")); got != -1 { |
| 88 | + t.Errorf("lookup() on empty map = %d, want -1", got) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func (s) TestBuildSliceMap(t *testing.T) { |
| 93 | + // Setup endpointMap with unsorted order in map to verify deterministic |
| 94 | + // sorting of fallbackPool. |
| 95 | + epMap := &endpointMap{ |
| 96 | + m: map[string]*endpointState{ |
| 97 | + "hostC": {index: 2}, |
| 98 | + "hostA": {index: 0}, |
| 99 | + "hostB": {index: 1}, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + tests := []struct { |
| 104 | + name string |
| 105 | + assignment *assignment |
| 106 | + want *sliceMap |
| 107 | + }{ |
| 108 | + { |
| 109 | + name: "nil-assignment-startup", |
| 110 | + assignment: nil, |
| 111 | + want: &sliceMap{fallbackPool: []int{0, 1, 2}}, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "valid-assignment", |
| 115 | + assignment: &assignment{ |
| 116 | + endpointNames: []string{"hostA", "hostB", "hostC", "hostD"}, |
| 117 | + slices: []slice{ |
| 118 | + {startKey: []byte("a"), endpoints: []int{0, 1}}, // hostA, hostB -> indices 0, 1 |
| 119 | + {startKey: []byte("m"), endpoints: []int{1, 2}}, // hostB, hostC -> indices 1, 2 |
| 120 | + }, |
| 121 | + generation: 42, |
| 122 | + }, |
| 123 | + want: &sliceMap{ |
| 124 | + slices: []sliceMapEntry{ |
| 125 | + {startKey: []byte("a"), endpoints: []int{0, 1}}, |
| 126 | + {startKey: []byte("m"), endpoints: []int{1, 2}}, |
| 127 | + }, |
| 128 | + fallbackPool: []int{0, 1, 2}, |
| 129 | + generation: 42, |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "assignment-with-unknown-host", |
| 134 | + assignment: &assignment{ |
| 135 | + endpointNames: []string{"hostA", "hostUnknown", "hostC"}, |
| 136 | + slices: []slice{ |
| 137 | + {startKey: []byte("a"), endpoints: []int{0, 1}}, // hostUnknown is skipped |
| 138 | + }, |
| 139 | + generation: 43, |
| 140 | + }, |
| 141 | + want: &sliceMap{ |
| 142 | + slices: []sliceMapEntry{ |
| 143 | + {startKey: []byte("a"), endpoints: []int{0}}, // Only hostA (index 0) |
| 144 | + }, |
| 145 | + fallbackPool: []int{0, 1, 2}, |
| 146 | + generation: 43, |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + for _, tc := range tests { |
| 152 | + t.Run(tc.name, func(t *testing.T) { |
| 153 | + got := buildSliceMap(epMap, tc.assignment) |
| 154 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 155 | + t.Errorf("buildSliceMap() diff (-want +got):\n%s", diff) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func (e sliceMapEntry) Equal(b sliceMapEntry) bool { |
| 162 | + return bytes.Equal(e.startKey, b.startKey) && slices.Equal(e.endpoints, b.endpoints) |
| 163 | +} |
| 164 | + |
| 165 | +func (sm *sliceMap) Equal(b *sliceMap) bool { |
| 166 | + if sm == nil || b == nil { |
| 167 | + return sm == b |
| 168 | + } |
| 169 | + fallbackPoolEqual := slices.Equal(sm.fallbackPool, b.fallbackPool) |
| 170 | + slicesEqual := slices.EqualFunc(sm.slices, b.slices, func(x, y sliceMapEntry) bool { return x.Equal(y) }) |
| 171 | + return sm.generation == b.generation && fallbackPoolEqual && slicesEqual |
| 172 | +} |
| 173 | + |
| 174 | +func BenchmarkSliceMap_Lookup(b *testing.B) { |
| 175 | + for _, numSlices := range []int{1, 10, 100, 1000, 10000} { |
| 176 | + for _, keySize := range []int{16, 32, 64, 128, 256, 512} { |
| 177 | + b.Run(fmt.Sprintf("slices_%d_keySize_%d", numSlices, keySize), func(b *testing.B) { |
| 178 | + sm := &sliceMap{ |
| 179 | + slices: make([]sliceMapEntry, numSlices), |
| 180 | + } |
| 181 | + for i := 0; i < numSlices; i++ { |
| 182 | + // Generate lexicographically sorted keys to serve as slice |
| 183 | + // boundaries. We multiply by 1000 to create ranges (gaps) between |
| 184 | + // successive slices (e.g., Slice 0 covers [0, 1000), Slice 1 covers |
| 185 | + // [1000, 2000)). This allows us to test lookups that fall |
| 186 | + // *mid-slice*, rather than just exact matches. |
| 187 | + // |
| 188 | + // We use zero-padding on the left via "%0*d" (where * is the keySize |
| 189 | + // width). This fills the key to its full length (e.g., 512 bytes) |
| 190 | + // with leading zeros. Scanning long identical prefixes forces |
| 191 | + // bytes.Compare to traverse the full length, simulating a |
| 192 | + // conservative, worst-case latency scenario. |
| 193 | + key := fmt.Appendf(nil, "%0*d", keySize, i*1000) |
| 194 | + sm.slices[i] = sliceMapEntry{startKey: key, endpoints: []int{i}} |
| 195 | + } |
| 196 | + |
| 197 | + // Pre-generate a pool of lookup keys. This helps avoid |
| 198 | + // measuring string formatting overhead inside the timer loop. |
| 199 | + lookupKeys := make([][]byte, 10000) |
| 200 | + for i := 0; i < 10000; i++ { |
| 201 | + // Distribute the 10,000 lookup keys proportionally across the entire |
| 202 | + // synthetic keyspace [0, numSlices * 1000). This ensures a good mix |
| 203 | + // of boundary hits and interior hits. |
| 204 | + val := i * (numSlices * 1000) / 10000 |
| 205 | + lookupKeys[i] = fmt.Appendf(nil, "%0*d", keySize, val) |
| 206 | + } |
| 207 | + |
| 208 | + var i int |
| 209 | + for b.Loop() { |
| 210 | + sm.lookup(lookupKeys[i%10000]) |
| 211 | + i++ |
| 212 | + } |
| 213 | + }) |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments