Search in sources :

Example 6 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState in project lucene-solr by apache.

the class ConnectionManager method process.

@Override
public void process(WatchedEvent event) {
    if (event.getState() == AuthFailed || event.getState() == Disconnected || event.getState() == Expired) {
        log.warn("Watcher {} name: {} got event {} path: {} type: {}", this, name, event, event.getPath(), event.getType());
    } else {
        log.debug("Watcher {} name: {} got event {} path: {} type: {}", this, name, event, event.getPath(), event.getType());
    }
    if (isClosed) {
        log.debug("Client->ZooKeeper status change trigger but we are already closed");
        return;
    }
    KeeperState state = event.getState();
    if (state == KeeperState.SyncConnected) {
        connected();
        connectionStrategy.connected();
    } else if (state == Expired) {
        // we don't call disconnected here, because we know we are expired
        connected = false;
        likelyExpiredState = LikelyExpiredState.EXPIRED;
        log.warn("Our previous ZooKeeper session was expired. Attempting to reconnect to recover relationship with ZooKeeper...");
        if (beforeReconnect != null) {
            try {
                beforeReconnect.command();
            } catch (Exception e) {
                log.warn("Exception running beforeReconnect command", e);
            }
        }
        do {
            // try again to create a new connection.
            try {
                connectionStrategy.reconnect(zkServerAddress, client.getZkClientTimeout(), this, new ZkClientConnectionStrategy.ZkUpdate() {

                    @Override
                    public void update(SolrZooKeeper keeper) {
                        try {
                            waitForConnected(Long.MAX_VALUE);
                        } catch (Exception e1) {
                            closeKeeper(keeper);
                            throw new RuntimeException(e1);
                        }
                        log.info("Connection with ZooKeeper reestablished.");
                        try {
                            client.updateKeeper(keeper);
                        } catch (InterruptedException e) {
                            closeKeeper(keeper);
                            Thread.currentThread().interrupt();
                            // we must have been asked to stop
                            throw new RuntimeException(e);
                        } catch (Exception t) {
                            closeKeeper(keeper);
                            throw new RuntimeException(t);
                        }
                        if (onReconnect != null) {
                            onReconnect.command();
                        }
                    }
                });
                break;
            } catch (Exception e) {
                SolrException.log(log, "", e);
                log.info("Could not connect due to error, sleeping for 5s and trying agian");
                waitSleep(1000);
            }
        } while (!isClosed);
        log.info("Connected:" + connected);
    } else if (state == KeeperState.Disconnected) {
        log.warn("zkClient has disconnected");
        disconnected();
        connectionStrategy.disconnected();
    } else if (state == KeeperState.AuthFailed) {
        log.warn("zkClient received AuthFailed");
    }
}
Also used : KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) SolrException(org.apache.solr.common.SolrException) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState in project zookeeper by apache.

the class ClientCnxn method queueEvent.

void queueEvent(String clientPath, int err, Set<Watcher> materializedWatchers, EventType eventType) {
    KeeperState sessionState = KeeperState.SyncConnected;
    if (KeeperException.Code.SESSIONEXPIRED.intValue() == err || KeeperException.Code.CONNECTIONLOSS.intValue() == err) {
        sessionState = Event.KeeperState.Disconnected;
    }
    WatchedEvent event = new WatchedEvent(eventType, sessionState, clientPath);
    eventThread.queueEvent(event, materializedWatchers);
}
Also used : KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState)

Example 8 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState in project zookeeper by apache.

the class WatchedEventTest method testCreatingWatchedEventFromWrapper.

@Test
public void testCreatingWatchedEventFromWrapper() {
    // Make sure we can handle any type of correct wrapper
    EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class);
    EnumSet<KeeperState> allStates = EnumSet.allOf(KeeperState.class);
    WatchedEvent we;
    WatcherEvent wep;
    for (EventType et : allTypes) {
        for (KeeperState ks : allStates) {
            wep = new WatcherEvent(et.getIntValue(), ks.getIntValue(), "blah");
            we = new WatchedEvent(wep);
            Assert.assertEquals(et, we.getType());
            Assert.assertEquals(ks, we.getState());
            Assert.assertEquals("blah", we.getPath());
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) WatcherEvent(org.apache.zookeeper.proto.WatcherEvent) Test(org.junit.Test)

Example 9 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState in project canal by alibaba.

the class CanalController method start.

public void start() throws Throwable {
    logger.info("## start the canal server[{}:{}]", ip, port);
    // 创建整个canal的工作节点
    final String path = ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port);
    initCid(path);
    if (zkclientx != null) {
        this.zkclientx.subscribeStateChanges(new IZkStateListener() {

            public void handleStateChanged(KeeperState state) throws Exception {
            }

            public void handleNewSession() throws Exception {
                initCid(path);
            }
        });
    }
    // 优先启动embeded服务
    embededCanalServer.start();
    // 尝试启动一下非lazy状态的通道
    for (Map.Entry<String, InstanceConfig> entry : instanceConfigs.entrySet()) {
        final String destination = entry.getKey();
        InstanceConfig config = entry.getValue();
        // 创建destination的工作节点
        if (!embededCanalServer.isStart(destination)) {
            // HA机制启动
            ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(destination);
            if (!config.getLazy() && !runningMonitor.isStart()) {
                runningMonitor.start();
            }
        }
        if (autoScan) {
            instanceConfigMonitors.get(config.getMode()).register(destination, defaultAction);
        }
    }
    if (autoScan) {
        instanceConfigMonitors.get(globalInstanceConfig.getMode()).start();
        for (InstanceConfigMonitor monitor : instanceConfigMonitors.values()) {
            if (!monitor.isStart()) {
                monitor.start();
            }
        }
    }
    // 启动网络接口
    canalServer.start();
}
Also used : IZkStateListener(org.I0Itec.zkclient.IZkStateListener) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Map(java.util.Map) MigrateMap(com.google.common.collect.MigrateMap) ManagerInstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.ManagerInstanceConfigMonitor) SpringInstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.SpringInstanceConfigMonitor) InstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.InstanceConfigMonitor) CanalServerException(com.alibaba.otter.canal.server.exception.CanalServerException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CanalException(com.alibaba.otter.canal.common.CanalException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException)

Example 10 with KeeperState

use of org.apache.zookeeper.Watcher.Event.KeeperState in project pinpoint by naver.

the class ZookeeperUtils method isDisconnectedEvent.

public static boolean isDisconnectedEvent(WatchedEvent event) {
    KeeperState state = event.getState();
    EventType eventType = event.getType();
    return isDisconnectedEvent(state, eventType);
}
Also used : EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState)

Aggregations

KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)11 EventType (org.apache.zookeeper.Watcher.Event.EventType)7 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Test (org.junit.Test)3 CanalException (com.alibaba.otter.canal.common.CanalException)1 ServerRunningMonitor (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor)1 InstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.InstanceConfigMonitor)1 ManagerInstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.ManagerInstanceConfigMonitor)1 SpringInstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.SpringInstanceConfigMonitor)1 CanalServerException (com.alibaba.otter.canal.server.exception.CanalServerException)1 MigrateMap (com.google.common.collect.MigrateMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 IZkStateListener (org.I0Itec.zkclient.IZkStateListener)1 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)1 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)1 SolrException (org.apache.solr.common.SolrException)1 KeeperException (org.apache.zookeeper.KeeperException)1 Watcher (org.apache.zookeeper.Watcher)1