Search in sources :

Example 1 with ClusterInfo

use of io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo in project Mycat-Server by MyCATApache.

the class XmltoZkMain method main.

public static void main(String[] args) throws JAXBException, InterruptedException {
    // 加载zk总服务
    ZookeeperProcessListen zkListen = new ZookeeperProcessListen();
    // 得到集群名称
    String custerName = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTERID);
    // 得到基本路径
    String basePath = ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_BASE.getKey();
    basePath = basePath + ZookeeperPath.ZK_SEPARATOR.getKey() + custerName;
    zkListen.setBasePath(basePath);
    // 获得zk的连接信息
    CuratorFramework zkConn = buildConnection(ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_URL));
    // 获得公共的xml转换器对象
    XmlProcessBase xmlProcess = new XmlProcessBase();
    // 进行xmltozk的schema文件的操作
    new SchemasxmlTozkLoader(zkListen, zkConn, xmlProcess);
    // 进行xmltozk的server文件的操作
    new ServerxmlTozkLoader(zkListen, zkConn, xmlProcess);
    // 进行rule文件到zk的操作
    new RulesxmlTozkLoader(zkListen, zkConn, xmlProcess);
    // 进行序列信息入zk中
    new SequenceTozkLoader(zkListen, zkConn, xmlProcess);
    // 缓存配制信息
    new EcachesxmlTozkLoader(zkListen, zkConn, xmlProcess);
    // 将其他信息加载的zk中
    new OthermsgTozkLoader(zkListen, zkConn, xmlProcess);
    // 初始化xml转换操作
    xmlProcess.initJaxbClass();
    // 加载通知进程
    zkListen.notifly(ZkNofiflyCfg.ZK_NOTIFLY_LOAD_ALL.getKey());
    String clusterNodes = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTER_NODES);
    String clusterSize = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTER_SIZE);
    ClusterInfo info = new ClusterInfo();
    info.setClusterNodes(clusterNodes);
    info.setClusterSize(Integer.parseInt(clusterSize));
    try {
        zkConn.setData().forPath(basePath, JSON.toJSONBytes(info));
    } catch (Exception e) {
        LOGGER.error("error", e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) EcachesxmlTozkLoader(io.mycat.config.loader.zkprocess.xmltozk.listen.EcachesxmlTozkLoader) ClusterInfo(io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo) RulesxmlTozkLoader(io.mycat.config.loader.zkprocess.xmltozk.listen.RulesxmlTozkLoader) XmlProcessBase(io.mycat.config.loader.zkprocess.parse.XmlProcessBase) ServerxmlTozkLoader(io.mycat.config.loader.zkprocess.xmltozk.listen.ServerxmlTozkLoader) OthermsgTozkLoader(io.mycat.config.loader.zkprocess.xmltozk.listen.OthermsgTozkLoader) ZookeeperProcessListen(io.mycat.config.loader.zkprocess.comm.ZookeeperProcessListen) SchemasxmlTozkLoader(io.mycat.config.loader.zkprocess.xmltozk.listen.SchemasxmlTozkLoader) JAXBException(javax.xml.bind.JAXBException) SequenceTozkLoader(io.mycat.config.loader.zkprocess.xmltozk.listen.SequenceTozkLoader)

Example 2 with ClusterInfo

use of io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo 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 3 with ClusterInfo

use of io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo 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)

Aggregations

ClusterInfo (io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo)3 InterProcessMutex (org.apache.curator.framework.recipes.locks.InterProcessMutex)2 ZookeeperProcessListen (io.mycat.config.loader.zkprocess.comm.ZookeeperProcessListen)1 XmlProcessBase (io.mycat.config.loader.zkprocess.parse.XmlProcessBase)1 EcachesxmlTozkLoader (io.mycat.config.loader.zkprocess.xmltozk.listen.EcachesxmlTozkLoader)1 OthermsgTozkLoader (io.mycat.config.loader.zkprocess.xmltozk.listen.OthermsgTozkLoader)1 RulesxmlTozkLoader (io.mycat.config.loader.zkprocess.xmltozk.listen.RulesxmlTozkLoader)1 SchemasxmlTozkLoader (io.mycat.config.loader.zkprocess.xmltozk.listen.SchemasxmlTozkLoader)1 SequenceTozkLoader (io.mycat.config.loader.zkprocess.xmltozk.listen.SequenceTozkLoader)1 ServerxmlTozkLoader (io.mycat.config.loader.zkprocess.xmltozk.listen.ServerxmlTozkLoader)1 SchemaConfig (io.mycat.config.model.SchemaConfig)1 TableConfig (io.mycat.config.model.TableConfig)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 JAXBException (javax.xml.bind.JAXBException)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorTransactionFinal (org.apache.curator.framework.api.transaction.CuratorTransactionFinal)1