Search in sources :

Example 86 with ChildData

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

the class ZkRegistryBase method populateCache.

protected final void populateCache(PathChildrenCache instancesCache, boolean doInvokeListeners) {
    for (ChildData childData : instancesCache.getCurrentData()) {
        byte[] data = getWorkerData(childData, workerNodePrefix);
        if (data == null)
            continue;
        String nodeName = extractNodeName(childData);
        if (!nodeName.startsWith(workerNodePrefix))
            continue;
        int ephSeqVersion = extractSeqNum(nodeName);
        try {
            ServiceRecord srv = encoder.fromBytes(childData.getPath(), data);
            InstanceType instance = createServiceInstance(srv);
            addToCache(childData.getPath(), instance.getHost(), instance);
            if (doInvokeListeners) {
                for (ServiceInstanceStateChangeListener<InstanceType> listener : stateChangeListeners) {
                    listener.onCreate(instance, ephSeqVersion);
                }
            }
        } catch (IOException e) {
            LOG.error("Unable to decode data for zkpath: {}." + " Ignoring from current instances list..", childData.getPath());
        }
    }
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) IOException(java.io.IOException) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord)

Example 87 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project paascloud-master by paascloud.

the class ZookeeperRegistryCenter method get.

/**
 * Get string.
 *
 * @param key the key
 *
 * @return the string
 */
@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 88 with ChildData

use of org.apache.curator.framework.recipes.cache.ChildData in project paascloud-master by paascloud.

the class MqConsumerChangeListener method childEvent.

/**
 * Child event.
 *
 * @param client the client
 * @param event  the event
 */
@Override
public void childEvent(final CuratorFramework client, final TreeCacheEvent event) {
    ChildData data = event.getData();
    if (data == null) {
        return;
    }
    String path = data.getPath();
    if (GlobalConstant.ZK_REGISTRY_CONSUMER_ROOT_PATH.equals(path) || GlobalConstant.ZK_REGISTRY_CONSUMER_ROOT_PATH.equals(path)) {
        return;
    }
    String[] split = path.split(GlobalConstant.Symbol.SLASH);
    String dataStr = new String(data.getData());
    switch(event.getType()) {
        case NODE_ADDED:
            log.info("MqConsumerChangeListener CHILD_ADDED path={}, data={}", path, dataStr);
            if (split.length == GlobalConstant.Number.SIX_INT) {
                String appPath = path.substring(0, path.lastIndexOf(GlobalConstant.Symbol.SLASH));
                ReliableMessageRegisterDto dto = JSON.parseObject(getDirectly(client, appPath), ReliableMessageRegisterDto.class);
                if (this.getNumChildrenCount(client, appPath) > 0) {
                    tpcMqConsumerService.updateOnLineStatusByCid(dto.getConsumerGroup());
                    MqProducerBeanFactory.putCid(dto.getConsumerGroup());
                }
            }
            break;
        case NODE_REMOVED:
            log.info("MqConsumerChangeListener NODE_REMOVED path={}, data={}", path, dataStr);
            if (split.length == GlobalConstant.Number.SIX_INT) {
                String appPath = path.substring(0, path.lastIndexOf(GlobalConstant.Symbol.SLASH));
                ReliableMessageRegisterDto dto = JSON.parseObject(getDirectly(client, appPath), ReliableMessageRegisterDto.class);
                if (this.getNumChildrenCount(client, appPath) < 1) {
                    tpcMqConsumerService.updateOffLineStatusByCid(dto.getConsumerGroup());
                    MqProducerBeanFactory.rmCid(dto.getConsumerGroup());
                }
            }
            break;
        case NODE_UPDATED:
            log.error("MqConsumerChangeListener 本次版本不对更新做处理, path={}, data={}", path, new String(data.getData()));
            break;
        default:
            break;
    }
}
Also used : ReliableMessageRegisterDto(com.paascloud.core.registry.base.ReliableMessageRegisterDto) ChildData(org.apache.curator.framework.recipes.cache.ChildData)

Aggregations

ChildData (org.apache.curator.framework.recipes.cache.ChildData)88 Test (org.junit.Test)71 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)67 LeaderElectionJobListener (com.dangdang.ddframe.job.lite.internal.election.ElectionListenerManager.LeaderElectionJobListener)7 SimpleJobConfiguration (com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration)6 JobPausedStatusJobListener (com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener)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 IOException (java.io.IOException)4 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 KeeperException (org.apache.zookeeper.KeeperException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)3 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)3 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)3 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)3 ReliableMessageRegisterDto (com.paascloud.core.registry.base.ReliableMessageRegisterDto)2 ObjectInputStream (java.io.ObjectInputStream)2