Java Code Examples for org.apache.curator.framework.imps.CuratorFrameworkState#STARTED

The following examples show how to use org.apache.curator.framework.imps.CuratorFrameworkState#STARTED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized boolean start() {
    if (zkClient == null) {
        LOGGER.warn("Start zookeeper registry must be do init first!");
        return false;
    }
    if (zkClient.getState() == CuratorFrameworkState.STARTED) {
        return true;
    }
    try {
        zkClient.start();
    } catch (Throwable t) {
        throw new SofaRpcRuntimeException("Failed to start zookeeper zkClient", t);
    }
    return zkClient.getState() == CuratorFrameworkState.STARTED;
}
 
Example 2
Source File: StandbyNode.java    From niubi-job with Apache License 2.0 6 votes vote down vote up
@Override
public void relinquishLeadership() {
    try {
        if (jobCache != null) {
            jobCache.close();
        }
        LoggerHelper.info("job cache has been closed.");
    } catch (Throwable e) {
        LoggerHelper.warn("job cache close failed.", e);
    }
    LoggerHelper.info("begin stop scheduler manager.");
    schedulerManager.shutdown();
    if (client.getState() == CuratorFrameworkState.STARTED) {
        StandbyNodeData.Data data = new StandbyNodeData.Data(getNodeIp());
        standbyApiFactory.nodeApi().updateNode(nodePath, data);
        LoggerHelper.info(getNodeIp() + " has been shutdown. [" + data + "]");
    }
    LoggerHelper.info("clear node successfully.");
}
 
Example 3
Source File: LeaderInitiator.java    From spring-cloud-cluster with Apache License 2.0 6 votes vote down vote up
/**
 * Start the registration of the {@link #candidate} for leader election.
 */
@Override
public synchronized void start() {
	if (!this.running) {
		if (this.client.getState() != CuratorFrameworkState.STARTED) {
			// we want to do curator start here because it needs to
			// be started before leader selector and it gets a little
			// complicated to control ordering via beans so that
			// curator is fully started.
			this.client.start();
		}
		this.leaderSelector = new LeaderSelector(this.client, buildLeaderPath(), new LeaderListener());
		this.leaderSelector.setId(this.candidate.getId());
		this.leaderSelector.autoRequeue();
		this.leaderSelector.start();

		this.running = true;
	}
}
 
Example 4
Source File: ZookeeperHealthIndicator.java    From spring-cloud-zookeeper with Apache License 2.0 6 votes vote down vote up
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
	try {
		CuratorFrameworkState state = this.curator.getState();
		if (state != CuratorFrameworkState.STARTED) {
			builder.down().withDetail("error", "Client not started");
		}
		else if (this.curator.checkExists().forPath("/") == null) {
			builder.down().withDetail("error", "Root for namespace does not exist");
		}
		else {
			builder.up();
		}
		builder.withDetail("connectionString",
				this.curator.getZookeeperClient().getCurrentConnectionString())
				.withDetail("state", state);
	}
	catch (Exception e) {
		builder.down(e);
	}
}
 
Example 5
Source File: ZookeeperDistributedLock.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean lock(String lockPath) {
    logger.debug("{} trying to lock {}", client, lockPath);

    // curator closed in some case(like Expired),restart it
    if (curator.getState() != CuratorFrameworkState.STARTED) {
        curator = ZKUtil.getZookeeperClient(KylinConfig.getInstanceFromEnv());
    }

    lockInternal(lockPath);

    String lockOwner;
    try {
        lockOwner = peekLock(lockPath);
        if (client.equals(lockOwner)) {
            logger.info("{} acquired lock at {}", client, lockPath);
            return true;
        } else {
            logger.debug("{} failed to acquire lock at {}, which is held by {}", client, lockPath, lockOwner);
            return false;
        }
    } catch (ZkPeekLockInterruptException zpie) {
        logger.error("{} peek owner of lock interrupt while acquire lock at {}, check to release lock", client,
                lockPath);
        lockOwner = peekLock(lockPath);

        try {
            unlockInternal(lockOwner, lockPath);
        } catch (Exception anyEx) {
            // it's safe to swallow any exception here because here already been interrupted
            logger.warn("Exception caught to release lock when lock operation has been interrupted.", anyEx);
        }
        throw zpie;
    }
}
 
Example 6
Source File: DefaultZooKeeperClient.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
  if (client.getState() != CuratorFrameworkState.STARTED) {
    client.start();

    if (clusterId != null) {
      client.getConnectionStateListenable().addListener(connectionStateListener);
      checkClusterIdExists(clusterId, "start");
    }
  }
}
 
Example 7
Source File: TcpDiscoveryZookeeperIpFinder.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public void unregisterAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException {

    // if curator is not STARTED, we have nothing to unregister, because we are using ephemeral nodes,
    // which means that our addresses will only be registered in ZK as long as our connection is alive
    if (curator.getState() != CuratorFrameworkState.STARTED)
        return;

    if (log.isInfoEnabled())
        log.info("Unregistering addresses with ZooKeeper IP Finder: " + addrs);

    for (InetSocketAddress addr : addrs) {
        ServiceInstance<IgniteInstanceDetails> si = ourInstances.get(addr);
        if (si == null) {
            log.warning("Asked to unregister address from ZooKeeper IP Finder, but no match was found in local " +
                "instance map for: " + addrs);
            continue;
        }

        try {
            discovery.unregisterService(si);
        }
        catch (Exception e) {
            log.warning("Error while unregistering an address from ZooKeeper IP Finder: " + addr, e);
        }
    }
}
 
Example 8
Source File: ZookeeperClient.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭连接
 */
public void close() {
    logger.debug("--------zoo close");
    if (curator.getState() == CuratorFrameworkState.STARTED) {
        curator.close();
    }
}
 
Example 9
Source File: ZookeeperWorkerRegister.java    From idworker with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭注册
 */
@Override
public synchronized void logout() {
    CuratorFramework client = (CuratorFramework) regCenter.getRawClient();
    if (client != null && client.getState() == CuratorFrameworkState.STARTED) {
        // 移除注册节点(最大程度的自动释放资源)
        regCenter.remove(nodePath.getWorkerIdPath());
        // 关闭连接
        regCenter.close();
    }
}
 
Example 10
Source File: CuratorManager.java    From snowizard-discovery with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor
 * 
 * @param framework
 *            {@link CuratorFramework}
 */
public CuratorManager(final CuratorFramework framework) {
    this.framework = checkNotNull(framework);
    // start framework directly to allow other bundles to interact with zookeeper
    // during their run() method.
    if (framework.getState() != CuratorFrameworkState.STARTED) {
        framework.start();
    }
}
 
Example 11
Source File: ZookeeperHealthIndicator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void check(HealthIndicatorCallback healthCallback) {
    CuratorFrameworkState state = curatorService.getCurator().getState();
    if (state == CuratorFrameworkState.STARTED) {
        healthCallback.inform(Health.healthy().build());
    } else {
        healthCallback.inform(Health.unhealthy().withDetail("state", state).build());
    }
}
 
Example 12
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy() {
    closePathChildrenCache(INTERFACE_CONFIG_CACHE);
    closePathChildrenCache(INTERFACE_OVERRIDE_CACHE);
    if (zkClient != null && zkClient.getState() == CuratorFrameworkState.STARTED) {
        zkClient.close();
    }
    providerUrls.clear();
    consumerUrls.clear();
}
 
Example 13
Source File: ZookeeperManager.java    From chronus with Apache License 2.0 4 votes vote down vote up
public boolean checkZookeeperState() throws Exception {
    return curator != null && curator.getState() == CuratorFrameworkState.STARTED && curator.getZookeeperClient().isConnected();
}
 
Example 14
Source File: ZookeeperTemplate.java    From easyooo-framework with Apache License 2.0 4 votes vote down vote up
public boolean isStarted(){
	return getState() == CuratorFrameworkState.STARTED;
}
 
Example 15
Source File: HAContext.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
public void startCuratorFramework() {
  if (curatorFramework.getState() != CuratorFrameworkState.STARTED) {
    curatorFramework.start();
  }
}
 
Example 16
Source File: ZookeeperRegistry.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
private CuratorFramework getAndCheckZkClient() {
    if (zkClient == null || zkClient.getState() != CuratorFrameworkState.STARTED) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_ZOOKEEPER_CLIENT_UNAVAILABLE));
    }
    return zkClient;
}
 
Example 17
Source File: ManagedCuratorFramework.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void stop() throws Exception {
    if (_curator.getState() == CuratorFrameworkState.STARTED) {
        Closeables.close(_curator, true);
    }
}
 
Example 18
Source File: ZookeeperInvoker.java    From Thunder with Apache License 2.0 4 votes vote down vote up
public boolean isStarted() {
    return client.getState() == CuratorFrameworkState.STARTED;
}
 
Example 19
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 4 votes vote down vote up
private CuratorFramework getAndCheckZkClient() {
    if (zkClient == null || zkClient.getState() != CuratorFrameworkState.STARTED) {
        throw new SofaRpcRuntimeException("Zookeeper client is not available");
    }
    return zkClient;
}
 
Example 20
Source File: Service.java    From ranger with Apache License 2.0 4 votes vote down vote up
public boolean isRunning() {
    return curatorFramework != null
            && (curatorFramework.getState() == CuratorFrameworkState.STARTED);
}