Search in sources :

Example 6 with LeaderAndEpoch

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());
    }
}
Also used : LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch)

Example 7 with LeaderAndEpoch

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();
}
Also used : LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 8 with LeaderAndEpoch

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();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch)

Example 9 with LeaderAndEpoch

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);
        }
    }
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) Record(org.apache.kafka.common.record.Record) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ControlRecordType(org.apache.kafka.common.record.ControlRecordType)

Aggregations

LeaderAndEpoch (org.apache.kafka.raft.LeaderAndEpoch)9 Test (org.junit.jupiter.api.Test)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1 OptionalLong (java.util.OptionalLong)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)1 RegisterBrokerRecord (org.apache.kafka.common.metadata.RegisterBrokerRecord)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)1 ControlRecordType (org.apache.kafka.common.record.ControlRecordType)1 Record (org.apache.kafka.common.record.Record)1 MetadataRecordSerde (org.apache.kafka.metadata.MetadataRecordSerde)1 COMMIT (org.apache.kafka.metalog.MockMetaLogManagerListener.COMMIT)1 LAST_COMMITTED_OFFSET (org.apache.kafka.metalog.MockMetaLogManagerListener.LAST_COMMITTED_OFFSET)1 SHUTDOWN (org.apache.kafka.metalog.MockMetaLogManagerListener.SHUTDOWN)1