Search in sources :

Example 1 with IZkChildListener

use of org.I0Itec.zkclient.IZkChildListener in project databus by linkedin.

the class GroupLeadershipConnectionZkClientImpl method joinGroup.

@Override
public GroupLeadershipSession joinGroup(final String baseZkPath, final String groupName, String memberName, final AcceptLeadershipCallback acceptLeadershipCallback) {
    final String resolvedMemberName = memberName == null ? makeUniqueMemberName() : memberName;
    LOG.info("joinGroup: groupName=" + groupName + "; memberName=" + memberName + "; resolvedMemberName=" + resolvedMemberName);
    ensurePersistentMembersZkPathExists(baseZkPath, groupName);
    final GroupLeadershipSessionZkClientImpl groupLeadershipSession = new GroupLeadershipSessionZkClientImpl(baseZkPath, groupName, resolvedMemberName, acceptLeadershipCallback);
    //Create and set call backs for data and child changes;
    IZkChildListener childListener = new IZkChildListener() {

        @Override
        public void handleChildChange(String parentPath, List<String> currentChildren) throws Exception {
            takeLeadershipIfNeeded(groupLeadershipSession);
        }
    };
    //Data change call back ; try and get notified on creation of the ephemeral member node
    IZkDataListener dataListener = new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
            LOG.info("LeaderElection: Data change in: " + dataPath);
            takeLeadershipIfNeeded(groupLeadershipSession);
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            relinquishLeadership(groupLeadershipSession);
        }
    };
    groupLeadershipSession.setListener(childListener);
    groupLeadershipSession.setDataListener(dataListener);
    //create a ephemeral node for the member
    String memberPath = makeMemberNodePath(baseZkPath, groupName, memberName);
    String path = _zkClient.createEphemeralSequential(memberPath, groupLeadershipSession.getUniqueID());
    groupLeadershipSession.setMemberZkName(memberName + path.substring(path.length() - 10));
    String dataWatchPath = makeMemberNodePath(baseZkPath, groupName, groupLeadershipSession.getMemberZkName());
    LOG.info("Created path:" + path + " zkName = " + groupLeadershipSession.getMemberZkName() + " data watch path: " + dataWatchPath);
    //subscribe for data changes in this node; in case the node is not created at time takeLeadership is invoked
    //could be a back door to trigger takeLeadershipIfNeeded without a restart
    _zkClient.subscribeDataChanges(dataWatchPath, dataListener);
    takeLeadershipIfNeeded(groupLeadershipSession);
    return groupLeadershipSession;
}
Also used : IZkChildListener(org.I0Itec.zkclient.IZkChildListener) ArrayList(java.util.ArrayList) List(java.util.List) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Example 2 with IZkChildListener

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

the class ZookeeperRegistry method subscribeService.

@Override
protected void subscribeService(final URL url, final ServiceListener serviceListener) {
    try {
        clientLock.lock();
        ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners == null) {
            serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>());
            childChangeListeners = serviceListeners.get(url);
        }
        IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
        if (zkChildListener == null) {
            childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {

                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) {
                    serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds));
                    LoggerUtil.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
                }
            });
            zkChildListener = childChangeListeners.get(serviceListener);
        }
        // 防止旧节点未正常注销
        removeNode(url, ZkNodeType.CLIENT);
        createNode(url, ZkNodeType.CLIENT);
        String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        zkClient.subscribeChildChanges(serverTypePath, zkChildListener);
        LoggerUtil.info(String.format("[ZookeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr()));
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to subscribe %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Example 3 with IZkChildListener

use of org.I0Itec.zkclient.IZkChildListener in project jesos by groupon.

the class ZookeeperMasterDetector method start.

public void start() {
    if (!running.getAndSet(true)) {
        processChildList(client.getChildren(zookeeperPath));
        client.subscribeChildChanges(zookeeperPath, new IZkChildListener() {

            @Override
            public void handleChildChange(final String parentPath, final List<String> currentChildren) throws Exception {
                checkState(zookeeperPath.equals(parentPath), "Received Event for %s (expected %s)", parentPath, zookeeperPath);
                processChildList(currentChildren);
            }
        });
    }
}
Also used : IZkChildListener(org.I0Itec.zkclient.IZkChildListener) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException)

Example 4 with IZkChildListener

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

the class ZookeeperRegistry method reconnectClient.

@SuppressWarnings("rawtypes")
private void reconnectClient() {
    if (serviceListeners != null && !serviceListeners.isEmpty()) {
        try {
            clientLock.lock();
            for (Map.Entry entry : serviceListeners.entrySet()) {
                URL url = (URL) entry.getKey();
                ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
                if (childChangeListeners != null) {
                    for (Map.Entry e : childChangeListeners.entrySet()) {
                        subscribeService(url, (ServiceListener) e.getKey());
                    }
                }
            }
            for (Map.Entry entry : commandListeners.entrySet()) {
                URL url = (URL) entry.getKey();
                ConcurrentHashMap<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
                if (dataChangeListeners != null) {
                    for (Map.Entry e : dataChangeListeners.entrySet()) {
                        subscribeCommand(url, (CommandListener) e.getKey());
                    }
                }
            }
            LoggerUtil.info("[{}] reconnect all clients", registryClassName);
        } finally {
            clientLock.unlock();
        }
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) URL(com.weibo.api.motan.rpc.URL)

Example 5 with IZkChildListener

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

the class ZookeeperRegistry method unsubscribeService.

@Override
protected void unsubscribeService(URL url, ServiceListener serviceListener) {
    try {
        clientLock.lock();
        Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners != null) {
            IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
            if (zkChildListener != null) {
                zkClient.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener);
                childChangeListeners.remove(serviceListener);
            }
        }
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unsubscribe service %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Aggregations

IZkChildListener (org.I0Itec.zkclient.IZkChildListener)5 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 IZkDataListener (org.I0Itec.zkclient.IZkDataListener)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)1 URL (com.weibo.api.motan.rpc.URL)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1