Search in sources :

Example 6 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project elastic-job by dangdangdotcom.

the class JobOperationListenerManagerTest method assertJobPausedStatusJobListenerWhenIsJobPausedPathAndRemove.

@Test
public void assertJobPausedStatusJobListenerWhenIsJobPausedPathAndRemove() {
    JobRegistry.getInstance().addJobScheduleController("test_job", jobScheduleController);
    jobOperationListenerManager.new JobPausedStatusJobListener().dataChanged(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_REMOVED, new ChildData("/test_job/servers/" + ip + "/paused", null, "".getBytes())), "/test_job/servers/" + ip + "/paused");
    verify(jobScheduleController, times(0)).pauseJob();
    verify(jobScheduleController).resumeJob();
    verify(serverService).clearJobPausedStatus();
}
Also used : JobPausedStatusJobListener(com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Example 7 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project elastic-job by dangdangdotcom.

the class JobOperationListenerManagerTest method assertJobPausedStatusJobListenerWhenIsJobPausedPathButJobIsNotExisted.

@Test
public void assertJobPausedStatusJobListenerWhenIsJobPausedPathButJobIsNotExisted() {
    jobOperationListenerManager.new JobPausedStatusJobListener().dataChanged(null, new TreeCacheEvent(TreeCacheEvent.Type.NODE_ADDED, new ChildData("/test_job/servers/" + ip + "/paused", null, "".getBytes())), "/test_job/servers/" + ip + "/paused");
    verify(jobScheduleController, times(0)).pauseJob();
    verify(jobScheduleController, times(0)).resumeJob();
}
Also used : JobPausedStatusJobListener(com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Example 8 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project elastic-job by dangdangdotcom.

the class ZookeeperRegistryCenter method get.

@Override
public String get(final String key) {
    TreeCache cache = findTreeCache(key);
    if (null == cache) {
        return getDirectly(key);
    }
    ChildData resultInCache = cache.getCurrentData(key);
    if (null != resultInCache) {
        return null == resultInCache.getData() ? null : new String(resultInCache.getData(), Charsets.UTF_8);
    }
    return getDirectly(key);
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData)

Example 9 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project flink by apache.

the class ZooKeeperLeaderElectionService method nodeChanged.

@Override
public void nodeChanged() throws Exception {
    try {
        // leaderSessionID is null if the leader contender has not yet confirmed the session ID
        if (leaderLatch.hasLeadership()) {
            synchronized (lock) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Leader node changed while {} is the leader with session ID {}.", leaderContender.getAddress(), confirmedLeaderSessionID);
                }
                if (confirmedLeaderSessionID != null) {
                    ChildData childData = cache.getCurrentData();
                    if (childData == null) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Writing leader information into empty node by {}.", leaderContender.getAddress());
                        }
                        writeLeaderInformation(confirmedLeaderSessionID);
                    } else {
                        byte[] data = childData.getData();
                        if (data == null || data.length == 0) {
                            // the data field seems to be empty, rewrite information
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Writing leader information into node with empty data field by {}.", leaderContender.getAddress());
                            }
                            writeLeaderInformation(confirmedLeaderSessionID);
                        } else {
                            ByteArrayInputStream bais = new ByteArrayInputStream(data);
                            ObjectInputStream ois = new ObjectInputStream(bais);
                            String leaderAddress = ois.readUTF();
                            UUID leaderSessionID = (UUID) ois.readObject();
                            if (!leaderAddress.equals(this.leaderContender.getAddress()) || (leaderSessionID == null || !leaderSessionID.equals(confirmedLeaderSessionID))) {
                                // the data field does not correspond to the expected leader information
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Correcting leader information by {}.", leaderContender.getAddress());
                                }
                                writeLeaderInformation(confirmedLeaderSessionID);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        leaderContender.handleError(new Exception("Could not handle node changed event.", e));
        throw e;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ChildData(org.apache.curator.framework.recipes.cache.ChildData) UUID(java.util.UUID) KeeperException(org.apache.zookeeper.KeeperException) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project hadoop by apache.

the class ZKDelegationTokenSecretManager method loadFromZKCache.

/**
   * Load the PathChildrenCache into the in-memory map. Possible caches to be
   * loaded are keyCache and tokenCache.
   *
   * @param isTokenCache true if loading tokenCache, false if loading keyCache.
   */
private void loadFromZKCache(final boolean isTokenCache) {
    final String cacheName = isTokenCache ? "token" : "key";
    LOG.info("Starting to load {} cache.", cacheName);
    final List<ChildData> children;
    if (isTokenCache) {
        children = tokenCache.getCurrentData();
    } else {
        children = keyCache.getCurrentData();
    }
    int count = 0;
    for (ChildData child : children) {
        try {
            if (isTokenCache) {
                processTokenAddOrUpdate(child);
            } else {
                processKeyAddOrUpdate(child.getData());
            }
        } catch (Exception e) {
            LOG.info("Ignoring node {} because it failed to load.", child.getPath());
            LOG.debug("Failure exception:", e);
            ++count;
        }
    }
    if (count > 0) {
        LOG.warn("Ignored {} nodes while loading {} cache.", count, cacheName);
    }
    LOG.info("Loaded {} cache.", cacheName);
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException)

Aggregations

ChildData (org.apache.curator.framework.recipes.cache.ChildData)119 Test (org.junit.Test)75 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)70 IOException (java.io.IOException)10 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)9 CuratorFramework (org.apache.curator.framework.CuratorFramework)8 LeaderElectionJobListener (com.dangdang.ddframe.job.lite.internal.election.ElectionListenerManager.LeaderElectionJobListener)7 SimpleJobConfiguration (com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration)6 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)6 KeeperException (org.apache.zookeeper.KeeperException)6 JobPausedStatusJobListener (com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener)5 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)5 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)5 TestSimpleJob (com.dangdang.ddframe.job.lite.fixture.TestSimpleJob)4 CronSettingAndJobEventChangedJobListener (com.dangdang.ddframe.job.lite.internal.config.ConfigurationListenerManager.CronSettingAndJobEventChangedJobListener)4 MonitorExecutionChangedJobListener (com.dangdang.ddframe.job.lite.internal.execution.ExecutionListenerManager.MonitorExecutionChangedJobListener)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ServiceDTO (io.fabric8.gateway.ServiceDTO)3 ObjectInputStream (java.io.ObjectInputStream)3 URISyntaxException (java.net.URISyntaxException)3