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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
| @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface HmilyTCC { String confirmMethod() default ""; String cancelMethod() default ""; TransTypeEnum pattern() default TransTypeEnum.TCC; }
@Aspect public abstract class AbstractHmilyTransactionAspect { private final HmilyTransactionInterceptor interceptor = new HmilyGlobalInterceptor();
@Pointcut("@annotation(org.dromara.hmily.annotation.HmilyTCC) || @annotation(org.dromara.hmily.annotation.HmilyTAC) || @annotation(org.dromara.hmily.annotation.HmilyXA)") public void hmilyInterceptor() { }
@Around("hmilyInterceptor()") public Object interceptTccMethod(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable { return interceptor.invoke(proceedingJoinPoint); } }
public class HmilyGlobalInterceptor implements HmilyTransactionInterceptor { private static RpcParameterLoader parameterLoader; private static final EnumMap<TransTypeEnum, HmilyTransactionHandlerRegistry> REGISTRY = new EnumMap<>(TransTypeEnum.class);
static { parameterLoader = Optional.ofNullable(ExtensionLoaderFactory.load(RpcParameterLoader.class)).orElse(new LocalParameterLoader()); }
static { REGISTRY.put(TransTypeEnum.TCC, ExtensionLoaderFactory.load(HmilyTransactionHandlerRegistry.class, "tcc")); REGISTRY.put(TransTypeEnum.TAC, ExtensionLoaderFactory.load(HmilyTransactionHandlerRegistry.class, "tac")); REGISTRY.put(TransTypeEnum.XA, ExtensionLoaderFactory.load(HmilyTransactionHandlerRegistry.class, "xa")); }
@Override public Object invoke(final ProceedingJoinPoint pjp) throws Throwable { HmilyTransactionContext context = parameterLoader.load(); return invokeWithinTransaction(context, pjp); }
private Object invokeWithinTransaction(final HmilyTransactionContext context, final ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); return getRegistry(signature.getMethod()).select(context).handleTransaction(point, context); }
private HmilyTransactionHandlerRegistry getRegistry(final Method method) { if (method.isAnnotationPresent(HmilyTCC.class)) { return REGISTRY.get(TransTypeEnum.TCC); } else if (method.isAnnotationPresent(HmilyTAC.class)) { return REGISTRY.get(TransTypeEnum.TAC); } else if (method.isAnnotationPresent(HmilyXA.class)) { return REGISTRY.get(TransTypeEnum.XA); } else { return REGISTRY.get(TransTypeEnum.TAC); } } }
@HmilySPI(value = "springCloud") public class SpringCloudParameterLoader implements RpcParameterLoader { @Override public HmilyTransactionContext load() { HmilyTransactionContext hmilyTransactionContext = null; try { final RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); hmilyTransactionContext = RpcMediator.getInstance().acquire(key -> ((ServletRequestAttributes) requestAttributes).getRequest().getHeader(key)); } catch (IllegalStateException ex) { LogUtil.warn(LOGGER, () -> "can not acquire request info:" + ex.getLocalizedMessage()); } return hmilyTransactionContext; } }
@HmilySPI("tcc") public class HmilyTccTransactionHandlerRegistry extends AbstractHmilyTransactionHandlerRegistry { @Override public void register() { getHandlers().put(HmilyRoleEnum.START, new StarterHmilyTccTransactionHandler()); getHandlers().put(HmilyRoleEnum.PARTICIPANT, new ParticipantHmilyTccTransactionHandler()); getHandlers().put(HmilyRoleEnum.CONSUMER, new ConsumeHmilyTccTransactionHandler()); getHandlers().put(HmilyRoleEnum.LOCAL, new LocalHmilyTccTransactionHandler()); } }
public abstract class AbstractHmilyTransactionHandlerRegistry implements HmilyTransactionHandlerRegistry { @Override public HmilyTransactionHandler select(final HmilyTransactionContext context) { if (Objects.isNull(context)) { return getHandler(HmilyRoleEnum.START); } else { if (context.getRole() == HmilyRoleEnum.LOCAL.getCode()) { return getHandler(HmilyRoleEnum.LOCAL); } else if (context.getRole() == HmilyRoleEnum.PARTICIPANT.getCode() || context.getRole() == HmilyRoleEnum.START.getCode()) { return getHandler(HmilyRoleEnum.PARTICIPANT); } return getHandler(HmilyRoleEnum.CONSUMER); } } }
public class StarterHmilyTccTransactionHandler implements HmilyTransactionHandler, AutoCloseable { @Override public Object handleTransaction(final ProceedingJoinPoint point, final HmilyTransactionContext context) throws Throwable { Object returnValue; MetricsReporter.counterIncrement(LabelNames.TRANSACTION_TOTAL, new String[]{TransTypeEnum.TCC.name()}); LocalDateTime starterTime = LocalDateTime.now(); try { HmilyTransaction hmilyTransaction = executor.preTry(point); try { returnValue = point.proceed(); hmilyTransaction.setStatus(HmilyActionEnum.TRYING.getCode()); executor.updateStartStatus(hmilyTransaction); } catch (Throwable throwable) { final HmilyTransaction currentTransaction = HmilyTransactionHolder.getInstance().getCurrentTransaction(); disruptor.getProvider().onData(() -> { MetricsReporter.counterIncrement(LabelNames.TRANSACTION_STATUS, new String[]{TransTypeEnum.TCC.name(), HmilyRoleEnum.START.name(), HmilyActionEnum.CANCELING.name()}); executor.globalCancel(currentTransaction); }); throw throwable; } final HmilyTransaction currentTransaction = HmilyTransactionHolder.getInstance().getCurrentTransaction(); disruptor.getProvider().onData(() -> { MetricsReporter.counterIncrement(LabelNames.TRANSACTION_STATUS, new String[]{TransTypeEnum.TCC.name(), HmilyRoleEnum.START.name(), HmilyActionEnum.CONFIRMING.name()}); executor.globalConfirm(currentTransaction); }); } finally { HmilyContextHolder.remove(); executor.remove(); MetricsReporter.recordTime(LabelNames.TRANSACTION_LATENCY, new String[]{TransTypeEnum.TCC.name()}, starterTime.until(LocalDateTime.now(), ChronoUnit.MILLIS)); } return returnValue; } }
public final class HmilyTccTransactionExecutor { public HmilyTransaction preTry(final ProceedingJoinPoint point) { LogUtil.debug(LOGGER, () -> "......hmily tcc transaction starter...."); HmilyTransaction hmilyTransaction = createHmilyTransaction(); HmilyRepositoryStorage.createHmilyTransaction(hmilyTransaction); HmilyParticipant hmilyParticipant = buildHmilyParticipant(point, null, null, HmilyRoleEnum.START.getCode(), hmilyTransaction.getTransId()); HmilyRepositoryStorage.createHmilyParticipant(hmilyParticipant); hmilyTransaction.registerParticipant(hmilyParticipant); HmilyTransactionHolder.getInstance().set(hmilyTransaction); HmilyTransactionContext context = new HmilyTransactionContext(); context.setAction(HmilyActionEnum.TRYING.getCode()); context.setTransId(hmilyTransaction.getTransId()); context.setRole(HmilyRoleEnum.START.getCode()); context.setTransType(TransTypeEnum.TCC.name()); HmilyContextHolder.set(context); return hmilyTransaction; } private HmilyParticipant buildHmilyParticipant(final ProceedingJoinPoint point, final Long participantId, final Long participantRefId, final int role, final Long transId) { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Object[] args = point.getArgs(); final HmilyTCC hmilyTCC = method.getAnnotation(HmilyTCC.class); String confirmMethodName = hmilyTCC.confirmMethod(); String cancelMethodName = hmilyTCC.cancelMethod(); if (StringUtils.isBlank(confirmMethodName) || StringUtils.isBlank(cancelMethodName)) { return null; } HmilyParticipant hmilyParticipant = new HmilyParticipant(); if (null == participantId) { hmilyParticipant.setParticipantId(IdWorkerUtils.getInstance().createUUID()); } else { hmilyParticipant.setParticipantId(participantId); } if (null != participantRefId) { hmilyParticipant.setParticipantRefId(participantRefId); } Class<?> clazz = point.getTarget().getClass(); hmilyParticipant.setTransId(transId); hmilyParticipant.setTransType(TransTypeEnum.TCC.name()); hmilyParticipant.setStatus(HmilyActionEnum.PRE_TRY.getCode()); hmilyParticipant.setRole(role); hmilyParticipant.setTargetClass(clazz.getName()); hmilyParticipant.setTargetMethod(method.getName()); if (StringUtils.isNoneBlank(confirmMethodName)) { hmilyParticipant.setConfirmMethod(confirmMethodName); HmilyInvocation confirmInvocation = new HmilyInvocation(clazz.getInterfaces()[0], method.getName(), method.getParameterTypes(), args); hmilyParticipant.setConfirmHmilyInvocation(confirmInvocation); } if (StringUtils.isNoneBlank(cancelMethodName)) { hmilyParticipant.setCancelMethod(cancelMethodName); HmilyInvocation cancelInvocation = new HmilyInvocation(clazz.getInterfaces()[0], method.getName(), method.getParameterTypes(), args); hmilyParticipant.setCancelHmilyInvocation(cancelInvocation); } return hmilyParticipant; } }
public class HmilyRepositoryStorage { public static void createHmilyTransaction(final HmilyTransaction hmilyTransaction) { if (Objects.nonNull(hmilyTransaction)) { PUBLISHER.publishEvent(hmilyTransaction, EventTypeEnum.CREATE_HMILY_TRANSACTION.getCode()); } } }
public final class HmilyRepositoryEventPublisher implements AutoCloseable { public void publishEvent(final HmilyTransaction hmilyTransaction, final int type) { HmilyRepositoryEvent event = new HmilyRepositoryEvent(); event.setType(type); event.setHmilyTransaction(hmilyTransaction); event.setTransId(hmilyTransaction.getTransId()); push(event); } private void push(final HmilyRepositoryEvent event) { if (Objects.nonNull(hmilyConfig) && hmilyConfig.isAsyncRepository()) { disruptor.getProvider().onData(event); } else { HmilyRepositoryEventDispatcher.getInstance().doDispatch(event); } } }
public class HmilyRepositoryEventConsumer implements HmilyDisruptorConsumer<HmilyRepositoryEvent> { private ConsistentHashSelector executor; public HmilyRepositoryEventConsumer(final ConsistentHashSelector executor) { this.executor = executor; } @Override public void execute(final HmilyRepositoryEvent event) { Long transId = event.getTransId(); executor.select(String.valueOf(transId)).execute(() -> { HmilyRepositoryEventDispatcher.getInstance().doDispatch(event); event.clear(); }); } }
public class HmilyContextHolder { private static HmilyContext hmilyContext;
static { HmilyConfig hmilyConfig = ConfigEnv.getInstance().getConfig(HmilyConfig.class); if (Objects.isNull(hmilyConfig)) { hmilyContext = new ThreadLocalHmilyContext(); } else { hmilyContext = Optional.ofNullable(ExtensionLoaderFactory.load(HmilyContext.class, hmilyConfig.getContextTransmittalMode())).orElse(new ThreadLocalHmilyContext()); } }
public static void set(final HmilyTransactionContext context) { hmilyContext.set(context); } }
|