com.taobao.drc.clusterclient.impl.DefaultClusterClientImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of consumer-core Show documentation
Show all versions of consumer-core Show documentation
The java consumer core component for Data Transmission Service
package com.taobao.drc.clusterclient.impl;
import com.taobao.drc.clusterclient.*;
import com.taobao.drc.clusterclient.coordinator.CoordinatorManager;
import com.taobao.drc.clusterclient.coordinator.impl.DefaultCoordinatorFactory;
import com.taobao.drc.clusterclient.partition.BaseCheckpoint;
import com.taobao.drc.clusterclient.partition.IPartition;
import com.taobao.drc.clusterclient.partition.PartitionStateChangeListener;
import com.taobao.drc.clusterclient.util.NetworkUtils;
import com.aliyun.drc.client.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/**
* @author yangyang
* @since 2017/7/17
*/
public class DefaultClusterClientImpl,
L extends MessageListener,
F extends PartitionClientFactory> {
private static final Logger logger = LoggerFactory.getLogger(DefaultClusterClientImpl.class);
private static final String DEFAULT_LOCAL_IP = "127.0.0.1";
private final List> notifiers;
private final DefaultPartitionManager> partitionManager;
private boolean started = false;
private boolean stopped = false;
private final C context;
public DefaultClusterClientImpl(C context,
PartitionClientFactory> partitionClientFactory,
List messageListeners,
List partitionStateChangeListeners,
String version,Listener clientCtrListener) {
this(context, partitionClientFactory, messageListeners, partitionStateChangeListeners, version,clientCtrListener,
CoordinatorManager.getInstance(true, new DefaultCoordinatorFactory(), context.getThreadNamePrefix(),
context.getMaxConsumerNumPerCoordinator(), context.getMaxIOThreadNumPerCoordinator()));
}
public DefaultClusterClientImpl(C context, PartitionClientFactory> partitionClientFactory,
List messageListeners, List partitionStateChangeListeners, String version,
Listener clientCtrListener,
CoordinatorManager coordinatorManager) {
if (messageListeners.isEmpty()) {
throw new IllegalArgumentException("DefaultClusterClientImpl No listeners!");
}
this.context = context;
this.notifiers = new ArrayList>();
/**
* IdentityHashMap:只有在key完全相等(同一个引用),才会覆盖
*/
Map listenerMap = new IdentityHashMap();
for (L listener : messageListeners) {
if (listenerMap.put(listener, true) == null) {
notifiers.add(new MessageNotifier(listener, context.getMaxSizeOfNotifyMessageQueue(),
context.getThreadNamePrefix(), context.isNotifierFair()));
} else {
logger.warn("Listener [{}] has been added multiple times", listener);
}
}
logger.debug("DefaultClusterClientImpl:messageListeners num:[{}]", messageListeners.size());
this.partitionManager = new DefaultPartitionManager>(coordinatorManager,
partitionClientFactory, context, version, getLocalIp(), notifiers, partitionStateChangeListeners,clientCtrListener);
}
private String getLocalIp() {
try {
return NetworkUtils.getLocalIp();
} catch (Exception e) {
logger.warn("Failed to get local ip, fallback to [{}]", DEFAULT_LOCAL_IP, e);
return DEFAULT_LOCAL_IP;
}
}
public void start() throws ExecutionException, InterruptedException {
if (!started) {
for (MessageNotifier notifier : this.notifiers) {
notifier.start();
}
this.partitionManager.start();
started = true;
logger.info("DefaultClusterClientImpl Started cluster client(partitionManager) guid:{},group:{}",context.getAppGuid(), context.getAppGroup());
} else {
logger.warn("Cluster client for [{}][{}] has already been started", context.getAppGuid(),context.getAppGroup());
}
}
/*获取统计信息*/
public String getMessage(boolean withMetrics){
return partitionManager.getMessage(withMetrics);
}
public void stop() throws InterruptedException {
if (!started) {
logger.warn("Cluster client for [{}][{}] cannot be stopped before started", context.getAppGuid(),
context.getAppGroup());
return;
}
if (!stopped) {
partitionManager.stop();
for (MessageNotifier notifier : this.notifiers) {
notifier.shutdown();
}
partitionManager.waitForStop(60);
stopped = true;
logger.info("Stopped cluster client for [{}][{}]", context.getAppGuid(), context.getAppGroup());
} else {
partitionManager.waitForStop(60);
logger.warn("Cluster client for [{}][{}] has already been stopped", context.getAppGuid(),
context.getAppGroup());
}
}
public void waitForStop(long timeLimitInSec) throws InterruptedException {
if (!stopped) {
logger.warn("Cluster client for [{}][{}] has not yet been stopped", context.getAppGuid(),
context.getAppGroup());
return;
}
partitionManager.waitForStop(timeLimitInSec);
}
public boolean isStopping(){
if(!stopped){
return partitionManager.isStopping();
}
return false;
}
}