use of com.alibaba.otter.shared.arbitrate.model.EtlEventData in project otter by alibaba.
the class SelectZooKeeperArbitrateEvent method await.
// private TerminArbitrateEvent terminEvent;
/**
* <pre>
* 算法:
* 1. 检查当前的Permit,阻塞等待其授权(解决Channel的pause状态处理)
* 2. 开始阻塞获取符合条件的processId,创建空的EventData对象,添加next node信息后直接返回
* </pre>
*/
public EtlEventData await(Long pipelineId) throws InterruptedException {
Assert.notNull(pipelineId);
PermitMonitor permitMonitor = ArbitrateFactory.getInstance(pipelineId, PermitMonitor.class);
// 阻塞等待授权
permitMonitor.waitForPermit();
SelectStageListener selectStageListener = ArbitrateFactory.getInstance(pipelineId, SelectStageListener.class);
// 符合条件的processId
Long processId = selectStageListener.waitForProcess();
ChannelStatus status = permitMonitor.getChannelPermit();
if (status.isStart()) {
try {
EtlEventData eventData = new EtlEventData();
eventData.setPipelineId(pipelineId);
eventData.setProcessId(processId);
// 返回当前时间
eventData.setStartTime(new Date().getTime());
// 获取下一个处理节点信息
Node node = LoadBalanceFactory.getNextExtractNode(pipelineId);
if (node == null) {
// terminEvent.single(termin);
throw new ArbitrateException("Select_single", "no next node");
} else {
eventData.setNextNid(node.getId());
// 标记为已使用
markUsed(eventData);
// 只有这一条路返回
return eventData;
}
} catch (ZkNoNodeException e) {
logger.error("pipeline[{}] processId[{}] is invalid , retry again", pipelineId, processId);
// /出现节点不存在,说明出现了error情况,递归调用重新获取一次
return await(pipelineId);
} catch (ZkException e) {
throw new ArbitrateException("Select_await", e.getMessage(), e);
}
} else {
logger.warn("pipelineId[{}] select ignore processId[{}] by status[{}]", new Object[] { pipelineId, processId, status });
// add by ljh 2013-02-01
// 遇到一个bug:
// a. 某台机器发起了一个RESTART指令,然后开始删除process列表
// b. 此时另一个台机器(select工作节点),并没有收到PAUSE的推送,导致还会再创建一个process节点
// c. 后续收到PAUSE指令后,丢弃了processId,就出现了unused的processId
// 这里删除了,要考虑一个问题,就是和restart指令在并行删除同一个processId时的并发考虑,目前来看没问题
String path = StagePathUtils.getProcess(pipelineId, processId);
// 忽略删除失败
zookeeper.delete(path);
// 递归调用
return await(pipelineId);
}
}
use of com.alibaba.otter.shared.arbitrate.model.EtlEventData in project otter by alibaba.
the class TransformZooKeeperArbitrateEvent method await.
/**
* <pre>
* 算法:
* 1. 检查当前的Permit,阻塞等待其授权(解决Channel的pause状态处理)
* 2. 开始阻塞获取符合条件的processId
* 3. 检查当前的即时Permit状态 (在阻塞获取processId过程会出现一些error信号,process节点会被删除)
* 4. 获取Select传递的EventData数据,添加next node信息后直接返回
* </pre>
*
* @return
*/
public EtlEventData await(Long pipelineId) throws InterruptedException {
Assert.notNull(pipelineId);
PermitMonitor permitMonitor = ArbitrateFactory.getInstance(pipelineId, PermitMonitor.class);
// 阻塞等待授权
permitMonitor.waitForPermit();
TransformStageListener transformStageListener = ArbitrateFactory.getInstance(pipelineId, TransformStageListener.class);
// 符合条件的processId
Long processId = transformStageListener.waitForProcess();
ChannelStatus status = permitMonitor.getChannelPermit();
if (status.isStart()) {
// 即时查询一下当前的状态,状态随时可能会变
// 根据pipelineId+processId构造对应的path
String path = StagePathUtils.getExtractStage(pipelineId, processId);
try {
byte[] data = zookeeper.readData(path);
EtlEventData eventData = JsonUtils.unmarshalFromByte(data, EtlEventData.class);
// 下一个节点信息即为自己
eventData.setNextNid(ArbitrateConfigUtils.getCurrentNid());
// 只有这一条路返回
return eventData;
} catch (ZkNoNodeException e) {
logger.error("pipeline[{}] processId[{}] is invalid , retry again", pipelineId, processId);
// /出现节点不存在,说明出现了error情况,递归调用重新获取一次
return await(pipelineId);
} catch (ZkException e) {
throw new ArbitrateException("transform_await", e.getMessage(), e);
}
} else {
logger.info("pipelineId[{}] transform ignore processId[{}] by status[{}]", new Object[] { pipelineId, processId, status });
// 递归调用
return await(pipelineId);
}
}
use of com.alibaba.otter.shared.arbitrate.model.EtlEventData in project otter by alibaba.
the class ExtractStageListener method stageChannged.
public void stageChannged(Long processId, List<String> stageNodes) {
try {
// 1. 根据pipelineId+processId构造对应的path
String path = StagePathUtils.getProcess(getPipelineId(), processId);
// 2.1 判断是否存在了current节点
if (stageNodes.contains(currentNode)) {
if (replyProcessIds.remove(processId)) {
if (logger.isDebugEnabled()) {
logger.debug("## remove reply id [{}]", processId);
}
}
// 不需要监听了
return;
}
if (replyProcessIds.contains(processId)) {
// 避免重复处理
return;
}
// 2.2 判断是否存在了prev节点
if (stageNodes.contains(prevNode)) {
// 2.2.1 获取上一个节点的next node节点信息
byte[] data = zookeeper.readData(path + "/" + prevNode);
EtlEventData eventData = JsonUtils.unmarshalFromByte(data, EtlEventData.class);
if (eventData.getNextNid().equals(ArbitrateConfigUtils.getCurrentNid())) {
// 添加到返回队列,唤醒wait阻塞
addReply(processId);
}
}
} catch (ZkNoNodeException e) {
// 出现节点不存在,说明出现了error情况
} catch (ZkException e) {
logger.error("ExtractStageListener", e);
}
}
use of com.alibaba.otter.shared.arbitrate.model.EtlEventData in project otter by alibaba.
the class LoadStageListener method stageChannged.
public void stageChannged(Long processId, List<String> stageNodes) {
try {
// 1. 根据pipelineId+processId构造对应的path
String path = StagePathUtils.getProcess(getPipelineId(), processId);
if (replyProcessIds.contains(processId)) {
// 避免重复处理
return;
}
// 2.2 判断是否存在了prev节点
if (stageNodes.contains(prevNode)) {
// 2.2.1 获取上一个节点的next node节点信息
byte[] data = zookeeper.readData(path + "/" + prevNode);
EtlEventData eventData = JsonUtils.unmarshalFromByte(data, EtlEventData.class);
if (eventData.getNextNid().equals(ArbitrateConfigUtils.getCurrentNid())) {
List<Long> currentProcessIds = stageMonitor.getCurrentProcessIds(false);
if (currentProcessIds.contains(processId) && currentProcessIds.get(0).equals(processId)) {
// 判断是否是当前最小的processId节点,轮到自己处理了
// 添加到返回队列,唤醒wait阻塞
addReply(processId);
}
}
}
} catch (ZkNoNodeException e) {
// 出现节点不存在,说明出现了error情况
} catch (ZkException e) {
logger.error("LoadStageListener", e);
}
}
use of com.alibaba.otter.shared.arbitrate.model.EtlEventData in project otter by alibaba.
the class ArbitrateViewServiceImpl method listProcesses.
public List<ProcessStat> listProcesses(Long channelId, Long pipelineId) {
List<ProcessStat> processStats = new ArrayList<ProcessStat>();
String processRoot = ManagePathUtils.getProcessRoot(channelId, pipelineId);
IZkConnection connection = zookeeper.getConnection();
// zkclient会将获取stat信息和正常的操作分开,使用原生的zk进行优化
ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();
// 获取所有的process列表
List<String> processNodes = zookeeper.getChildren(processRoot);
List<Long> processIds = new ArrayList<Long>();
for (String processNode : processNodes) {
processIds.add(ManagePathUtils.getProcessId(processNode));
}
Collections.sort(processIds);
for (int i = 0; i < processIds.size(); i++) {
Long processId = processIds.get(i);
// 当前的process可能会有变化
ProcessStat processStat = new ProcessStat();
processStat.setPipelineId(pipelineId);
processStat.setProcessId(processId);
List<StageStat> stageStats = new ArrayList<StageStat>();
processStat.setStageStats(stageStats);
try {
String processPath = ManagePathUtils.getProcess(channelId, pipelineId, processId);
Stat zkProcessStat = new Stat();
List<String> stages = orginZk.getChildren(processPath, false, zkProcessStat);
Collections.sort(stages, new StageComparator());
StageStat prev = null;
for (String stage : stages) {
// 循环每个process下的stage
String stagePath = processPath + "/" + stage;
Stat zkStat = new Stat();
StageStat stageStat = new StageStat();
stageStat.setPipelineId(pipelineId);
stageStat.setProcessId(processId);
byte[] bytes = orginZk.getData(stagePath, false, zkStat);
if (bytes != null && bytes.length > 0) {
// 特殊处理zookeeper里的data信息,manager没有对应node中PipeKey的对象,所以导致反序列化会失败,需要特殊处理,删除'@'符号
String json = StringUtils.remove(new String(bytes, "UTF-8"), '@');
EtlEventData data = JsonUtils.unmarshalFromString(json, EtlEventData.class);
stageStat.setNumber(data.getNumber());
stageStat.setSize(data.getSize());
Map exts = new HashMap();
if (!CollectionUtils.isEmpty(data.getExts())) {
exts.putAll(data.getExts());
}
exts.put("currNid", data.getCurrNid());
exts.put("nextNid", data.getNextNid());
exts.put("desc", data.getDesc());
stageStat.setExts(exts);
}
if (prev != null) {
// 对应的start时间为上一个节点的结束时间
stageStat.setStartTime(prev.getEndTime());
} else {
// process的最后修改时间,select
stageStat.setStartTime(zkProcessStat.getMtime());
// await成功后会设置USED标志位
}
stageStat.setEndTime(zkStat.getMtime());
if (ArbitrateConstants.NODE_SELECTED.equals(stage)) {
stageStat.setStage(StageType.SELECT);
} else if (ArbitrateConstants.NODE_EXTRACTED.equals(stage)) {
stageStat.setStage(StageType.EXTRACT);
} else if (ArbitrateConstants.NODE_TRANSFORMED.equals(stage)) {
stageStat.setStage(StageType.TRANSFORM);
// } else if
// (ArbitrateConstants.NODE_LOADED.equals(stage)) {
// stageStat.setStage(StageType.LOAD);
}
prev = stageStat;
stageStats.add(stageStat);
}
// 添加一个当前正在处理的
StageStat currentStageStat = new StageStat();
currentStageStat.setPipelineId(pipelineId);
currentStageStat.setProcessId(processId);
if (prev == null) {
byte[] bytes = orginZk.getData(processPath, false, zkProcessStat);
if (bytes == null || bytes.length == 0) {
// 直接认为未使用,忽略之
continue;
}
ProcessNodeEventData nodeData = JsonUtils.unmarshalFromByte(bytes, ProcessNodeEventData.class);
if (nodeData.getStatus().isUnUsed()) {
// 跳过该process
continue;
} else {
// select操作
currentStageStat.setStage(StageType.SELECT);
currentStageStat.setStartTime(zkProcessStat.getMtime());
}
} else {
// 判断上一个节点,确定当前的stage
StageType stage = prev.getStage();
if (stage.isSelect()) {
currentStageStat.setStage(StageType.EXTRACT);
} else if (stage.isExtract()) {
currentStageStat.setStage(StageType.TRANSFORM);
} else if (stage.isTransform()) {
currentStageStat.setStage(StageType.LOAD);
} else if (stage.isLoad()) {
// 已经是最后一个节点了
continue;
}
// 开始时间为上一个节点的结束时间
currentStageStat.setStartTime(prev.getEndTime());
}
if (currentStageStat.getStage().isLoad()) {
// load必须为第一个process节点
if (i == 0) {
stageStats.add(currentStageStat);
}
} else {
// 其他情况都添加
stageStats.add(currentStageStat);
}
} catch (NoNodeException e) {
// ignore
} catch (KeeperException e) {
throw new ArbitrateException(e);
} catch (InterruptedException e) {
// ignore
} catch (UnsupportedEncodingException e) {
// ignore
}
processStats.add(processStat);
}
return processStats;
}
Aggregations