Search in sources :

Example 91 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class ShardingIT method test_P_PersistShardingContentIfNecessary.

/**
 * NamespaceShardingService is not necessary to persist the sharding result content that is not changed<br/>
 * https://github.com/vipshop/Saturn/issues/88
 */
@Test
public void test_P_PersistShardingContentIfNecessary() throws Exception {
    // 启动1个executor
    Main executor1 = startOneNewExecutorList();
    Thread.sleep(1000);
    // 启动第一个作业
    Thread.sleep(1000);
    String jobName1 = "test_P_PersistShardingContentIfNecessary";
    final JobConfiguration jobConfiguration1 = new JobConfiguration(jobName1);
    jobConfiguration1.setCron("* * 1 * * ?");
    jobConfiguration1.setJobType(JobType.JAVA_JOB.toString());
    jobConfiguration1.setJobClass(SimpleJavaJob.class.getCanonicalName());
    jobConfiguration1.setShardingTotalCount(1);
    jobConfiguration1.setShardingItemParameters("0=0");
    jobConfiguration1.setPreferList("abc");
    jobConfiguration1.setUseDispreferList(false);
    addJob(jobConfiguration1);
    Thread.sleep(1000);
    enableJob(jobName1);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean docheck() {
            return isNeedSharding(jobConfiguration1);
        }
    }, 10);
    runAtOnce(jobName1);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean docheck() {
            return !isNeedSharding(jobConfiguration1);
        }
    }, 10);
    long mtime = ((CuratorFramework) regCenter.getRawClient()).checkExists().forPath(SaturnExecutorsNode.getShardingContentElementNodePath("0")).getMtime();
    // 禁用作业
    Thread.sleep(1000);
    disableJob(jobName1);
    Thread.sleep(1000);
    waitForFinish(new FinishCheck() {

        @Override
        public boolean docheck() {
            return !isNeedSharding(jobConfiguration1);
        }
    }, 10);
    long mtime2 = ((CuratorFramework) regCenter.getRawClient()).checkExists().forPath(SaturnExecutorsNode.getShardingContentElementNodePath("0")).getMtime();
    assertThat(mtime).isEqualTo(mtime2);
    stopExecutorList();
    forceRemoveJob(jobName1);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) SimpleJavaJob(com.vip.saturn.it.job.SimpleJavaJob) Main(com.vip.saturn.job.executor.Main) JobConfiguration(com.vip.saturn.job.internal.config.JobConfiguration) Test(org.junit.Test)

Example 92 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class NamespaceShardingServiceTest method leadershipElectionTest.

@Test
public void leadershipElectionTest() throws Exception {
    String namespace = "MyNamespace";
    CuratorFramework curatorFramework = nestedZkUtils.createClient(namespace);
    // 启动第一个
    String ip1 = "127.0.0.1";
    CuratorFramework curatorFramework1 = nestedZkUtils.createClient(namespace);
    NamespaceShardingManager namespaceShardingManager1 = new NamespaceShardingManager(curatorFramework1, namespace, ip1, null, null);
    namespaceShardingManager1.start();
    Thread.sleep(1000);
    // 验证leadership
    assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip1);
    // 启动第二个
    String ip2 = "127.0.0.2";
    CuratorFramework curatorFramework2 = nestedZkUtils.createClient(namespace);
    NamespaceShardingManager namespaceShardingManager2 = new NamespaceShardingManager(curatorFramework2, namespace, ip2, null, null);
    namespaceShardingManager2.start();
    Thread.sleep(1000);
    // 验证leadership
    assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip1);
    // 启动第三个
    String ip3 = "127.0.0.3";
    CuratorFramework curatorFramework3 = nestedZkUtils.createClient(namespace);
    NamespaceShardingManager namespaceShardingManager3 = new NamespaceShardingManager(curatorFramework3, namespace, ip3, null, null);
    namespaceShardingManager3.start();
    Thread.sleep(1000);
    // 验证leadership
    assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip1);
    // 停止第一个
    namespaceShardingManager1.stopWithCurator();
    Thread.sleep(1000);
    // 验证leadership
    assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isIn(ip2, ip3);
    // 停止第二个
    namespaceShardingManager2.stopWithCurator();
    Thread.sleep(1000);
    // 验证leadership
    assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip3);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Test(org.junit.Test)

Example 93 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class NestedZkUtils method createClient.

public CuratorFramework createClient(String namespace) throws InterruptedException {
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework curatorFramework = // long
    builder.connectString("127.0.0.1:" + port).sessionTimeoutMs(600 * 1000).retryPolicy(new RetryNTimes(3, 1000)).namespace(namespace).build();
    curatorFramework.start();
    curatorFramework.blockUntilConnected();
    return curatorFramework;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory)

Example 94 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class InitSaturnJob method main.

/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    String zkConnection = System.getProperty("VIP_SATURN_ZK_CONNECTION");
    System.out.println("zkConnection=" + zkConnection);
    if (zkConnection == null || "".equals(zkConnection.trim())) {
        zkConnection = "localhost:2182";
    }
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkConnection).namespace("saturn-it.vip.com").sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
    client.start();
    addJavaJob(client, "demoJavaJob");
    System.out.println("done add a java-job.");
    addShellJob(client, "demoShellJob");
    System.out.println("done add a shell-job.");
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 95 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class CuratorRepositoryImpl method connect.

@Override
public CuratorFramework connect(final String connectString, final String namespace, final String digest) {
    Builder builder = CuratorFrameworkFactory.builder().connectString(connectString).sessionTimeoutMs(SESSION_TIMEOUT).connectionTimeoutMs(CONNECTION_TIMEOUT).retryPolicy(new ExponentialBackoffRetry(1000, 3, 3000));
    if (namespace != null) {
        builder.namespace(namespace);
    }
    if (!Strings.isNullOrEmpty(digest)) {
        builder.authorization("digest", digest.getBytes()).aclProvider(new ACLProvider() {

            @Override
            public List<ACL> getDefaultAcl() {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }

            @Override
            public List<ACL> getAclForPath(final String path) {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }
        });
    }
    CuratorFramework client = builder.build();
    client.start();
    boolean established = false;
    try {
        established = client.blockUntilConnected(WAITING_SECONDS, TimeUnit.SECONDS);
    } catch (final InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    if (established) {
        return client;
    }
    CloseableUtils.closeQuietly(client);
    return null;
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder) List(java.util.List)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)824 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)267 Test (org.junit.Test)168 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)120 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)98 KeeperException (org.apache.zookeeper.KeeperException)84 IOException (java.io.IOException)75 ConnectionState (org.apache.curator.framework.state.ConnectionState)70 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)52 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)50 RetryNTimes (org.apache.curator.retry.RetryNTimes)48 ExecutorService (java.util.concurrent.ExecutorService)47 ArrayList (java.util.ArrayList)44 RetryPolicy (org.apache.curator.RetryPolicy)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Stat (org.apache.zookeeper.data.Stat)35 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)33 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)32