|
49 | 49 | import org.junit.jupiter.api.BeforeAll;
|
50 | 50 | import org.junit.jupiter.api.Test;
|
51 | 51 | import org.junit.jupiter.api.extension.RegisterExtension;
|
| 52 | +import org.junit.jupiter.params.ParameterizedTest; |
| 53 | +import org.junit.jupiter.params.provider.ValueSource; |
52 | 54 |
|
53 | 55 | import java.util.ArrayList;
|
54 | 56 | import java.util.Arrays;
|
@@ -177,47 +179,63 @@ void testLeaderAndIsr() throws Exception {
|
177 | 179 | assertThat(zookeeperClient.getLeaderAndIsr(tableBucket)).isEmpty();
|
178 | 180 | }
|
179 | 181 |
|
180 |
| - @Test |
181 |
| - void testBatchCreateLeaderAndIsr() throws Exception { |
182 |
| - List<RegisterTableBucketLeadAndIsrInfo> noPartitionTableBucket = new ArrayList<>(); |
183 |
| - // non-partition table |
184 |
| - List<LeaderAndIsr> noPartitionleaderAndIsrList = new ArrayList<>(); |
| 182 | + @ParameterizedTest |
| 183 | + @ValueSource(booleans = {true, false}) |
| 184 | + void testBatchCreateAndUpdateLeaderAndIsr(boolean isPartitionTable) throws Exception { |
| 185 | + List<RegisterTableBucketLeadAndIsrInfo> tableBucketInfo = new ArrayList<>(); |
| 186 | + List<LeaderAndIsr> leaderAndIsrList = new ArrayList<>(); |
185 | 187 | for (int i = 0; i < 100; i++) {
|
186 |
| - TableBucket tableBucket = new TableBucket(1, i); |
| 188 | + TableBucket tableBucket = |
| 189 | + isPartitionTable ? new TableBucket(1, 2L, i) : new TableBucket(1, i); |
187 | 190 | LeaderAndIsr leaderAndIsr =
|
188 | 191 | new LeaderAndIsr(i, 10, Arrays.asList(i + 1, i + 2, i + 3), 100, 1000);
|
189 |
| - noPartitionleaderAndIsrList.add(leaderAndIsr); |
190 |
| - noPartitionTableBucket.add( |
191 |
| - new RegisterTableBucketLeadAndIsrInfo(tableBucket, leaderAndIsr, null, null)); |
| 192 | + leaderAndIsrList.add(leaderAndIsr); |
| 193 | + RegisterTableBucketLeadAndIsrInfo info = |
| 194 | + isPartitionTable |
| 195 | + ? new RegisterTableBucketLeadAndIsrInfo( |
| 196 | + tableBucket, leaderAndIsr, "partition" + i, null) |
| 197 | + : new RegisterTableBucketLeadAndIsrInfo( |
| 198 | + tableBucket, leaderAndIsr, null, null); |
| 199 | + tableBucketInfo.add(info); |
192 | 200 | }
|
193 |
| - zookeeperClient.batchRegisterLeaderAndIsrForTablePartition(noPartitionTableBucket); |
| 201 | + // batch create |
| 202 | + zookeeperClient.batchRegisterLeaderAndIsrForTablePartition(tableBucketInfo); |
194 | 203 |
|
195 | 204 | for (int i = 0; i < 100; i++) {
|
| 205 | + // each should register successful |
196 | 206 | Optional<LeaderAndIsr> optionalLeaderAndIsr =
|
197 |
| - zookeeperClient.getLeaderAndIsr(noPartitionTableBucket.get(i).getTableBucket()); |
| 207 | + zookeeperClient.getLeaderAndIsr(tableBucketInfo.get(i).getTableBucket()); |
198 | 208 | assertThat(optionalLeaderAndIsr.isPresent()).isTrue();
|
199 |
| - assertThat(optionalLeaderAndIsr.get()).isIn(noPartitionleaderAndIsrList); |
| 209 | + assertThat(optionalLeaderAndIsr.get()).isIn(leaderAndIsrList); |
200 | 210 | }
|
201 | 211 |
|
202 |
| - List<RegisterTableBucketLeadAndIsrInfo> partitionTableBucket = new ArrayList<>(); |
203 |
| - // partition table |
204 |
| - List<LeaderAndIsr> partitionleaderAndIsrList = new ArrayList<>(); |
205 |
| - for (int i = 0; i < 100; i++) { |
206 |
| - TableBucket tableBucket = new TableBucket(1, 2L, i); |
207 |
| - LeaderAndIsr leaderAndIsr = |
208 |
| - new LeaderAndIsr(i, 10, Arrays.asList(i + 1, i + 2, i + 3), 100, 1000); |
209 |
| - partitionleaderAndIsrList.add(leaderAndIsr); |
210 |
| - partitionTableBucket.add( |
211 |
| - new RegisterTableBucketLeadAndIsrInfo( |
212 |
| - tableBucket, leaderAndIsr, "partition" + i, null)); |
213 |
| - } |
214 |
| - |
215 |
| - zookeeperClient.batchRegisterLeaderAndIsrForTablePartition(partitionTableBucket); |
| 212 | + Map<TableBucket, LeaderAndIsr> updateMap = |
| 213 | + tableBucketInfo.stream() |
| 214 | + .collect( |
| 215 | + Collectors.toMap( |
| 216 | + RegisterTableBucketLeadAndIsrInfo::getTableBucket, |
| 217 | + RegisterTableBucketLeadAndIsrInfo::getLeaderAndIsr)); |
| 218 | + List<LeaderAndIsr> leaderAndIsrUpdateList = new ArrayList<>(); |
| 219 | + updateMap |
| 220 | + .entrySet() |
| 221 | + .forEach( |
| 222 | + entry -> { |
| 223 | + LeaderAndIsr originalLeaderAndIsr = entry.getValue(); |
| 224 | + LeaderAndIsr adjustLeaderAndIsr = |
| 225 | + originalLeaderAndIsr.newLeaderAndIsr( |
| 226 | + LeaderAndIsr.NO_LEADER, |
| 227 | + originalLeaderAndIsr.isr().subList(0, 1)); |
| 228 | + leaderAndIsrUpdateList.add(adjustLeaderAndIsr); |
| 229 | + entry.setValue(adjustLeaderAndIsr); |
| 230 | + }); |
| 231 | + // batch update |
| 232 | + zookeeperClient.batchUpdateLeaderAndIsr(updateMap); |
216 | 233 | for (int i = 0; i < 100; i++) {
|
| 234 | + // each should update successful |
217 | 235 | Optional<LeaderAndIsr> optionalLeaderAndIsr =
|
218 |
| - zookeeperClient.getLeaderAndIsr(partitionTableBucket.get(i).getTableBucket()); |
| 236 | + zookeeperClient.getLeaderAndIsr(tableBucketInfo.get(i).getTableBucket()); |
219 | 237 | assertThat(optionalLeaderAndIsr.isPresent()).isTrue();
|
220 |
| - assertThat(optionalLeaderAndIsr.get()).isIn(partitionleaderAndIsrList); |
| 238 | + assertThat(optionalLeaderAndIsr.get()).isIn(leaderAndIsrUpdateList); |
221 | 239 | }
|
222 | 240 | }
|
223 | 241 |
|
|
0 commit comments