Search in sources :

Example 1 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project jeesuite-libs by vakinge.

the class ZkJobRegistry method register.

@Override
public synchronized void register(JobConfig conf) {
    // 是否第一个启动节点
    boolean isFirstNode = false;
    Calendar now = Calendar.getInstance();
    long currentTimeMillis = now.getTimeInMillis();
    conf.setModifyTime(currentTimeMillis);
    if (groupPath == null) {
        groupPath = ROOT + conf.getGroupName();
    }
    if (nodeStateParentPath == null) {
        nodeStateParentPath = groupPath + "/nodes";
    }
    String path = getPath(conf);
    final String jobName = conf.getJobName();
    if (!zkClient.exists(nodeStateParentPath)) {
        isFirstNode = true;
        zkClient.createPersistent(nodeStateParentPath, true);
    } else {
        // 检查是否有节点
        if (!isFirstNode) {
            isFirstNode = zkClient.getChildren(nodeStateParentPath).size() == 0;
        }
    }
    if (!zkClient.exists(path)) {
        zkClient.createPersistent(path, true);
    }
    // 是否要更新ZK的conf配置
    boolean updateConfInZK = isFirstNode;
    if (!updateConfInZK) {
        JobConfig configFromZK = getConfigFromZK(path, null);
        if (configFromZK != null) {
            // 3.配置文件修改是30分钟前
            if (!StringUtils.equals(configFromZK.getCronExpr(), conf.getCronExpr())) {
                updateConfInZK = true;
            } else if (configFromZK.getNextFireTime() != null && configFromZK.getNextFireTime().before(now.getTime())) {
                updateConfInZK = true;
            } else if (currentTimeMillis - configFromZK.getModifyTime() > TimeUnit.MINUTES.toMillis(30)) {
                updateConfInZK = true;
            } else {
                if (!JobContext.getContext().getNodeId().equals(configFromZK.getCurrentNodeId())) {
                    List<String> nodes = zkClient.getChildren(nodeStateParentPath);
                    updateConfInZK = !nodes.contains(configFromZK.getCurrentNodeId());
                }
            }
        } else {
            // zookeeper 该job不存在?
            updateConfInZK = true;
        }
        // 拿ZK上的配置覆盖当前的
        if (!updateConfInZK) {
            conf = configFromZK;
        }
    }
    if (updateConfInZK) {
        conf.setCurrentNodeId(JobContext.getContext().getNodeId());
        zkClient.writeData(path, JsonUtils.toJson(conf));
    }
    schedulerConfgs.put(conf.getJobName(), conf);
    // 订阅同步信息变化
    zkClient.subscribeDataChanges(path, new IZkDataListener() {

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            schedulerConfgs.remove(jobName);
        }

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
            if (data == null)
                return;
            JobConfig _jobConfig = JsonUtils.toObject(data.toString(), JobConfig.class);
            schedulerConfgs.put(jobName, _jobConfig);
        }
    });
    // 
    regAndSubscribeNodeEvent();
    logger.info("finish register schConfig:{}", ToStringBuilder.reflectionToString(conf, ToStringStyle.MULTI_LINE_STYLE));
}
Also used : Calendar(java.util.Calendar) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) JobConfig(com.jeesuite.scheduler.model.JobConfig)

Example 2 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project helix by apache.

the class TestZkClient method testZkClientMonitor.

@Test
public void testZkClientMonitor() throws Exception {
    final String TEST_TAG = "test_monitor";
    final String TEST_KEY = "test_key";
    final String TEST_DATA = "testData";
    final String TEST_ROOT = "/my_cluster/IDEALSTATES";
    final String TEST_NODE = "/test_zkclient_monitor";
    final String TEST_PATH = TEST_ROOT + TEST_NODE;
    ZkClient.Builder builder = new ZkClient.Builder();
    builder.setZkServer(ZK_ADDR).setMonitorKey(TEST_KEY).setMonitorType(TEST_TAG).setMonitorRootPathOnly(false);
    ZkClient zkClient = builder.build();
    final long TEST_DATA_SIZE = zkClient.serialize(TEST_DATA, TEST_PATH).length;
    if (_zkClient.exists(TEST_PATH)) {
        _zkClient.delete(TEST_PATH);
    }
    if (!_zkClient.exists(TEST_ROOT)) {
        _zkClient.createPersistent(TEST_ROOT, true);
    }
    MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = MBeanRegistrar.buildObjectName(MonitorDomainNames.HelixZkClient.name(), ZkClientMonitor.MONITOR_TYPE, TEST_TAG, ZkClientMonitor.MONITOR_KEY, TEST_KEY);
    ObjectName rootname = MBeanRegistrar.buildObjectName(MonitorDomainNames.HelixZkClient.name(), ZkClientMonitor.MONITOR_TYPE, TEST_TAG, ZkClientMonitor.MONITOR_KEY, TEST_KEY, ZkClientPathMonitor.MONITOR_PATH, "Root");
    ObjectName idealStatename = MBeanRegistrar.buildObjectName(MonitorDomainNames.HelixZkClient.name(), ZkClientMonitor.MONITOR_TYPE, TEST_TAG, ZkClientMonitor.MONITOR_KEY, TEST_KEY, ZkClientPathMonitor.MONITOR_PATH, "IdealStates");
    Assert.assertTrue(beanServer.isRegistered(rootname));
    Assert.assertTrue(beanServer.isRegistered(idealStatename));
    // Test exists
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadTotalLatencyCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadLatencyGauge.Max"), 0);
    zkClient.exists(TEST_ROOT);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 1);
    Assert.assertTrue((long) beanServer.getAttribute(rootname, "ReadTotalLatencyCounter") >= 0);
    Assert.assertTrue((long) beanServer.getAttribute(rootname, "ReadLatencyGauge.Max") >= 0);
    // Test create
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteBytesCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteBytesCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteTotalLatencyCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteLatencyGauge.Max"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteTotalLatencyCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteLatencyGauge.Max"), 0);
    zkClient.create(TEST_PATH, TEST_DATA, CreateMode.PERSISTENT);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteCounter"), 1);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteBytesCounter"), TEST_DATA_SIZE);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteCounter"), 1);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteBytesCounter"), TEST_DATA_SIZE);
    long origWriteTotalLatencyCounter = (long) beanServer.getAttribute(rootname, "WriteTotalLatencyCounter");
    Assert.assertTrue(origWriteTotalLatencyCounter >= 0);
    Assert.assertTrue((long) beanServer.getAttribute(rootname, "WriteLatencyGauge.Max") >= 0);
    long origIdealStatesWriteTotalLatencyCounter = (long) beanServer.getAttribute(idealStatename, "WriteTotalLatencyCounter");
    Assert.assertTrue(origIdealStatesWriteTotalLatencyCounter >= 0);
    Assert.assertTrue((long) beanServer.getAttribute(idealStatename, "WriteLatencyGauge.Max") >= 0);
    // Test read
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 1);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadBytesCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadCounter"), 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadBytesCounter"), 0);
    long origReadTotalLatencyCounter = (long) beanServer.getAttribute(rootname, "ReadTotalLatencyCounter");
    long origIdealStatesReadTotalLatencyCounter = (long) beanServer.getAttribute(idealStatename, "ReadTotalLatencyCounter");
    Assert.assertEquals(origIdealStatesReadTotalLatencyCounter, 0);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadLatencyGauge.Max"), 0);
    zkClient.readData(TEST_PATH, new Stat());
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 2);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadBytesCounter"), TEST_DATA_SIZE);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadCounter"), 1);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadBytesCounter"), TEST_DATA_SIZE);
    Assert.assertTrue((long) beanServer.getAttribute(rootname, "ReadTotalLatencyCounter") >= origReadTotalLatencyCounter);
    Assert.assertTrue((long) beanServer.getAttribute(idealStatename, "ReadTotalLatencyCounter") >= origIdealStatesReadTotalLatencyCounter);
    Assert.assertTrue((long) beanServer.getAttribute(idealStatename, "ReadLatencyGauge.Max") >= 0);
    zkClient.getChildren(TEST_PATH);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 3);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadBytesCounter"), TEST_DATA_SIZE);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadCounter"), 2);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadBytesCounter"), TEST_DATA_SIZE);
    zkClient.getStat(TEST_PATH);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 4);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadBytesCounter"), TEST_DATA_SIZE);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadCounter"), 3);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadBytesCounter"), TEST_DATA_SIZE);
    zkClient.readDataAndStat(TEST_PATH, new Stat(), true);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 5);
    ZkAsyncCallbacks.ExistsCallbackHandler callbackHandler = new ZkAsyncCallbacks.ExistsCallbackHandler();
    zkClient.asyncExists(TEST_PATH, callbackHandler);
    callbackHandler.waitForSuccess();
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 6);
    // Test write
    zkClient.writeData(TEST_PATH, TEST_DATA);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteCounter"), 2);
    Assert.assertEquals((long) beanServer.getAttribute(rootname, "WriteBytesCounter"), TEST_DATA_SIZE * 2);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteCounter"), 2);
    Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "WriteBytesCounter"), TEST_DATA_SIZE * 2);
    Assert.assertTrue((long) beanServer.getAttribute(rootname, "WriteTotalLatencyCounter") >= origWriteTotalLatencyCounter);
    Assert.assertTrue((long) beanServer.getAttribute(idealStatename, "WriteTotalLatencyCounter") >= origIdealStatesWriteTotalLatencyCounter);
    // Test data change count
    final Lock lock = new ReentrantLock();
    final Condition callbackFinish = lock.newCondition();
    zkClient.subscribeDataChanges(TEST_PATH, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            lock.lock();
            try {
                callbackFinish.signal();
            } finally {
                lock.unlock();
            }
        }
    });
    lock.lock();
    _zkClient.delete(TEST_PATH);
    Assert.assertTrue(callbackFinish.await(10, TimeUnit.SECONDS));
    Assert.assertEquals((long) beanServer.getAttribute(name, "DataChangeEventCounter"), 1);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) HelixException(org.apache.helix.HelixException) ObjectName(javax.management.ObjectName) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Stat(org.apache.zookeeper.data.Stat) MBeanServer(javax.management.MBeanServer) Test(org.testng.annotations.Test)

Example 3 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project helix by apache.

the class TestZkFlapping method testCloseZkClientInZkClientEventThread.

@Test
public void testCloseZkClientInZkClientEventThread() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    final CountDownLatch waitCallback = new CountDownLatch(1);
    final ZkClient client = new ZkClient(ZK_ADDR, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    String path = String.format("/%s", clusterName);
    client.createEphemeral(path);
    client.subscribeDataChanges(path, new IZkDataListener() {

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
        }

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
            client.close();
            waitCallback.countDown();
        }
    });
    client.writeData(path, new ZNRecord("test"));
    waitCallback.await();
    Assert.assertFalse(_gZkClient.exists(path), "Ephemeral node: " + path + " should be removed after ZkClient#close() in its own event-thread");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : IZkDataListener(org.I0Itec.zkclient.IZkDataListener) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 4 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project samza by apache.

the class TestZkLeaderElector method testNonLeaderFailure.

/**
 * Tests that a non-leader failure updates the Zk tree and participants' state correctly
 */
@Test
public void testNonLeaderFailure() {
    /**
     * electionLatch and count together verify that:
     * 1. the registered listeners are actually invoked by the ZkClient on the correct path
     * 2. for a single participant failure, at-most 1 other participant is notified
     */
    final CountDownLatch electionLatch = new CountDownLatch(1);
    final AtomicInteger count = new AtomicInteger(0);
    BooleanResult isLeader1 = new BooleanResult();
    BooleanResult isLeader2 = new BooleanResult();
    BooleanResult isLeader3 = new BooleanResult();
    // Processor-1
    ZkUtils zkUtils1 = getZkUtilsWithNewClient();
    zkUtils1.registerProcessorAndGetId(new ProcessorData("processor1", "1"));
    ZkLeaderElector leaderElector1 = new ZkLeaderElector("processor1", zkUtils1, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            count.incrementAndGet();
        }
    });
    leaderElector1.setLeaderElectorListener(() -> isLeader1.res = true);
    // Processor-2
    ZkUtils zkUtils2 = getZkUtilsWithNewClient();
    zkUtils2.registerProcessorAndGetId(new ProcessorData("processor2", "2"));
    ZkLeaderElector leaderElector2 = new ZkLeaderElector("processor2", zkUtils2, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            count.incrementAndGet();
        }
    });
    leaderElector2.setLeaderElectorListener(() -> isLeader2.res = true);
    // Processor-3
    ZkUtils zkUtils3 = getZkUtilsWithNewClient();
    final String path3 = zkUtils3.registerProcessorAndGetId(new ProcessorData("processor3", "3"));
    ZkLeaderElector leaderElector3 = new ZkLeaderElector("processor3", zkUtils3, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            String registeredIdStr = ZkKeyBuilder.parseIdFromPath(path3);
            Assert.assertNotNull(registeredIdStr);
            String predecessorIdStr = ZkKeyBuilder.parseIdFromPath(dataPath);
            Assert.assertNotNull(predecessorIdStr);
            try {
                int selfId = Integer.parseInt(registeredIdStr);
                int predecessorId = Integer.parseInt(predecessorIdStr);
                Assert.assertEquals(1, selfId - predecessorId);
            } catch (Exception e) {
                Assert.fail("Exception in LeaderElectionListener!");
            }
            count.incrementAndGet();
            electionLatch.countDown();
        }
    });
    leaderElector3.setLeaderElectorListener(() -> isLeader3.res = true);
    // Join Leader Election
    leaderElector1.tryBecomeLeader();
    leaderElector2.tryBecomeLeader();
    leaderElector3.tryBecomeLeader();
    Assert.assertTrue(TestZkUtils.testWithDelayBackOff(() -> isLeader1.res, 2, 100));
    Assert.assertFalse(TestZkUtils.testWithDelayBackOff(() -> isLeader2.res, 2, 100));
    Assert.assertFalse(TestZkUtils.testWithDelayBackOff(() -> isLeader3.res, 2, 100));
    List<String> currentActiveProcessors = zkUtils1.getSortedActiveProcessorsZnodes();
    Assert.assertEquals(3, currentActiveProcessors.size());
    zkUtils2.close();
    currentActiveProcessors.remove(1);
    try {
        Assert.assertTrue(electionLatch.await(5, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        Assert.fail("Interrupted while waiting for leaderElection listener callback to complete!");
    }
    Assert.assertEquals(1, count.get());
    Assert.assertEquals(currentActiveProcessors, zkUtils1.getSortedActiveProcessorsZnodes());
    // Clean up
    zkUtils1.close();
    zkUtils3.close();
}
Also used : IZkDataListener(org.I0Itec.zkclient.IZkDataListener) CountDownLatch(java.util.concurrent.CountDownLatch) SamzaException(org.apache.samza.SamzaException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project motan by weibocom.

the class ZookeeperRegistry method unsubscribeCommand.

@Override
protected void unsubscribeCommand(URL url, CommandListener commandListener) {
    try {
        clientLock.lock();
        Map<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
        if (dataChangeListeners != null) {
            IZkDataListener zkDataListener = dataChangeListeners.get(commandListener);
            if (zkDataListener != null) {
                zkClient.unsubscribeDataChanges(ZkUtils.toCommandPath(url), zkDataListener);
                dataChangeListeners.remove(commandListener);
            }
        }
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unsubscribe command %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Aggregations

IZkDataListener (org.I0Itec.zkclient.IZkDataListener)17 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)7 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 Test (org.testng.annotations.Test)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)2 CallbackHandler (org.apache.helix.manager.zk.CallbackHandler)2 SamzaException (org.apache.samza.SamzaException)2 JobConfig (com.jeesuite.scheduler.model.JobConfig)1 MonitorCommond (com.jeesuite.scheduler.monitor.MonitorCommond)1 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)1 URL (com.weibo.api.motan.rpc.URL)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1