use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class GroupTest method testGroupClose.
// Tests that if close() is executed right after start(), there are no left over entries.
// (see https://github.com/jboss-fuse/fuse/issues/133)
@Test
public void testGroupClose() throws Exception {
int port = findFreePort();
NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + port).connectionTimeoutMs(6000).sessionTimeoutMs(6000).retryPolicy(new RetryNTimes(10, 100));
CuratorFramework curator = builder.build();
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
String groupNode = "/singletons/test" + System.currentTimeMillis();
curator.create().creatingParentsIfNeeded().forPath(groupNode);
for (int i = 0; i < 100; i++) {
ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
group.add(listener);
group.update(new NodeState("foo"));
group.start();
group.close();
List<String> entries = curator.getChildren().forPath(groupNode);
assertTrue(entries.isEmpty() || group.isUnstable());
if (group.isUnstable()) {
// let's wait for session timeout
curator.close();
curator = builder.build();
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
}
}
curator.close();
cnxnFactory.shutdown();
cnxnFactory.join();
}
use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class GroupTest method testAddFieldIgnoredOnParse.
@Test
public void testAddFieldIgnoredOnParse() throws Exception {
int port = findFreePort();
NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
String groupNode = "/singletons/test" + System.currentTimeMillis();
curator.create().creatingParentsIfNeeded().forPath(groupNode);
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
final ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
group.add(listener);
group.start();
GroupCondition groupCondition = new GroupCondition();
group.add(groupCondition);
group.update(new NodeState("foo"));
assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));
ChildData currentData = group.getCurrentData().get(0);
final int version = currentData.getStat().getVersion();
NodeState lastState = group.getLastState();
String json = lastState.toString();
System.err.println("JSON:" + json);
String newValWithNewField = json.substring(0, json.lastIndexOf('}')) + ",\"Rubbish\":\"Rubbish\"}";
curator.getZookeeperClient().getZooKeeper().setData(group.getId(), newValWithNewField.getBytes(), version);
assertTrue(group.isMaster());
int attempts = 0;
while (attempts++ < 5 && version == group.getCurrentData().get(0).getStat().getVersion()) {
TimeUnit.SECONDS.sleep(1);
}
assertNotEquals("We see the updated version", version, group.getCurrentData().get(0).getStat().getVersion());
System.err.println("CurrentData:" + group.getCurrentData());
group.close();
curator.close();
cnxnFactory.shutdown();
cnxnFactory.join();
}
use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class GroupTest method testOrder.
@Test
public void testOrder() throws Exception {
int port = findFreePort();
CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
curator.start();
final String path = "/singletons/test/Order" + System.currentTimeMillis();
ArrayList<ZooKeeperGroup> members = new ArrayList<ZooKeeperGroup>();
for (int i = 0; i < 4; i++) {
ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, path, NodeState.class);
group.add(listener);
members.add(group);
}
for (ZooKeeperGroup group : members) {
assertFalse(group.isConnected());
assertFalse(group.isMaster());
}
NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
// first to start should be master if members are ordered...
int i = 0;
for (ZooKeeperGroup group : members) {
group.start();
group.update(new NodeState("foo" + i));
i++;
// wait for registration
while (group.getId() == null) {
TimeUnit.MILLISECONDS.sleep(100);
}
}
boolean firsStartedIsMaster = members.get(0).isMaster();
for (ZooKeeperGroup group : members) {
group.close();
}
curator.close();
cnxnFactory.shutdown();
cnxnFactory.join();
assertTrue("first started is master", firsStartedIsMaster);
}
use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class ServiceFactoryTest method testCallbackOnDisconnectCanClose.
@Test
public void testCallbackOnDisconnectCanClose() throws Exception {
curator.close();
LOG.info("....");
SocketProxy socketProxy = new SocketProxy(new URI("tcp://localhost:" + zkPort));
final CuratorFramework proxyCurator = CuratorFrameworkFactory.builder().connectString("localhost:" + socketProxy.getUrl().getPort()).sessionTimeoutMs(5000).connectionTimeoutMs(3000).retryPolicy(new RetryNTimes(10, 1000)).build();
proxyCurator.start();
proxyCurator.getZookeeperClient().blockUntilConnectedOrTimedOut();
LOG.info("curator is go: " + proxyCurator);
final String path = "/singletons/test/threads" + System.currentTimeMillis() + "**";
final ArrayList<Runnable> members = new ArrayList<Runnable>();
final int nThreads = 1;
final CountDownLatch gotDisconnectEvent = new CountDownLatch(1);
class GroupRunnable implements Runnable, GroupListener<NodeState> {
final int id;
private final BlockingQueue<Integer> jobQueue = new LinkedBlockingDeque<Integer>();
ZooKeeperGroup<NodeState> group;
NodeState nodeState;
public GroupRunnable(int id) {
this.id = id;
members.add(this);
nodeState = new NodeState("foo" + id);
}
@Override
public void run() {
group = new ZooKeeperGroup<NodeState>(proxyCurator, path, NodeState.class);
group.add(this);
LOG.info("run: Added: " + this);
try {
while (true) {
switch(jobQueue.take()) {
case 0:
LOG.info("run: close: " + this);
try {
group.close();
} catch (IOException ignored) {
}
return;
case 1:
LOG.info("run: start: " + this);
group.start();
group.update(nodeState);
break;
case 2:
LOG.info("run: update: " + this);
nodeState.setId(nodeState.getId() + id);
group.update(nodeState);
break;
}
}
} catch (InterruptedException exit) {
}
}
@Override
public void groupEvent(Group<NodeState> group, GroupEvent event) {
LOG.info("Got: event: " + event);
if (event.equals(GroupEvent.DISCONNECTED)) {
gotDisconnectEvent.countDown();
}
}
}
;
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
for (int i = 0; i < nThreads; i++) {
executorService.execute(new GroupRunnable(i));
}
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
groupRunnable.jobQueue.offer(1);
// wait for registration
while (groupRunnable.group == null || groupRunnable.group.getId() == null) {
TimeUnit.MILLISECONDS.sleep(100);
}
}
boolean firsStartedIsMaster = ((GroupRunnable) members.get(0)).group.isMaster();
assertTrue("first started is master", firsStartedIsMaster);
LOG.info("got master...");
// lets see how long they take to notice a no responses to heart beats
socketProxy.pause();
// splash in an update
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
groupRunnable.jobQueue.offer(2);
}
boolean hasMaster = true;
while (hasMaster) {
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
hasMaster &= groupRunnable.group.isMaster();
}
if (hasMaster) {
LOG.info("Waiting for no master state on proxy pause");
TimeUnit.SECONDS.sleep(1);
}
}
try {
boolean gotDisconnect = gotDisconnectEvent.await(15, TimeUnit.SECONDS);
assertTrue("got disconnect event", gotDisconnect);
LOG.info("do close");
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
groupRunnable.jobQueue.offer(0);
}
executorService.shutdown();
// at a min when the session has expired
boolean allThreadComplete = executorService.awaitTermination(6, TimeUnit.SECONDS);
assertTrue("all threads complete", allThreadComplete);
} finally {
proxyCurator.close();
socketProxy.close();
}
}
use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class ServiceFactoryTest method testThreadsOnTimeout.
@Test
public void testThreadsOnTimeout() throws Exception {
curator.close();
SocketProxy socketProxy = new SocketProxy(new URI("tcp://localhost:" + zkPort));
final CuratorFramework proxyCurator = CuratorFrameworkFactory.builder().connectString("localhost:" + socketProxy.getUrl().getPort()).sessionTimeoutMs(5000).connectionTimeoutMs(3000).retryPolicy(new RetryNTimes(10, 1000)).build();
proxyCurator.start();
proxyCurator.getZookeeperClient().blockUntilConnectedOrTimedOut();
LOG.info("curator is go: " + proxyCurator);
final String path = "/singletons/test/threads" + System.currentTimeMillis() + "**";
final ArrayList<Runnable> members = new ArrayList<Runnable>();
final int nThreads = 1;
class GroupRunnable implements Runnable, GroupListener<NodeState> {
final int id;
private final BlockingQueue<Integer> jobQueue = new LinkedBlockingDeque<Integer>();
ZooKeeperGroup<NodeState> group;
NodeState nodeState;
public GroupRunnable(int id) {
this.id = id;
members.add(this);
nodeState = new NodeState("foo" + id);
}
@Override
public void run() {
group = new ZooKeeperGroup<NodeState>(proxyCurator, path, NodeState.class);
group.add(this);
LOG.info("run: Added: " + this);
try {
while (true) {
switch(jobQueue.take()) {
case 0:
LOG.info("run: close: " + this);
try {
group.close();
} catch (IOException ignored) {
}
return;
case 1:
LOG.info("run: start: " + this);
group.start();
group.update(nodeState);
break;
case 2:
LOG.info("run: update: " + this);
nodeState.setId(nodeState.getId() + id);
group.update(nodeState);
break;
}
}
} catch (InterruptedException exit) {
}
}
@Override
public void groupEvent(Group<NodeState> group, GroupEvent event) {
}
}
;
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
for (int i = 0; i < nThreads; i++) {
executorService.execute(new GroupRunnable(i));
}
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
groupRunnable.jobQueue.offer(1);
// wait for registration
while (groupRunnable.group == null || groupRunnable.group.getId() == null) {
TimeUnit.MILLISECONDS.sleep(100);
}
}
boolean firsStartedIsMaster = ((GroupRunnable) members.get(0)).group.isMaster();
assertTrue("first started is master", firsStartedIsMaster);
LOG.info("got master...");
// lets see how long they take to notice a no responses to heart beats
socketProxy.pause();
// splash in an update
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
groupRunnable.jobQueue.offer(2);
}
boolean hasMaster = true;
while (hasMaster) {
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
hasMaster &= groupRunnable.group.isMaster();
}
if (hasMaster) {
LOG.info("Waiting for no master state on proxy pause");
TimeUnit.SECONDS.sleep(1);
}
}
for (Runnable r : members) {
GroupRunnable groupRunnable = (GroupRunnable) r;
groupRunnable.jobQueue.offer(0);
}
executorService.shutdown();
// at a min when the session has expired
boolean allThreadComplete = executorService.awaitTermination(6, TimeUnit.SECONDS);
proxyCurator.close();
socketProxy.close();
assertTrue("all threads complete", allThreadComplete);
}
Aggregations