Merge pull request #374 from alexandnpu/feature/avoid_use_multiline_exception_directly

try to avoid use multiline exception directly
This commit is contained in:
agapple
2017-09-23 11:11:27 +08:00
committed by GitHub
9 changed files with 57 additions and 65 deletions

View File

@@ -3,7 +3,6 @@ package com.alibaba.otter.canal.client.impl;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -107,9 +106,10 @@ public class ClusterCanalConnector implements CanalConnector {
this.filter = filter; this.filter = filter;
return; return;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when subscribing from server:{}\n{}", logger.warn(String.format(
currentConnector != null ? currentConnector.getAddress() : "null", "something goes wrong when subscribing from server: %s",
ExceptionUtils.getFullStackTrace(t)); currentConnector != null ? currentConnector.getAddress() : "null"),
t);
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -126,9 +126,8 @@ public class ClusterCanalConnector implements CanalConnector {
currentConnector.unsubscribe(); currentConnector.unsubscribe();
return; return;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when unsubscribing from server:{}\n{}", logger.warn(String.format("something goes wrong when unsubscribing from server:%s",
currentConnector != null ? currentConnector.getAddress() : "null", currentConnector != null ? currentConnector.getAddress() : "null"), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -144,9 +143,8 @@ public class ClusterCanalConnector implements CanalConnector {
Message msg = currentConnector.get(batchSize); Message msg = currentConnector.get(batchSize);
return msg; return msg;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when getting data from server:{}\n{}", logger.warn(String.format("something goes wrong when getting data from server:%s",
currentConnector != null ? currentConnector.getAddress() : "null", currentConnector != null ? currentConnector.getAddress() : "null"), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -162,9 +160,8 @@ public class ClusterCanalConnector implements CanalConnector {
Message msg = currentConnector.get(batchSize, timeout, unit); Message msg = currentConnector.get(batchSize, timeout, unit);
return msg; return msg;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when getting data from server:{}\n{}", logger.warn(String.format("something goes wrong when getting data from server:%s",
currentConnector != null ? currentConnector.getAddress() : "null", currentConnector != null ? currentConnector.getAddress() : "null"), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -180,9 +177,8 @@ public class ClusterCanalConnector implements CanalConnector {
Message msg = currentConnector.getWithoutAck(batchSize); Message msg = currentConnector.getWithoutAck(batchSize);
return msg; return msg;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when getWithoutAck data from server:{}\n{}", logger.warn(String.format("something goes wrong when getWithoutAck data from server:%s",
currentConnector.getAddress(), currentConnector.getAddress()), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -198,9 +194,8 @@ public class ClusterCanalConnector implements CanalConnector {
Message msg = currentConnector.getWithoutAck(batchSize, timeout, unit); Message msg = currentConnector.getWithoutAck(batchSize, timeout, unit);
return msg; return msg;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when getWithoutAck data from server:{}\n{}", logger.warn(String.format("something goes wrong when getWithoutAck data from server:%s",
currentConnector.getAddress(), currentConnector.getAddress()), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -216,9 +211,8 @@ public class ClusterCanalConnector implements CanalConnector {
currentConnector.rollback(batchId); currentConnector.rollback(batchId);
return; return;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when rollbacking data from server:{}\n{}", logger.warn(String.format("something goes wrong when rollbacking data from server:%s",
currentConnector.getAddress(), currentConnector.getAddress()), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -234,9 +228,8 @@ public class ClusterCanalConnector implements CanalConnector {
currentConnector.rollback(); currentConnector.rollback();
return; return;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when rollbacking data from server:{}\n{}", logger.warn(String.format("something goes wrong when rollbacking data from server:%s",
currentConnector.getAddress(), currentConnector.getAddress()), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");
@@ -253,9 +246,8 @@ public class ClusterCanalConnector implements CanalConnector {
currentConnector.ack(batchId); currentConnector.ack(batchId);
return; return;
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("something goes wrong when acking data from server:{}\n{}", logger.warn(String.format("something goes wrong when acking data from server:%s",
currentConnector.getAddress(), currentConnector.getAddress()), t);
ExceptionUtils.getFullStackTrace(t));
times++; times++;
restart(); restart();
logger.info("restart the connector for next round retry."); logger.info("restart the connector for next round retry.");

View File

@@ -4,7 +4,6 @@ import java.io.FileInputStream;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -21,6 +20,10 @@ public class CanalLauncher {
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
try { try {
logger.info("## set default uncaught exception handler");
setGlobalUncaughtExceptionHandler();
logger.info("## load canal configurations");
String conf = System.getProperty("canal.conf", "classpath:canal.properties"); String conf = System.getProperty("canal.conf", "classpath:canal.properties");
Properties properties = new Properties(); Properties properties = new Properties();
if (conf.startsWith(CLASSPATH_URL_PREFIX)) { if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
@@ -41,8 +44,7 @@ public class CanalLauncher {
logger.info("## stop the canal server"); logger.info("## stop the canal server");
controller.stop(); controller.stop();
} catch (Throwable e) { } catch (Throwable e) {
logger.warn("##something goes wrong when stopping canal Server:\n{}", logger.warn("##something goes wrong when stopping canal Server:", e);
ExceptionUtils.getFullStackTrace(e));
} finally { } finally {
logger.info("## canal server is down."); logger.info("## canal server is down.");
} }
@@ -50,9 +52,17 @@ public class CanalLauncher {
}); });
} catch (Throwable e) { } catch (Throwable e) {
logger.error("## Something goes wrong when starting up the canal Server:\n{}", logger.error("## Something goes wrong when starting up the canal Server:", e);
ExceptionUtils.getFullStackTrace(e));
System.exit(0); System.exit(0);
} }
} }
private static void setGlobalUncaughtExceptionHandler() {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.error("UnCaughtException", e);
}
});
}
} }

View File

@@ -13,7 +13,6 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -186,7 +185,7 @@ public class SpringInstanceConfigMonitor extends AbstractCanalLifeCycle implemen
logger.info("auto notify start {} successful.", destination); logger.info("auto notify start {} successful.", destination);
} catch (Throwable e) { } catch (Throwable e) {
logger.error("scan add found[{}] but start failed", destination, ExceptionUtils.getFullStackTrace(e)); logger.error(String.format("scan add found[%s] but start failed", destination), e);
} }
} }
@@ -197,7 +196,7 @@ public class SpringInstanceConfigMonitor extends AbstractCanalLifeCycle implemen
lastFiles.remove(destination); lastFiles.remove(destination);
logger.info("auto notify stop {} successful.", destination); logger.info("auto notify stop {} successful.", destination);
} catch (Throwable e) { } catch (Throwable e) {
logger.error("scan delete found[{}] but stop failed", destination, ExceptionUtils.getFullStackTrace(e)); logger.error(String.format("scan delete found[%s] but stop failed", destination), e);
actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除 actions.put(destination, action);// 再重新加回去,下一次scan时再执行删除
} }
} }
@@ -209,9 +208,8 @@ public class SpringInstanceConfigMonitor extends AbstractCanalLifeCycle implemen
action.reload(destination); action.reload(destination);
logger.info("auto notify reload {} successful.", destination); logger.info("auto notify reload {} successful.", destination);
} catch (Throwable e) { } catch (Throwable e) {
logger.error("scan reload found[{}] but reload failed", logger.error(String.format("scan reload found[%s] but reload failed",
destination, destination), e);
ExceptionUtils.getFullStackTrace(e));
} }
} }
} }

View File

@@ -4,7 +4,6 @@ import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -69,7 +68,7 @@ public class MysqlConnector {
negotiate(channel); negotiate(channel);
} catch (Exception e) { } catch (Exception e) {
disconnect(); disconnect();
throw new IOException("connect " + this.address + " failure:" + ExceptionUtils.getStackTrace(e)); throw new IOException("connect " + this.address + " failure", e);
} }
} else { } else {
logger.error("the channel can't be connected twice."); logger.error("the channel can't be connected twice.");
@@ -89,7 +88,7 @@ public class MysqlConnector {
} }
logger.info("disConnect MysqlConnection to {}...", address); logger.info("disConnect MysqlConnection to {}...", address);
} catch (Exception e) { } catch (Exception e) {
throw new IOException("disconnect " + this.address + " failure:" + ExceptionUtils.getStackTrace(e)); throw new IOException("disconnect " + this.address + " failure", e);
} }
// 执行一次quit // 执行一次quit
@@ -101,7 +100,7 @@ public class MysqlConnector {
MysqlUpdateExecutor executor = new MysqlUpdateExecutor(connector); MysqlUpdateExecutor executor = new MysqlUpdateExecutor(connector);
executor.update("KILL CONNECTION " + connectionId); executor.update("KILL CONNECTION " + connectionId);
} catch (Exception e) { } catch (Exception e) {
throw new IOException("KILL DUMP " + connectionId + " failure:" + ExceptionUtils.getStackTrace(e)); throw new IOException("KILL DUMP " + connectionId + " failure", e);
} finally { } finally {
if (connector != null) { if (connector != null) {
connector.disconnect(); connector.disconnect();

View File

@@ -1,7 +1,5 @@
package com.alibaba.otter.canal.example; package com.alibaba.otter.canal.example;
import org.apache.commons.lang.exception.ExceptionUtils;
import com.alibaba.otter.canal.client.CanalConnector; import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors; import com.alibaba.otter.canal.client.CanalConnectors;
@@ -41,7 +39,7 @@ public class ClusterCanalClientTest extends AbstractCanalClientTest {
logger.info("## stop the canal client"); logger.info("## stop the canal client");
clientTest.stop(); clientTest.stop();
} catch (Throwable e) { } catch (Throwable e) {
logger.warn("##something goes wrong when stopping canal:\n{}", ExceptionUtils.getFullStackTrace(e)); logger.warn("##something goes wrong when stopping canal:", e);
} finally { } finally {
logger.info("## canal client is down."); logger.info("## canal client is down.");
} }

View File

@@ -2,8 +2,6 @@ package com.alibaba.otter.canal.example;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import org.apache.commons.lang.exception.ExceptionUtils;
import com.alibaba.otter.canal.client.CanalConnector; import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors; import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.common.utils.AddressUtils; import com.alibaba.otter.canal.common.utils.AddressUtils;
@@ -39,7 +37,7 @@ public class SimpleCanalClientTest extends AbstractCanalClientTest {
logger.info("## stop the canal client"); logger.info("## stop the canal client");
clientTest.stop(); clientTest.stop();
} catch (Throwable e) { } catch (Throwable e) {
logger.warn("##something goes wrong when stopping canal:\n{}", ExceptionUtils.getFullStackTrace(e)); logger.warn("##something goes wrong when stopping canal:", e);
} finally { } finally {
logger.info("## canal client is down."); logger.info("## canal client is down.");
} }

View File

@@ -430,7 +430,7 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
} }
} catch (Throwable e) { } catch (Throwable e) {
logger.warn("heartBeat run failed " + ExceptionUtils.getStackTrace(e)); logger.warn("heartBeat run failed ", e);
} }
} }

View File

@@ -6,7 +6,6 @@ import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -167,25 +166,25 @@ public class MysqlConnection implements ErosaConnection {
try { try {
update("set wait_timeout=9999999"); update("set wait_timeout=9999999");
} catch (Exception e) { } catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update wait_timeout failed", e);
} }
try { try {
update("set net_write_timeout=1800"); update("set net_write_timeout=1800");
} catch (Exception e) { } catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update net_write_timeout failed", e);
} }
try { try {
update("set net_read_timeout=1800"); update("set net_read_timeout=1800");
} catch (Exception e) { } catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update net_read_timeout failed", e);
} }
try { try {
// 设置服务端返回结果时不做编码转化,直接按照数据库的二进制编码进行发送,由客户端自己根据需求进行编码转化 // 设置服务端返回结果时不做编码转化,直接按照数据库的二进制编码进行发送,由客户端自己根据需求进行编码转化
update("set names 'binary'"); update("set names 'binary'");
} catch (Exception e) { } catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update names failed", e);
} }
try { try {
@@ -195,7 +194,7 @@ public class MysqlConnection implements ErosaConnection {
// 但也不能乱设置,需要和mysql server的checksum配置一致,不然RotateLogEvent会出现乱码 // 但也不能乱设置,需要和mysql server的checksum配置一致,不然RotateLogEvent会出现乱码
update("set @master_binlog_checksum= '@@global.binlog_checksum'"); update("set @master_binlog_checksum= '@@global.binlog_checksum'");
} catch (Exception e) { } catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update master_binlog_checksum failed", e);
} }
try { try {
@@ -204,7 +203,7 @@ public class MysqlConnection implements ErosaConnection {
update("set @slave_uuid=uuid()"); update("set @slave_uuid=uuid()");
} catch (Exception e) { } catch (Exception e) {
if (!StringUtils.contains(e.getMessage(), "Unknown system variable")) { if (!StringUtils.contains(e.getMessage(), "Unknown system variable")) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update slave_uuid failed", e);
} }
} }
@@ -212,7 +211,7 @@ public class MysqlConnection implements ErosaConnection {
// mariadb针对特殊的类型,需要设置session变量 // mariadb针对特殊的类型,需要设置session变量
update("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'"); update("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'");
} catch (Exception e) { } catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e)); logger.warn("update mariadb_slave_capability failed", e);
} }
} }

View File

@@ -10,7 +10,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import com.alibaba.otter.canal.common.utils.JsonUtils; import com.alibaba.otter.canal.common.utils.JsonUtils;
@@ -237,19 +236,19 @@ public class MysqlEventParser extends AbstractMysqlEventParser implements CanalE
((HeartBeatCallback) haController).onFailed(e); ((HeartBeatCallback) haController).onFailed(e);
} }
reconnect = true; reconnect = true;
logger.warn("connect failed by " + ExceptionUtils.getStackTrace(e)); logger.warn("connect failed by ", e);
} catch (IOException e) { } catch (IOException e) {
if (haController != null && haController instanceof HeartBeatCallback) { if (haController != null && haController instanceof HeartBeatCallback) {
((HeartBeatCallback) haController).onFailed(e); ((HeartBeatCallback) haController).onFailed(e);
} }
reconnect = true; reconnect = true;
logger.warn("connect failed by " + ExceptionUtils.getStackTrace(e)); logger.warn("connect failed by ", e);
} catch (Throwable e) { } catch (Throwable e) {
if (haController != null && haController instanceof HeartBeatCallback) { if (haController != null && haController instanceof HeartBeatCallback) {
((HeartBeatCallback) haController).onFailed(e); ((HeartBeatCallback) haController).onFailed(e);
} }
reconnect = true; reconnect = true;
logger.warn("connect failed by " + ExceptionUtils.getStackTrace(e)); logger.warn("connect failed by ", e);
} }
} }
@@ -549,9 +548,8 @@ public class MysqlEventParser extends AbstractMysqlEventParser implements CanalE
return entryPosition; return entryPosition;
} }
} catch (Exception e) { } catch (Exception e) {
logger.warn("the binlogfile:{} doesn't exist, to continue to search the next binlogfile , caused by {}", logger.warn(String.format("the binlogfile:%s doesn't exist, to continue to search the next binlogfile , caused by",
startSearchBinlogFile, startSearchBinlogFile), e);
ExceptionUtils.getFullStackTrace(e));
int binlogSeqNum = Integer.parseInt(startSearchBinlogFile.substring(startSearchBinlogFile.indexOf(".") + 1)); int binlogSeqNum = Integer.parseInt(startSearchBinlogFile.substring(startSearchBinlogFile.indexOf(".") + 1));
if (binlogSeqNum <= 1) { if (binlogSeqNum <= 1) {
logger.warn("Didn't find the corresponding binlog files"); logger.warn("Didn't find the corresponding binlog files");