use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.
the class DistributedDoubleBarrierTest method testEnterTimeout.
/**
* Test return value of enter() in case of timeout
*
* If not all members enter the barrier as expected, it should return false. Subsequent enter() should return false until
* it meets the barrier criteria
*/
@Test
public void testEnterTimeout() throws Exception {
final Timing timing = new Timing(2, TimeUnit.SECONDS);
final AtomicInteger count = new AtomicInteger(0);
final ExecutorService service = Executors.newCachedThreadPool();
final List<Future<Void>> futures = Lists.newArrayList();
for (int i = 1; i < QTY - 1; ++i) {
Future<Void> worker = 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/testEnterTimeout", QTY);
log.info("Entering with timeout {} seconds", timing.seconds());
Assert.assertFalse("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
count.incrementAndGet();
log.info("Entering again with timeout {} seconds", timing.seconds());
Assert.assertFalse("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
count.decrementAndGet();
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
futures.add(worker);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals("enter/leave count", count.get(), 0);
futures.clear();
for (int i = 0; i < QTY; ++i) {
Future<Void> worker = 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/testEnterTimeout", QTY);
log.info("Entering with timeout {} seconds", timing.seconds());
Assert.assertTrue("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
count.incrementAndGet();
log.info("Leaving with timeout {} seconds", timing.seconds());
Assert.assertTrue("Return value of enter()", barrier.leave(timing.seconds(), TimeUnit.SECONDS));
count.decrementAndGet();
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
futures.add(worker);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals("enter/leave count", count.get(), 0);
}
use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.
the class DistributedDoubleBarrierTest method testBasic.
/**
* Test case port from Apache Curator
*/
@Test
public void testBasic() throws Exception {
final Timing timing = new Timing();
final List<Closeable> closeables = Lists.newArrayList();
ZkConnection zkConnection = createConnection(10 * 1000);
CuratorFramework client = zkConnection.curator();
try {
closeables.add(client);
client.start();
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 {
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testBasic", QTY);
Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS));
synchronized (DistributedDoubleBarrierTest.this) {
int thisCount = count.incrementAndGet();
if (thisCount > max.get()) {
max.set(thisCount);
}
}
postEnterLatch.countDown();
Assert.assertTrue(timing.awaitLatch(postEnterLatch));
Assert.assertEquals(count.get(), QTY);
Assert.assertTrue(barrier.leave(10, TimeUnit.SECONDS));
count.decrementAndGet();
postLeaveLatch.countDown();
Assert.assertTrue(timing.awaitLatch(postLeaveLatch));
return null;
}
});
futures.add(future);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals(count.get(), 0);
Assert.assertEquals(max.get(), QTY);
} finally {
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.
the class CoordinatorClientImpl method start.
@Override
public void start() throws IOException {
if (_zkConnection.curator().isStarted()) {
return;
}
_zkConnection.curator().getConnectionStateListenable().addListener(new org.apache.curator.framework.state.ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, final ConnectionState newState) {
log.info("Entering stateChanged method : {}", newState);
_connectionStateWorker.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Iterator<ConnectionStateListener> it = _listener.iterator();
while (it.hasNext()) {
ConnectionStateListener listener = it.next();
try {
switch(newState) {
case RECONNECTED:
case CONNECTED:
{
listener.connectionStateChanged(ConnectionStateListener.State.CONNECTED);
break;
}
case LOST:
case SUSPENDED:
{
listener.connectionStateChanged(ConnectionStateListener.State.DISCONNECTED);
break;
}
}
} catch (Exception e) {
log.warn("Connection listener threw", e);
}
}
return null;
}
});
}
});
_zkConnection.connect();
// writing local node to zk
initInetAddressEntry();
try {
checkAndCreateSiteSpecificSection();
String servicePath = getServicePath();
EnsurePath path = new EnsurePath(servicePath);
path.ensure(_zkConnection.curator().getZookeeperClient());
} catch (Exception e) {
// desired
if ((_connectionStateWorker != null) && (!_connectionStateWorker.isShutdown())) {
_connectionStateWorker.shutdownNow();
}
if (nodeChangeWorker != null && !nodeChangeWorker.isShutdown()) {
nodeChangeWorker.shutdownNow();
}
throw CoordinatorException.fatals.errorConnectingCoordinatorService(e);
}
}
use of org.apache.curator.framework.CuratorFramework in project metron by apache.
the class SensorParserConfigServiceImplTest method setUp.
@Before
public void setUp() throws Exception {
objectMapper = mock(ObjectMapper.class);
curatorFramework = mock(CuratorFramework.class);
Environment environment = mock(Environment.class);
Authentication authentication = mock(Authentication.class);
when(authentication.getName()).thenReturn(user);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(environment.getProperty(GROK_TEMP_PATH_SPRING_PROPERTY)).thenReturn("./target");
grokService = new GrokServiceImpl(environment, mock(Grok.class), new HdfsServiceImpl(new Configuration()));
cache = mock(ConfigurationsCache.class);
sensorParserConfigService = new SensorParserConfigServiceImpl(objectMapper, curatorFramework, grokService, cache);
}
use of org.apache.curator.framework.CuratorFramework in project metron by apache.
the class ConfigurationsUtils method uploadConfigsToZookeeper.
public static void uploadConfigsToZookeeper(String globalConfigPath, String parsersConfigPath, String enrichmentsConfigPath, String indexingConfigPath, String profilerConfigPath, String zookeeperUrl) throws Exception {
try (CuratorFramework client = getClient(zookeeperUrl)) {
client.start();
uploadConfigsToZookeeper(globalConfigPath, parsersConfigPath, enrichmentsConfigPath, indexingConfigPath, profilerConfigPath, client);
}
}
Aggregations