Search in sources :

Example 41 with ChildData

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

the class GroupMember method getCurrentMembers.

/**
 * Return the current view of membership. The keys are the IDs
 * of the members. The values are each member's payload
 *
 * @return membership
 */
public Map<String, byte[]> getCurrentMembers() {
    ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder();
    boolean thisIdAdded = false;
    for (ChildData data : cache.getCurrentData()) {
        String id = idFromPath(data.getPath());
        thisIdAdded = thisIdAdded || id.equals(thisId);
        builder.put(id, data.getData());
    }
    if (!thisIdAdded) {
        // this instance is always a member
        builder.put(thisId, pen.getData());
    }
    return builder.build();
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 42 with ChildData

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

the class ZooKeeperConfigurationSource method start.

/**
 * Adds a listener to the pathChildrenCache, initializes the cache, then starts the cache-management background thread
 *
 * @throws Exception
 */
public void start() throws Exception {
    // create the watcher for future configuration updatess
    pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

        public void childEvent(CuratorFramework aClient, PathChildrenCacheEvent event) throws Exception {
            Type eventType = event.getType();
            ChildData data = event.getData();
            String path = null;
            if (data != null) {
                path = data.getPath();
                // scrub configRootPath out of the key name
                String key = removeRootPath(path);
                byte[] value = data.getData();
                String stringValue = new String(value, charset);
                logger.debug("received update to pathName [{}], eventType [{}]", path, eventType);
                logger.debug("key [{}], and value [{}]", key, stringValue);
                // fire event to all listeners
                Map<String, Object> added = null;
                Map<String, Object> changed = null;
                Map<String, Object> deleted = null;
                if (eventType == Type.CHILD_ADDED) {
                    added = new HashMap<String, Object>(1);
                    added.put(key, stringValue);
                } else if (eventType == Type.CHILD_UPDATED) {
                    changed = new HashMap<String, Object>(1);
                    changed.put(key, stringValue);
                } else if (eventType == Type.CHILD_REMOVED) {
                    deleted = new HashMap<String, Object>(1);
                    deleted.put(key, stringValue);
                }
                WatchedUpdateResult result = WatchedUpdateResult.createIncremental(added, changed, deleted);
                fireEvent(result);
            }
        }
    });
    // passing true to trigger an initial rebuild upon starting.  (blocking call)
    pathChildrenCache.start(true);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Type(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) HashMap(java.util.HashMap) ChildData(org.apache.curator.framework.recipes.cache.ChildData) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult)

Example 43 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project sharding-jdbc by shardingjdbc.

the class ZookeeperRegistryCenter method watch.

@Override
public void watch(final String key, final EventListener eventListener) {
    final String path = key + "/";
    if (!caches.containsKey(path)) {
        addCacheData(key);
    }
    TreeCache cache = caches.get(path);
    cache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception {
            ChildData data = event.getData();
            if (null == data || null == data.getPath()) {
                return;
            }
            eventListener.onChange(new DataChangedEvent(getEventType(event), data.getPath(), null == data.getData() ? null : new String(data.getData(), "UTF-8")));
        }

        private DataChangedEvent.Type getEventType(final TreeCacheEvent event) {
            switch(event.getType()) {
                case NODE_UPDATED:
                    return DataChangedEvent.Type.UPDATED;
                case NODE_REMOVED:
                    return DataChangedEvent.Type.DELETED;
                default:
                    return DataChangedEvent.Type.IGNORED;
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) OperationTimeoutException(org.apache.zookeeper.KeeperException.OperationTimeoutException)

Example 44 with ChildData

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

the class TestZookeeperClient method testEntriesReturnsRelativePaths.

@Test
public void testEntriesReturnsRelativePaths() throws Exception {
    final ChildData child = Mockito.mock(ChildData.class);
    Mockito.when(child.getPath()).thenReturn(abspath);
    Mockito.when(child.getData()).thenReturn(data);
    final List<ChildData> children = Lists.newArrayList(child);
    Mockito.when(client.getCache().getCurrentData()).thenReturn(children);
    final Iterator<Map.Entry<String, byte[]>> entries = client.entries();
    // returned entry must contain the given relative path
    final Map.Entry<String, byte[]> expected = new ImmutableEntry<>(path, data);
    assertEquals("entries do not match", expected, entries.next());
}
Also used : ImmutableEntry(org.apache.drill.common.collections.ImmutableEntry) ImmutableEntry(org.apache.drill.common.collections.ImmutableEntry) ChildData(org.apache.curator.framework.recipes.cache.ChildData) Map(java.util.Map) Test(org.junit.Test)

Example 45 with ChildData

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

the class TestZookeeperClient method testGetWithEventualConsistencyHitsCache.

@Test
public void testGetWithEventualConsistencyHitsCache() {
    Mockito.when(client.getCache().getCurrentData(abspath)).thenReturn(null);
    assertEquals("get should return null", null, client.get(path));
    Mockito.when(client.getCache().getCurrentData(abspath)).thenReturn(new ChildData(abspath, null, data));
    assertEquals("get should return data", data, client.get(path, false));
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) Test(org.junit.Test)

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