Search in sources :

Example 6 with CuratorZookeeperClient

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");
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) CuratorZookeeperClient(org.apache.curator.CuratorZookeeperClient) UnavailableException(alluxio.exception.status.UnavailableException) UnavailableException(alluxio.exception.status.UnavailableException)

Example 7 with CuratorZookeeperClient

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());
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) EventProcessorSystem(io.pravega.controller.eventProcessor.EventProcessorSystem) EventStreamClientFactory(io.pravega.client.EventStreamClientFactory) CheckpointStore(io.pravega.controller.store.checkpoint.CheckpointStore) ZKCheckpointStore(io.pravega.controller.store.checkpoint.ZKCheckpointStore) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) ZKCheckpointStore(io.pravega.controller.store.checkpoint.ZKCheckpointStore) CuratorFramework(org.apache.curator.framework.CuratorFramework) CompletableFuture(java.util.concurrent.CompletableFuture) StreamTransactionMetadataTasks(io.pravega.controller.task.Stream.StreamTransactionMetadataTasks) TableMetadataTasks(io.pravega.controller.task.KeyValueTable.TableMetadataTasks) BucketStore(io.pravega.controller.store.stream.BucketStore) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KVTableMetadataStore(io.pravega.controller.store.kvtable.KVTableMetadataStore) EventProcessorGroup(io.pravega.controller.eventProcessor.EventProcessorGroup) LinkedList(java.util.LinkedList) Listenable(org.apache.curator.framework.listen.Listenable) CuratorZookeeperClient(org.apache.curator.CuratorZookeeperClient) EventProcessorConfig(io.pravega.controller.eventProcessor.EventProcessorConfig) Test(org.junit.Test)

Example 8 with CuratorZookeeperClient

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;
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) CuratorZookeeperClient(org.apache.curator.CuratorZookeeperClient)

Example 9 with CuratorZookeeperClient

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);
}
Also used : CuratorZookeeperClient(org.apache.curator.CuratorZookeeperClient)

Example 10 with CuratorZookeeperClient

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");
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ContainerBalancer(io.pravega.controller.fault.ContainerBalancer) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) CuratorZookeeperClient(org.apache.curator.CuratorZookeeperClient) SegmentContainerMonitor(io.pravega.controller.fault.SegmentContainerMonitor) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Listenable(org.apache.curator.framework.listen.Listenable) Before(org.junit.Before)

Aggregations

CuratorZookeeperClient (org.apache.curator.CuratorZookeeperClient)10 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Stat (org.apache.zookeeper.data.Stat)3 IOException (java.io.IOException)2 Listenable (org.apache.curator.framework.listen.Listenable)2 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)2 RetryOneTime (org.apache.curator.retry.RetryOneTime)2 Timing (org.apache.curator.test.Timing)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 UnavailableException (alluxio.exception.status.UnavailableException)1 EventStreamClientFactory (io.pravega.client.EventStreamClientFactory)1 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)1 EventProcessorConfig (io.pravega.controller.eventProcessor.EventProcessorConfig)1 EventProcessorGroup (io.pravega.controller.eventProcessor.EventProcessorGroup)1 EventProcessorSystem (io.pravega.controller.eventProcessor.EventProcessorSystem)1 ContainerBalancer (io.pravega.controller.fault.ContainerBalancer)1 SegmentContainerMonitor (io.pravega.controller.fault.SegmentContainerMonitor)1 CheckpointStore (io.pravega.controller.store.checkpoint.CheckpointStore)1