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

The following examples show how to use org.apache.curator.framework.recipes.barriers.DistributedBarrier. 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: BarrierTest.java    From BigData-In-Practice with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    for (int i = 0; i < clientNUms; i++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    CuratorFramework client = ZKUtils.getClient();
                    client.start();
                    barrier = new DistributedBarrier(client, barrierPath);
                    System.out.println(Thread.currentThread().getName() + " 号 barrier 设置");
                    barrier.setBarrier();
                    barrier.waitOnBarrier();
                    System.out.println("启动...");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    Thread.sleep(2000);
    barrier.removeBarrier();
}
 
Example #2
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 #3
Source File: HdfsSnapshotAction.java    From occurrence with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the START/SET or END/REMOVE on a barrier based on the action.
 * @param config configuration settings
 * @param action action to be performed
 * @param directory to snapshot
 * @param wfId workflow Id, it is used as the snapshot name
 */
private static void doInBarrier(Properties config, Action action, String directory, String wfId) {
  try(CuratorFramework curator = curator(config)) {
    curator.start();
    String lockPath = config.getProperty(LOCK_PATH) + config.getProperty(LOCK_NAME);
    DistributedBarrier barrier = new DistributedBarrier(curator, lockPath);
    if(Action.START == action) {
      LOG.info("Waiting for barrier {}", lockPath);
      barrier.waitOnBarrier();
      LOG.info("Setting barrier {}", lockPath);
      barrier.setBarrier();
      createHdfsSnapshot(directory, wfId);
      LOG.info("Removing barrier {}", lockPath);
      barrier.removeBarrier();
    } else if(Action.END == action) {
      LOG.info("Removing barrier {}", lockPath);
      barrier.removeBarrier();
      deleteHdfsSnapshot(directory, wfId);
    } else {
      LOG.error("No action performed");
    }
  } catch (Exception ex) {
    LOG.error("Error handling barrier {}", config);
  }
}
 
Example #4
Source File: DistributedDoubleBarrierExample.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);
		DistributedBarrier controlBarrier = new DistributedBarrier(client, PATH);
		controlBarrier.setBarrier();
		
		for (int i = 0; i < QTY; ++i) {
			final DistributedBarrier barrier = new DistributedBarrier(client, PATH);
			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 + " waits on Barrier");
					barrier.waitOnBarrier();
					System.out.println("Client #" + index + " begins");
					return null;
				}
			};
			service.submit(task);
		}
		
		Thread.sleep(10000);
		System.out.println("all Barrier instances should wait the condition");
		
		
		controlBarrier.removeBarrier();
		
		
		service.shutdown();
		service.awaitTermination(10, TimeUnit.MINUTES);

	}

}