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();
}
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) {
}
}
}
}
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) {
}
}
}
}
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) {
}
}
}
}
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();
}
Aggregations