Search in sources :

Example 51 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project accumulo by apache.

the class ZooConfigurationFactory method getInstance.

/**
 * Gets a configuration object for the given instance with the given parent. Repeated calls will return the same object.
 *
 * @param inst
 *          instance; if null, instance is determined from HDFS
 * @param zcf
 *          {@link ZooCacheFactory} for building {@link ZooCache} to contact ZooKeeper (required)
 * @param parent
 *          parent configuration (required)
 * @return configuration
 */
ZooConfiguration getInstance(Instance inst, ZooCacheFactory zcf, AccumuloConfiguration parent) {
    String instanceId;
    if (inst == null) {
        // InstanceID should be the same across all volumes, so just choose one
        VolumeManager fs;
        try {
            fs = VolumeManagerImpl.get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Path instanceIdPath = Accumulo.getAccumuloInstanceIdPath(fs);
        instanceId = ZooUtil.getInstanceIDFromHdfs(instanceIdPath, parent);
    } else {
        instanceId = inst.getInstanceID();
    }
    ZooConfiguration config;
    synchronized (instances) {
        config = instances.get(instanceId);
        if (config == null) {
            ZooCache propCache;
            // The purpose of this watcher is a hack. It forces the creation on a new zoocache instead of using a shared one. This was done so that the zoocache
            // would update less, causing the configuration update count to changes less.
            Watcher watcher = new Watcher() {

                @Override
                public void process(WatchedEvent arg0) {
                }
            };
            if (inst == null) {
                propCache = zcf.getZooCache(parent.get(Property.INSTANCE_ZK_HOST), (int) parent.getTimeInMillis(Property.INSTANCE_ZK_TIMEOUT), watcher);
            } else {
                propCache = zcf.getZooCache(inst.getZooKeepers(), inst.getZooKeepersSessionTimeOut(), watcher);
            }
            config = new ZooConfiguration(instanceId, propCache, parent);
            instances.put(instanceId, config);
        }
    }
    return config;
}
Also used : Path(org.apache.hadoop.fs.Path) WatchedEvent(org.apache.zookeeper.WatchedEvent) VolumeManager(org.apache.accumulo.server.fs.VolumeManager) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) ZooCache(org.apache.accumulo.fate.zookeeper.ZooCache)

Example 52 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project disgear by yangbutao.

the class LeaderElector method checkIfIamLeader.

private void checkIfIamLeader(final int seq, final BaseElectionContext context, boolean replacement) throws KeeperException, InterruptedException, IOException {
    final String holdElectionPath = context.electionPath + ELECTION_NODE;
    List<String> seqs = zkClient.getChildren(holdElectionPath, null);
    sortSeqs(seqs);
    List<Integer> intSeqs = getSeqs(seqs);
    if (intSeqs.size() == 0) {
        log.warn("Our node is no longer to be leader");
        return;
    }
    if (seq <= intSeqs.get(0)) {
        try {
            zkClient.delete(context.leaderPath, -1);
        } catch (Exception e) {
        }
        runIamLeaderProcess(context, replacement);
    } else {
        int i = 1;
        for (; i < intSeqs.size(); i++) {
            int s = intSeqs.get(i);
            if (seq < s) {
                break;
            }
        }
        int index = i - 2;
        if (index < 0) {
            log.warn("Our node is no longer to be leader");
            return;
        }
        zkClient.getData(holdElectionPath + "/" + seqs.get(index), new Watcher() {

            public void process(WatchedEvent event) {
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                // am I the next leader?
                try {
                    checkIfIamLeader(seq, context, true);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                } catch (IOException e) {
                    log.warn("", e);
                } catch (Exception e) {
                    log.warn("", e);
                }
            }
        }, null, true);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 53 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project disgear by yangbutao.

the class ZookeeperController method registerInZk.

public void registerInZk(CollectionDesc desc) throws Exception {
    final String baseUrl = getBaseUrl();
    final String collection = desc.getCollectionName();
    final String coreZkNodeName = desc.getCoreNodeName();
    String shardId = desc.getShardId();
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(ZkStateReader.BASE_URL_PROP, baseUrl);
    props.put(ZkStateReader.CORE_NAME_PROP, collection);
    props.put(ZkStateReader.NODE_NAME_PROP, getNodeName());
    if (log.isInfoEnabled()) {
        log.info("Register replica - " + " address:" + baseUrl + " collection:" + collection + " shard:" + shardId);
    }
    ZkNodeProps leaderProps = new ZkNodeProps(props);
    try {
        joinElection(desc, false);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    } catch (KeeperException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    publish(desc, ZkStateReader.ACTIVE, true);
    zkStateReader.updateClusterState(true);
    // init,start the node for slave mode
    byte[] initData = zkClient.getData(electionContexts.get(nodeName).leaderPath, new Watcher() {

        public void process(WatchedEvent event) {
            if (EventType.None.equals(event.getType())) {
                return;
            }
            try {
                synchronized (this) {
                    final Watcher thisWatch = this;
                    Stat stat = new Stat();
                    byte[] data = zkClient.getData(electionContexts.get(nodeName).leaderPath, thisWatch, stat);
                    Map<String, Object> stateMap = (Map<String, Object>) ZkStateReader.fromJSON(data);
                    // [core=col1, node_name=node1,
                    // base_url=10.1.1.26:6379]
                    System.out.print(stateMap);
                    // cpmpare with leader before
                    String leaderNodeName = (String) stateMap.get("node_name");
                    String nodeName = getNodeName();
                    if (!nodeName.equals(leaderNodeName)) {
                        // current is slave
                        String base_url = (String) stateMap.get("base_url");
                        String[] args = base_url.split(":");
                        ShellExec.runExec(scriptPath + File.separator + "redis_slaves.sh " + args[0] + " " + args[1]);
                    }
                }
            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                    log.warn("ZooKeeper watch triggered, but agent cannot talk to ZK");
                    return;
                }
                log.error("", e);
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                // 
                Thread.currentThread().interrupt();
                log.warn("", e);
                return;
            }
        }
    }, null);
    Map<String, Object> stateMap = (Map<String, Object>) ZkStateReader.fromJSON(initData);
    // [core=col1, node_name=node1, base_url=10.1.1.26:6379]
    System.out.print(stateMap);
    String leaderNodeName = (String) stateMap.get("node_name");
    String nodeName = getNodeName();
    if (!nodeName.equals(leaderNodeName)) {
        // set current to  slave mode
        String base_url = (String) stateMap.get("base_url");
        String[] args = base_url.split(":");
        ShellExec.runExec(scriptPath + File.separator + "redis_slaves.sh " + args[0] + " " + args[1]);
    }
}
Also used : HashMap(java.util.HashMap) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

Example 54 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project pinpoint by naver.

the class CuratorZookeeperClientTest method getChildrenTest.

@Test
public void getChildrenTest() throws Exception {
    ZooKeeper zooKeeper = createZookeeper();
    try {
        String testNodePath = createTestNodePath();
        ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(testNodePath);
        List<String> childrenNode = curatorZookeeperClient.getChildNodeList(pathAndNode.getPath(), true);
        Assert.assertTrue(childrenNode.isEmpty());
        zooKeeper.create(testNodePath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        awaitility().until(eventHoldingZookeeperEventWatcher::getLastWatchedEvent, notNullValue());
        WatchedEvent lastWatchedEvent = eventHoldingZookeeperEventWatcher.getLastWatchedEvent();
        Assert.assertEquals(Watcher.Event.EventType.NodeChildrenChanged, lastWatchedEvent.getType());
        childrenNode = curatorZookeeperClient.getChildNodeList(pathAndNode.getPath(), false);
        Assert.assertFalse(childrenNode.isEmpty());
    } finally {
        ZKUtils.closeQuietly(zooKeeper);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZKPaths(org.apache.curator.utils.ZKPaths) Test(org.junit.Test)

Example 55 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project commons by twitter.

the class SingletonServiceTest method testNonLeaderDisconnect.

@Test
public void testNonLeaderDisconnect() throws Exception {
    CountDownLatch elected = new CountDownLatch(1);
    listener.onLeading(EasyMock.<LeaderControl>anyObject());
    expectLastCall().andAnswer(countDownAnswer(elected));
    listener.onDefeated(null);
    expectLastCall().anyTimes();
    control.replay();
    ZooKeeperClient zkClient = createZkClient();
    String path = "/fake/path";
    // Create a fake leading candidate node to ensure that the leader in this test is never
    // elected.
    ZooKeeperUtils.ensurePath(zkClient, ZooKeeperUtils.OPEN_ACL_UNSAFE, path);
    String leaderNode = zkClient.get().create(path + "/" + SingletonService.LEADER_ELECT_NODE_PREFIX, "fake_leader".getBytes(), ZooKeeperUtils.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    serverSet = new ServerSetImpl(zkClient, path);
    candidate = SingletonService.createSingletonCandidate(zkClient, path, ZooKeeperUtils.OPEN_ACL_UNSAFE);
    DefeatOnDisconnectLeader leader = new DefeatOnDisconnectLeader(zkClient, listener);
    service = new SingletonService(serverSet, candidate);
    service.lead(InetSocketAddress.createUnresolved("foo", PORT_A), ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo", PORT_B)), leader);
    final CountDownLatch disconnected = new CountDownLatch(1);
    zkClient.register(new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if ((event.getType() == EventType.None) && (event.getState() == KeeperState.Disconnected)) {
                disconnected.countDown();
            }
        }
    });
    shutdownNetwork();
    disconnected.await();
    restartNetwork();
    zkClient.get().delete(leaderNode, ZooKeeperUtils.ANY_VERSION);
    // Upon deletion of the fake leader node, the candidate should become leader.
    elected.await();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6