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

The following examples show how to use org.apache.curator.framework.state.ConnectionState#SUSPENDED . 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: ZkLeaderSelector.java    From binlake with Apache License 2.0 6 votes vote down vote up
public void stateChanged(CuratorFramework client, ConnectionState newState) {
    try {
        LogUtils.warn.warn(client.getData().toString() + " state change to " + newState);
    } catch (Throwable exp) {
        // catch any null pointer exception
    }

    if (newState == ConnectionState.SUSPENDED || newState == ConnectionState.LOST) {
        try {
            refreshLogPos(); // 链接状态变更 则需要将最新的binlog offset 刷新到 zk
        } catch (Exception e) {
            // 更新binlog 位置异常
            LogUtils.error.error("state change to " + newState + " refresh log position error", e);
        }
        killWorkAndAbandonLeader();
    }
}
 
Example 2
Source File: LeaderSelectorListenerAdapter.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.SUSPENDED) || (newState == ConnectionState.LOST) )
    {
        throw new CancelLeadershipException();
    }
}
 
Example 3
Source File: TestLeaderSelectorWithExecutor.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState newState)
{
    if ( (newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED) )
    {
        if ( ourThread != null )
        {
            ourThread.interrupt();
        }
    }
}
 
Example 4
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 5
Source File: DistributedSequenceHandler.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
    if (newState == ConnectionState.SUSPENDED || newState == ConnectionState.LOST) {
        this.isLeader = false;
        leaderExecutor.shutdownNow();
        throw new CancelLeadershipException();
    }
}
 
Example 6
Source File: ClusterLeaderManager.java    From cicada with MIT License 5 votes vote down vote up
@Override
public void stateChanged(final CuratorFramework client, final ConnectionState state) {
  if (state == ConnectionState.LOST || state == ConnectionState.SUSPENDED) {
    log.warn("connection state changed, now");
    isStop.set(true);
  }
}
 
Example 7
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 8
Source File: DefaultScheduler.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
    if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
        try {
            shutdown();
        } catch (SchedulerException e) {
            throw new RuntimeException("failed to shutdown scheduler", e);
        }
    }
}
 
Example 9
Source File: TestLeaderSelectorWithExecutor.java    From curator with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState newState)
{
    if ( (newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED) )
    {
        if ( ourThread != null )
        {
            ourThread.interrupt();
        }
    }
}
 
Example 10
Source File: CuratorLeaderElectionManager.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void stateChanged(final CuratorFramework client, final ConnectionState newState) {
    logger.info("{} Connection State changed to {}", this, newState.name());

    if (newState == ConnectionState.SUSPENDED || newState == ConnectionState.LOST) {
        if (leader) {
            logger.info("Because Connection State was changed to {}, will relinquish leadership for role '{}'", newState, roleName);
        }

        setLeader(false);
    }

    super.stateChanged(client, newState);
}
 
Example 11
Source File: LeaderService.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState newState) {
    if (newState == ConnectionState.LOST || newState == ConnectionState.SUSPENDED) {
        LOG.debug("Lost leadership due to ZK state change to {}: {}", newState, getId());
        closeLeaderLatch();
    }
}
 
Example 12
Source File: ZooKeeperCheckpointIDCounter.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
	if (newState == ConnectionState.SUSPENDED || newState == ConnectionState.LOST) {
		lastState = newState;
	}
}
 
Example 13
Source File: ZooKeeperCheckpointIDCounter.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
	if (newState == ConnectionState.SUSPENDED || newState == ConnectionState.LOST) {
		lastState = newState;
	}
}
 
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);
    }
}