Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.pulsar.broker.PulsarService;
import org.awaitility.Awaitility;
Expand All @@ -32,12 +33,13 @@ public class TransactionMetaStoreAssignmentTest extends TransactionMetaStoreTest
@Test(groups = "broker")
public void testTransactionMetaStoreAssignAndFailover() throws IOException {

int transactionMetaStoreCount = 0;
for (PulsarService pulsarService : pulsarServices) {
transactionMetaStoreCount += pulsarService.getTransactionMetadataStoreService().getStores().size();
}

Assert.assertEquals(transactionMetaStoreCount, 16);
Awaitility.await()
.untilAsserted(() -> {
int transactionMetaStoreCount = Arrays.stream(pulsarServices)
.mapToInt(pulsarService -> pulsarService.getTransactionMetadataStoreService().getStores().size())
.sum();
Assert.assertEquals(transactionMetaStoreCount, 16);
});

PulsarService crashedMetaStore = null;
for (int i = pulsarServices.length - 1; i >= 0; i--) {
Expand All @@ -62,16 +64,11 @@ public void testTransactionMetaStoreAssignAndFailover() throws IOException {

Awaitility.await()
.untilAsserted(() -> {

int transactionMetaStoreCount2 = 0;
for (PulsarService pulsarService : pulsarServices) {
transactionMetaStoreCount2 += pulsarService.getTransactionMetadataStoreService().getStores()
.size();
}

int transactionMetaStoreCount2 = Arrays.stream(pulsarServices)
.mapToInt(pulsarService -> pulsarService.getTransactionMetadataStoreService().getStores().size())
.sum();
Assert.assertEquals(transactionMetaStoreCount2, 16);
});

transactionCoordinatorClient.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import lombok.Cleanup;

import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.coordination.CoordinationService;
import org.apache.pulsar.metadata.api.coordination.LeaderElection;
Expand Down Expand Up @@ -139,6 +136,7 @@ public void testReacquireLocksAfterSessionLost() throws Exception {

@Test
public void testReacquireLeadershipAfterSessionLost() throws Exception {
// --- init
@Cleanup
MetadataStoreExtended store = MetadataStoreExtended.create(zks.getConnectionString(),
MetadataStoreConfig.builder()
Expand All @@ -147,7 +145,6 @@ public void testReacquireLeadershipAfterSessionLost() throws Exception {

BlockingQueue<SessionEvent> sessionEvents = new LinkedBlockingQueue<>();
store.registerSessionListener(sessionEvents::add);

BlockingQueue<LeaderElectionState> leaderElectionEvents = new LinkedBlockingQueue<>();
String path = newKey();

Expand All @@ -156,39 +153,29 @@ public void testReacquireLeadershipAfterSessionLost() throws Exception {
@Cleanup
LeaderElection<String> le1 = coordinationService.getLeaderElection(String.class, path,
leaderElectionEvents::add);

// --- test manual elect
le1.elect("value-1").join();
assertEquals(le1.getState(), LeaderElectionState.Leading);

LeaderElectionState les = leaderElectionEvents.poll(5, TimeUnit.SECONDS);
assertEquals(les, LeaderElectionState.Leading);

// --- expire session
zks.expireSession(((ZKMetadataStore) store).getZkSessionId());

SessionEvent e = sessionEvents.poll(5, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.ConnectionLost);

e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.SessionLost);

Awaitility.await().untilAsserted(() -> {
assertEquals(le1.getState(), LeaderElectionState.Leading);
});

les = leaderElectionEvents.poll();
assertNull(les);

// --- test le1 can be leader
Awaitility.await()
.untilAsserted(()-> assertEquals(le1.getState(),LeaderElectionState.Leading)); // reacquire leadership
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.Reconnected);
e = sessionEvents.poll(10, TimeUnit.SECONDS);
assertEquals(e, SessionEvent.SessionReestablished);

Awaitility.await().untilAsserted(() -> {
assertEquals(le1.getState(), LeaderElectionState.Leading);
});
les = leaderElectionEvents.poll();
assertNull(les);

Awaitility.await()
.untilAsserted(()-> assertEquals(le1.getState(),LeaderElectionState.Leading));
assertTrue(store.get(path).join().isPresent());
}
}