-
Notifications
You must be signed in to change notification settings - Fork 13.6k
/
ReplicaVerificationTool.java
744 lines (665 loc) · 35 KB
/
ReplicaVerificationTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.tools;
import joptsimple.OptionParser;
import joptsimple.OptionSpec;
import org.apache.kafka.clients.ApiVersions;
import org.apache.kafka.clients.ClientRequest;
import org.apache.kafka.clients.ClientResponse;
import org.apache.kafka.clients.ClientUtils;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.ManualMetadataUpdater;
import org.apache.kafka.clients.NetworkClient;
import org.apache.kafka.clients.NetworkClientUtils;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.ListTopicsOptions;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.TopicPartitionReplica;
import org.apache.kafka.common.Uuid;
import org.apache.kafka.common.message.FetchResponseData;
import org.apache.kafka.common.metrics.Metrics;
import org.apache.kafka.common.network.ChannelBuilder;
import org.apache.kafka.common.network.NetworkReceive;
import org.apache.kafka.common.network.Selectable;
import org.apache.kafka.common.network.Selector;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.record.RecordBatch;
import org.apache.kafka.common.requests.AbstractRequest;
import org.apache.kafka.common.requests.FetchRequest;
import org.apache.kafka.common.requests.FetchResponse;
import org.apache.kafka.common.requests.ListOffsetsRequest;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.LogContext;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.server.util.CommandDefaultOptions;
import org.apache.kafka.server.util.CommandLineUtils;
import org.apache.kafka.server.util.ShutdownableThread;
import org.apache.kafka.server.util.TopicFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import static java.lang.String.format;
/**
* For verifying the consistency among replicas.
* <p>
* 1. start a fetcher on every broker
* 2. each fetcher does the following
* 2.1 issues fetch request
* 2.2 puts the fetched result in a shared buffer
* 2.3 waits for all other fetchers to finish step 2.2
* 2.4 one of the fetchers verifies the consistency of fetched results among replicas
* <p>
* The consistency verification is up to the high watermark. The tool reports the
* max lag between the verified offset and the high watermark among all partitions.
* <p>
* If a broker goes down, the verification of the partitions on that broker is delayed
* until the broker is up again.
* <p>
* Caveats:
* 1. The tool needs all brokers to be up at startup time.
* 2. The tool doesn't handle out of range offsets.
*/
public class ReplicaVerificationTool {
private static final Logger LOG = LoggerFactory.getLogger(ReplicaVerificationTool.class);
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
public static void main(String[] args) {
try {
ReplicaVerificationToolOptions options = new ReplicaVerificationToolOptions(args);
// getting topic metadata
LOG.info("Getting topic metadata...");
String brokerList = options.brokerHostsAndPorts();
try (Admin adminClient = createAdminClient(brokerList)) {
Collection<TopicDescription> topicsMetadata = listTopicsMetadata(adminClient);
Map<Integer, Node> brokerInfo = brokerDetails(adminClient);
Map<String, Uuid> topicIds = topicsMetadata.stream().collect(Collectors.toMap(TopicDescription::name, TopicDescription::topicId));
List<TopicDescription> filteredTopicMetadata = topicsMetadata.stream().filter(
topicMetadata -> options.topicsIncludeFilter().isTopicAllowed(topicMetadata.name(), false)
).collect(Collectors.toList());
if (filteredTopicMetadata.isEmpty()) {
LOG.error("No topics found. {} if specified, is either filtering out all topics or there is no topic.", options.topicsIncludeOpt);
Exit.exit(1);
}
List<TopicPartitionReplica> topicPartitionReplicas = filteredTopicMetadata.stream().flatMap(
topicMetadata -> topicMetadata.partitions().stream().flatMap(
partitionMetadata -> partitionMetadata.replicas().stream().map(
node -> new TopicPartitionReplica(topicMetadata.name(), partitionMetadata.partition(), node.id())
)
)
).collect(Collectors.toList());
LOG.debug("Selected topic partitions: {}", topicPartitionReplicas);
Map<Integer, List<TopicPartition>> brokerToTopicPartitions = topicPartitionReplicas.stream()
.collect(Collectors.groupingBy(
TopicPartitionReplica::brokerId,
Collectors.mapping(
replica -> new TopicPartition(replica.topic(), replica.partition()),
Collectors.toList()
)
));
LOG.debug("Topic partitions per broker: {}", brokerToTopicPartitions);
Map<TopicPartition, Integer> expectedReplicasPerTopicPartition = topicPartitionReplicas.stream()
.collect(Collectors.groupingBy(
replica -> new TopicPartition(replica.topic(), replica.partition()),
Collectors.collectingAndThen(
Collectors.toList(),
List::size
)
));
LOG.debug("Expected replicas per topic partition: {}", expectedReplicasPerTopicPartition);
List<TopicPartition> topicPartitions = filteredTopicMetadata.stream()
.flatMap(topicMetadata -> topicMetadata.partitions().stream()
.map(partitionMetadata -> new TopicPartition(topicMetadata.name(), partitionMetadata.partition()))
)
.collect(Collectors.toList());
Properties consumerProps = consumerConfig(brokerList);
ReplicaBuffer replicaBuffer = new ReplicaBuffer(expectedReplicasPerTopicPartition,
initialOffsets(topicPartitions, consumerProps, options.initialOffsetTime()),
brokerToTopicPartitions.size(), options.reportInterval());
// create all replica fetcher threads
int verificationBrokerId = brokerToTopicPartitions.entrySet().iterator().next().getKey();
AtomicInteger counter = new AtomicInteger(0);
List<ReplicaFetcher> fetcherThreads = brokerToTopicPartitions.entrySet().stream()
.map(entry -> {
int brokerId = entry.getKey();
Iterable<TopicPartition> partitions = entry.getValue();
return new ReplicaFetcher(
"ReplicaFetcher-" + brokerId,
brokerInfo.get(brokerId),
partitions,
topicIds,
replicaBuffer,
options.fetchSize(),
options.maxWaitMs(),
1,
brokerId == verificationBrokerId,
consumerProps,
counter.incrementAndGet()
);
})
.collect(Collectors.toList());
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOG.info("Stopping all fetchers");
fetcherThreads.forEach(replicaFetcher -> {
try {
replicaFetcher.shutdown();
} catch (InterruptedException ignored) {
}
});
}));
fetcherThreads.forEach(Thread::start);
System.out.printf("%s: verification process is started%n",
DATE_FORMAT.format(new Date(Time.SYSTEM.milliseconds())));
}
} catch (Throwable e) {
System.err.println(e.getMessage());
System.err.println(Utils.stackTrace(e));
Exit.exit(1);
}
}
private static Map<TopicPartition, Long> initialOffsets(List<TopicPartition> topicPartitions, Properties consumerConfig, long initialOffsetTime) {
try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerConfig)) {
if (ListOffsetsRequest.LATEST_TIMESTAMP == initialOffsetTime) {
return consumer.endOffsets(topicPartitions).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} else if (ListOffsetsRequest.EARLIEST_TIMESTAMP == initialOffsetTime) {
return consumer.beginningOffsets(topicPartitions).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} else {
Map<TopicPartition, Long> timestampsToSearch = topicPartitions.stream()
.collect(Collectors.toMap(Function.identity(), tp -> initialOffsetTime));
return consumer.offsetsForTimes(timestampsToSearch).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().offset()));
}
}
}
private static Properties consumerConfig(String brokerUrl) {
Properties properties = new Properties();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerUrl);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "ReplicaVerification");
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return properties;
}
private static Map<Integer, Node> brokerDetails(Admin adminClient) throws ExecutionException, InterruptedException {
return adminClient.describeCluster().nodes().get().stream().collect(Collectors.toMap(Node::id, Function.identity()));
}
private static Collection<TopicDescription> listTopicsMetadata(Admin adminClient) throws ExecutionException, InterruptedException {
Set<String> topics = adminClient.listTopics(new ListTopicsOptions().listInternal(true)).names().get();
return adminClient.describeTopics(topics).allTopicNames().get().values();
}
private static Admin createAdminClient(String brokerList) {
Properties props = new Properties();
props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, brokerList);
return Admin.create(props);
}
private static class ReplicaVerificationToolOptions extends CommandDefaultOptions {
private final OptionSpec<String> brokerListOpt;
private final OptionSpec<Integer> fetchSizeOpt;
private final OptionSpec<Integer> maxWaitMsOpt;
private final OptionSpec<String> topicWhiteListOpt;
private final OptionSpec<String> topicsIncludeOpt;
private final OptionSpec<Long> initialOffsetTimeOpt;
private final OptionSpec<Long> reportIntervalOpt;
ReplicaVerificationToolOptions(String[] args) {
super(args);
brokerListOpt = parser.accepts("broker-list", "REQUIRED: The list of hostname and port of the server to connect to.")
.withRequiredArg()
.describedAs("hostname:port,...,hostname:port")
.ofType(String.class);
fetchSizeOpt = parser.accepts("fetch-size", "The fetch size of each request.")
.withRequiredArg()
.describedAs("bytes")
.ofType(Integer.class)
.defaultsTo(ConsumerConfig.DEFAULT_MAX_PARTITION_FETCH_BYTES);
maxWaitMsOpt = parser.accepts("max-wait-ms", "The max amount of time each fetch request waits.")
.withRequiredArg()
.describedAs("ms")
.ofType(Integer.class)
.defaultsTo(1_000);
topicWhiteListOpt = parser.accepts("topic-white-list", "DEPRECATED use --topics-include instead; " +
"ignored if --topics-include specified. List of topics to verify replica consistency.")
.withRequiredArg()
.describedAs("Java regex (String)")
.ofType(String.class)
.defaultsTo(".*");
topicsIncludeOpt = parser.accepts("topics-include", "List of topics to verify replica consistency.")
.withRequiredArg()
.describedAs("Java regex (String)")
.ofType(String.class)
.defaultsTo(".*");
initialOffsetTimeOpt = parser.accepts("time", "Timestamp for getting the initial offsets.")
.withRequiredArg()
.describedAs("timestamp/-1(latest)/-2(earliest)")
.ofType(Long.class)
.defaultsTo(-1L);
reportIntervalOpt = parser.accepts("report-interval-ms", "The reporting interval.")
.withRequiredArg()
.describedAs("ms")
.ofType(Long.class)
.defaultsTo(30_000L);
options = parser.parse(args);
if (args.length == 0 || options.has(helpOpt)) {
CommandLineUtils.printUsageAndExit(parser, "Validate that all replicas for a set of topics have the same data.");
}
if (options.has(versionOpt)) {
CommandLineUtils.printVersionAndExit();
}
CommandLineUtils.checkRequiredArgs(parser, options, brokerListOpt);
CommandLineUtils.checkInvalidArgs(parser, options, topicsIncludeOpt, topicWhiteListOpt);
}
String brokerHostsAndPorts() {
String brokerList = options.valueOf(brokerListOpt);
validateBrokerList(parser, brokerList);
return brokerList;
}
void validateBrokerList(OptionParser parser, String brokerList) {
if (parser == null || brokerList == null) {
throw new RuntimeException("No option parser or broker list found");
}
if (brokerList.isEmpty()) {
CommandLineUtils.printUsageAndExit(parser, "Empty broker list option");
}
String[] hostPorts;
if (brokerList.contains(",")) hostPorts = brokerList.split(",");
else hostPorts = new String[]{brokerList};
String[] validHostPort = Arrays.stream(hostPorts)
.filter(hostPortData -> Utils.getPort(hostPortData) != null)
.toArray(String[]::new);
if (validHostPort.length == 0 || validHostPort.length != hostPorts.length) {
CommandLineUtils.printUsageAndExit(parser, "Invalid broker list option");
}
}
TopicFilter.IncludeList topicsIncludeFilter() {
String regex = options.valueOf(options.has(topicsIncludeOpt) ? topicsIncludeOpt : topicWhiteListOpt);
try {
Pattern.compile(regex);
} catch (PatternSyntaxException e) {
throw new RuntimeException(format("%s is an invalid regex", regex));
}
return new TopicFilter.IncludeList(regex);
}
int fetchSize() {
return options.valueOf(fetchSizeOpt);
}
int maxWaitMs() {
return options.valueOf(maxWaitMsOpt);
}
long initialOffsetTime() {
return options.valueOf(initialOffsetTimeOpt);
}
long reportInterval() {
return options.valueOf(reportIntervalOpt);
}
}
private static class MessageInfo {
final int replicaId;
final long offset;
final long nextOffset;
final long checksum;
MessageInfo(int replicaId, long offset, long nextOffset, long checksum) {
this.replicaId = replicaId;
this.offset = offset;
this.nextOffset = nextOffset;
this.checksum = checksum;
}
}
protected static class ReplicaBuffer {
private final Map<TopicPartition, Integer> expectedReplicasPerTopicPartition;
private final int expectedNumFetchers;
private final long reportInterval;
private final Map<TopicPartition, Long> fetchOffsetMap;
private final Map<TopicPartition, Map<Integer, FetchResponseData.PartitionData>> recordsCache;
private final AtomicReference<CountDownLatch> fetcherBarrier;
private final AtomicReference<CountDownLatch> verificationBarrier;
private volatile long lastReportTime;
private long maxLag;
private long offsetWithMaxLag;
private TopicPartition maxLagTopicAndPartition;
ReplicaBuffer(Map<TopicPartition, Integer> expectedReplicasPerTopicPartition,
Map<TopicPartition, Long> initialOffsets,
int expectedNumFetchers,
long reportInterval) {
this.expectedReplicasPerTopicPartition = expectedReplicasPerTopicPartition;
this.expectedNumFetchers = expectedNumFetchers;
this.reportInterval = reportInterval;
this.fetchOffsetMap = new HashMap<>();
this.recordsCache = new HashMap<>();
this.fetcherBarrier = new AtomicReference<>(new CountDownLatch(expectedNumFetchers));
this.verificationBarrier = new AtomicReference<>(new CountDownLatch(1));
this.lastReportTime = Time.SYSTEM.milliseconds();
this.maxLag = -1L;
this.offsetWithMaxLag = -1L;
for (TopicPartition topicPartition : expectedReplicasPerTopicPartition.keySet()) {
recordsCache.put(topicPartition, new HashMap<>());
}
// set initial offsets
for (Map.Entry<TopicPartition, Long> entry : initialOffsets.entrySet()) {
TopicPartition tp = entry.getKey();
Long offset = entry.getValue();
fetchOffsetMap.put(tp, offset);
}
}
void createNewFetcherBarrier() {
fetcherBarrier.set(new CountDownLatch(expectedNumFetchers));
}
CountDownLatch getFetcherBarrier() {
return fetcherBarrier.get();
}
void createNewVerificationBarrier() {
verificationBarrier.set(new CountDownLatch(1));
}
CountDownLatch getVerificationBarrier() {
return verificationBarrier.get();
}
void addFetchedData(TopicPartition topicPartition,
int replicaId,
FetchResponseData.PartitionData partitionData) {
recordsCache.get(topicPartition).put(replicaId, partitionData);
}
long getOffset(TopicPartition topicPartition) {
return fetchOffsetMap.get(topicPartition);
}
void verifyCheckSum(Consumer<String> println) {
LOG.debug("Begin verification");
maxLag = -1L;
for (Map.Entry<TopicPartition, Map<Integer, FetchResponseData.PartitionData>> cacheEntry : recordsCache.entrySet()) {
TopicPartition topicPartition = cacheEntry.getKey();
Map<Integer, FetchResponseData.PartitionData> fetchResponsePerReplica = cacheEntry.getValue();
LOG.debug("Verifying {}", topicPartition);
assert fetchResponsePerReplica.size() == expectedReplicasPerTopicPartition.get(topicPartition) :
"fetched " + fetchResponsePerReplica.size() + " replicas for " + topicPartition +
", but expected " + expectedReplicasPerTopicPartition.get(topicPartition) + " replicas";
Map<Integer, Iterator<? extends RecordBatch>> recordBatchIteratorMap = new HashMap<>();
for (Map.Entry<Integer, FetchResponseData.PartitionData> fetchResEntry : fetchResponsePerReplica.entrySet()) {
int replicaId = fetchResEntry.getKey();
FetchResponseData.PartitionData fetchResponse = fetchResEntry.getValue();
Iterator<? extends RecordBatch> recordIterator =
FetchResponse.recordsOrFail(fetchResponse).batches().iterator();
recordBatchIteratorMap.put(replicaId, recordIterator);
}
long maxHw = fetchResponsePerReplica.values().stream()
.mapToLong(FetchResponseData.PartitionData::highWatermark)
.max().orElse(-1L);
boolean isMessageInAllReplicas = true;
// iterate one message at a time from every replica, until high watermark is reached
while (isMessageInAllReplicas) {
Optional<MessageInfo> messageInfoFromFirstReplicaOpt = Optional.empty();
for (Map.Entry<Integer, Iterator<? extends RecordBatch>> batchEntry : recordBatchIteratorMap.entrySet()) {
int replicaId = batchEntry.getKey();
Iterator<? extends RecordBatch> recordBatchIterator = batchEntry.getValue();
try {
if (recordBatchIterator.hasNext()) {
RecordBatch batch = recordBatchIterator.next();
// only verify up to the high watermark
if (batch.lastOffset() >= fetchResponsePerReplica.get(replicaId).highWatermark()) {
isMessageInAllReplicas = false;
} else {
if (!messageInfoFromFirstReplicaOpt.isPresent()) {
messageInfoFromFirstReplicaOpt = Optional.of(
new MessageInfo(replicaId, batch.lastOffset(), batch.nextOffset(), batch.checksum())
);
} else {
MessageInfo messageInfoFromFirstReplica = messageInfoFromFirstReplicaOpt.get();
if (messageInfoFromFirstReplica.offset != batch.lastOffset()) {
println.accept(DATE_FORMAT.format(new Date(Time.SYSTEM.milliseconds())) +
": partition " + topicPartition +
": replica " + messageInfoFromFirstReplica.replicaId +
"'s offset " + messageInfoFromFirstReplica.offset +
" doesn't match replica " + replicaId +
"'s offset " + batch.lastOffset());
Exit.exit(1);
}
if (messageInfoFromFirstReplica.checksum != batch.checksum())
println.accept(DATE_FORMAT.format(new Date(Time.SYSTEM.milliseconds())) +
": partition " + topicPartition +
" has unmatched checksum at offset " + batch.lastOffset() +
"; replica " + messageInfoFromFirstReplica.replicaId +
"'s checksum " + messageInfoFromFirstReplica.checksum +
"; replica " + replicaId + "'s checksum " + batch.checksum());
}
}
} else {
isMessageInAllReplicas = false;
}
} catch (Throwable t) {
throw new RuntimeException("Error in processing replica " + replicaId +
" in partition " + topicPartition + " at offset " +
fetchOffsetMap.get(topicPartition), t);
}
}
if (isMessageInAllReplicas) {
long nextOffset = messageInfoFromFirstReplicaOpt.map(messageInfo -> messageInfo.nextOffset).orElse(-1L);
fetchOffsetMap.put(topicPartition, nextOffset);
LOG.debug("{} replicas match at offset {} for {}",
expectedReplicasPerTopicPartition.get(topicPartition), nextOffset, topicPartition);
}
}
if (maxHw - fetchOffsetMap.get(topicPartition) > maxLag) {
offsetWithMaxLag = fetchOffsetMap.get(topicPartition);
maxLag = maxHw - offsetWithMaxLag;
maxLagTopicAndPartition = topicPartition;
}
fetchResponsePerReplica.clear();
}
long currentTimeMs = Time.SYSTEM.milliseconds();
if (currentTimeMs - lastReportTime > reportInterval) {
println.accept(DATE_FORMAT.format(new Date(currentTimeMs)) +
": max lag is " + maxLag + " for partition " +
maxLagTopicAndPartition + " at offset " + offsetWithMaxLag +
" among " + recordsCache.size() + " partitions");
lastReportTime = currentTimeMs;
}
}
}
private static class ReplicaFetcher extends ShutdownableThread {
private final Node sourceBroker;
private final Iterable<TopicPartition> topicPartitions;
private final Map<String, Uuid> topicIds;
private final ReplicaBuffer replicaBuffer;
private final int fetchSize;
private final int maxWait;
private final int minBytes;
private final boolean doVerification;
private final ReplicaFetcherBlockingSend fetchEndpoint;
private final Map<Uuid, String> topicNames;
public ReplicaFetcher(String name,
Node sourceBroker,
Iterable<TopicPartition> topicPartitions,
Map<String, Uuid> topicIds,
ReplicaBuffer replicaBuffer,
int fetchSize,
int maxWait,
int minBytes,
boolean doVerification,
Properties consumerConfig,
int fetcherId) {
super(name);
this.sourceBroker = sourceBroker;
this.topicPartitions = topicPartitions;
this.topicIds = topicIds;
this.replicaBuffer = replicaBuffer;
this.fetchSize = fetchSize;
this.maxWait = maxWait;
this.minBytes = minBytes;
this.doVerification = doVerification;
this.fetchEndpoint = new ReplicaFetcherBlockingSend(sourceBroker, new ConsumerConfig(consumerConfig), new Metrics(),
Time.SYSTEM, fetcherId, "broker-" + FetchRequest.DEBUGGING_CONSUMER_ID + "-fetcher-" + fetcherId);
this.topicNames = topicIds.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
}
@Override
public void doWork() {
CountDownLatch fetcherBarrier = replicaBuffer.getFetcherBarrier();
CountDownLatch verificationBarrier = replicaBuffer.getVerificationBarrier();
Map<TopicPartition, FetchRequest.PartitionData> requestMap = new LinkedHashMap<>();
for (TopicPartition topicPartition : topicPartitions) {
requestMap.put(topicPartition, new FetchRequest.PartitionData(
topicIds.getOrDefault(topicPartition.topic(), Uuid.ZERO_UUID),
replicaBuffer.getOffset(topicPartition),
0L,
fetchSize,
Optional.empty()
));
}
FetchRequest.Builder fetchRequestBuilder = FetchRequest.Builder.forReplica(
ApiKeys.FETCH.latestVersion(),
FetchRequest.DEBUGGING_CONSUMER_ID,
-1,
maxWait,
minBytes,
requestMap
);
LOG.debug("Issuing fetch request");
FetchResponse fetchResponse = null;
try {
ClientResponse clientResponse = fetchEndpoint.sendRequest(fetchRequestBuilder);
fetchResponse = (FetchResponse) clientResponse.responseBody();
} catch (Throwable t) {
if (!isRunning())
throw new RuntimeException(t);
}
if (fetchResponse != null) {
fetchResponse.responseData(topicNames, ApiKeys.FETCH.latestVersion()).forEach((tp, partitionData) ->
replicaBuffer.addFetchedData(tp, sourceBroker.id(), partitionData));
} else {
for (TopicPartition topicAndPartition : topicPartitions) {
replicaBuffer.addFetchedData(
topicAndPartition,
sourceBroker.id(),
FetchResponse.partitionResponse(topicAndPartition.partition(), Errors.NONE)
);
}
}
fetcherBarrier.countDown();
LOG.debug("Done fetching");
// wait for all fetchers to finish
try {
fetcherBarrier.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
LOG.debug("Ready for verification");
// one of the fetchers will do the verification
if (doVerification) {
LOG.debug("Do verification");
replicaBuffer.verifyCheckSum(System.out::println);
replicaBuffer.createNewFetcherBarrier();
replicaBuffer.createNewVerificationBarrier();
LOG.debug("Created new barrier");
verificationBarrier.countDown();
}
try {
verificationBarrier.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
LOG.debug("Done verification");
}
}
private static class ReplicaFetcherBlockingSend {
private final Node sourceNode;
private final Time time;
private final int socketTimeout;
private final NetworkClient networkClient;
ReplicaFetcherBlockingSend(Node sourceNode,
ConsumerConfig consumerConfig,
Metrics metrics,
Time time,
int fetcherId,
String clientId) {
this.sourceNode = sourceNode;
this.time = time;
this.socketTimeout = consumerConfig.getInt(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG);
LogContext logContext = new LogContext();
ChannelBuilder channelBuilder = ClientUtils.createChannelBuilder(consumerConfig, time, logContext);
Selector selector = new Selector(
NetworkReceive.UNLIMITED,
consumerConfig.getLong(ConsumerConfig.CONNECTIONS_MAX_IDLE_MS_CONFIG),
metrics,
time,
"replica-fetcher",
new HashMap<String, String>() {{
put("broker-id", sourceNode.idString());
put("fetcher-id", String.valueOf(fetcherId));
}},
false,
channelBuilder,
logContext
);
this.networkClient = new NetworkClient(
selector,
new ManualMetadataUpdater(),
clientId,
1,
0,
0,
Selectable.USE_DEFAULT_BUFFER_SIZE,
consumerConfig.getInt(ConsumerConfig.RECEIVE_BUFFER_CONFIG),
consumerConfig.getInt(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG),
consumerConfig.getLong(ConsumerConfig.SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG),
consumerConfig.getLong(ConsumerConfig.SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG),
time,
false,
new ApiVersions(),
logContext
);
}
ClientResponse sendRequest(AbstractRequest.Builder<? extends AbstractRequest> requestBuilder) {
try {
if (!NetworkClientUtils.awaitReady(networkClient, sourceNode, time, socketTimeout))
throw new SocketTimeoutException("Failed to connect within " + socketTimeout + " ms");
else {
ClientRequest clientRequest = networkClient.newClientRequest(sourceNode.idString(),
requestBuilder, time.milliseconds(), true);
return NetworkClientUtils.sendAndReceive(networkClient, clientRequest, time);
}
} catch (Throwable e) {
networkClient.close(sourceNode.idString());
throw new RuntimeException(e);
}
}
}
}