Search in sources :

Example 11 with InterProcessMutex

use of org.apache.curator.framework.recipes.locks.InterProcessMutex in project incubator-atlas by apache.

the class SetupStepsTest method shouldThrowSetupExceptionAndNotDoSetupIfSetupInProgressNodeExists.

@Test
public void shouldThrowSetupExceptionAndNotDoSetupIfSetupInProgressNodeExists() throws Exception {
    Set<SetupStep> steps = new LinkedHashSet<>();
    SetupStep setupStep1 = mock(SetupStep.class);
    steps.add(setupStep1);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    setupServerIdSelectionMocks();
    setupSetupInProgressPathMocks(ZooDefs.Ids.OPEN_ACL_UNSAFE, mock(Stat.class));
    InterProcessMutex lock = mock(InterProcessMutex.class);
    when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(lock);
    SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
    try {
        setupSteps.runSetup();
    } catch (Exception e) {
        assertTrue(e instanceof SetupException);
    }
    verifyZeroInteractions(setupStep1);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Stat(org.apache.zookeeper.data.Stat) SetupException(org.apache.atlas.setup.SetupException) SetupStep(org.apache.atlas.setup.SetupStep) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) SetupException(org.apache.atlas.setup.SetupException) Test(org.testng.annotations.Test)

Example 12 with InterProcessMutex

use of org.apache.curator.framework.recipes.locks.InterProcessMutex in project incubator-atlas by apache.

the class SetupStepsTest method shouldDeleteSetupInProgressNodeAfterCompletion.

@Test
public void shouldDeleteSetupInProgressNodeAfterCompletion() throws Exception {
    Set<SetupStep> steps = new LinkedHashSet<>();
    SetupStep setupStep1 = mock(SetupStep.class);
    steps.add(setupStep1);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("digest:user:pwd");
    List<ACL> aclList = Arrays.asList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "user:pwd")));
    setupServerIdSelectionMocks();
    DeleteBuilder deleteBuilder = setupSetupInProgressPathMocks(aclList).getRight();
    InterProcessMutex lock = mock(InterProcessMutex.class);
    when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(lock);
    SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
    setupSteps.runSetup();
    verify(deleteBuilder).forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT + SetupSteps.SETUP_IN_PROGRESS_NODE);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ACL(org.apache.zookeeper.data.ACL) SetupStep(org.apache.atlas.setup.SetupStep) Id(org.apache.zookeeper.data.Id) DeleteBuilder(org.apache.curator.framework.api.DeleteBuilder) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) Test(org.testng.annotations.Test)

Example 13 with InterProcessMutex

use of org.apache.curator.framework.recipes.locks.InterProcessMutex in project yyl_example by Relucent.

the class CuratorDistributedLockTest method main.

/**
	 * 下面的程序会启动几个线程去争夺锁,拿到锁的线程会占用5秒
	 */
public static void main(String[] args) throws InterruptedException {
    // 1.Connect to zk
    CuratorFramework client = CuratorFrameworkFactory.newClient(ZK_ADDRESS, new RetryNTimes(10, 5000));
    client.start();
    System.out.println(client.getState());
    System.out.println("zk client start successfully!");
    InterProcessMutex lock = new InterProcessMutex(client, ZK_LOCK_PATH);
    for (int i = 0; i < 3; i++) {
        new Thread(() -> {
            doWithLock(client, lock);
        }, "Thread-" + i).start();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex)

Aggregations

InterProcessMutex (org.apache.curator.framework.recipes.locks.InterProcessMutex)13 SetupStep (org.apache.atlas.setup.SetupStep)7 LinkedHashSet (java.util.LinkedHashSet)6 Test (org.testng.annotations.Test)6 SetupException (org.apache.atlas.setup.SetupException)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 ClusterInfo (io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 ACL (org.apache.zookeeper.data.ACL)2 Id (org.apache.zookeeper.data.Id)2 InOrder (org.mockito.InOrder)2 SchemaConfig (io.mycat.config.model.SchemaConfig)1 TableConfig (io.mycat.config.model.TableConfig)1 RuleConfig (io.mycat.config.model.rule.RuleConfig)1 PartitionByCRC32PreSlot (io.mycat.route.function.PartitionByCRC32PreSlot)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1