Search in sources :

Example 6 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project Mycat_plus by coderczp.

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 7 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project dcos-commons by mesosphere.

the class CuratorPersister method recursiveDelete.

@Override
public void recursiveDelete(String unprefixedPath) throws PersisterException {
    final String path = withFrameworkPrefix(unprefixedPath);
    if (path.equals(serviceRootPath)) {
        // Special case: If we're being told to delete root, we should instead delete the contents OF root. We don't
        // have access to delete the root-level '/dcos-service-<svcname>' node itself, despite having created it.
        /* Effective 5/8/2017:
             * We cannot delete the root node of a service directly.
             * This is due to the ACLs on the global DC/OS ZK.
             *
             * The root has:
             * [zk: localhost:2181(CONNECTED) 1] getAcl /
             * 'world,'anyone
             * : cr
             * 'ip,'127.0.0.1
             * : cdrwa
             *
             * Our service nodes have:
             * [zk: localhost:2181(CONNECTED) 0] getAcl /dcos-service-hello-world
             * 'world,'anyone
             * : cdrwa
             *
             * The best we can do is to wipe everything under the root node. A proposed way to "fix" things
             * lives at https://jira.mesosphere.com/browse/INFINITY-1470.
             */
        LOGGER.debug("Deleting children of root {}", path);
        try {
            CuratorTransactionFinal transaction = client.inTransaction().check().forPath(serviceRootPath).and();
            Set<String> pendingDeletePaths = new HashSet<>();
            for (String child : client.getChildren().forPath(serviceRootPath)) {
                // Custom logic for root-level children: don't delete the lock node
                if (child.equals(CuratorLocker.LOCK_PATH_NAME)) {
                    continue;
                }
                String childPath = PersisterUtils.join(serviceRootPath, child);
                transaction = deleteChildrenOf(client, childPath, transaction, pendingDeletePaths).delete().forPath(childPath).and();
            }
            transaction.commit();
        } catch (Exception e) {
            throw new PersisterException(Reason.STORAGE_ERROR, String.format("Unable to delete children of root %s: %s", path, e.getMessage()), e);
        }
        // Need to explicitly set null or else curator will return a zero-bytes value later:
        set(unprefixedPath, null);
    } else {
        // Normal case: Delete node itself and any/all children.
        LOGGER.debug("Deleting {} (and any children)", path);
        try {
            client.delete().deletingChildrenIfNeeded().forPath(path);
        } catch (KeeperException.NoNodeException e) {
            throw new PersisterException(Reason.NOT_FOUND, String.format("Path to delete does not exist: %s", path), e);
        } catch (Exception e) {
            throw new PersisterException(Reason.STORAGE_ERROR, String.format("Unable to delete %s", path), e);
        }
    }
}
Also used : CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) PersisterException(com.mesosphere.sdk.storage.PersisterException) KeeperException(org.apache.zookeeper.KeeperException) PersisterException(com.mesosphere.sdk.storage.PersisterException) KeeperException(org.apache.zookeeper.KeeperException)

Example 8 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project vespa by vespa-engine.

the class Curator method createAtomically.

/**
 * Creates all the given paths in a single transaction. Any paths which already exists are ignored.
 */
public void createAtomically(Path... paths) {
    try {
        CuratorTransaction transaction = framework().inTransaction();
        for (Path path : paths) {
            if (!exists(path)) {
                transaction = transaction.create().forPath(path.getAbsolute(), new byte[0]).and();
            }
        }
        ((CuratorTransactionFinal) transaction).commit();
    } catch (Exception e) {
        throw new RuntimeException("Could not create " + Arrays.toString(paths), e);
    }
}
Also used : Path(com.yahoo.path.Path) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal)

Example 9 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project Mycat-Server by MyCATApache.

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());
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) TaskNode(io.mycat.migrate.TaskNode) SchemaConfig(io.mycat.config.model.SchemaConfig) MigrateTask(io.mycat.migrate.MigrateTask) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) TableConfig(io.mycat.config.model.TableConfig) PartitionByCRC32PreSlot(io.mycat.route.function.PartitionByCRC32PreSlot)

Example 10 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project helios by spotify.

the class DefaultZooKeeperClient method transaction.

@Override
public Collection<CuratorTransactionResult> transaction(final List<ZooKeeperOperation> operations) throws KeeperException {
    assertClusterIdFlagTrue();
    log.debug("transaction: {}", operations);
    if (operations.isEmpty()) {
        return emptyList();
    }
    // Assemble transaction
    final CuratorTransactionFinal transaction = (CuratorTransactionFinal) client.inTransaction();
    for (final ZooKeeperOperation operation : operations) {
        try {
            operation.register(transaction);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
    // Commit
    try {
        return transaction.commit();
    } catch (Exception e) {
        throwIfInstanceOf(e, KeeperException.class);
        throw new RuntimeException(e);
    }
}
Also used : CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

CuratorTransactionFinal (org.apache.curator.framework.api.transaction.CuratorTransactionFinal)20 KeeperException (org.apache.zookeeper.KeeperException)8 IOException (java.io.IOException)7 CuratorTransaction (org.apache.curator.framework.api.transaction.CuratorTransaction)7 SchemaConfig (io.mycat.config.model.SchemaConfig)4 TableConfig (io.mycat.config.model.TableConfig)4 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)3 RetryableCoordinatorException (com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException)3 UnknownHostException (java.net.UnknownHostException)3 List (java.util.List)3 CuratorTransactionBridge (org.apache.curator.framework.api.transaction.CuratorTransactionBridge)3 TransactionCreateBuilder (org.apache.curator.framework.api.transaction.TransactionCreateBuilder)3 Test (org.junit.Test)3 Configuration (com.emc.storageos.coordinator.common.Configuration)2 PropertyInfoMapper.decodeFromString (com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString)2 ClusterInfo (io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo)2 MigrateTask (io.mycat.migrate.MigrateTask)2 TaskNode (io.mycat.migrate.TaskNode)2 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)2