Search in sources :

Example 6 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)

Example 7 with InterProcessMutex

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

the class SwitchPrepareListener method checkSwitch.

private void checkSwitch(PathChildrenCacheEvent event) {
    InterProcessMutex taskLock = null;
    try {
        String path = event.getData().getPath();
        String taskPath = path.substring(0, path.lastIndexOf("/_prepare/"));
        String taskID = taskPath.substring(taskPath.lastIndexOf('/') + 1, taskPath.length());
        String lockPath = ZKUtils.getZKBasePath() + "lock/" + taskID + ".lock";
        List<String> sucessDataHost = ZKUtils.getConnection().getChildren().forPath(path.substring(0, path.lastIndexOf('/')));
        List<MigrateTask> allTaskList = MigrateUtils.queryAllTask(taskPath, sucessDataHost);
        TaskNode pTaskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
        ConcurrentMap<String, List<PartitionByCRC32PreSlot.Range>> tableRuleMap = RouteCheckRule.migrateRuleMap.containsKey(pTaskNode.getSchema().toUpperCase()) ? RouteCheckRule.migrateRuleMap.get(pTaskNode.getSchema().toUpperCase()) : new ConcurrentHashMap();
        tableRuleMap.put(pTaskNode.getTable().toUpperCase(), MigrateUtils.convertAllTask(allTaskList));
        RouteCheckRule.migrateRuleMap.put(pTaskNode.getSchema().toUpperCase(), tableRuleMap);
        taskLock = new InterProcessMutex(ZKUtils.getConnection(), lockPath);
        taskLock.acquire(20, TimeUnit.SECONDS);
        List<String> dataHost = ZKUtils.getConnection().getChildren().forPath(taskPath);
        if (getRealSize(dataHost) == sucessDataHost.size()) {
            TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
            if (taskNode.getStatus() == 1) {
                //prepare switch
                taskNode.setStatus(2);
                LOGGER.info("task switch:", new Date());
                ZKUtils.getConnection().setData().forPath(taskPath, JSON.toJSONBytes(taskNode));
            }
        }
    } catch (Exception e) {
        LOGGER.error("error:", e);
    } finally {
        if (taskLock != null) {
            try {
                taskLock.release();
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) Date(java.util.Date) PartitionByCRC32PreSlot(io.mycat.route.function.PartitionByCRC32PreSlot)

Example 8 with InterProcessMutex

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

the class SwitchCleanListener method checkSwitch.

private void checkSwitch(PathChildrenCacheEvent event) {
    InterProcessMutex taskLock = null;
    try {
        String path = event.getData().getPath();
        String taskPath = path.substring(0, path.lastIndexOf("/_clean/"));
        String taskID = taskPath.substring(taskPath.lastIndexOf('/') + 1, taskPath.length());
        String lockPath = ZKUtils.getZKBasePath() + "lock/" + taskID + ".lock";
        List<String> sucessDataHost = ZKUtils.getConnection().getChildren().forPath(path.substring(0, path.lastIndexOf('/')));
        TaskNode pTaskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
        String custerName = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTERID);
        ClusterInfo clusterInfo = JSON.parseObject(ZKUtils.getConnection().getData().forPath("/mycat/" + custerName), ClusterInfo.class);
        List<String> clusterNodeList = Splitter.on(',').omitEmptyStrings().splitToList(clusterInfo.getClusterNodes());
        if (sucessDataHost.size() == clusterNodeList.size()) {
            RouteCheckRule.migrateRuleMap.remove(pTaskNode.getSchema().toUpperCase());
            List<String> needToCloseWatch = new ArrayList<>();
            List<String> dataHosts = ZKUtils.getConnection().getChildren().forPath(taskPath);
            for (String dataHostName : dataHosts) {
                if ("_prepare".equals(dataHostName) || "_commit".equals(dataHostName) || "_clean".equals(dataHostName)) {
                    needToCloseWatch.add(taskPath + "/" + dataHostName);
                }
            }
            ZKUtils.closeWatch(needToCloseWatch);
            taskLock = new InterProcessMutex(ZKUtils.getConnection(), lockPath);
            taskLock.acquire(20, TimeUnit.SECONDS);
            TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
            if (taskNode.getStatus() == 3) {
                //clean sucess
                taskNode.setStatus(5);
                for (String dataHostName : dataHosts) {
                    if ("_prepare".equals(dataHostName) || "_commit".equals(dataHostName) || "_clean".equals(dataHostName))
                        continue;
                    List<MigrateTask> migrateTaskList = JSON.parseArray(new String(ZKUtils.getConnection().getData().forPath(taskPath + "/" + dataHostName), "UTF-8"), MigrateTask.class);
                    int slaveId = migrateTaskList.get(0).getSlaveId();
                    String slavePath = ZKUtils.getZKBasePath() + "slaveIDs/" + dataHostName + "/" + slaveId;
                    if (ZKUtils.getConnection().checkExists().forPath(slavePath) != null) {
                        ZKUtils.getConnection().delete().forPath(slavePath);
                    }
                }
                ZKUtils.getConnection().setData().forPath(taskPath, JSON.toJSONBytes(taskNode));
                LOGGER.info("task end", new Date());
            }
        }
    } catch (Exception e) {
        LOGGER.error("error:", e);
    } finally {
        if (taskLock != null) {
            try {
                taskLock.release();
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : ClusterInfo(io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo) ArrayList(java.util.ArrayList) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) Date(java.util.Date)

Example 9 with InterProcessMutex

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

the class SwitchCommitListener method checkCommit.

private void checkCommit(PathChildrenCacheEvent event) {
    InterProcessMutex taskLock = null;
    try {
        String path = event.getData().getPath();
        String taskPath = path.substring(0, path.lastIndexOf("/_commit/"));
        String taskID = taskPath.substring(taskPath.lastIndexOf('/') + 1, taskPath.length());
        String lockPath = ZKUtils.getZKBasePath() + "lock/" + taskID + ".lock";
        List<String> sucessDataHost = ZKUtils.getConnection().getChildren().forPath(path.substring(0, path.lastIndexOf('/')));
        String custerName = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTERID);
        ClusterInfo clusterInfo = JSON.parseObject(ZKUtils.getConnection().getData().forPath("/mycat/" + custerName), ClusterInfo.class);
        List<String> clusterNodeList = Splitter.on(',').omitEmptyStrings().splitToList(clusterInfo.getClusterNodes());
        if (sucessDataHost.size() == clusterNodeList.size()) {
            List<String> taskDataHost = ZKUtils.getConnection().getChildren().forPath(taskPath);
            List<MigrateTask> allTaskList = MigrateUtils.queryAllTask(taskPath, taskDataHost);
            taskLock = new InterProcessMutex(ZKUtils.getConnection(), lockPath);
            taskLock.acquire(120, TimeUnit.SECONDS);
            TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
            if (taskNode.getStatus() == 2) {
                taskNode.setStatus(3);
                //开始切换 且个节点已经禁止写入并且无原有写入在执行
                try {
                    CuratorTransactionFinal transactionFinal = null;
                    check(taskID, allTaskList);
                    SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(taskNode.getSchema());
                    TableConfig tableConfig = schemaConfig.getTables().get(taskNode.getTable().toUpperCase());
                    List<String> newDataNodes = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(taskNode.getAdd());
                    List<String> allNewDataNodes = tableConfig.getDataNodes();
                    allNewDataNodes.addAll(newDataNodes);
                    //先修改rule config
                    InterProcessMutex ruleLock = new InterProcessMutex(ZKUtils.getConnection(), ZKUtils.getZKBasePath() + "lock/rules.lock");
                    ;
                    try {
                        ruleLock.acquire(30, TimeUnit.SECONDS);
                        transactionFinal = modifyZkRules(transactionFinal, tableConfig.getRule().getFunctionName(), newDataNodes);
                        transactionFinal = modifyTableConfigRules(transactionFinal, taskNode.getSchema(), taskNode.getTable(), newDataNodes);
                    } finally {
                        ruleLock.release();
                    }
                    transactionFinal = modifyRuleData(transactionFinal, allTaskList, tableConfig, allNewDataNodes);
                    transactionFinal.setData().forPath(taskPath, JSON.toJSONBytes(taskNode));
                    clean(taskID, allTaskList);
                    transactionFinal.commit();
                    forceTableRuleToLocal();
                    pushACKToClean(taskPath);
                } catch (Exception e) {
                    //todo 异常to  Zk
                    LOGGER.error("error:", e);
                }
            //todo   清理规则     顺利拉下ruledata保证一定更新到本地
            } else if (taskNode.getStatus() == 3) {
                forceTableRuleToLocal();
                pushACKToClean(taskPath);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error:", e);
    } finally {
        if (taskLock != null) {
            try {
                taskLock.release();
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : ClusterInfo(io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo) SchemaConfig(io.mycat.config.model.SchemaConfig) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) TableConfig(io.mycat.config.model.TableConfig) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 10 with InterProcessMutex

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

the class SetupStepsTest method shouldRunRegisteredSetupSteps.

@Test
public void shouldRunRegisteredSetupSteps() 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);
    SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
    setupSteps.runSetup();
    verify(setupStep1).run();
    verify(setupStep2).run();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) 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