@Override public AlphaResponse send(TxEvent event) { if (event.type() == SagaStartedEvent) { throw new OmegaException("Failed to process subsequent requests because no alpha server is available"); } try { return availableMessageSenders.take().send(event); } catch (InterruptedException e) { throw new OmegaException("Failed to send event " + event + " due to interruption", e); } } }
@Override public AlphaResponse preIntercept(String parentTxId, String compensationMethod, int timeout, String retriesMethod, int retries, Object... message) { try { return sender.send(new SagaStartedEvent(omegaContext.globalTxId(), omegaContext.localTxId(), timeout)); } catch (OmegaException e) { throw new TransactionalException(e.getMessage(), e.getCause()); } }
@Override public Object[] deserialize(byte[] message) { try { Input input = new Input(new ByteArrayInputStream(message)); Kryo kryo = pool.borrow(); Object[] objects = kryo.readObjectOrNull(input, Object[].class); pool.release(kryo); return objects; } catch (KryoException e) { throw new OmegaException("Unable to deserialize message", e); } } }
@Override public AlphaResponse send(TxEvent event) { do { MessageSender messageSender = fastestSender(); try { long startTime = System.nanoTime(); AlphaResponse response = messageSender.send(event); senders.put(messageSender, System.nanoTime() - startTime); return response; } catch (OmegaException e) { throw e; } catch (Exception e) { LOG.error("Retry sending event {} due to failure", event, e); // very large latency on exception senders.put(messageSender, Long.MAX_VALUE); } } while (!Thread.currentThread().isInterrupted()); throw new OmegaException("Failed to send event " + event + " due to interruption"); }
@Override public void doWith(Method method) throws IllegalArgumentException { if (!method.isAnnotationPresent(Compensable.class)) { return; } String compensationMethod = method.getAnnotation(Compensable.class).compensationMethod(); try { if (!compensationMethod.isEmpty()) { Method signature = bean.getClass().getDeclaredMethod(compensationMethod, method.getParameterTypes()); String key = getTargetBean(bean).getClass().getDeclaredMethod(compensationMethod, method.getParameterTypes()).toString(); compensationContext.addCompensationContext(key, signature, bean); LOG.debug("Found compensation method [{}] in {}", compensationMethod, bean.getClass().getCanonicalName()); } } catch (Exception e) { throw new OmegaException( "No such compensation method [" + compensationMethod + "] found in " + bean.getClass().getCanonicalName(), e); } }
LOG.error(errorMessage); interceptor.onError(parentTxId, compensationMethodSignature(joinPoint, compensable, method), e); throw new OmegaException(errorMessage);
@Override public void postIntercept(String parentTxId, String compensationMethod) { AlphaResponse response = sender.send(new SagaEndedEvent(omegaContext.globalTxId(), omegaContext.localTxId())); if (response.aborted()) { throw new OmegaException("transaction " + parentTxId + " is aborted"); } }