Search in sources :

Example 1 with DataSourcing

use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project canal by alibaba.

the class CanalInstanceWithManager method initEventParser.

protected void initEventParser() {
    logger.info("init eventParser begin...");
    SourcingType type = parameters.getSourcingType();
    List<List<DataSourcing>> groupDbAddresses = parameters.getGroupDbAddresses();
    if (!CollectionUtils.isEmpty(groupDbAddresses)) {
        // 取第一个分组的数量,主备分组的数量必须一致
        int size = groupDbAddresses.get(0).size();
        List<CanalEventParser> eventParsers = new ArrayList<CanalEventParser>();
        for (int i = 0; i < size; i++) {
            List<InetSocketAddress> dbAddress = new ArrayList<InetSocketAddress>();
            SourcingType lastType = null;
            for (List<DataSourcing> groupDbAddress : groupDbAddresses) {
                if (lastType != null && !lastType.equals(groupDbAddress.get(i).getType())) {
                    throw new CanalException(String.format("master/slave Sourcing type is unmatch. %s vs %s", lastType, groupDbAddress.get(i).getType()));
                }
                lastType = groupDbAddress.get(i).getType();
                dbAddress.add(groupDbAddress.get(i).getDbAddress());
            }
            // 初始化其中的一个分组parser
            eventParsers.add(doInitEventParser(lastType, dbAddress));
        }
        if (eventParsers.size() > 1) {
            // 如果存在分组,构造分组的parser
            GroupEventParser groupEventParser = new GroupEventParser();
            groupEventParser.setEventParsers(eventParsers);
            this.eventParser = groupEventParser;
        } else {
            this.eventParser = eventParsers.get(0);
        }
    } else {
        // 创建一个空数据库地址的parser,可能使用了tddl指定地址,启动的时候才会从tddl获取地址
        this.eventParser = doInitEventParser(type, new ArrayList<InetSocketAddress>());
    }
    logger.info("init eventParser end! \n\t load CanalEventParser:{}", eventParser.getClass().getName());
}
Also used : SourcingType(com.alibaba.otter.canal.instance.manager.model.CanalParameter.SourcingType) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) CanalEventParser(com.alibaba.otter.canal.parse.CanalEventParser) GroupEventParser(com.alibaba.otter.canal.parse.inbound.group.GroupEventParser) ArrayList(java.util.ArrayList) List(java.util.List) DataSourcing(com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing) CanalException(com.alibaba.otter.canal.common.CanalException)

Example 2 with DataSourcing

use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.

the class CanalAction method doAdd.

/**
     * 添加canal
     */
public void doAdd(@FormGroup("canalInfo") Group canalInfo, @FormGroup("canalParameterInfo") Group canalParameterInfo, @FormField(name = "formCanalError", group = "canalInfo") CustomErrors err, @FormField(name = "formHeartBeatError", group = "canalParameterInfo") CustomErrors heartBeatErr, Navigator nav) throws Exception {
    Canal canal = new Canal();
    CanalParameter parameter = new CanalParameter();
    canalInfo.setProperties(canal);
    canalParameterInfo.setProperties(parameter);
    String zkClustersString = canalParameterInfo.getField("zkClusters").getStringValue();
    String[] zkClusters = StringUtils.split(zkClustersString, ";");
    parameter.setZkClusters(Arrays.asList(zkClusters));
    Long zkClusterId = canalParameterInfo.getField("autoKeeperClusterId").getLongValue();
    parameter.setZkClusterId(zkClusterId);
    canal.setCanalParameter(parameter);
    String dbAddressesString = canalParameterInfo.getField("groupDbAddresses").getStringValue();
    // 第一层的分号代表主备概念,,第二层逗号代表分组概念
    if (StringUtils.isNotEmpty(dbAddressesString)) {
        List<List<DataSourcing>> dbSocketAddress = new ArrayList<List<DataSourcing>>();
        String[] dbAddresses = StringUtils.split(dbAddressesString, ";");
        for (String dbAddressString : dbAddresses) {
            List<DataSourcing> groupDbSocketAddress = new ArrayList<DataSourcing>();
            String[] groupDbAddresses = StringUtils.split(dbAddressString, ",");
            for (String groupDbAddress : groupDbAddresses) {
                String[] strs = StringUtils.split(groupDbAddress, ":");
                InetSocketAddress address = new InetSocketAddress(strs[0].trim(), Integer.valueOf(strs[1]));
                SourcingType type = parameter.getSourcingType();
                if (strs.length > 2) {
                    type = SourcingType.valueOf(strs[2]);
                }
                groupDbSocketAddress.add(new DataSourcing(type, address));
            }
            dbSocketAddress.add(groupDbSocketAddress);
        }
        parameter.setGroupDbAddresses(dbSocketAddress);
    }
    String positionsString = canalParameterInfo.getField("positions").getStringValue();
    if (StringUtils.isNotEmpty(positionsString)) {
        String[] positions = StringUtils.split(positionsString, ";");
        parameter.setPositions(Arrays.asList(positions));
    }
    if (parameter.getDetectingEnable() && StringUtils.startsWithIgnoreCase(parameter.getDetectingSQL(), "select")) {
        heartBeatErr.setMessage("invaliedHeartBeat");
        return;
    }
    try {
        canalService.create(canal);
    } catch (RepeatConfigureException rce) {
        err.setMessage("invalidCanal");
        return;
    }
    if (parameter.getSourcingType().isMysql() && parameter.getSlaveId() == null) {
        parameter.setSlaveId(10000 + canal.getId());
        // 再次更新一下slaveId
        try {
            canalService.modify(canal);
        } catch (RepeatConfigureException rce) {
            err.setMessage("invalidCanal");
            return;
        }
    }
    nav.redirectTo(WebConstant.CANAL_LIST_LINK);
}
Also used : SourcingType(com.alibaba.otter.canal.instance.manager.model.CanalParameter.SourcingType) Canal(com.alibaba.otter.canal.instance.manager.model.Canal) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) RepeatConfigureException(com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException) CanalParameter(com.alibaba.otter.canal.instance.manager.model.CanalParameter) ArrayList(java.util.ArrayList) List(java.util.List) DataSourcing(com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing)

Example 3 with DataSourcing

use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.

the class CanalAction method doEdit.

/**
     * 修改canal
     */
public void doEdit(@FormGroup("canalInfo") Group canalInfo, @FormGroup("canalParameterInfo") Group canalParameterInfo, @FormField(name = "formCanalError", group = "canalInfo") CustomErrors err, @FormField(name = "formHeartBeatError", group = "canalParameterInfo") CustomErrors heartBeatErr, Navigator nav) throws Exception {
    Canal canal = new Canal();
    CanalParameter parameter = new CanalParameter();
    canalInfo.setProperties(canal);
    canalParameterInfo.setProperties(parameter);
    String zkClustersString = canalParameterInfo.getField("zkClusters").getStringValue();
    String[] zkClusters = StringUtils.split(zkClustersString, ";");
    parameter.setZkClusters(Arrays.asList(zkClusters));
    Long zkClusterId = canalParameterInfo.getField("autoKeeperClusterId").getLongValue();
    parameter.setZkClusterId(zkClusterId);
    String dbAddressesString = canalParameterInfo.getField("groupDbAddresses").getStringValue();
    if (StringUtils.isNotEmpty(dbAddressesString)) {
        List<List<DataSourcing>> dbSocketAddress = new ArrayList<List<DataSourcing>>();
        String[] dbAddresses = StringUtils.split(dbAddressesString, ";");
        for (String dbAddressString : dbAddresses) {
            List<DataSourcing> groupDbSocketAddress = new ArrayList<DataSourcing>();
            String[] groupDbAddresses = StringUtils.split(dbAddressString, ",");
            for (String groupDbAddress : groupDbAddresses) {
                String[] strs = StringUtils.split(groupDbAddress, ":");
                InetSocketAddress address = new InetSocketAddress(strs[0].trim(), Integer.valueOf(strs[1]));
                SourcingType type = parameter.getSourcingType();
                if (strs.length > 2) {
                    type = SourcingType.valueOf(strs[2]);
                }
                groupDbSocketAddress.add(new DataSourcing(type, address));
            }
            dbSocketAddress.add(groupDbSocketAddress);
        }
        parameter.setGroupDbAddresses(dbSocketAddress);
    }
    String positionsString = canalParameterInfo.getField("positions").getStringValue();
    if (StringUtils.isNotEmpty(positionsString)) {
        String[] positions = StringUtils.split(positionsString, ";");
        parameter.setPositions(Arrays.asList(positions));
    }
    if (parameter.getDetectingEnable() && StringUtils.startsWithIgnoreCase(parameter.getDetectingSQL(), "select")) {
        heartBeatErr.setMessage("invaliedHeartBeat");
        return;
    }
    canal.setCanalParameter(parameter);
    try {
        canalService.modify(canal);
    } catch (RepeatConfigureException rce) {
        err.setMessage("invalidCanal");
        return;
    }
    nav.redirectToLocation("canalList.htm");
}
Also used : SourcingType(com.alibaba.otter.canal.instance.manager.model.CanalParameter.SourcingType) Canal(com.alibaba.otter.canal.instance.manager.model.Canal) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) RepeatConfigureException(com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException) CanalParameter(com.alibaba.otter.canal.instance.manager.model.CanalParameter) ArrayList(java.util.ArrayList) List(java.util.List) DataSourcing(com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing)

Example 4 with DataSourcing

use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.

the class NumberFormatUtil method formatGroupDbAddress.

public String formatGroupDbAddress(SourcingType defaultType, List<List<DataSourcing>> groupDbAddresses) {
    StringBuilder builder = new StringBuilder();
    for (List<DataSourcing> groupDbAddress : groupDbAddresses) {
        List<String> address = new ArrayList<String>();
        for (DataSourcing dbAddress : groupDbAddress) {
            StringBuilder dbAddressBuilder = new StringBuilder();
            dbAddressBuilder.append(dbAddress.getDbAddress().getAddress().getHostName());
            dbAddressBuilder.append(":");
            dbAddressBuilder.append(String.valueOf(dbAddress.getDbAddress().getPort()));
            if (!defaultType.equals(dbAddress.getType())) {
                dbAddressBuilder.append(":").append(dbAddress.getType().name());
            }
            address.add(dbAddressBuilder.toString());
        }
        builder.append(StringUtils.join(address, ',')).append(";");
    }
    return builder.toString();
}
Also used : ArrayList(java.util.ArrayList) DataSourcing(com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing)

Example 5 with DataSourcing

use of com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing in project otter by alibaba.

the class SeniorCanal method getUrl.

public String getUrl() {
    CanalParameter parameter = getCanalParameter();
    if (parameter.getHaMode().isMedia()) {
        return "media://" + parameter.getMediaGroup();
    } else {
        StringBuilder address = new StringBuilder("jdbc://");
        for (List<DataSourcing> groupAddress : parameter.getGroupDbAddresses()) {
            int i = 0;
            for (DataSourcing dbAddress : groupAddress) {
                ++i;
                address.append(dbAddress.getDbAddress().getAddress().getHostName()).append(":").append(dbAddress.getDbAddress().getPort());
                if (i < groupAddress.size()) {
                    address.append(',');
                }
            }
            address.append(';');
        }
        return address.toString();
    }
}
Also used : CanalParameter(com.alibaba.otter.canal.instance.manager.model.CanalParameter) DataSourcing(com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing)

Aggregations

DataSourcing (com.alibaba.otter.canal.instance.manager.model.CanalParameter.DataSourcing)5 ArrayList (java.util.ArrayList)4 CanalParameter (com.alibaba.otter.canal.instance.manager.model.CanalParameter)3 SourcingType (com.alibaba.otter.canal.instance.manager.model.CanalParameter.SourcingType)3 InetSocketAddress (java.net.InetSocketAddress)3 List (java.util.List)3 Canal (com.alibaba.otter.canal.instance.manager.model.Canal)2 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)2 CanalException (com.alibaba.otter.canal.common.CanalException)1 CanalEventParser (com.alibaba.otter.canal.parse.CanalEventParser)1 GroupEventParser (com.alibaba.otter.canal.parse.inbound.group.GroupEventParser)1