Java Code Examples for org.apache.curator.framework.state.ConnectionState#CONNECTED

The following examples show how to use org.apache.curator.framework.state.ConnectionState#CONNECTED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SharedCacheCoordinatorTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Test
public void testEphemeralNodeReconnect() throws Exception {
    String ephemeralNodePath = Whitebox.getInternalState(cacheCoordinator, "serverIdentifierPath");
    boolean exists = curatorClient.checkExists().forPath(ephemeralNodePath) != null;
    assertTrue("Ephemeral server node " + ephemeralNodePath + " doesn't exist before a zookeeper restart", exists);
    
    final ConnectionState[] state = new ConnectionState[] {ConnectionState.CONNECTED};
    curatorClient.getConnectionStateListenable().addListener((client, newState) -> state[0] = newState);
    
    testingZooKeeperServer.restart();
    
    for (int i = 0; i < 50; ++i) {
        if (ConnectionState.RECONNECTED.equals(state[0]))
            break;
        Thread.sleep(200L);
    }
    assertEquals("Client never reconnected.", ConnectionState.RECONNECTED, state[0]);
    
    for (int i = 0; i < 50; ++i) {
        exists = curatorClient.checkExists().forPath(ephemeralNodePath) != null;
        if (exists)
            break;
        Thread.sleep(200L);
    }
    assertTrue("Ephemeral node " + ephemeralNodePath + " was not recreated.", exists);
}
 
Example 2
Source File: ServiceDiscoveryImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
    if ( (newState == ConnectionState.RECONNECTED) || (newState == ConnectionState.CONNECTED) )
    {
        try
        {
            log.debug("Re-registering due to reconnection");
            reRegisterServices();
        }
        catch ( Exception e )
        {
            ThreadUtils.checkInterrupted(e);
            log.error("Could not re-register instances after reconnection", e);
        }
    }
}
 
Example 3
Source File: ServiceDiscoveryImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
    if ( (newState == ConnectionState.RECONNECTED) || (newState == ConnectionState.CONNECTED) )
    {
        try
        {
            log.debug("Re-registering due to reconnection");
            reRegisterServices();
        }
        catch (InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }
        catch ( Exception e )
        {
            log.error("Could not re-register instances after reconnection", e);
        }
    }
}
 
Example 4
Source File: ZKClusterCoordinator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
  if(newState == ConnectionState.CONNECTED) {
    ZKClusterCoordinator.this.initialConnection.countDown();
    client.getConnectionStateListenable().removeListener(this);
  }
}
 
Example 5
Source File: StateKeeper.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) {
    log.warn("state changed, state:{}", newState);
    if (ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState) {
        log.warn("session change:{}, register again", newState);
        registerAll();
    }
}
 
Example 6
Source File: StateKeeper.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) {
    log.warn("state changed, state:{}", newState);
    if (ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState) {
        log.warn("session change:{}, register again", newState);
        registerAll();
    }
}
 
Example 7
Source File: ZKWatcher.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState connectionState) {
	if (connectionState == ConnectionState.CONNECTED) {
		logger.info("connected established");
		countDownLatch.countDown();
	} else if (connectionState == ConnectionState.LOST) {
		logger.info("connection lost, waiting for reconection");
		try {
			reconnect();
		} catch (Exception e) {
			logger.error("reconnect error", e);
		}
	}

}
 
Example 8
Source File: ZKClusterClient.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
  if(newState == ConnectionState.CONNECTED) {
    ZKClusterClient.this.initialConnection.countDown();
    client.getConnectionStateListenable().removeListener(this);
  }
}
 
Example 9
Source File: Quorum.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
    try {
        if (newState == ConnectionState.CONNECTED) {
            initZookeeper();
        }
        else if (newState == ConnectionState.RECONNECTED) {
            refreshNodes();
        }
    }
    catch (Exception x) {
        _log.error("error from zookeeper", x);
    }
}
 
Example 10
Source File: ZKShardLockManager.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
    log.info("Connection to Zookeeper toggled to state " + connectionState.toString());
    connected = connectionState == ConnectionState.CONNECTED || connectionState == ConnectionState.RECONNECTED;
    if (connectionState == ConnectionState.LOST) {
        log.error("Connection to Zookeeper toggled to state " + connectionState.toString());
        this.handleZookeeperConnectionFailed();
    } else if (connectionState == ConnectionState.RECONNECTED) {
        log.info("Reconnected to zookeeper, forcing lock scavenge");
        forceLockScavenge();
    } else {
        log.info("Connection to Zookeeper toggled to state " + connectionState.toString());
    }
}
 
Example 11
Source File: CuratorFrameworkImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public void start()
{
    log.info("Starting");
    if ( !state.compareAndSet(CuratorFrameworkState.LATENT, CuratorFrameworkState.STARTED) )
    {
        throw new IllegalStateException("Cannot be started more than once");
    }

    try
    {
        connectionStateManager.start(); // ordering dependency - must be called before client.start()

        final ConnectionStateListener listener = new ConnectionStateListener()
        {
            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState)
            {
                if ( ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState )
                {
                    logAsErrorConnectionErrors.set(true);
                }
            }
        };

        this.getConnectionStateListenable().addListener(listener);

        client.start();

        executorService = Executors.newSingleThreadScheduledExecutor(threadFactory);
        executorService.submit(new Callable<Object>()
        {
            @Override
            public Object call() throws Exception
            {
                backgroundOperationsLoop();
                return null;
            }
        });
    }
    catch ( Exception e )
    {
        ThreadUtils.checkInterrupted(e);
        handleBackgroundOperationException(null, e);
    }
}
 
Example 12
Source File: CuratorFrameworkImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public void start()
{
    log.info("Starting");
    if ( !state.compareAndSet(CuratorFrameworkState.LATENT, CuratorFrameworkState.STARTED) )
    {
        throw new IllegalStateException("Cannot be started more than once");
    }

    try
    {
        connectionStateManager.start(); // ordering dependency - must be called before client.start()

        final ConnectionStateListener listener = new ConnectionStateListener()
        {
            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState)
            {
                if ( ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState )
                {
                    logAsErrorConnectionErrors.set(true);
                }
            }

            @Override
            public boolean doNotProxy()
            {
                return true;
            }
        };

        this.getConnectionStateListenable().addListener(listener);

        client.start();

        executorService = Executors.newSingleThreadScheduledExecutor(threadFactory);
        executorService.submit(new Callable<Object>()
        {
            @Override
            public Object call() throws Exception
            {
                backgroundOperationsLoop();
                return null;
            }
        });

        if ( ensembleTracker != null )
        {
            ensembleTracker.start();
        }

        log.info(schemaSet.toDocumentation());
    }
    catch ( Exception e )
    {
        ThreadUtils.checkInterrupted(e);
        handleBackgroundOperationException(null, e);
    }
}
 
Example 13
Source File: TestFrameworkBackground.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testListenerConnectedAtStart() throws Exception
{
    server.stop();

    Timing timing = new Timing(2);
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(0, 0));
    try
    {
        client.start();
        AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);

        final CountDownLatch connectedLatch = new CountDownLatch(1);
        final AtomicBoolean firstListenerAction = new AtomicBoolean(true);
        final AtomicReference<ConnectionState> firstListenerState = new AtomicReference<>();
        ConnectionStateListener listener = (client1, newState) ->
        {
            if ( firstListenerAction.compareAndSet(true, false) )
            {
                firstListenerState.set(newState);
                System.out.println("First listener state is " + newState);
            }
            if ( newState == ConnectionState.CONNECTED )
            {
                connectedLatch.countDown();
            }
        };
        client.getConnectionStateListenable().addListener(listener);

        // due to CURATOR-72, this was causing a LOST event to precede the CONNECTED event
        async.create().forPath("/foo");

        server.restart();

        Assert.assertTrue(timing.awaitLatch(connectedLatch));
        Assert.assertFalse(firstListenerAction.get());
        ConnectionState firstconnectionState = firstListenerState.get();
        Assert.assertEquals(firstconnectionState, ConnectionState.CONNECTED, "First listener state MUST BE CONNECTED but is " + firstconnectionState);
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}