use of org.apache.curator.framework.CuratorFramework in project nifi by apache.
the class CuratorNodeProtocolSender method getServiceAddress.
@Override
protected synchronized InetSocketAddress getServiceAddress() throws IOException {
if (coordinatorAddress != null) {
return coordinatorAddress;
}
final RetryPolicy retryPolicy = new RetryNTimes(0, 0);
final CuratorFramework curatorClient = CuratorFrameworkFactory.newClient(zkConfig.getConnectString(), zkConfig.getSessionTimeoutMillis(), zkConfig.getConnectionTimeoutMillis(), retryPolicy);
curatorClient.start();
try {
// Get coordinator address and add watcher to change who we are heartbeating to if the value changes.
final byte[] coordinatorAddressBytes = curatorClient.getData().usingWatcher(new Watcher() {
@Override
public void process(final WatchedEvent event) {
coordinatorAddress = null;
}
}).forPath(coordinatorPath);
if (coordinatorAddressBytes == null || coordinatorAddressBytes.length == 0) {
throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
}
final String address = new String(coordinatorAddressBytes, StandardCharsets.UTF_8);
final String[] splits = address.split(":");
if (splits.length != 2) {
final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but this is not in the expected format of <hostname>:<port>", address);
logger.error(message);
throw new ProtocolException(message);
}
logger.info("Determined that Cluster Coordinator is located at {}; will use this address for sending heartbeat messages", address);
final String hostname = splits[0];
final int port;
try {
port = Integer.parseInt(splits[1]);
if (port < 1 || port > 65535) {
throw new NumberFormatException("Port must be in the range of 1 - 65535 but got " + port);
}
} catch (final NumberFormatException nfe) {
final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but the port is not a valid port number", address);
logger.error(message);
throw new ProtocolException(message);
}
final InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(hostname, port);
coordinatorAddress = socketAddress;
return socketAddress;
} catch (final NoNodeException nne) {
logger.info("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
} catch (final NoClusterCoordinatorException ncce) {
throw ncce;
} catch (Exception e) {
throw new IOException("Unable to determine Cluster Coordinator from ZooKeeper", e);
} finally {
curatorClient.close();
}
}
use of org.apache.curator.framework.CuratorFramework in project nifi by apache.
the class CuratorLeaderElectionManager method createClient.
private CuratorFramework createClient() {
// Create a new client because we don't want to try indefinitely for this to occur.
final RetryPolicy retryPolicy = new RetryNTimes(1, 100);
final CuratorACLProviderFactory aclProviderFactory = new CuratorACLProviderFactory();
final CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkConfig.getConnectString()).sessionTimeoutMs(zkConfig.getSessionTimeoutMillis()).connectionTimeoutMs(zkConfig.getConnectionTimeoutMillis()).retryPolicy(retryPolicy).aclProvider(aclProviderFactory.create(zkConfig)).defaultData(new byte[0]).build();
client.start();
return client;
}
use of org.apache.curator.framework.CuratorFramework in project nifi by apache.
the class CuratorLeaderElectionManager method determineLeaderExternal.
/**
* Use a new Curator client to determine which node is the elected leader for the given role.
*
* @param roleName the name of the role
* @return the id of the elected leader, or <code>null</code> if no leader has been selected or if unable to determine
* the leader from ZooKeeper
*/
private String determineLeaderExternal(final String roleName) {
final CuratorFramework client = createClient();
try {
final LeaderSelectorListener electionListener = new LeaderSelectorListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
}
};
final String electionPath = getElectionPath(roleName);
// Note that we intentionally do not auto-requeue here, and we do not start the selector. We do not
// want to join the leader election. We simply want to observe.
final LeaderSelector selector = new LeaderSelector(client, electionPath, electionListener);
try {
final Participant leader = selector.getLeader();
return leader == null ? null : leader.getId();
} catch (final KeeperException.NoNodeException nne) {
// If there is no ZNode, then there is no elected leader.
return null;
} catch (final Exception e) {
logger.warn("Unable to determine the Elected Leader for role '{}' due to {}; assuming no leader has been elected", roleName, e.toString());
if (logger.isDebugEnabled()) {
logger.warn("", e);
}
return null;
}
} finally {
client.close();
}
}
use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.
the class ZkConnection method build.
/**
* Builds zk connector. Note that this method does not initiate a connection. {@link ZkConnection#connect()} must be called to connect
* to cluster.
* <p/>
* This separation is provided so that callbacks can be setup separately prior to connection to cluster.
*/
public void build() {
try {
_zkConnection = CuratorFrameworkFactory.builder().connectString(_connectString).connectionTimeoutMs(DEFAULT_CONN_TIMEOUT).canBeReadOnly(true).sessionTimeoutMs(_timeoutMs).retryPolicy(new RetryUntilElapsed(_timeoutMs, RETRY_INTERVAL_MS)).build();
_zkConnection.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
_logger.warn("Unknown exception in curator stack", e);
}
});
_zkConnection.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
_logger.info("Current connection state {}", newState);
}
});
if (FileUtils.exists(siteIdFile)) {
siteId = new String(FileUtils.readDataFromFile(siteIdFile));
siteId = siteId.trim();
_logger.info("Current site id is {}", siteId);
}
} catch (Exception e) {
throw CoordinatorException.fatals.failedToBuildZKConnector(e);
}
}
use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.
the class DistributedDoubleBarrierTest method testMultiClient.
/**
* Test case port from Apache Curator
*/
@Test
public void testMultiClient() throws Exception {
final Timing timing = new Timing();
final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger max = new AtomicInteger(0);
List<Future<Void>> futures = Lists.newArrayList();
ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < QTY; ++i) {
Future<Void> future = service.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
ZkConnection zkConnection = createConnection(60 * 1000);
CuratorFramework client = zkConnection.curator();
try {
zkConnection.connect();
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testMultiClient", QTY);
log.info("Entering with timeout {}", timing.seconds());
Assert.assertTrue("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
log.info("Entered");
synchronized (DistributedDoubleBarrierTest.this) {
int thisCount = count.incrementAndGet();
if (thisCount > max.get()) {
max.set(thisCount);
}
}
postEnterLatch.countDown();
Assert.assertTrue("postEnterLatch", timing.awaitLatch(postEnterLatch));
Assert.assertEquals("entered count", count.get(), QTY);
log.info("Leaving timeout {}", timing.seconds());
Assert.assertTrue("Return value of leave()", barrier.leave(timing.seconds(), TimeUnit.SECONDS));
log.info("Left");
count.decrementAndGet();
postLeaveLatch.countDown();
Assert.assertTrue("postLeaveLatch", timing.awaitLatch(postLeaveLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
futures.add(future);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals(count.get(), 0);
Assert.assertEquals(max.get(), QTY);
}
Aggregations