1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| public final class TbActorMailbox implements TbActorCtx {
private static final boolean HIGH_PRIORITY = true; private static final boolean NORMAL_PRIORITY = false;
private static final boolean FREE = false; private static final boolean BUSY = true;
private static final boolean NOT_READY = false; private static final boolean READY = true;
private final TbActorSystem system; private final TbActorSystemSettings settings; private final TbActorId selfId; private final TbActorRef parentRef; private final TbActor actor; private final Dispatcher dispatcher;
private final ConcurrentLinkedQueue<TbActorMsg> highPriorityMsgs = new ConcurrentLinkedQueue<>(); private final ConcurrentLinkedQueue<TbActorMsg> normalPriorityMsgs = new ConcurrentLinkedQueue<>();
private final AtomicBoolean busy = new AtomicBoolean(FREE); private final AtomicBoolean ready = new AtomicBoolean(NOT_READY); private final AtomicBoolean destroyInProgress = new AtomicBoolean(); private volatile TbActorStopReason stopReason;
public void initActor() { dispatcher.getExecutor().execute(() -> tryInit(1)); }
private void tryInit(int attempt) { try { log.debug("[{}] Trying to init actor, attempt: {}", selfId, attempt); if (!destroyInProgress.get()) { actor.init(this); if (!destroyInProgress.get()) { ready.set(READY); tryProcessQueue(false); } } } catch (Throwable t) { log.debug("[{}] Failed to init actor, attempt: {}", selfId, attempt, t); int attemptIdx = attempt + 1; InitFailureStrategy strategy = actor.onInitFailure(attempt, t); if (strategy.isStop() || (settings.getMaxActorInitAttempts() > 0 && attemptIdx > settings.getMaxActorInitAttempts())) { log.info("[{}] Failed to init actor, attempt {}, going to stop attempts.", selfId, attempt, t); stopReason = TbActorStopReason.INIT_FAILED; destroy(); } else if (strategy.getRetryDelay() > 0) { log.info("[{}] Failed to init actor, attempt {}, going to retry in attempts in {}ms", selfId, attempt, strategy.getRetryDelay()); log.debug("[{}] Error", selfId, t); system.getScheduler().schedule(() -> dispatcher.getExecutor().execute(() -> tryInit(attemptIdx)), strategy.getRetryDelay(), TimeUnit.MILLISECONDS); } else { log.info("[{}] Failed to init actor, attempt {}, going to retry immediately", selfId, attempt); log.debug("[{}] Error", selfId, t); dispatcher.getExecutor().execute(() -> tryInit(attemptIdx)); } } }
@Override public void tell(TbActorMsg actorMsg) { enqueue(actorMsg, NORMAL_PRIORITY); }
@Override public void tellWithHighPriority(TbActorMsg actorMsg) { enqueue(actorMsg, HIGH_PRIORITY); }
private void enqueue(TbActorMsg msg, boolean highPriority) { if (!destroyInProgress.get()) { if (highPriority) { highPriorityMsgs.add(msg); } else { normalPriorityMsgs.add(msg); } tryProcessQueue(true); } else { if (highPriority && msg.getMsgType().equals(MsgType.RULE_NODE_UPDATED_MSG)) { synchronized (this) { if (stopReason == TbActorStopReason.INIT_FAILED) { destroyInProgress.set(false); stopReason = null; initActor(); } else { msg.onTbActorStopped(stopReason); } } } else { msg.onTbActorStopped(stopReason); } } }
private void tryProcessQueue(boolean newMsg) { if (ready.get() == READY) { if (newMsg || !highPriorityMsgs.isEmpty() || !normalPriorityMsgs.isEmpty()) {
if (busy.compareAndSet(FREE, BUSY)) { dispatcher.getExecutor().execute(this::processMailbox); } else { log.trace("[{}] MessageBox is busy, new msg: {}", selfId, newMsg); } } else { log.trace("[{}] MessageBox is empty, new msg: {}", selfId, newMsg); } } else { log.trace("[{}] MessageBox is not ready, new msg: {}", selfId, newMsg); } }
private void processMailbox() { boolean noMoreElements = false; for (int i = 0; i < settings.getActorThroughput(); i++) { TbActorMsg msg = highPriorityMsgs.poll(); if (msg == null) { msg = normalPriorityMsgs.poll(); } if (msg != null) { try { log.debug("[{}] Going to process message: {}", selfId, msg);
actor.process(msg);
} catch (TbRuleNodeUpdateException updateException){ stopReason = TbActorStopReason.INIT_FAILED; destroy(); } catch (Throwable t) { log.debug("[{}] Failed to process message: {}", selfId, msg, t); ProcessFailureStrategy strategy = actor.onProcessFailure(t); if (strategy.isStop()) { system.stop(selfId); } } } else { noMoreElements = true; break; } }
if (noMoreElements) { busy.set(FREE); dispatcher.getExecutor().execute(() -> tryProcessQueue(false)); } else { dispatcher.getExecutor().execute(this::processMailbox); } }
@Override public void tell(TbActorId target, TbActorMsg actorMsg) { system.tell(target, actorMsg); }
@Override public void broadcastToChildren(TbActorMsg msg) { system.broadcastToChildren(selfId, msg); }
@Override public void broadcastToChildren(TbActorMsg msg, Predicate<TbActorId> childFilter) { system.broadcastToChildren(selfId, childFilter, msg); }
}
|