Search in sources :

Example 1 with SHUTDOWN

use of org.apache.kafka.metalog.MockMetaLogManagerListener.SHUTDOWN in project kafka by apache.

the class LocalLogManagerTest method testCommits.

/**
 * Test that all the log managers see all the commits.
 */
@Test
public void testCommits() throws Exception {
    try (LocalLogManagerTestEnv env = LocalLogManagerTestEnv.createWithMockListeners(3, Optional.empty())) {
        LeaderAndEpoch leaderInfo = env.waitForLeader();
        int leaderId = leaderInfo.leaderId().orElseThrow(() -> new AssertionError("Current leader is undefined"));
        LocalLogManager activeLogManager = env.logManagers().get(leaderId);
        int epoch = activeLogManager.leaderAndEpoch().epoch();
        List<ApiMessageAndVersion> messages = Arrays.asList(new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerId(0), (short) 0), new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerId(1), (short) 0), new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerId(2), (short) 0));
        assertEquals(3, activeLogManager.scheduleAppend(epoch, messages));
        for (LocalLogManager logManager : env.logManagers()) {
            waitForLastCommittedOffset(3, logManager);
        }
        List<MockMetaLogManagerListener> listeners = env.logManagers().stream().map(m -> (MockMetaLogManagerListener) m.listeners().get(0)).collect(Collectors.toList());
        env.close();
        for (MockMetaLogManagerListener listener : listeners) {
            List<String> events = listener.serializedEvents();
            assertEquals(SHUTDOWN, events.get(events.size() - 1));
            int foundIndex = 0;
            for (String event : events) {
                if (event.startsWith(COMMIT)) {
                    assertEquals(messages.get(foundIndex).message().toString(), event.substring(COMMIT.length() + 1));
                    foundIndex++;
                }
            }
            assertEquals(messages.size(), foundIndex);
        }
    }
}
Also used : Arrays(java.util.Arrays) TestUtils(org.apache.kafka.test.TestUtils) LAST_COMMITTED_OFFSET(org.apache.kafka.metalog.MockMetaLogManagerListener.LAST_COMMITTED_OFFSET) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) SHUTDOWN(org.apache.kafka.metalog.MockMetaLogManagerListener.SHUTDOWN) Test(org.junit.jupiter.api.Test) List(java.util.List) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) COMMIT(org.apache.kafka.metalog.MockMetaLogManagerListener.COMMIT) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) Optional(java.util.Optional) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Timeout(org.junit.jupiter.api.Timeout) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) Test(org.junit.jupiter.api.Test)

Aggregations

Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1 Collectors (java.util.stream.Collectors)1 RegisterBrokerRecord (org.apache.kafka.common.metadata.RegisterBrokerRecord)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 LeaderAndEpoch (org.apache.kafka.raft.LeaderAndEpoch)1 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)1 TestUtils (org.apache.kafka.test.TestUtils)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Test (org.junit.jupiter.api.Test)1 Timeout (org.junit.jupiter.api.Timeout)1