|
| 1 | +package com.ctrip.framework.apollo.adminservice.aop; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.collect.Maps; |
| 5 | +import com.google.gson.Gson; |
| 6 | + |
| 7 | +import com.ctrip.framework.apollo.biz.config.BizConfig; |
| 8 | +import com.ctrip.framework.apollo.biz.entity.Item; |
| 9 | +import com.ctrip.framework.apollo.biz.entity.Namespace; |
| 10 | +import com.ctrip.framework.apollo.biz.entity.Release; |
| 11 | +import com.ctrip.framework.apollo.biz.service.ItemService; |
| 12 | +import com.ctrip.framework.apollo.biz.service.NamespaceLockService; |
| 13 | +import com.ctrip.framework.apollo.biz.service.NamespaceService; |
| 14 | +import com.ctrip.framework.apollo.biz.service.ReleaseService; |
| 15 | +import com.ctrip.framework.apollo.common.constants.GsonType; |
| 16 | +import com.ctrip.framework.apollo.common.dto.ItemChangeSets; |
| 17 | +import com.ctrip.framework.apollo.common.dto.ItemDTO; |
| 18 | +import com.ctrip.framework.apollo.common.exception.BadRequestException; |
| 19 | +import com.ctrip.framework.apollo.core.utils.StringUtils; |
| 20 | + |
| 21 | +import org.aspectj.lang.annotation.After; |
| 22 | +import org.aspectj.lang.annotation.Aspect; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.stereotype.Component; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * unlock namespace if is redo operation. |
| 33 | + * -------------------------------------------- |
| 34 | + * For example: If namespace has a item K1 = v1 |
| 35 | + * -------------------------------------------- |
| 36 | + * First operate: change k1 = v2 (lock namespace) |
| 37 | + * Second operate: change k1 = v1 (unlock namespace) |
| 38 | + * |
| 39 | + */ |
| 40 | +@Aspect |
| 41 | +@Component |
| 42 | +public class NamespaceUnlockAspect { |
| 43 | + |
| 44 | + private Gson gson = new Gson(); |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private NamespaceLockService namespaceLockService; |
| 48 | + @Autowired |
| 49 | + private NamespaceService namespaceService; |
| 50 | + @Autowired |
| 51 | + private ItemService itemService; |
| 52 | + @Autowired |
| 53 | + private ReleaseService releaseService; |
| 54 | + @Autowired |
| 55 | + private BizConfig bizConfig; |
| 56 | + |
| 57 | + |
| 58 | + //create item |
| 59 | + @After("@annotation(PreAcquireNamespaceLock) && args(appId, clusterName, namespaceName, item, ..)") |
| 60 | + public void requireLockAdvice(String appId, String clusterName, String namespaceName, |
| 61 | + ItemDTO item) { |
| 62 | + tryUnlock(namespaceService.findOne(appId, clusterName, namespaceName)); |
| 63 | + } |
| 64 | + |
| 65 | + //update item |
| 66 | + @After("@annotation(PreAcquireNamespaceLock) && args(appId, clusterName, namespaceName, itemId, item, ..)") |
| 67 | + public void requireLockAdvice(String appId, String clusterName, String namespaceName, long itemId, |
| 68 | + ItemDTO item) { |
| 69 | + tryUnlock(namespaceService.findOne(appId, clusterName, namespaceName)); |
| 70 | + } |
| 71 | + |
| 72 | + //update by change set |
| 73 | + @After("@annotation(PreAcquireNamespaceLock) && args(appId, clusterName, namespaceName, changeSet, ..)") |
| 74 | + public void requireLockAdvice(String appId, String clusterName, String namespaceName, |
| 75 | + ItemChangeSets changeSet) { |
| 76 | + tryUnlock(namespaceService.findOne(appId, clusterName, namespaceName)); |
| 77 | + } |
| 78 | + |
| 79 | + //delete item |
| 80 | + @After("@annotation(PreAcquireNamespaceLock) && args(itemId, operator, ..)") |
| 81 | + public void requireLockAdvice(long itemId, String operator) { |
| 82 | + Item item = itemService.findOne(itemId); |
| 83 | + if (item == null) { |
| 84 | + throw new BadRequestException("item not exist."); |
| 85 | + } |
| 86 | + tryUnlock(namespaceService.findOne(item.getNamespaceId())); |
| 87 | + } |
| 88 | + |
| 89 | + private void tryUnlock(Namespace namespace) { |
| 90 | + if (bizConfig.isNamespaceLockSwitchOff()) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (!isModified(namespace)) { |
| 95 | + namespaceLockService.unlock(namespace.getId()); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + boolean isModified(Namespace namespace) { |
| 101 | + Release release = releaseService.findLatestActiveRelease(namespace); |
| 102 | + List<Item> items = itemService.findItems(namespace.getId()); |
| 103 | + |
| 104 | + if (release == null) { |
| 105 | + return hasNormalItems(items); |
| 106 | + } |
| 107 | + |
| 108 | + Map<String, String> releasedConfiguration = gson.fromJson(release.getConfigurations(), GsonType.CONFIG); |
| 109 | + Map<String, String> configurationFromItems = Maps.newHashMap(); |
| 110 | + |
| 111 | + for (Item item : items) { |
| 112 | + String key = item.getKey(); |
| 113 | + if (StringUtils.isBlank(key)) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + //added |
| 117 | + if (releasedConfiguration.get(key) == null) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + configurationFromItems.put(key, item.getValue()); |
| 121 | + } |
| 122 | + |
| 123 | + for (Map.Entry<String, String> entry : releasedConfiguration.entrySet()) { |
| 124 | + String key = entry.getKey(); |
| 125 | + String value = entry.getValue(); |
| 126 | + |
| 127 | + //deleted or modified |
| 128 | + if (!Objects.equals(configurationFromItems.get(key), value)) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + private boolean hasNormalItems(List<Item> items) { |
| 138 | + for (Item item : items) { |
| 139 | + if (!StringUtils.isEmpty(item.getKey())) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments