use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.
the class SelectProcessListener method processChanged.
public void processChanged(List<Long> processIds) {
super.processChanged(processIds);
// add by ljh at 2012-09-13,解决zookeeper ConnectionLoss问题
for (Long processId : processIds) {
if (!replyProcessIds.contains(processId)) {
logger.warn("process is not in order, please check processId:{}", processId);
addReply(processId);
}
}
try {
String path = StagePathUtils.getProcessRoot(getPipelineId());
// 根据并行度创建任务
int size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - processIds.size();
if (size > 0) {
// 创建一个节点
PermitMonitor permit = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
if (permit.isPermit() == false) {
// 如果非授权,则不做任何处理
return;
}
String mainStemPath = StagePathUtils.getMainStem(getPipelineId());
byte[] bytes = zookeeper.readData(mainStemPath, true);
if (bytes == null) {
return;
}
MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
if (eventData.getNid().equals(ArbitrateConfigUtils.getCurrentNid()) == false) {
// 如果非自己设置的mainStem,则不做任何处理
return;
}
synchronized (this) {
// 重新再取一次, dobble-check
List<String> currentProcesses = zookeeper.getChildren(path);
size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - currentProcesses.size();
if (size > 0) {
// 创建一个节点
ProcessNodeEventData nodeData = new ProcessNodeEventData();
// 标记为未使用
nodeData.setStatus(ProcessNodeEventData.Status.UNUSED);
nodeData.setMode(ArbitrateMode.RPC);
nodeData.setNid(ArbitrateConfigUtils.getCurrentNid());
byte[] nodeBytes = JsonUtils.marshalToByte(nodeData);
String processPath = zookeeper.create(path + "/", nodeBytes, CreateMode.PERSISTENT_SEQUENTIAL);
// 创建为顺序的节点
String processNode = StringUtils.substringAfterLast(processPath, "/");
// 添加到当前的process列表
Long processId = StagePathUtils.getProcessId(processNode);
addReply(processId);
}
}
}
} catch (ZkException e) {
// 出现异常后进行一次recovery,读取一下当前最新值,解决出现ConnectionLoss时create成功问题
recovery(getPipelineId());
logger.error("add process error!", e);
}
}
use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.
the class MainstemMonitor method check.
/**
* 检查当前的状态
*/
public boolean check() {
String path = StagePathUtils.getMainStem(getPipelineId());
try {
byte[] bytes = zookeeper.readData(path);
Long nid = ArbitrateConfigUtils.getCurrentNid();
MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
// 更新下为最新值
activeData = eventData;
// 检查下nid是否为自己
boolean result = nid.equals(eventData.getNid());
if (!result) {
logger.warn("mainstem is running in node[{}] , but not in node[{}]", eventData.getNid(), nid);
}
return result;
} catch (ZkNoNodeException e) {
logger.warn("mainstem is not run any in node");
return false;
} catch (ZkInterruptedException e) {
logger.warn("mainstem check is interrupt");
// 清除interrupt标记
Thread.interrupted();
return check();
} catch (ZkException e) {
logger.warn("mainstem check is failed");
return false;
}
}
use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.
the class MainstemMonitor method initMainstem.
public void initMainstem() {
if (isStop()) {
return;
}
PermitMonitor permitMonitor = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
ChannelStatus status = permitMonitor.getChannelPermit(true);
if (status.isStop()) {
// 如果已经关闭则退出
return;
}
Long nid = ArbitrateConfigUtils.getCurrentNid();
String path = StagePathUtils.getMainStem(getPipelineId());
MainStemEventData data = new MainStemEventData();
data.setStatus(MainStemEventData.Status.TAKEING);
data.setPipelineId(getPipelineId());
// 设置当前的nid
data.setNid(nid);
// 序列化
byte[] bytes = JsonUtils.marshalToByte(data);
try {
mutex.set(false);
zookeeper.create(path, bytes, CreateMode.EPHEMERAL);
activeData = data;
// 触发一下事件
processActiveEnter();
mutex.set(true);
} catch (ZkNodeExistsException e) {
bytes = zookeeper.readData(path, true);
if (bytes == null) {
// 如果不存在节点,立即尝试一次
initMainstem();
} else {
activeData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
if (nid.equals(activeData.getNid())) {
// reload时会重复创建,如果是自己就触发一下
mutex.set(true);
}
}
}
}
use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.
the class PermitMonitor method initOppositeMainStemStatus.
private void initOppositeMainStemStatus(byte[] bytes) {
MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
MainStemEventData.Status newStatus = eventData.getStatus();
if (logger.isDebugEnabled()) {
logger.debug("pipeline[{}] new oppositeMainStemStatus is [{}]", getPipelineId(), newStatus);
}
synchronized (this) {
if (!oppositeMainStemStatus.equals(newStatus)) {
oppositeMainStemStatus = newStatus;
permitSem();
}
}
}
use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.
the class OtterDownStreamHandler method startDetecting.
private void startDetecting() {
// 直接发送已追上的状态,保持和eromanga兼容处理
MainStemEventData mainStemData = new MainStemEventData();
mainStemData.setPipelineId(pipelineId);
mainStemData.setStatus(MainStemEventData.Status.OVERTAKE);
arbitrateEventService.mainStemEvent().single(mainStemData);
// 启动异步线程定时监控,一定会有数据过来
String schedulerName = String.format("pipelineId = %s , CanalDetecting", String.valueOf(pipelineId));
scheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory(schedulerName));
future = scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
MDC.put(OtterConstants.splitPipelineLogFileKey, String.valueOf(pipelineId));
// (因为会有心跳包数据,理论上时间间隔会小于一定值)
if (isDelayed(System.currentTimeMillis(), lastEventExecuteTime)) {
notifyFailed();
} else {
notifySuccessed();
}
} catch (Exception e) {
logger.error("heartbeat check failed!", e);
} finally {
MDC.remove(OtterConstants.splitPipelineLogFileKey);
}
}
}, detectingIntervalInSeconds, detectingIntervalInSeconds, TimeUnit.SECONDS);
}
Aggregations