Skip to content

Commit

Permalink
error handle & roam remove final
Browse files Browse the repository at this point in the history
  • Loading branch information
995517265@qq.com committed Mar 23, 2022
1 parent 8d8036c commit 51d0624
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

/**
* @author zjn
* leaf error handle method
* default shutdown and not store
* node error handle enum
*/
public enum ErrorHandleEnum {
/*
Expand All @@ -27,12 +26,7 @@ public enum ErrorHandleEnum {
/*
* shut down process
*/
SHUT_DOWN((byte) 3),

/*
* shut down process with store
*/
SHUT_DOWN_STORE((byte) 4);
SHUT_DOWN((byte) 3);

private static final Map<Byte, ErrorHandleEnum> MAP = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public enum NodeRunStateEnum {
/*
* none
*/
NONE((byte) 2);
NONE((byte) 2),
/*
* shutdown
*/
SHUT_DOWN((byte) 3);

private static final Map<Byte, NodeRunStateEnum> MAP = new HashMap<>();

Expand Down
42 changes: 1 addition & 41 deletions ice-core/src/main/java/com/ice/core/base/BaseLeaf.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ice.core.base;

import com.ice.common.enums.ErrorHandleEnum;
import com.ice.common.enums.NodeRunStateEnum;
import com.ice.common.exception.NodeException;
import com.ice.core.context.IceContext;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -16,51 +14,13 @@
@EqualsAndHashCode(callSuper = true)
public abstract class BaseLeaf extends BaseNode {

/*
* default SHUT_DOWN
*/
private ErrorHandleEnum iceErrorHandleEnum = ErrorHandleEnum.SHUT_DOWN;

/*
* process node
* @return process result
*/
@Override
protected NodeRunStateEnum processNode(IceContext cxt) {
try {
return doLeaf(cxt);
} catch (Exception e) {
switch (iceErrorHandleEnum) {
case CONTINUE_NONE:
if (this.isIceNodeDebug()) {
log.error("error occur in {} handle with none", this.findIceNodeId());
}
return NodeRunStateEnum.NONE;
case CONTINUE_FALSE:
if (this.isIceNodeDebug()) {
log.error("error occur in {} handle with false", this.findIceNodeId());
}
return NodeRunStateEnum.FALSE;
case CONTINUE_TRUE:
if (this.isIceNodeDebug()) {
log.error("error occur in {} handle with true", this.findIceNodeId());
}
return NodeRunStateEnum.TRUE;
case SHUT_DOWN:
if (this.isIceNodeDebug()) {
log.error("error occur in {} handle with shut down", this.findIceNodeId());
}
throw new NodeException(this.findIceNodeId(), e);
case SHUT_DOWN_STORE:
if (this.isIceNodeDebug()) {
log.error("error occur in {} handle with shut down store", this.findIceNodeId());
}
//TODO store
throw new NodeException(this.findIceNodeId(), e);
default:
throw e;
}
}
return doLeaf(cxt);
}

/*
Expand Down
34 changes: 31 additions & 3 deletions ice-core/src/main/java/com/ice/core/base/BaseNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.ice.common.enums.NodeRunStateEnum;
import com.ice.common.enums.TimeTypeEnum;
import com.ice.core.context.IceContext;
import com.ice.core.utils.IceErrorHandle;
import com.ice.core.utils.IceTimeUtils;
import com.ice.core.utils.ProcessUtils;
import lombok.Data;

/**
* @author zjn
* 基础Node
* 注意:开发时应避免与基础字段一致
* ice base node
* Note: it should be avoided to be consistent with the basic field during development
*/
@Data
public abstract class BaseNode {
Expand Down Expand Up @@ -56,6 +57,11 @@ public abstract class BaseNode {
private boolean iceTransaction;

private String iceLogName;
/*
* node error handle res from config
* this config is high priority than custom error handle method
*/
private NodeRunStateEnum iceErrorStateEnum;

/*
* process
Expand All @@ -82,7 +88,25 @@ public NodeRunStateEnum process(IceContext cxt) {
ProcessUtils.collectRejectInfo(cxt.getProcessInfo(), this);
return NodeRunStateEnum.FALSE;
}
NodeRunStateEnum res = processNode(cxt);
NodeRunStateEnum res;
try {
res = processNode(cxt);
} catch (Exception e) {
/*error occur use error handle method*/
NodeRunStateEnum errorRunState = errorHandle(cxt);
if (this.iceErrorStateEnum != null) {
/*error handle in config is high priority then error method return*/
errorRunState = this.iceErrorStateEnum;
}
if (errorRunState == null || errorRunState == NodeRunStateEnum.SHUT_DOWN) {
/*shutdown process and throw e*/
ProcessUtils.collectInfo(cxt.getProcessInfo(), this, start, NodeRunStateEnum.SHUT_DOWN);
throw e;
} else {
/*error but continue*/
res = errorRunState;
}
}
ProcessUtils.collectInfo(cxt.getProcessInfo(), this, start, res);
return iceInverse ?
res == NodeRunStateEnum.TRUE ?
Expand All @@ -96,6 +120,10 @@ public NodeRunStateEnum process(IceContext cxt) {
*/
protected abstract NodeRunStateEnum processNode(IceContext cxt);

public NodeRunStateEnum errorHandle(IceContext cxt) {
return IceErrorHandle.handleError(this, cxt);
}

public Long getIceNodeId() {
return iceNodeId;
}
Expand Down
9 changes: 0 additions & 9 deletions ice-core/src/main/java/com/ice/core/builder/LeafBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.ice.core.builder;

import com.ice.common.enums.ErrorHandleEnum;
import com.ice.common.enums.TimeTypeEnum;
import com.ice.core.base.BaseLeaf;
import com.ice.core.base.BaseNode;
Expand All @@ -12,9 +11,6 @@ public class LeafBuilder extends BaseBuilder {

public LeafBuilder(BaseLeaf leaf) {
super(leaf);
if (leaf.getIceErrorHandleEnum() == null) {
leaf.setIceErrorHandleEnum(ErrorHandleEnum.SHUT_DOWN);
}
}

public static LeafBuilder leaf(BaseLeaf leaf) {
Expand Down Expand Up @@ -45,9 +41,4 @@ public LeafBuilder end(long end) {
public LeafBuilder timeType(TimeTypeEnum typeEnum) {
return (LeafBuilder) super.timeType(typeEnum);
}

public LeafBuilder errorHandle(ErrorHandleEnum handleEnum) {
((BaseLeaf) this.getNode()).setIceErrorHandleEnum(handleEnum);
return this;
}
}
32 changes: 26 additions & 6 deletions ice-core/src/main/java/com/ice/core/context/IceRoam.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author zjn
* based on HashMap extend
*/
public final class IceRoam extends ConcurrentHashMap<String, Object> implements Serializable {
public class IceRoam extends ConcurrentHashMap<String, Object> implements Serializable {

public IceRoam(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
Expand Down Expand Up @@ -89,11 +89,6 @@ public <T> T getMulti(String multiKey) {
return (T) endMap.get(keys[i]);
}

@SuppressWarnings("unchecked")
public <T> T getValue(Object key) {
return (T) get(key);
}

/*
* Multi source find value value
* prefix with '@' string directing to roam value
Expand All @@ -120,4 +115,29 @@ public <T> T getUnion(Object union) {
public <T> T putValue(String key, T value) {
return (T) put(key, value);
}

@SuppressWarnings("unchecked")
public <T> T getValue(Object key) {
return (T) get(key);
}

public <T> T getUnion(Object union, T defaultValue) {
T res = getUnion(union);
return res == null ? defaultValue : res;
}

public <T> T getValue(Object key, T defaultValue) {
T res = getValue(key);
return res == null ? defaultValue : res;
}

public <T> T getMulti(String multiKey, T defaultValue) {
T res = getMulti(multiKey);
return res == null ? defaultValue : res;
}

public Object get(String key, Object defaultValue) {
Object res = get(key);
return res == null ? defaultValue : res;
}
}
36 changes: 36 additions & 0 deletions ice-core/src/main/java/com/ice/core/utils/IceErrorHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ice.core.utils;

import com.ice.common.enums.NodeRunStateEnum;
import com.ice.core.base.BaseNode;
import com.ice.core.context.IceContext;
import lombok.Data;

/**
* @author zjn
* IceErrorHandle
* used on error occur in node
*/
@Data
public abstract class IceErrorHandle {

private static IceErrorHandle handle = new DefaultIceErrorHandle();

public static void setHandle(IceErrorHandle customHandle) {
handle = customHandle;
}

public static NodeRunStateEnum handleError(BaseNode node, IceContext cxt) {
return handle.handle(node, cxt);
}

protected abstract NodeRunStateEnum handle(BaseNode node, IceContext cxt);

private static class DefaultIceErrorHandle extends IceErrorHandle {

@Override
protected NodeRunStateEnum handle(BaseNode node, IceContext cxt) {
//default do noting and shutdown
return NodeRunStateEnum.SHUT_DOWN;
}
}
}
4 changes: 4 additions & 0 deletions ice-core/src/main/java/com/ice/core/utils/ProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private ProcessUtils() {
* T true F false
* R reject(the forward return false)
* N None
* S shutdown
* [iceNodeId:process class name:process return:time used]
* remarks:
* 1.-I inverse active
Expand All @@ -36,6 +37,9 @@ public static void collectInfo(StringBuilder sb, BaseNode node, long start, Node
case NONE:
state = 'N';
break;
case SHUT_DOWN:
state = 'S';
break;
default:
state = '?';
break;
Expand Down

0 comments on commit 51d0624

Please sign in to comment.