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