Search in sources :

Example 1 with ZooKeeper

use of com.linkedin.d2.discovery.stores.zk.ZooKeeper in project rest.li by linkedin.

the class TestD2ZKQuorumFailover method setup.

private void setup() throws IOException, Exception {
    // Start _quorum
    _quorum = new ZKQuorum(QUORUM_SIZE);
    _quorum.startAll();
    _quorum.assertAllPeersUp();
    _zkUriString = "zk://" + _quorum.getHosts();
    _zkHosts = _quorum.getHosts().split(",");
    // Register clusters/services with zookeeper _quorum
    LoadBalancerClientCli.runDiscovery(_quorum.getHosts(), "/d2", D2_CONFIG_DATA);
    // Echo servers startup
    startAllEchoServers();
    assertAllEchoServersRunning(_echoServers);
    // Get LoadBalancer Client
    _cli = new LoadBalancerClientCli(_quorum.getHosts(), "/d2");
    _client = _cli.createZKFSTogglingLBClient(_quorum.getHosts(), "/d2", null);
    assertAllEchoServersRegistered(_cli.getZKClient(), _zkUriString, _echoServers);
    assertQuorumProcessAllRequests(D2_CONFIG_DATA);
}
Also used : LoadBalancerClientCli(com.linkedin.d2.balancer.util.LoadBalancerClientCli) ZKQuorum(com.linkedin.d2.quorum.ZKQuorum)

Example 2 with ZooKeeper

use of com.linkedin.d2.discovery.stores.zk.ZooKeeper in project rest.li by linkedin.

the class LoadBalancerClientCli method getServicesGroups.

public static List<String> getServicesGroups(ZKConnection zkclient, String basePath) throws Exception {
    List<String> servicesGroups = new ArrayList<String>();
    ZooKeeper zook = zkclient.getZooKeeper();
    List<String> children = zook.getChildren(basePath, false);
    for (String child : children) {
        if (!child.equalsIgnoreCase("clusters") && !child.equalsIgnoreCase("uris")) {
            servicesGroups.add(child);
        }
    }
    return servicesGroups;
}
Also used : ZooKeeper(com.linkedin.d2.discovery.stores.zk.ZooKeeper) ArrayList(java.util.ArrayList)

Example 3 with ZooKeeper

use of com.linkedin.d2.discovery.stores.zk.ZooKeeper in project rest.li by linkedin.

the class ZKFSDirectory method getServiceNames.

@Override
public void getServiceNames(final Callback<List<String>> callback) {
    final ZooKeeper zk = _connection.getZooKeeper();
    final String path = ZKFSUtil.servicePath(_basePath);
    zk.getChildren(path, false, new AsyncCallback.Children2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch(code) {
                case OK:
                    callback.onSuccess(children);
                    break;
                case NONODE:
                    callback.onSuccess(Collections.<String>emptyList());
                    break;
                default:
                    callback.onError(KeeperException.create(code));
                    break;
            }
        }
    }, null);
}
Also used : ZooKeeper(com.linkedin.d2.discovery.stores.zk.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) AsyncCallback(org.apache.zookeeper.AsyncCallback)

Example 4 with ZooKeeper

use of com.linkedin.d2.discovery.stores.zk.ZooKeeper in project rest.li by linkedin.

the class ZKFSDirectory method getClusterNames.

@Override
public void getClusterNames(final Callback<List<String>> callback) {
    final ZooKeeper zk = _connection.getZooKeeper();
    final String path = ZKFSUtil.clusterPath(_basePath);
    zk.getChildren(path, false, new AsyncCallback.Children2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            KeeperException.Code code = KeeperException.Code.get(rc);
            switch(code) {
                case OK:
                    callback.onSuccess(children);
                    break;
                case NONODE:
                    callback.onSuccess(Collections.<String>emptyList());
                    break;
                default:
                    callback.onError(KeeperException.create(code));
                    break;
            }
        }
    }, null);
}
Also used : ZooKeeper(com.linkedin.d2.discovery.stores.zk.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) AsyncCallback(org.apache.zookeeper.AsyncCallback)

Example 5 with ZooKeeper

use of com.linkedin.d2.discovery.stores.zk.ZooKeeper in project rest.li by linkedin.

the class ZKFSLoadBalancer method start.

@Override
public void start(final Callback<None> callback) {
    LOG.info("Starting ZKFSLoadBalancer");
    LOG.info("ZK connect string: {}", _connectString);
    LOG.info("ZK session timeout: {}ms", _sessionTimeout);
    LOG.info("ZK initial connect timeout: {}ms", _initialZKTimeout);
    if (_connectString == null || _connectString.isEmpty()) {
        callback.onError(new IllegalArgumentException("ZooKeeper connection string is null or empty"));
        return;
    }
    if (_zkFlagFile == null) {
        LOG.info("ZK flag file not specified");
    } else {
        LOG.info("ZK flag file: {}", _zkFlagFile.getAbsolutePath());
        LOG.info("ZK currently suppressed by flag file: {}", suppressZK());
    }
    _zkConnection = new ZKConnection(_connectString, _sessionTimeout, _shutdownAsynchronously, _isSymlinkAware);
    final TogglingLoadBalancer balancer = _loadBalancerFactory.createLoadBalancer(_zkConnection, _executor);
    // all other cases, we service requests from the old LoadBalancer until the new one is started
    if (_currentLoadBalancer == null) {
        _currentLoadBalancer = balancer;
    }
    Callback<None> wrapped = new Callback<None>() {

        @Override
        public void onSuccess(None none) {
            _currentLoadBalancer = balancer;
            callback.onSuccess(none);
        }

        @Override
        public void onError(Throwable e) {
            callback.onError(e);
        }
    };
    if (!_startupCallback.compareAndSet(null, wrapped)) {
        throw new IllegalStateException("Startup already in progress");
    }
    _executor.execute(new PropertyEventThread.PropertyEvent("startup") {

        @Override
        public void innerRun() {
            _zkConnection.addStateListener(new ZKListener(balancer));
            try {
                _zkConnection.start();
            } catch (Exception e) {
                LOG.error("Failed to start ZooKeeper (bad configuration?), enabling backup stores", e);
                Callback<None> startupCallback = _startupCallback.getAndSet(null);
                // TODO this should never be null
                balancer.enableBackup(startupCallback);
                return;
            }
            LOG.info("Started ZooKeeper");
            _executor.schedule(new Runnable() {

                @Override
                public void run() {
                    Callback<None> startupCallback = _startupCallback.getAndSet(null);
                    if (startupCallback != null) {
                        // Noone has enabled the stores yet either way
                        LOG.error("No response from ZooKeeper within {}ms, enabling backup stores", _initialZKTimeout);
                        balancer.enableBackup(startupCallback);
                    }
                }
            }, _initialZKTimeout, TimeUnit.MILLISECONDS);
        }
    });
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) TogglingLoadBalancer(com.linkedin.d2.balancer.util.TogglingLoadBalancer) KeeperException(org.apache.zookeeper.KeeperException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Callback(com.linkedin.common.callback.Callback) None(com.linkedin.common.util.None) PropertyEventThread(com.linkedin.d2.discovery.event.PropertyEventThread)

Aggregations

Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 JSONObject (org.json.simple.JSONObject)6 None (com.linkedin.common.util.None)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 D2Client (com.linkedin.d2.balancer.D2Client)4 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)4 ZooKeeper (com.linkedin.d2.discovery.stores.zk.ZooKeeper)4 List (java.util.List)4 ScheduledFuture (java.util.concurrent.ScheduledFuture)4 URI (java.net.URI)3 AsyncCallback (org.apache.zookeeper.AsyncCallback)3 KeeperException (org.apache.zookeeper.KeeperException)3 Stat (org.apache.zookeeper.data.Stat)3 ParseException (org.json.simple.parser.ParseException)3 Test (org.testng.annotations.Test)3 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)2