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

The following examples show how to use org.apache.curator.framework.state.ConnectionState#RECONNECTED . 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: 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 2
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 3
Source File: TestLeaderSelectorEdges.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
    if ( newState == ConnectionState.RECONNECTED )
    {
        reconnected.countDown();
    }
}
 
Example 4
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 5
Source File: CuratorAdvertiser.java    From snowizard-discovery with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * TODO - figure out how to register this listener
 */
@Override
public void stateChanged(final CuratorFramework client,
        final ConnectionState newState) {
    if (newState == ConnectionState.RECONNECTED) {
        registerAvailability();
    }
}
 
Example 6
Source File: PersistentPathChildrenCache.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(final CuratorFramework client, final ConnectionState newState) {
  log.debug("connection state change: {}", newState);
  if (newState == ConnectionState.RECONNECTED) {
    synced = false;
    reactor.signal();
  }
  fireConnectionStateChanged(newState);
}
 
Example 7
Source File: TestLeaderSelectorEdges.java    From curator with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
    if ( newState == ConnectionState.RECONNECTED )
    {
        reconnected.countDown();
    }
}
 
Example 8
Source File: RegistryCenterConnectionStateListener.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(final CuratorFramework client, final ConnectionState newState) {
    if (JobRegistry.getInstance().isShutdown(jobName)) {
        return;
    }
    JobScheduleController jobScheduleController = JobRegistry.getInstance().getJobScheduleController(jobName);
    if (ConnectionState.SUSPENDED == newState || ConnectionState.LOST == newState) {
        jobScheduleController.pauseJob();
    } else if (ConnectionState.RECONNECTED == newState) {
        serverService.persistOnline(serverService.isEnableServer(JobRegistry.getInstance().getJobInstance(jobName).getIp()));
        instanceService.persistOnline();
        executionService.clearRunningInfo(shardingService.getLocalShardingItems());
        jobScheduleController.resumeJob();
    }
}
 
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: ZkConnectionStatListener.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState state) {
    if (ConnectionState.SUSPENDED == state || ConnectionState.LOST == state) {
        logger.info(String.format("%s has lost connection from zookeeper", servicePath));
    } else if (ConnectionState.RECONNECTED == state) {
        try {
            PathUtil.rebalance(registryCenter, registryConfig, changeListener, servicePath);
        } catch (Exception e) {
            logger.error(String.format("%s connection reconnected to rebalance error", servicePath), e);
        }
    }
}
 
Example 11
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 12
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 13
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 14
Source File: TestSharedCount.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testDisconnectReconnectWithMultipleClients() throws Exception
{
    Timing timing = new Timing();
    CuratorFramework curatorFramework1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(10, 500));
    CuratorFramework curatorFramework2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(10, 500));

    curatorFramework1.start();
    curatorFramework1.checkExists().forPath("/");   // clear initial connect events
    curatorFramework2.start();
    curatorFramework2.checkExists().forPath("/");   // clear initial connect events

    final String sharedCountPath = "/count";
    final int initialCount = 10;
    SharedCount sharedCount1 = new SharedCount(curatorFramework1, sharedCountPath, initialCount);
    SharedCount sharedCountWithFaultyWatcher = createSharedCountWithFaultyWatcher(curatorFramework2, sharedCountPath, initialCount);

    class MySharedCountListener implements SharedCountListener
    {
        final public Phaser gotSuspendEvent = new Phaser(1);
        final public Phaser gotChangeEvent = new Phaser(1);
        final public Phaser getReconnectEvent = new Phaser(1);
        final public AtomicInteger numChangeEvents = new AtomicInteger(0);

        @Override
        public void countHasChanged(SharedCountReader sharedCount, int newCount) throws Exception
        {
            numChangeEvents.incrementAndGet();
            gotChangeEvent.arrive();
        }

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState)
        {
            if (newState == ConnectionState.SUSPENDED) {
                gotSuspendEvent.arrive();
            } else if (newState == ConnectionState.RECONNECTED) {
                getReconnectEvent.arrive();
            }
        }
    }

    MySharedCountListener listener1 = new MySharedCountListener();
    sharedCount1.addListener(listener1);
    sharedCount1.start();
    MySharedCountListener listener2 = new MySharedCountListener();
    sharedCountWithFaultyWatcher.addListener(listener2);

    try
    {
        sharedCount1.setCount(12);
        Assert.assertEquals(listener1.gotChangeEvent.awaitAdvanceInterruptibly(0, timing.seconds(), TimeUnit.SECONDS), 1);
        Assert.assertEquals(sharedCount1.getCount(), 12);

        Assert.assertEquals(sharedCountWithFaultyWatcher.getCount(), 10);
        // new counter with faultyWatcher start
        sharedCountWithFaultyWatcher.start();

        for (int i = 0; i < 10; i++) {
            sharedCount1.setCount(13 + i);
            Assert.assertEquals(sharedCount1.getCount(), 13 + i);

            server.restart();

            Assert.assertEquals(listener2.getReconnectEvent.awaitAdvanceInterruptibly(i, timing.forWaiting().seconds(), TimeUnit.SECONDS), i + 1);
            // CURATOR-311 introduces to Curator's client reading server's shared count value
            // when client's state gets ConnectionState.RECONNECTED. Following tests ensures that.
            Assert.assertEquals(listener2.gotChangeEvent.awaitAdvanceInterruptibly(i, timing.forWaiting().seconds(), TimeUnit.SECONDS), i + 1);
            Assert.assertEquals(sharedCountWithFaultyWatcher.getCount(), 13 + i);
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(sharedCount1);
        CloseableUtils.closeQuietly(curatorFramework1);
        CloseableUtils.closeQuietly(sharedCountWithFaultyWatcher);
        CloseableUtils.closeQuietly(curatorFramework2);
    }
}
 
Example 15
Source File: ZooKeeperRegistrarService.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void stateChanged(final CuratorFramework client, final ConnectionState newState) {
  if (newState == ConnectionState.RECONNECTED) {
    reactor.signal();
  }
}
 
Example 16
Source File: ZooKeeperTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
public static ConnectionTrigger reconnectedTrigger() {
    return new ConnectionTrigger(ConnectionState.RECONNECTED);
}
 
Example 17
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);
    }
}