use of org.apache.pulsar.metadata.api.coordination.CoordinationService in project pulsar by apache.
the class PulsarService method startLeaderElectionService.
protected void startLeaderElectionService() {
this.leaderElectionService = new LeaderElectionService(coordinationService, getSafeWebServiceAddress(), state -> {
if (state == LeaderElectionState.Leading) {
LOG.info("This broker was elected leader");
if (getConfiguration().isLoadBalancerEnabled()) {
long loadSheddingInterval = TimeUnit.MINUTES.toMillis(getConfiguration().getLoadBalancerSheddingIntervalMinutes());
long resourceQuotaUpdateInterval = TimeUnit.MINUTES.toMillis(getConfiguration().getLoadBalancerResourceQuotaUpdateIntervalMinutes());
if (loadSheddingTask != null) {
loadSheddingTask.cancel(false);
}
if (loadResourceQuotaTask != null) {
loadResourceQuotaTask.cancel(false);
}
loadSheddingTask = loadManagerExecutor.scheduleAtFixedRate(new LoadSheddingTask(loadManager), loadSheddingInterval, loadSheddingInterval, TimeUnit.MILLISECONDS);
loadResourceQuotaTask = loadManagerExecutor.scheduleAtFixedRate(new LoadResourceQuotaUpdaterTask(loadManager), resourceQuotaUpdateInterval, resourceQuotaUpdateInterval, TimeUnit.MILLISECONDS);
}
} else {
if (leaderElectionService != null) {
LOG.info("This broker is a follower. Current leader is {}", leaderElectionService.getCurrentLeader());
}
if (loadSheddingTask != null) {
loadSheddingTask.cancel(false);
loadSheddingTask = null;
}
if (loadResourceQuotaTask != null) {
loadResourceQuotaTask.cancel(false);
loadResourceQuotaTask = null;
}
}
});
leaderElectionService.start();
}
use of org.apache.pulsar.metadata.api.coordination.CoordinationService in project pulsar by apache.
the class LeaderElectionTest method leaderNodeIsDeletedExternally.
@Test(dataProvider = "impl")
public void leaderNodeIsDeletedExternally(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
@Cleanup CoordinationService coordinationService = new CoordinationServiceImpl(store);
BlockingQueue<LeaderElectionState> notifications = new LinkedBlockingDeque<>();
@Cleanup LeaderElection<String> leaderElection = coordinationService.getLeaderElection(String.class, "/my/leader-election", t -> {
notifications.add(t);
});
LeaderElectionState les = leaderElection.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(notifications.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
store.delete("/my/leader-election", Optional.empty()).join();
assertEquals(notifications.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
assertEquals(les, LeaderElectionState.Leading);
}
use of org.apache.pulsar.metadata.api.coordination.CoordinationService in project pulsar by apache.
the class LeaderElectionTest method revalidateLeaderWithDifferentSessionsSameValue.
@Test(dataProvider = "impl")
public void revalidateLeaderWithDifferentSessionsSameValue(String provider, Supplier<String> urlSupplier) throws Exception {
if (provider.equals("Memory") || provider.equals("RocksDB")) {
// There are no multiple sessions for the local memory provider
return;
}
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
@Cleanup MetadataStoreExtended store2 = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
String path = newKey();
@Cleanup CoordinationService cs = new CoordinationServiceImpl(store);
@Cleanup LeaderElection<String> le = cs.getLeaderElection(String.class, path, __ -> {
});
store2.put(path, ObjectMapperFactory.getThreadLocal().writeValueAsBytes("test-1"), Optional.of(-1L), EnumSet.of(CreateOption.Ephemeral)).join();
LeaderElectionState les = le.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(le.getLeaderValue().join(), Optional.of("test-1"));
assertEquals(le.getLeaderValueIfPresent(), Optional.of("test-1"));
}
use of org.apache.pulsar.metadata.api.coordination.CoordinationService in project pulsar by apache.
the class LeaderElectionTest method basicTest.
@Test(dataProvider = "impl")
public void basicTest(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
@Cleanup CoordinationService coordinationService = new CoordinationServiceImpl(store);
MetadataCache<String> cache = store.getMetadataCache(String.class);
BlockingQueue<LeaderElectionState> notifications = new LinkedBlockingDeque<>();
@Cleanup LeaderElection<String> leaderElection = coordinationService.getLeaderElection(String.class, "/my/leader-election", t -> {
notifications.add(t);
});
assertEquals(cache.get("/my/leader-election").join(), Optional.empty());
LeaderElectionState les = leaderElection.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(notifications.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
assertEquals(cache.get("/my/leader-election").join(), Optional.of("test-1"));
leaderElection.close();
assertEquals(cache.get("/my/leader-election").join(), Optional.empty());
}
use of org.apache.pulsar.metadata.api.coordination.CoordinationService in project pulsar by apache.
the class LeaderElectionTest method closeAll.
@Test(dataProvider = "impl")
public void closeAll(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
MetadataCache<String> cache = store.getMetadataCache(String.class);
CoordinationService cs = new CoordinationServiceImpl(store);
LeaderElection<String> le1 = cs.getLeaderElection(String.class, "/my/leader-election-1", t -> {
});
LeaderElection<String> le2 = cs.getLeaderElection(String.class, "/my/leader-election-2", t -> {
});
LeaderElectionState les1 = le1.elect("test-1").join();
assertEquals(les1, LeaderElectionState.Leading);
LeaderElectionState les2 = le2.elect("test-2").join();
assertEquals(les2, LeaderElectionState.Leading);
cs.close();
assertEquals(cache.get("/my/leader-election-1").join(), Optional.empty());
assertEquals(cache.get("/my/leader-election-2").join(), Optional.empty());
}
Aggregations