This commit is contained in:
yu199195 2020-08-06 15:21:31 +08:00
parent b3ce02ee12
commit e8c47b0299
5 changed files with 59 additions and 45 deletions

View File

@ -19,6 +19,7 @@ package org.dromara.hmily.core.holder;
import java.util.Objects;
import java.util.Optional;
import org.dromara.hmily.common.utils.CollectionUtils;
import org.dromara.hmily.core.cache.HmilyParticipantCacheManager;
import org.dromara.hmily.repository.spi.entity.HmilyParticipant;
import org.dromara.hmily.repository.spi.entity.HmilyTransaction;
@ -66,6 +67,18 @@ public class HmilyTransactionHolder {
.ifPresent(c -> c.registerParticipant(hmilyParticipant));
}
/**
* Cache hmily participant.
*
* @param hmilyParticipant the hmily participant
*/
public void cacheHmilyParticipant(final HmilyParticipant hmilyParticipant) {
if (Objects.isNull(hmilyParticipant)) {
return;
}
HmilyParticipantCacheManager.getInstance().cacheHmilyParticipant(hmilyParticipant);
}
/**
* when nested transaction add participant.
*

View File

@ -37,7 +37,20 @@ public class HmilyRepositoryStorage {
* @param hmilyTransaction the hmily transaction
*/
public static void createHmilyTransaction(final HmilyTransaction hmilyTransaction) {
PUBLISHER.publishEvent(hmilyTransaction, EventTypeEnum.CREATE_HMILY_TRANSACTION.getCode());
if (Objects.nonNull(hmilyTransaction)) {
PUBLISHER.publishEvent(hmilyTransaction, EventTypeEnum.CREATE_HMILY_TRANSACTION.getCode());
}
}
/**
* Update hmily transaction status.
*
* @param hmilyTransaction the hmily transaction
*/
public static void updateHmilyTransactionStatus(final HmilyTransaction hmilyTransaction) {
if (Objects.nonNull(hmilyTransaction)) {
PUBLISHER.publishEvent(hmilyTransaction, EventTypeEnum.UPDATE_HMILY_TRANSACTION_STATUS.getCode());
}
}
/**
@ -46,17 +59,29 @@ public class HmilyRepositoryStorage {
* @param hmilyParticipant the hmily participant
*/
public static void createHmilyParticipant(final HmilyParticipant hmilyParticipant) {
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.CREATE_HMILY_PARTICIPANT.getCode());
if (Objects.nonNull(hmilyParticipant)) {
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.CREATE_HMILY_PARTICIPANT.getCode());
}
}
/**
* Update hmily participant status.
*
* @param hmilyParticipant the hmily participant
*/
public static void updateHmilyParticipantStatus(final HmilyParticipant hmilyParticipant) {
if (Objects.nonNull(hmilyParticipant)) {
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.UPDATE_HMILY_PARTICIPANT_STATUS.getCode());
}
}
/**
* Remove hmily participant.
*
* @param hmilyParticipant the hmily participant
*/
public static void removeHmilyParticipant(final HmilyParticipant hmilyParticipant) {
if (null != hmilyParticipant) {
if (Objects.nonNull(hmilyParticipant)) {
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.REMOVE_HMILY_PARTICIPANT.getCode());
}
}

View File

@ -21,6 +21,7 @@ import java.util.Map;
import java.util.Objects;
import org.dromara.hmily.common.enums.HmilyRoleEnum;
import org.dromara.hmily.core.context.HmilyTransactionContext;
import org.dromara.hmily.core.holder.HmilyTransactionHolder;
/**
* The type Abstract hmily transaction handler.

View File

@ -21,12 +21,10 @@ import com.google.common.collect.Lists;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.annotation.TransTypeEnum;
import org.dromara.hmily.common.enums.EventTypeEnum;
import org.dromara.hmily.common.enums.ExecutorTypeEnum;
import org.dromara.hmily.common.enums.HmilyActionEnum;
import org.dromara.hmily.common.enums.HmilyRoleEnum;
@ -38,16 +36,15 @@ import org.dromara.hmily.common.utils.StringUtils;
import org.dromara.hmily.core.cache.HmilyParticipantCacheManager;
import org.dromara.hmily.core.context.HmilyContextHolder;
import org.dromara.hmily.core.context.HmilyTransactionContext;
import org.dromara.hmily.core.disruptor.publisher.HmilyRepositoryEventPublisher;
import org.dromara.hmily.core.holder.HmilyTransactionHolder;
import org.dromara.hmily.core.reflect.HmilyReflector;
import org.dromara.hmily.core.repository.HmilyRepositoryStorage;
import org.dromara.hmily.repository.spi.entity.HmilyInvocation;
import org.dromara.hmily.repository.spi.entity.HmilyParticipant;
import org.dromara.hmily.repository.spi.entity.HmilyTransaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* this is hmily transaction manager.
*
@ -59,8 +56,6 @@ public final class HmilyTccTransactionExecutor {
private static final HmilyTccTransactionExecutor INSTANCE = new HmilyTccTransactionExecutor();
private static final HmilyRepositoryEventPublisher PUBLISHER = HmilyRepositoryEventPublisher.getInstance();
private HmilyTccTransactionExecutor() {
}
@ -78,9 +73,9 @@ public final class HmilyTccTransactionExecutor {
LogUtil.debug(LOGGER, () -> "......hmily tcc transaction starter....");
//build tccTransaction
HmilyTransaction hmilyTransaction = createHmilyTransaction();
PUBLISHER.publishEvent(hmilyTransaction, EventTypeEnum.CREATE_HMILY_TRANSACTION.getCode());
HmilyRepositoryStorage.createHmilyTransaction(hmilyTransaction);
HmilyParticipant hmilyParticipant = buildHmilyParticipant(point, null, null, HmilyRoleEnum.START.getCode(), hmilyTransaction.getTransId());
Optional.ofNullable(hmilyParticipant).ifPresent(h -> PUBLISHER.publishEvent(h, EventTypeEnum.CREATE_HMILY_PARTICIPANT.getCode()));
HmilyRepositoryStorage.createHmilyParticipant(hmilyParticipant);
hmilyTransaction.registerParticipant(hmilyParticipant);
//save tccTransaction in threadLocal
HmilyTransactionHolder.getInstance().set(hmilyTransaction);
@ -105,11 +100,8 @@ public final class HmilyTccTransactionExecutor {
public HmilyParticipant preTryParticipant(final HmilyTransactionContext context, final ProceedingJoinPoint point) {
LogUtil.debug(LOGGER, "participant hmily tcc transaction start..{}", context::toString);
final HmilyParticipant hmilyParticipant = buildHmilyParticipant(point, context.getParticipantId(), context.getParticipantRefId(), HmilyRoleEnum.PARTICIPANT.getCode(), context.getTransId());
//cache by guava
if (Objects.nonNull(hmilyParticipant)) {
HmilyParticipantCacheManager.getInstance().cacheHmilyParticipant(hmilyParticipant);
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.CREATE_HMILY_PARTICIPANT.getCode());
}
HmilyTransactionHolder.getInstance().cacheHmilyParticipant(hmilyParticipant);
HmilyRepositoryStorage.createHmilyParticipant(hmilyParticipant);
//publishEvent
//Nested transaction support
context.setRole(HmilyRoleEnum.PARTICIPANT.getCode());
@ -131,13 +123,13 @@ public final class HmilyTccTransactionExecutor {
return;
}
currentTransaction.setStatus(HmilyActionEnum.CONFIRMING.getCode());
updateHmilyTransactionStatus(currentTransaction);
HmilyRepositoryStorage.updateHmilyTransactionStatus(currentTransaction);
final List<HmilyParticipant> hmilyParticipants = currentTransaction.getHmilyParticipants();
for (HmilyParticipant hmilyParticipant : hmilyParticipants) {
try {
if (hmilyParticipant.getRole() == HmilyRoleEnum.START.getCode()) {
HmilyReflector.executor(HmilyActionEnum.CONFIRMING, ExecutorTypeEnum.LOCAL, hmilyParticipant);
removeHmilyParticipant(hmilyParticipant);
HmilyRepositoryStorage.removeHmilyParticipant(hmilyParticipant);
} else {
HmilyReflector.executor(HmilyActionEnum.CONFIRMING, ExecutorTypeEnum.RPC, hmilyParticipant);
}
@ -159,7 +151,7 @@ public final class HmilyTccTransactionExecutor {
if (hmilyParticipant.getParticipantId().equals(selfParticipantId)) {
final Object result = HmilyReflector.executor(HmilyActionEnum.CONFIRMING, ExecutorTypeEnum.LOCAL, hmilyParticipant);
results.add(result);
removeHmilyParticipant(hmilyParticipant);
HmilyRepositoryStorage.removeHmilyParticipant(hmilyParticipant);
} else {
final Object result = HmilyReflector.executor(HmilyActionEnum.CONFIRMING, ExecutorTypeEnum.RPC, hmilyParticipant);
results.add(result);
@ -186,13 +178,13 @@ public final class HmilyTccTransactionExecutor {
}
currentTransaction.setStatus(HmilyActionEnum.CANCELING.getCode());
//update cancel
updateHmilyTransactionStatus(currentTransaction);
HmilyRepositoryStorage.updateHmilyTransactionStatus(currentTransaction);
final List<HmilyParticipant> hmilyParticipants = currentTransaction.getHmilyParticipants();
for (HmilyParticipant hmilyParticipant : hmilyParticipants) {
try {
if (hmilyParticipant.getRole() == HmilyRoleEnum.START.getCode()) {
HmilyReflector.executor(HmilyActionEnum.CANCELING, ExecutorTypeEnum.LOCAL, hmilyParticipant);
removeHmilyParticipant(hmilyParticipant);
HmilyRepositoryStorage.removeHmilyParticipant(hmilyParticipant);
} else {
HmilyReflector.executor(HmilyActionEnum.CANCELING, ExecutorTypeEnum.RPC, hmilyParticipant);
}
@ -214,7 +206,7 @@ public final class HmilyTccTransactionExecutor {
HmilyParticipant selfHmilyParticipant = filterSelfHmilyParticipant(hmilyParticipants);
if (Objects.nonNull(selfHmilyParticipant)) {
selfHmilyParticipant.setStatus(HmilyActionEnum.CANCELING.getCode());
updateHmilyParticipantStatus(selfHmilyParticipant);
HmilyRepositoryStorage.updateHmilyParticipantStatus(selfHmilyParticipant);
}
List<Object> results = Lists.newArrayListWithCapacity(hmilyParticipants.size());
for (HmilyParticipant hmilyParticipant : hmilyParticipants) {
@ -222,7 +214,7 @@ public final class HmilyTccTransactionExecutor {
if (hmilyParticipant.getParticipantId().equals(selfParticipantId)) {
final Object result = HmilyReflector.executor(HmilyActionEnum.CANCELING, ExecutorTypeEnum.LOCAL, hmilyParticipant);
results.add(result);
removeHmilyParticipant(hmilyParticipant);
HmilyRepositoryStorage.removeHmilyParticipant(hmilyParticipant);
} else {
final Object result = HmilyReflector.executor(HmilyActionEnum.CANCELING, ExecutorTypeEnum.RPC, hmilyParticipant);
results.add(result);
@ -243,29 +235,11 @@ public final class HmilyTccTransactionExecutor {
* @param hmilyTransaction {@linkplain HmilyTransaction}
*/
public void updateStartStatus(final HmilyTransaction hmilyTransaction) {
updateHmilyTransactionStatus(hmilyTransaction);
HmilyRepositoryStorage.updateHmilyTransactionStatus(hmilyTransaction);
HmilyParticipant hmilyParticipant = filterStartHmilyParticipant(hmilyTransaction);
if (Objects.nonNull(hmilyParticipant)) {
hmilyParticipant.setStatus(hmilyTransaction.getStatus());
updateHmilyParticipantStatus(hmilyParticipant);
}
}
public void updateHmilyTransactionStatus(final HmilyTransaction hmilyTransaction) {
if (Objects.nonNull(hmilyTransaction)) {
PUBLISHER.publishEvent(hmilyTransaction, EventTypeEnum.UPDATE_HMILY_TRANSACTION_STATUS.getCode());
}
}
public void updateHmilyParticipantStatus(final HmilyParticipant hmilyParticipant) {
if (Objects.nonNull(hmilyParticipant)) {
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.UPDATE_HMILY_PARTICIPANT_STATUS.getCode());
}
}
public void removeHmilyParticipant(final HmilyParticipant hmilyParticipant) {
if (null != hmilyParticipant) {
PUBLISHER.publishEvent(hmilyParticipant, EventTypeEnum.REMOVE_HMILY_PARTICIPANT.getCode());
HmilyRepositoryStorage.updateHmilyParticipantStatus(hmilyParticipant);
}
}

View File

@ -27,6 +27,7 @@ import org.dromara.hmily.common.utils.DefaultValueUtils;
import org.dromara.hmily.core.cache.HmilyParticipantCacheManager;
import org.dromara.hmily.core.context.HmilyContextHolder;
import org.dromara.hmily.core.context.HmilyTransactionContext;
import org.dromara.hmily.core.repository.HmilyRepositoryStorage;
import org.dromara.hmily.core.service.HmilyTransactionHandler;
import org.dromara.hmily.tcc.executor.HmilyTccTransactionExecutor;
import org.dromara.hmily.repository.spi.entity.HmilyParticipant;
@ -50,14 +51,14 @@ public class ParticipantHmilyTccTransactionHandler implements HmilyTransactionHa
final Object proceed = point.proceed();
hmilyParticipant.setStatus(HmilyActionEnum.TRYING.getCode());
//update log status to try
executor.updateHmilyParticipantStatus(hmilyParticipant);
HmilyRepositoryStorage.updateHmilyParticipantStatus(hmilyParticipant);
return proceed;
} catch (Throwable throwable) {
//if exception ,delete log.
if (Objects.nonNull(hmilyParticipant)) {
HmilyParticipantCacheManager.getInstance().removeByKey(hmilyParticipant.getParticipantId());
}
executor.removeHmilyParticipant(hmilyParticipant);
HmilyRepositoryStorage.removeHmilyParticipant(hmilyParticipant);
throw throwable;
} finally {
HmilyContextHolder.remove();