Search in sources :

Example 1 with InterProcessMutex

use of org.apache.curator.framework.recipes.locks.InterProcessMutex in project Mycat-Server by MyCATApache.

the class MycatServer method initRuleData.

public void initRuleData() {
    if (!isUseZk())
        return;
    InterProcessMutex ruleDataLock = null;
    try {
        File file = new File(SystemConfig.getHomePath(), "conf" + File.separator + "ruledata");
        String path = ZKUtils.getZKBasePath() + "lock/ruledata.lock";
        ruleDataLock = new InterProcessMutex(ZKUtils.getConnection(), path);
        ruleDataLock.acquire(30, TimeUnit.SECONDS);
        File[] childFiles = file.listFiles();
        String basePath = ZKUtils.getZKBasePath() + "ruledata/";
        for (File childFile : childFiles) {
            CuratorFramework zk = ZKUtils.getConnection();
            if (zk.checkExists().forPath(basePath + childFile.getName()) == null) {
                zk.create().creatingParentsIfNeeded().forPath(basePath + childFile.getName(), Files.toByteArray(childFile));
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (ruleDataLock != null)
                ruleDataLock.release();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex)

Example 2 with InterProcessMutex

use of org.apache.curator.framework.recipes.locks.InterProcessMutex in project Mycat-Server by MyCATApache.

the class SwitchCommitListener method modifyRuleData.

private CuratorTransactionFinal modifyRuleData(CuratorTransactionFinal transactionFinal, List<MigrateTask> allTaskList, TableConfig tableConfig, List<String> allNewDataNodes) throws Exception {
    InterProcessMutex ruleDataLock = null;
    try {
        String path = ZKUtils.getZKBasePath() + "lock/ruledata.lock";
        ruleDataLock = new InterProcessMutex(ZKUtils.getConnection(), path);
        ruleDataLock.acquire(30, TimeUnit.SECONDS);
        RuleConfig ruleConfig = tableConfig.getRule();
        String ruleName = ((TableRuleAware) ruleConfig.getRuleAlgorithm()).getRuleName() + ".properties";
        String rulePath = ZKUtils.getZKBasePath() + "ruledata/" + ruleName;
        CuratorFramework zk = ZKUtils.getConnection();
        byte[] ruleData = zk.getData().forPath(rulePath);
        Properties prop = new Properties();
        prop.load(new ByteArrayInputStream(ruleData));
        for (MigrateTask migrateTask : allTaskList) {
            modifyRuleData(prop, migrateTask, allNewDataNodes);
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        prop.store(out, "WARNING   !!!Please do not modify or delete this file!!!");
        if (transactionFinal == null) {
            transactionFinal = ZKUtils.getConnection().inTransaction().setData().forPath(rulePath, out.toByteArray()).and();
        } else {
            transactionFinal.setData().forPath(rulePath, out.toByteArray());
        }
    } finally {
        try {
            if (ruleDataLock != null)
                ruleDataLock.release();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return transactionFinal;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ByteArrayInputStream(java.io.ByteArrayInputStream) RuleConfig(io.mycat.config.model.rule.RuleConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 3 with InterProcessMutex

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

the class SetupStepsTest method shouldReleaseLockOnException.

@Test
public void shouldReleaseLockOnException() 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);
    doThrow(new RuntimeException("Simulating setup failure.")).when(setupStep1).run();
    InterProcessMutex lock = mock(InterProcessMutex.class);
    when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(lock);
    InOrder inOrder = inOrder(lock, setupStep1);
    SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
    try {
        setupSteps.runSetup();
    } catch (Exception e) {
        assertTrue(e instanceof SetupException);
    }
    inOrder.verify(lock).acquire();
    inOrder.verify(setupStep1).run();
    inOrder.verify(lock).release();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InOrder(org.mockito.InOrder) 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 4 with InterProcessMutex

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

the class SetupStepsTest method shouldCreateSetupInProgressNode.

@Test
public void shouldCreateSetupInProgressNode() 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();
    CreateBuilder createBuilder = setupSetupInProgressPathMocks(aclList).getLeft();
    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(createBuilder).withACL(aclList);
    verify(createBuilder).forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT + SetupSteps.SETUP_IN_PROGRESS_NODE, "id2".getBytes(Charsets.UTF_8));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACL(org.apache.zookeeper.data.ACL) SetupStep(org.apache.atlas.setup.SetupStep) Id(org.apache.zookeeper.data.Id) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) Test(org.testng.annotations.Test)

Example 5 with InterProcessMutex

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

the class SetupStepsTest method shouldRunSetupStepsUnderLock.

@Test
public void shouldRunSetupStepsUnderLock() throws Exception {
    Set<SetupStep> steps = new LinkedHashSet<>();
    SetupStep setupStep1 = mock(SetupStep.class);
    SetupStep setupStep2 = mock(SetupStep.class);
    steps.add(setupStep1);
    steps.add(setupStep2);
    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);
    InterProcessMutex lock = mock(InterProcessMutex.class);
    when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(lock);
    InOrder inOrder = inOrder(lock, setupStep1, setupStep2);
    SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
    setupSteps.runSetup();
    inOrder.verify(lock).acquire();
    inOrder.verify(setupStep1).run();
    inOrder.verify(setupStep2).run();
    inOrder.verify(lock).release();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InOrder(org.mockito.InOrder) SetupStep(org.apache.atlas.setup.SetupStep) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) Test(org.testng.annotations.Test)

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