Search in sources :

Example 1 with CacheItem

use of com.alibaba.nacos.config.server.model.CacheItem in project nacos by alibaba.

the class ConfigCacheService method makeSure.

static CacheItem makeSure(final String groupKey) {
    CacheItem item = CACHE.get(groupKey);
    if (null != item) {
        return item;
    }
    CacheItem tmp = new CacheItem(groupKey);
    item = CACHE.putIfAbsent(groupKey, tmp);
    return (null == item) ? tmp : item;
}
Also used : CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Example 2 with CacheItem

use of com.alibaba.nacos.config.server.model.CacheItem in project nacos by alibaba.

the class ConfigCacheService method makeSure.

static CacheItem makeSure(final String groupKey, String encryptedDataKey, boolean isBeta) {
    CacheItem item = CACHE.get(groupKey);
    if (null != item) {
        setEncryptDateKey(item, encryptedDataKey, isBeta);
        return item;
    }
    CacheItem tmp = new CacheItem(groupKey);
    setEncryptDateKey(tmp, encryptedDataKey, isBeta);
    item = CACHE.putIfAbsent(groupKey, tmp);
    return (null == item) ? tmp : item;
}
Also used : CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Example 3 with CacheItem

use of com.alibaba.nacos.config.server.model.CacheItem in project nacos by alibaba.

the class ConfigCacheService method removeTag.

/**
 * Delete tag config file, and delete cache.
 *
 * @param dataId dataId string value.
 * @param group  group string value.
 * @param tenant tenant string value.
 * @param tag    tag string value.
 * @return remove success or not.
 */
public static boolean removeTag(String dataId, String group, String tenant, String tag) {
    final String groupKey = GroupKey2.getKey(dataId, group, tenant);
    final int lockResult = tryWriteLock(groupKey);
    // If data is non-existent.
    if (0 == lockResult) {
        DUMP_LOG.info("[remove-ok] {} not exist.", groupKey);
        return true;
    }
    // try to lock failed
    if (lockResult < 0) {
        DUMP_LOG.warn("[remove-error] write lock failed. {}", groupKey);
        return false;
    }
    try {
        if (!PropertyUtil.isDirectRead()) {
            DiskUtil.removeConfigInfo4Tag(dataId, group, tenant, tag);
        }
        CacheItem ci = CACHE.get(groupKey);
        ci.tagMd5.remove(tag);
        ci.tagLastModifiedTs.remove(tag);
        NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey, false, null, tag));
        return true;
    } finally {
        releaseWriteLock(groupKey);
    }
}
Also used : LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Example 4 with CacheItem

use of com.alibaba.nacos.config.server.model.CacheItem in project nacos by alibaba.

the class ConfigCacheService method updateMd5.

/**
 * Update md5 value.
 *
 * @param groupKey       groupKey string value.
 * @param md5            md5 string value.
 * @param lastModifiedTs lastModifiedTs long value.
 */
public static void updateMd5(String groupKey, String md5, long lastModifiedTs, String encryptedDataKey) {
    CacheItem cache = makeSure(groupKey, encryptedDataKey, false);
    if (cache.md5 == null || !cache.md5.equals(md5)) {
        cache.md5 = md5;
        cache.lastModifiedTs = lastModifiedTs;
        NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey));
    }
}
Also used : LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Example 5 with CacheItem

use of com.alibaba.nacos.config.server.model.CacheItem in project nacos by alibaba.

the class ConfigCacheService method tryWriteLock.

/**
 * Try to add write lock. If it succeeded, then it can call {@link #releaseWriteLock(String)}.And it won't call if
 * failed.
 *
 * @param groupKey groupKey string value.
 * @return 0 - No data and failed. Positive number 0 - Success. Negative number - lock failed。
 */
static int tryWriteLock(String groupKey) {
    CacheItem groupItem = CACHE.get(groupKey);
    int result = (null == groupItem) ? 0 : (groupItem.rwLock.tryWriteLock() ? 1 : -1);
    if (result < 0) {
        DEFAULT_LOG.warn("[write-lock] failed, {}, {}", result, groupKey);
    }
    return result;
}
Also used : CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Aggregations

CacheItem (com.alibaba.nacos.config.server.model.CacheItem)14 LocalDataChangeEvent (com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent)4 File (java.io.File)4 ConfigInfoBase (com.alibaba.nacos.config.server.model.ConfigInfoBase)2 ConfigCacheService (com.alibaba.nacos.config.server.service.ConfigCacheService)2 DiskUtil (com.alibaba.nacos.config.server.utils.DiskUtil)2 PropertyUtil (com.alibaba.nacos.config.server.utils.PropertyUtil)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 ConfigQueryResponse (com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse)1 FileTypeEnum (com.alibaba.nacos.config.server.enums.FileTypeEnum)1 ConfigInfoBetaWrapper (com.alibaba.nacos.config.server.model.ConfigInfoBetaWrapper)1 ConfigInfoTagWrapper (com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper)1 ConfigInfoWrapper (com.alibaba.nacos.config.server.model.ConfigInfoWrapper)1