mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
canal server metrics refactor.
This commit is contained in:
@@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
*/
|
||||
public class CanalServerWithNettyProfiler {
|
||||
|
||||
private static final ClientInstanceProfilerFactory DISABLED = new DefaultClientInstanceProfilerFactory();
|
||||
public static final ClientInstanceProfilerFactory DISABLED = new DefaultClientInstanceProfilerFactory();
|
||||
private volatile ClientInstanceProfilerFactory factory;
|
||||
private final ConcurrentMap<String, ClientInstanceProfiler> cliPfs;
|
||||
private final CanalServerWithEmbedded server;
|
||||
@@ -29,34 +29,47 @@ public class CanalServerWithNettyProfiler {
|
||||
return SingletonHolder.SINGLETON;
|
||||
}
|
||||
|
||||
public void profiling(String dest, ClientRequestResult result) {
|
||||
public void profiling(String destination, ClientRequestResult result) {
|
||||
if (isDisabled()) {
|
||||
return;
|
||||
}
|
||||
ClientInstanceProfiler profiler = tryGet(dest);
|
||||
ClientInstanceProfiler profiler = cliPfs.get(destination);
|
||||
if (profiler != null) {
|
||||
profiler.profiling(result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove instance profiler for specified instance.
|
||||
* Only accepted while instance is not running.
|
||||
* @param dest canal instance destination
|
||||
*/
|
||||
public void remove(String dest) throws IllegalStateException {
|
||||
public void start(String destination) {
|
||||
if (isDisabled()) {
|
||||
return;
|
||||
}
|
||||
synchronized (cliPfs) {
|
||||
if (server.isStart(dest)) {
|
||||
throw new IllegalStateException("Instance profiler should not be removed while running.");
|
||||
}
|
||||
cliPfs.remove(dest);
|
||||
if (server.isStart(destination)) {
|
||||
throw new IllegalStateException("Instance profiler should not be start while running.");
|
||||
}
|
||||
ClientInstanceProfiler profiler = factory.create(destination);
|
||||
profiler.start();
|
||||
cliPfs.put(destination, profiler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove instance profiler for specified instance.
|
||||
* Only accepted while instance is not running.
|
||||
* @param destination canal instance destination
|
||||
*/
|
||||
public void stop(String destination) {
|
||||
if (isDisabled()) {
|
||||
return;
|
||||
}
|
||||
if (server.isStart(destination)) {
|
||||
throw new IllegalStateException("Instance profiler should not be stop while running.");
|
||||
}
|
||||
ClientInstanceProfiler profiler = cliPfs.remove(destination);
|
||||
if (profiler != null && profiler.isStart()) {
|
||||
profiler.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void setFactory(ClientInstanceProfilerFactory factory) {
|
||||
public void setInstanceProfilerFactory(ClientInstanceProfilerFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@@ -64,15 +77,18 @@ public class CanalServerWithNettyProfiler {
|
||||
return factory == DISABLED || factory == null;
|
||||
}
|
||||
|
||||
private ClientInstanceProfiler tryGet(String dest) {
|
||||
private ClientInstanceProfiler tryGet(String destination) {
|
||||
//try fast get
|
||||
ClientInstanceProfiler profiler = cliPfs.get(dest);
|
||||
ClientInstanceProfiler profiler = cliPfs.get(destination);
|
||||
if (profiler == null) {
|
||||
synchronized (cliPfs) {
|
||||
if (server.isStart(dest)) {
|
||||
if (server.isStart(destination)) {
|
||||
// avoid overwriting
|
||||
cliPfs.putIfAbsent(dest, factory.create());
|
||||
profiler = cliPfs.get(dest);
|
||||
cliPfs.putIfAbsent(destination, factory.create(destination));
|
||||
profiler = cliPfs.get(destination);
|
||||
if (!profiler.isStart()) {
|
||||
profiler.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,7 +97,7 @@ public class CanalServerWithNettyProfiler {
|
||||
|
||||
private static class DefaultClientInstanceProfilerFactory implements ClientInstanceProfilerFactory {
|
||||
@Override
|
||||
public ClientInstanceProfiler create() {
|
||||
public ClientInstanceProfiler create(String destination) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.alibaba.otter.canal.server.netty;
|
||||
|
||||
import com.alibaba.otter.canal.common.CanalLifeCycle;
|
||||
import com.alibaba.otter.canal.server.netty.listener.ChannelFutureAggregator;
|
||||
|
||||
/**
|
||||
* @author Chuanyi Li
|
||||
*/
|
||||
public interface ClientInstanceProfiler {
|
||||
public interface ClientInstanceProfiler extends CanalLifeCycle {
|
||||
|
||||
String getDestination();
|
||||
|
||||
|
||||
@@ -5,6 +5,6 @@ package com.alibaba.otter.canal.server.netty;
|
||||
*/
|
||||
public interface ClientInstanceProfilerFactory {
|
||||
|
||||
ClientInstanceProfiler create();
|
||||
ClientInstanceProfiler create(String destination);
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.alibaba.otter.canal.server.netty.handler;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.otter.canal.server.netty.listener.ChannelFutureAggregator;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.exception.ExceptionUtils;
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
|
||||
@@ -15,18 +15,27 @@ public class ChannelFutureAggregator implements ChannelFutureListener {
|
||||
|
||||
private ClientRequestResult result;
|
||||
|
||||
public ChannelFutureAggregator(String dest, GeneratedMessage request, CanalPacket.PacketType type, int amount, long latency) {
|
||||
this(dest, request, type, amount, latency, 0);
|
||||
public ChannelFutureAggregator(String destination, GeneratedMessage request, CanalPacket.PacketType type, int amount, long latency, boolean empty) {
|
||||
this(destination, request, type, amount, latency, empty, (short) 0);
|
||||
}
|
||||
|
||||
private ChannelFutureAggregator(String dest, GeneratedMessage request, CanalPacket.PacketType type, int amount, long latency, int errorCode) {
|
||||
public ChannelFutureAggregator(String destination, GeneratedMessage request, CanalPacket.PacketType type, int amount, long latency) {
|
||||
this(destination, request, type, amount, latency, false, (short) 0);
|
||||
}
|
||||
|
||||
public ChannelFutureAggregator(String destination, GeneratedMessage request, CanalPacket.PacketType type, int amount, long latency, short errorCode) {
|
||||
this(destination, request, type, amount, latency, false, errorCode);
|
||||
}
|
||||
|
||||
private ChannelFutureAggregator(String destination, GeneratedMessage request, CanalPacket.PacketType type, int amount, long latency, boolean empty, short errorCode) {
|
||||
this.result = new ClientRequestResult.Builder()
|
||||
.dest(dest)
|
||||
.destination(destination)
|
||||
.type(type)
|
||||
.request(request)
|
||||
.amount(amount)
|
||||
.latency(latency)
|
||||
.errorCode(errorCode)
|
||||
.empty(empty)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -36,7 +45,7 @@ public class ChannelFutureAggregator implements ChannelFutureListener {
|
||||
if (future.getCause() != null) {
|
||||
result.channelError = future.getCause();
|
||||
}
|
||||
profiler().profiling(result.dest, result);
|
||||
profiler().profiling(result.destination, result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,39 +53,42 @@ public class ChannelFutureAggregator implements ChannelFutureListener {
|
||||
*/
|
||||
public static class ClientRequestResult {
|
||||
|
||||
private String dest;
|
||||
private String destination;
|
||||
private CanalPacket.PacketType type;
|
||||
private GeneratedMessage request;
|
||||
private int amount;
|
||||
private long latency;
|
||||
private int errorCode;
|
||||
private Throwable channelError;
|
||||
private int amount;
|
||||
private long latency;
|
||||
private short errorCode;
|
||||
private boolean empty;
|
||||
private Throwable channelError;
|
||||
|
||||
private ClientRequestResult() {}
|
||||
|
||||
private ClientRequestResult(Builder builder) {
|
||||
this.dest = Preconditions.checkNotNull(builder.dest);
|
||||
this.destination = Preconditions.checkNotNull(builder.destination);
|
||||
this.type = Preconditions.checkNotNull(builder.type);
|
||||
this.request = Preconditions.checkNotNull(builder.request);
|
||||
this.amount = builder.amount;
|
||||
this.latency = builder.latency;
|
||||
this.errorCode = builder.errorCode;
|
||||
this.empty = builder.empty;
|
||||
this.channelError = builder.channelError;
|
||||
}
|
||||
|
||||
// auto-generated
|
||||
public static class Builder {
|
||||
|
||||
private String dest;
|
||||
private String destination;
|
||||
private CanalPacket.PacketType type;
|
||||
private GeneratedMessage request;
|
||||
private int amount;
|
||||
private long latency;
|
||||
private int errorCode;
|
||||
private short errorCode;
|
||||
private boolean empty;
|
||||
private Throwable channelError;
|
||||
|
||||
Builder dest(String dest) {
|
||||
this.dest = dest;
|
||||
Builder destination(String destination) {
|
||||
this.destination = destination;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -100,23 +112,29 @@ public class ChannelFutureAggregator implements ChannelFutureListener {
|
||||
return this;
|
||||
}
|
||||
|
||||
Builder errorCode(int errorCode) {
|
||||
Builder errorCode(short errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
Builder empty(boolean empty) {
|
||||
this.empty = empty;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder channelError(Throwable channelError) {
|
||||
this.channelError = channelError;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fromPrototype(ClientRequestResult prototype) {
|
||||
dest = prototype.dest;
|
||||
destination = prototype.destination;
|
||||
type = prototype.type;
|
||||
request = prototype.request;
|
||||
amount = prototype.amount;
|
||||
latency = prototype.latency;
|
||||
errorCode = prototype.errorCode;
|
||||
empty = prototype.empty;
|
||||
channelError = prototype.channelError;
|
||||
return this;
|
||||
}
|
||||
@@ -126,8 +144,8 @@ public class ChannelFutureAggregator implements ChannelFutureListener {
|
||||
}
|
||||
}
|
||||
// getters
|
||||
public String getDest() {
|
||||
return dest;
|
||||
public String getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
public CanalPacket.PacketType getType() {
|
||||
@@ -146,10 +164,14 @@ public class ChannelFutureAggregator implements ChannelFutureListener {
|
||||
return latency;
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
public short getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public boolean getEmpty() {
|
||||
return empty;
|
||||
}
|
||||
|
||||
public Throwable getChannelError() {
|
||||
return channelError;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user