use of org.apache.kafka.raft.LeaderAndEpoch in project kafka by apache.
the class MockMetaLogManagerListener method handleLeaderChange.
@Override
public synchronized void handleLeaderChange(LeaderAndEpoch newLeaderAndEpoch) {
LeaderAndEpoch oldLeaderAndEpoch = this.leaderAndEpoch;
this.leaderAndEpoch = newLeaderAndEpoch;
if (newLeaderAndEpoch.isLeader(nodeId)) {
StringBuilder bld = new StringBuilder();
bld.append(NEW_LEADER).append(" ").append(nodeId).append(" ").append(newLeaderAndEpoch.epoch());
serializedEvents.add(bld.toString());
} else if (oldLeaderAndEpoch.isLeader(nodeId)) {
StringBuilder bld = new StringBuilder();
bld.append(RENOUNCE).append(" ").append(newLeaderAndEpoch.epoch());
serializedEvents.add(bld.toString());
}
}
use of org.apache.kafka.raft.LeaderAndEpoch in project kafka by apache.
the class LocalLogManagerTestEnv method waitForLeader.
LeaderAndEpoch waitForLeader() throws InterruptedException {
AtomicReference<LeaderAndEpoch> value = new AtomicReference<>(null);
TestUtils.retryOnExceptionWithTimeout(20000, 3, () -> {
LeaderAndEpoch result = null;
for (LocalLogManager logManager : logManagers) {
LeaderAndEpoch leader = logManager.leaderAndEpoch();
int nodeId = logManager.nodeId().getAsInt();
if (leader.isLeader(nodeId)) {
if (result != null) {
throw new RuntimeException("node " + nodeId + " thinks it's the leader, but so does " + result.leaderId());
}
result = leader;
}
}
if (result == null) {
throw new RuntimeException("No leader found.");
}
value.set(result);
});
return value.get();
}
use of org.apache.kafka.raft.LeaderAndEpoch in project kafka by apache.
the class QuorumControllerTestEnv method activeController.
QuorumController activeController() throws InterruptedException {
AtomicReference<QuorumController> value = new AtomicReference<>(null);
TestUtils.retryOnExceptionWithTimeout(20000, 3, () -> {
LeaderAndEpoch leader = logEnv.leaderAndEpoch();
for (QuorumController controller : controllers) {
if (OptionalInt.of(controller.nodeId()).equals(leader.leaderId()) && controller.curClaimEpoch() == leader.epoch()) {
value.set(controller);
break;
}
}
if (value.get() == null) {
throw new RuntimeException(String.format("Expected to see %s as leader", leader));
}
});
return value.get();
}
use of org.apache.kafka.raft.LeaderAndEpoch in project kafka by apache.
the class SnapshotFileReader method handleControlBatch.
private void handleControlBatch(FileChannelRecordBatch batch) {
for (Iterator<Record> iter = batch.iterator(); iter.hasNext(); ) {
Record record = iter.next();
try {
short typeId = ControlRecordType.parseTypeId(record.key());
ControlRecordType type = ControlRecordType.fromTypeId(typeId);
switch(type) {
case LEADER_CHANGE:
LeaderChangeMessage message = new LeaderChangeMessage();
message.read(new ByteBufferAccessor(record.value()), (short) 0);
listener.handleLeaderChange(new LeaderAndEpoch(OptionalInt.of(message.leaderId()), batch.partitionLeaderEpoch()));
break;
default:
log.error("Ignoring control record with type {} at offset {}", type, record.offset());
}
} catch (Throwable e) {
log.error("unable to read control record at offset {}", record.offset(), e);
}
}
}
Aggregations