public void start() {
if (running) {
return;
}
// 获取destination/filter参数
Pipeline pipeline = configClientService.findPipeline(pipelineId);
filter = makeFilterExpression(pipeline);
destination = pipeline.getParameters().getDestinationName();
batchSize = pipeline.getParameters().getMainstemBatchsize();
batchTimeout = pipeline.getParameters().getBatchTimeout();
ddlSync = pipeline.getParameters().getDdlSync();
// 暂时使用skip load代替
filterTableError = pipeline.getParameters().getSkipSelectException();
if (pipeline.getParameters().getDumpSelector() != null) {
dump = pipeline.getParameters().getDumpSelector();
}
if (pipeline.getParameters().getDumpSelectorDetail() != null) {
dumpDetail = pipeline.getParameters().getDumpSelectorDetail();
}
canalServer.setCanalInstanceGenerator(new CanalInstanceGenerator() {
public CanalInstance generate(String destination) {
Canal canal = canalConfigClient.findCanal(destination);
final OtterAlarmHandler otterAlarmHandler = new OtterAlarmHandler();
otterAlarmHandler.setPipelineId(pipelineId);
OtterContextLocator.autowire(otterAlarmHandler); // 注入一下spring资源
// 设置下slaveId,保证多个piplineId下重复引用时不重复
long slaveId = 10000;// 默认基数
if (canal.getCanalParameter().getSlaveId() != null) {
slaveId = canal.getCanalParameter().getSlaveId();
}
canal.getCanalParameter().setSlaveId(slaveId + pipelineId);
canal.getCanalParameter().setDdlIsolation(ddlSync);
canal.getCanalParameter().setFilterTableError(filterTableError);
CanalInstanceWithManager instance = new CanalInstanceWithManager(canal, filter) {
protected CanalHAController initHaController() {
HAMode haMode = parameters.getHaMode();
if (haMode.isMedia()) {
return new MediaHAController(parameters.getMediaGroup(),
parameters.getDbUsername(),
parameters.getDbPassword(),
parameters.getDefaultDatabaseName());
} else {
return super.initHaController();
}
}
protected void startEventParserInternal(CanalEventParser parser, boolean isGroup) {
super.startEventParserInternal(parser, isGroup);
if (eventParser instanceof MysqlEventParser) {
MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser;
CanalHAController haController = mysqlEventParser.getHaController();
if (haController instanceof MediaHAController) {
if (isGroup) {
throw new CanalException("not support group database use media HA");
}
((MediaHAController) haController).setCanalHASwitchable(mysqlEventParser);
}
if (!haController.isStart()) {
haController.start();
}
// 基于media的Ha,直接从tddl中获取数据库信息
if (haController instanceof MediaHAController) {
AuthenticationInfo authenticationInfo = ((MediaHAController) haController).getAvailableAuthenticationInfo();
((MysqlEventParser) eventParser).setMasterInfo(authenticationInfo);
}
}
}
};
instance.setAlarmHandler(otterAlarmHandler);
CanalEventSink eventSink = instance.getEventSink();
if (eventSink instanceof AbstractCanalEventSink) {
handler = new OtterDownStreamHandler();
handler.setPipelineId(pipelineId);
handler.setDetectingIntervalInSeconds(canal.getCanalParameter().getDetectingIntervalInSeconds());
OtterContextLocator.autowire(handler); // 注入一下spring资源
((AbstractCanalEventSink) eventSink).addHandler(handler, 0); // 添加到开头
handler.start();
}
return instance;
}
});
canalServer.start();
canalServer.start(destination);
this.clientIdentity = new ClientIdentity(destination, pipeline.getParameters().getMainstemClientId(), filter);
canalServer.subscribe(clientIdentity);// 发起一次订阅
running = true;
}