0

I'm trying to get the same functionality as a barrier in interprocess communication, but now for distributed nodes.

My problem is that I have multiple distributed processes over nodes. They have some non-deterministic setup, and after the setup I want them to start running at the same time.

Is there a simple framework which allows me to do this in Scala?

4

1 回答 1

1

使用 Curator 框架,更具体地说,DistributedDoubleBarrier您可以在需要同步的每个节点上设置屏障。这背后的原理是您可以设置需要进入屏障的节点数量,然后才能继续。

假设您在 example-zk:2128 上有一个(普通)zookeeper 实例,您可以在所有节点上使用以下构造:

package test.barrier;

//import
import org.apache.curator.*;
import org.apache.curator.framework.recipes.barriers.*;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.CuratorFramework;

class App {
        public App()
        {
                String zookeeperConnectionString = "example-zk:2181";
                String barrierPath = "/unique-string";

                RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                client.start();

                DistributedDoubleBarrier distDBarrier = new DistributedDoubleBarrier(client, barrierPath, 3);

                try{
                        System.out.println("Waiting on barrier");
                        distDBarrier.enter();
                        System.out.println("Apparently everybody was aboard! Continuing ...");
                }
                catch(Exception e)
                {
                        System.out.println("O no!: " + e.getMessage());
                }


        }

        public static void main (String[] args){
                new App();
        }   
}

请注意,屏障路径已“使用”,并且对于集群的每个运行实例,您可能需要创建一些唯一的路径字符串。

于 2015-08-17T12:35:50.250 回答