use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project Singularity by HubSpot.
the class TaskManager method createTaskAndDeletePendingTaskPrivate.
private void createTaskAndDeletePendingTaskPrivate(SingularityTask task) throws Exception {
// TODO: Should more of the below be done within a transaction?
deletePendingTask(task.getTaskRequest().getPendingTask().getPendingTaskId());
final long now = System.currentTimeMillis();
String msg = String.format("Task launched because of %s", task.getTaskRequest().getPendingTask().getPendingTaskId().getPendingType().name());
if (task.getTaskRequest().getPendingTask().getUser().isPresent()) {
msg = String.format("%s by %s", msg, task.getTaskRequest().getPendingTask().getUser().get());
}
if (task.getTaskRequest().getPendingTask().getMessage().isPresent()) {
msg = String.format("%s (%s)", msg, task.getTaskRequest().getPendingTask().getMessage().get());
}
saveTaskHistoryUpdate(new SingularityTaskHistoryUpdate(task.getTaskId(), now, ExtendedTaskState.TASK_LAUNCHED, Optional.of(msg), Optional.<String>absent()));
saveLastActiveTaskStatus(new SingularityTaskStatusHolder(task.getTaskId(), Optional.absent(), now, serverId, Optional.of(task.getAgentId().getValue())));
try {
final String path = getTaskPath(task.getTaskId());
CuratorTransactionFinal transaction = curator.inTransaction().create().forPath(path, taskTranscoder.toBytes(task)).and();
transaction.create().forPath(getActivePath(task.getTaskId().getId())).and().commit();
leaderCache.putActiveTask(task);
taskCache.set(path, task);
} catch (KeeperException.NodeExistsException nee) {
LOG.error("Task or active path already existed for {}", task.getTaskId());
}
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project Mycat_plus by coderczp.
the class MigrateHandler method handle.
public static void handle(String stmt, ServerConnection c) {
Map<String, String> map = parse(stmt);
String table = map.get("table");
String add = map.get("add");
if (table == null) {
writeErrMessage(c, "table cannot be null");
return;
}
if (add == null) {
writeErrMessage(c, "add cannot be null");
return;
}
String taskID = getUUID();
try {
SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(c.getSchema());
TableConfig tableConfig = schemaConfig.getTables().get(table.toUpperCase());
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (!(algorithm instanceof PartitionByCRC32PreSlot)) {
writeErrMessage(c, "table: " + table + " rule is not be PartitionByCRC32PreSlot");
return;
}
Map<Integer, List<Range>> integerListMap = ((PartitionByCRC32PreSlot) algorithm).getRangeMap();
integerListMap = (Map<Integer, List<Range>>) ObjectUtil.copyObject(integerListMap);
ArrayList<String> oldDataNodes = tableConfig.getDataNodes();
List<String> newDataNodes = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(add);
Map<String, List<MigrateTask>> tasks = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, PartitionByCRC32PreSlot.DEFAULT_SLOTS_NUM);
CuratorTransactionFinal transactionFinal = null;
String taskBase = ZKUtils.getZKBasePath() + "migrate/" + c.getSchema();
String taskPath = taskBase + "/" + taskID;
CuratorFramework client = ZKUtils.getConnection();
// 校验 之前同一个表的迁移任务未完成,则jzhi禁止继续
if (client.checkExists().forPath(taskBase) != null) {
List<String> childTaskList = client.getChildren().forPath(taskBase);
for (String child : childTaskList) {
TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskBase + "/" + child), TaskNode.class);
if (taskNode.getSchema().equalsIgnoreCase(c.getSchema()) && table.equalsIgnoreCase(taskNode.getTable()) && taskNode.getStatus() < 5) {
writeErrMessage(c, "table: " + table + " previous migrate task is still running,on the same time one table only one task");
return;
}
}
}
client.create().creatingParentsIfNeeded().forPath(taskPath);
TaskNode taskNode = new TaskNode();
taskNode.setSchema(c.getSchema());
taskNode.setSql(stmt);
taskNode.setTable(table);
taskNode.setAdd(add);
taskNode.setStatus(0);
Map<String, Integer> fromNodeSlaveIdMap = new HashMap<>();
List<MigrateTask> allTaskList = new ArrayList<>();
for (Map.Entry<String, List<MigrateTask>> entry : tasks.entrySet()) {
String key = entry.getKey();
List<MigrateTask> value = entry.getValue();
for (MigrateTask migrateTask : value) {
migrateTask.setSchema(c.getSchema());
// 分配slaveid只需要一个dataHost分配一个即可,后续任务执行模拟从节点只需要一个dataHost一个
String dataHost = getDataHostNameFromNode(migrateTask.getFrom());
if (fromNodeSlaveIdMap.containsKey(dataHost)) {
migrateTask.setSlaveId(fromNodeSlaveIdMap.get(dataHost));
} else {
migrateTask.setSlaveId(getSlaveIdFromZKForDataNode(migrateTask.getFrom()));
fromNodeSlaveIdMap.put(dataHost, migrateTask.getSlaveId());
}
}
allTaskList.addAll(value);
}
transactionFinal = client.inTransaction().setData().forPath(taskPath, JSON.toJSONBytes(taskNode)).and();
// 合并成dataHost级别任务
Map<String, List<MigrateTask>> dataHostMigrateMap = mergerTaskForDataHost(allTaskList);
for (Map.Entry<String, List<MigrateTask>> entry : dataHostMigrateMap.entrySet()) {
String key = entry.getKey();
List<MigrateTask> value = entry.getValue();
String path = taskPath + "/" + key;
transactionFinal = transactionFinal.create().forPath(path, JSON.toJSONBytes(value)).and();
}
transactionFinal.commit();
} catch (Exception e) {
LOGGER.error("migrate error", e);
writeErrMessage(c, "migrate error:" + e);
return;
}
writePackToClient(c, taskID);
LOGGER.info("task start", new Date());
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project coprhd-controller by CoprHD.
the class CoordinatorClientImpl method commitTransaction.
@Override
public void commitTransaction() throws CoordinatorException {
try {
CuratorTransaction handler = zkTransactionHandler.get();
CuratorTransactionFinal tx = (CuratorTransactionFinal) handler;
tx.commit();
zkTransactionHandler.remove();
} catch (Exception ex) {
throw CoordinatorException.fatals.unableToPersistTheConfiguration(ex);
}
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project coprhd-controller by CoprHD.
the class CoordinatorClientImpl method removeServiceConfiguration.
@Override
public void removeServiceConfiguration(String siteId, Configuration... configs) throws CoordinatorException {
for (int i = 0; i < configs.length; i++) {
Configuration config = configs[i];
String prefix = "";
if (siteId != null) {
prefix = getSitePrefix(siteId);
}
String servicePath = String.format("%1$s%2$s/%3$s/%4$s", prefix, ZkPath.CONFIG, config.getKind(), config.getId());
try {
CuratorTransaction handler = zkTransactionHandler.get();
if (handler != null) {
CuratorTransactionFinal tx = handler.delete().forPath(servicePath).and();
zkTransactionHandler.set(tx);
} else {
_zkConnection.curator().delete().forPath(servicePath);
}
} catch (KeeperException.NoNodeException ignore) {
// Ignore exception, don't re-throw
log.debug("Caught exception but ignoring it: " + ignore);
} catch (Exception e) {
throw CoordinatorException.fatals.unableToRemoveConfiguration(config.getId(), e);
}
}
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal 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) {
}
}
}
}
Aggregations