org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier Java Examples

The following examples show how to use org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier. 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: CuratorDistributedBarrier.java    From yuzhouwan with Apache License 2.0 6 votes vote down vote up
private void init() {
        CuratorFramework curatorFramework = CuratorFrameworkFactory
                .builder()
                .connectString("localhost:2181")
                .connectionTimeoutMs(3000)
                .sessionTimeoutMs(5000)
                .retryPolicy(new RetryNTimes(3, 2000))
                .namespace("distBarrier")
                .build();
        curatorFramework.start();
        distributedBarrier = new DistributedBarrier(curatorFramework, "/barrier");

//        try {
//            Stat stat = curatorFramework.checkExists().forPath("/double");
//            if (stat != null)
//                curatorFramework.delete().deletingChildrenIfNeeded().forPath("/double");
//            else
//                curatorFramework.create().creatingParentsIfNeeded()
//                      .withMode(CreateMode.PERSISTENT).forPath("/double");
//        } catch (Exception e) {
//            throw new RuntimeException("Cannot create path '/double' !!", e);
//        }
        distributedDoubleBarrier = new DistributedDoubleBarrier(curatorFramework, "/double", 3);
    }
 
Example #2
Source File: ClusterBarrierManagerTest.java    From Decision with Apache License 2.0 6 votes vote down vote up
@Test
public void testManageAckBarrierShouldBeOk() throws Exception {

    Integer ackTimeout = 500;
    String barrierPath = "/stratio/decision/barrier";
    Integer nodesExpected = 2;

    ClusterSyncManager clusterSyncManager = mock(ClusterSyncManager.class);
    when(clusterSyncManager.getClient()).thenReturn(curatorFramework);

    final ClusterBarrierManager clusterBarrierManager = PowerMockito.spy(new ClusterBarrierManager
            (clusterSyncManager, ackTimeout));

    barrier = mock(DistributedDoubleBarrier.class);
    when(barrier.enter(anyLong(), any(TimeUnit.class))).thenReturn(true);

    PowerMockito.doReturn(barrier).when(clusterBarrierManager).getDistributedDoubleBarrier(anyString(), anyInt());
    assertEquals(true, clusterBarrierManager.manageAckBarrier(barrierPath, nodesExpected));

}
 
Example #3
Source File: ClusterBarrierManagerTest.java    From Decision with Apache License 2.0 6 votes vote down vote up
@Test
public void testManageAckBarrierShouldBeKO() throws Exception {

    Integer ackTimeout = 500;
    String barrierPath = "/stratio/decision/barrier";
    Integer nodesExpected = 2;

    ClusterSyncManager clusterSyncManager = mock(ClusterSyncManager.class);
    when(clusterSyncManager.getClient()).thenReturn(curatorFramework);

    final ClusterBarrierManager clusterBarrierManager = PowerMockito.spy(new ClusterBarrierManager
            (clusterSyncManager, ackTimeout));

    barrier = mock(DistributedDoubleBarrier.class);
    when(barrier.enter(anyLong(), any(TimeUnit.class))).thenReturn(false);

    PowerMockito.doReturn(barrier).when(clusterBarrierManager).getDistributedDoubleBarrier(anyString(), anyInt());
    assertEquals(false, clusterBarrierManager.manageAckBarrier(barrierPath, nodesExpected));

}
 
Example #4
Source File: DoubleDistributedBarrierTest.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    CuratorFramework client = ZKUtils.getClient();
    client.start();
    ExecutorService service = Executors.newFixedThreadPool(clientNums);
    for (int i = 0; i < (clientNums + 2); ++i) {
        final DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, barrierPath, clientNums);
        final int index = i;
        Callable<Void> task = new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                Thread.sleep((long) (3 * Math.random()));
                System.out.println("Client #" + index + " 等待");
                if (false == barrier.enter(5, TimeUnit.SECONDS)) {
                    System.out.println("Client #" + index + " 等待超时!");
                    return null;
                }
                System.out.println("Client #" + index + " 进入");
                Thread.sleep((long) (3000 * Math.random()));
                barrier.leave();
                System.out.println("Client #" + index + " 结束");
                return null;
            }
        };
        service.submit(task);
    }
    service.shutdown();
    service.awaitTermination(10, TimeUnit.MINUTES);
    client.close();
    System.out.println("OK!");

}
 
Example #5
Source File: DistributedBarrierExample.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	try (TestingServer server = new TestingServer()) {
		CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
		client.start();

		ExecutorService service = Executors.newFixedThreadPool(QTY);
		for (int i = 0; i < QTY; ++i) {
			final DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, PATH, QTY);
			final int index = i;
			Callable<Void> task = new Callable<Void>() {
				@Override
				public Void call() throws Exception {
					
					Thread.sleep((long) (3 * Math.random()));
					System.out.println("Client #" + index + " enters");
					barrier.enter();
					System.out.println("Client #" + index + " begins");
					Thread.sleep((long) (3000 * Math.random()));
					barrier.leave();
					System.out.println("Client #" + index + " left");
					return null;
				}
			};
			service.submit(task);
		}
		
		
		service.shutdown();
		service.awaitTermination(10, TimeUnit.MINUTES);

	}

}
 
Example #6
Source File: ClusterBarrierManager.java    From Decision with Apache License 2.0 2 votes vote down vote up
public Boolean manageAckBarrier(String barrierPath, Integer nodesExpected) throws Exception {

        DistributedDoubleBarrier barrier =  getDistributedDoubleBarrier(barrierPath, nodesExpected);
        return barrier.enter(ackTimeout, TimeUnit.MILLISECONDS);

    }
 
Example #7
Source File: ClusterBarrierManager.java    From Decision with Apache License 2.0 2 votes vote down vote up
public DistributedDoubleBarrier getDistributedDoubleBarrier(String barrierPath, Integer nodesExpected) {

        return new DistributedDoubleBarrier(clusterSyncManagerInstance.getClient(), barrierPath,
                nodesExpected);

    }