use of org.apache.curator.CuratorZookeeperClient in project alluxio by Alluxio.
the class ZkMasterInquireClient method getPrimaryRpcAddress.
@Override
public synchronized InetSocketAddress getPrimaryRpcAddress() throws UnavailableException {
ensureStarted();
long startTime = System.currentTimeMillis();
int tried = 0;
try {
CuratorZookeeperClient curatorClient = mClient.getZookeeperClient();
// connection status explicitly.
for (int i = 0; i < 50; i++) {
if (curatorClient.isConnected()) {
break;
}
CommonUtils.sleepMs(20);
}
curatorClient.blockUntilConnectedOrTimedOut();
String leaderPath = mConnectDetails.getLeaderPath();
while (tried++ < mInquireRetryCount) {
ZooKeeper zookeeper = curatorClient.getZooKeeper();
if (zookeeper.exists(leaderPath, false) != null) {
List<String> masters = zookeeper.getChildren(leaderPath, null);
LOG.debug("Master addresses: {}", masters);
if (masters.size() >= 1) {
if (masters.size() == 1) {
return NetworkAddressUtils.parseInetSocketAddress(masters.get(0));
}
long maxTime = 0;
String leader = "";
for (String master : masters) {
Stat stat = zookeeper.exists(PathUtils.concatPath(leaderPath, master), null);
if (stat != null && stat.getCtime() > maxTime) {
maxTime = stat.getCtime();
leader = master;
}
}
LOG.debug("The leader master: {}", leader);
return NetworkAddressUtils.parseInetSocketAddress(leader);
}
} else {
LOG.info("{} does not exist ({})", leaderPath, tried);
}
CommonUtils.sleepMs(LOG, Constants.SECOND_MS);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
LOG.error("Error getting the leader master address from zookeeper. Zookeeper: {}", mConnectDetails, e);
} finally {
LOG.debug("Finished getPrimaryRpcAddress() in {}ms", System.currentTimeMillis() - startTime);
}
throw new UnavailableException("Failed to determine primary master rpc address");
}
use of org.apache.curator.CuratorZookeeperClient in project pravega by pravega.
the class ControllerEventProcessorsTest method testIsReady.
@Test(timeout = 30000L)
public void testIsReady() throws Exception {
LocalController controller = mock(LocalController.class);
StreamMetadataStore streamStore = mock(StreamMetadataStore.class);
BucketStore bucketStore = mock(BucketStore.class);
ConnectionPool connectionPool = mock(ConnectionPool.class);
StreamMetadataTasks streamMetadataTasks = mock(StreamMetadataTasks.class);
StreamTransactionMetadataTasks streamTransactionMetadataTasks = mock(StreamTransactionMetadataTasks.class);
KVTableMetadataStore kvtStore = mock(KVTableMetadataStore.class);
TableMetadataTasks kvtTasks = mock(TableMetadataTasks.class);
ControllerEventProcessorConfig config = ControllerEventProcessorConfigImpl.withDefault();
EventProcessorSystem system = mock(EventProcessorSystem.class);
CuratorZookeeperClient curatorZKClientMock = mock(CuratorZookeeperClient.class);
CuratorFramework client = mock(CuratorFramework.class);
Listenable listen = mock(Listenable.class);
doNothing().when(listen).addListener(any(ConnectionStateListener.class));
doReturn(listen).when(client).getConnectionStateListenable();
doReturn(curatorZKClientMock).when(client).getZookeeperClient();
doReturn(true).when(curatorZKClientMock).isConnected();
ZKCheckpointStore checkpointStore = (ZKCheckpointStore) CheckpointStoreFactory.createZKStore(client);
doAnswer(x -> null).when(streamMetadataTasks).initializeStreamWriters(any(), any());
doAnswer(x -> null).when(streamTransactionMetadataTasks).initializeStreamWriters(any(EventStreamClientFactory.class), any(ControllerEventProcessorConfig.class));
CompletableFuture<Boolean> createScopeResponseFuture = new CompletableFuture<>();
CompletableFuture<Void> createScopeSignalFuture = new CompletableFuture<>();
doAnswer(x -> {
createScopeSignalFuture.complete(null);
return createScopeResponseFuture;
}).when(controller).createScope(anyString());
LinkedBlockingQueue<CompletableFuture<Boolean>> createStreamResponses = new LinkedBlockingQueue<>();
LinkedBlockingQueue<CompletableFuture<Void>> createStreamSignals = new LinkedBlockingQueue<>();
List<CompletableFuture<Boolean>> createStreamResponsesList = new LinkedList<>();
List<CompletableFuture<Void>> createStreamSignalsList = new LinkedList<>();
for (int i = 0; i < 4; i++) {
CompletableFuture<Boolean> responseFuture = new CompletableFuture<>();
CompletableFuture<Void> signalFuture = new CompletableFuture<>();
createStreamResponsesList.add(responseFuture);
createStreamResponses.add(responseFuture);
createStreamSignalsList.add(signalFuture);
createStreamSignals.add(signalFuture);
}
// return a future from latches queue
doAnswer(x -> {
createStreamSignals.take().complete(null);
return createStreamResponses.take();
}).when(controller).createInternalStream(anyString(), anyString(), any());
@Cleanup ControllerEventProcessors processors = spy(new ControllerEventProcessors("host1", config, controller, checkpointStore, streamStore, bucketStore, connectionPool, streamMetadataTasks, streamTransactionMetadataTasks, kvtStore, kvtTasks, system, executorService()));
// Check isReady() method before invoking bootstrap
Assert.assertFalse(processors.getBootstrapCompleted().get());
Assert.assertTrue(processors.isMetadataServiceConnected());
Assert.assertFalse(processors.isRunning());
Assert.assertFalse(processors.isReady());
// Call bootstrap on ControllerEventProcessors
processors.bootstrap(streamTransactionMetadataTasks, streamMetadataTasks, kvtTasks);
// Wait on create scope being called.
createScopeSignalFuture.join();
createScopeResponseFuture.complete(true);
createStreamSignalsList.get(0).join();
createStreamSignalsList.get(1).join();
createStreamSignalsList.get(2).join();
createStreamSignalsList.get(3).join();
createStreamResponsesList.get(0).complete(true);
createStreamResponsesList.get(1).complete(true);
createStreamResponsesList.get(2).complete(true);
createStreamResponsesList.get(3).complete(true);
AssertExtensions.assertEventuallyEquals(true, () -> processors.getBootstrapCompleted().get(), 10000);
Assert.assertTrue(processors.isMetadataServiceConnected());
Assert.assertFalse(processors.isRunning());
Assert.assertFalse(processors.isReady());
EventProcessorGroup mockEventProcessorGroup = mock(EventProcessorGroup.class);
doNothing().when(mockEventProcessorGroup).awaitRunning();
doReturn(mockEventProcessorGroup).when(system).createEventProcessorGroup(any(EventProcessorConfig.class), any(CheckpointStore.class), any(ScheduledExecutorService.class));
processors.startAsync();
processors.awaitRunning();
Assert.assertTrue(processors.isMetadataServiceConnected());
Assert.assertTrue(processors.isBootstrapCompleted());
Assert.assertTrue(processors.isRunning());
Assert.assertTrue(processors.isReady());
}
use of org.apache.curator.CuratorZookeeperClient in project alluxio by Alluxio.
the class MasterInquireClient method getLeaderAddress.
/**
* @return the address of the current leader master
*/
public synchronized String getLeaderAddress() {
long startTime = System.currentTimeMillis();
int tried = 0;
try {
CuratorZookeeperClient curatorClient = mClient.getZookeeperClient();
// connection status explicitly.
for (int i = 0; i < 50; i++) {
if (curatorClient.isConnected()) {
break;
}
CommonUtils.sleepMs(20);
}
curatorClient.blockUntilConnectedOrTimedOut();
while (tried < mMaxTry) {
ZooKeeper zookeeper = curatorClient.getZooKeeper();
if (zookeeper.exists(mLeaderPath, false) != null) {
List<String> masters = zookeeper.getChildren(mLeaderPath, null);
LOG.debug("Master addresses: {}", masters);
if (masters.size() >= 1) {
if (masters.size() == 1) {
return masters.get(0);
}
long maxTime = 0;
String leader = "";
for (String master : masters) {
Stat stat = zookeeper.exists(PathUtils.concatPath(mLeaderPath, master), null);
if (stat != null && stat.getCtime() > maxTime) {
maxTime = stat.getCtime();
leader = master;
}
}
LOG.debug("The leader master: {}", leader);
return leader;
}
} else {
LOG.info("{} does not exist ({})", mLeaderPath, ++tried);
}
CommonUtils.sleepMs(LOG, Constants.SECOND_MS);
}
} catch (Exception e) {
LOG.error("Error getting the leader master address from zookeeper. Zookeeper address: {}", mZookeeperAddress, e);
} finally {
LOG.debug("Finished getLeaderAddress() in {}ms", System.currentTimeMillis() - startTime);
}
return null;
}
use of org.apache.curator.CuratorZookeeperClient in project xian by happyyangyuan.
the class NamespaceImpl method fixForNamespace.
String fixForNamespace(String path, boolean isSequential) {
if (ensurePathNeeded.get()) {
try {
final CuratorZookeeperClient zookeeperClient = client.getZookeeperClient();
RetryLoop.callWithRetry(zookeeperClient, new Callable<Object>() {
@Override
public Object call() throws Exception {
ZKPaths.mkdirs(zookeeperClient.getZooKeeper(), ZKPaths.makePath("/", namespace), true, client.getAclProvider(), true);
return null;
}
});
ensurePathNeeded.set(false);
} catch (Exception e) {
ThreadUtils.checkInterrupted(e);
client.logError("Ensure path threw exception", e);
}
}
return ZKPaths.fixForNamespace(namespace, path, isSequential);
}
use of org.apache.curator.CuratorZookeeperClient in project pravega by pravega.
the class SegmentContainerMonitorHealthContributorTest method setup.
@Before
public void setup() {
HostControllerStore hostStore = mock(HostControllerStore.class);
CuratorFramework client = mock(CuratorFramework.class);
ContainerBalancer balancer = mock(ContainerBalancer.class);
CuratorZookeeperClient curatorZKClientMock = mock(CuratorZookeeperClient.class);
Listenable listen = mock(Listenable.class);
doNothing().when(listen).addListener(any(ConnectionStateListener.class));
doReturn(listen).when(client).getConnectionStateListenable();
doReturn(curatorZKClientMock).when(client).getZookeeperClient();
doReturn(true).when(curatorZKClientMock).isConnected();
monitor = spy(new SegmentContainerMonitor(hostStore, client, balancer, 1));
contributor = new SegmentContainerMonitorHealthContributor("segmentcontainermonitor", monitor);
builder = Health.builder().name("monitor");
}
Aggregations