Search in sources :

Example 11 with CacheItem

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

the class ConfigCacheService method dump.

/**
 * Save config file and update md5 value in cache.
 *
 * @param dataId         dataId string value.
 * @param group          group string value.
 * @param tenant         tenant string value.
 * @param content        content string value.
 * @param lastModifiedTs lastModifiedTs.
 * @param type           file type.
 * @return dumpChange success or not.
 */
public static boolean dump(String dataId, String group, String tenant, String content, long lastModifiedTs, String type, String encryptedDataKey) {
    String groupKey = GroupKey2.getKey(dataId, group, tenant);
    CacheItem ci = makeSure(groupKey, encryptedDataKey, false);
    ci.setType(type);
    final int lockResult = tryWriteLock(groupKey);
    assert (lockResult != 0);
    if (lockResult < 0) {
        DUMP_LOG.warn("[dump-error] write lock failed. {}", groupKey);
        return false;
    }
    try {
        final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
        if (lastModifiedTs < ConfigCacheService.getLastModifiedTs(groupKey)) {
            DUMP_LOG.warn("[dump-ignore] the content is old. groupKey={}, md5={}, lastModifiedOld={}, " + "lastModifiedNew={}", groupKey, md5, ConfigCacheService.getLastModifiedTs(groupKey), lastModifiedTs);
            return true;
        }
        if (md5.equals(ConfigCacheService.getContentMd5(groupKey)) && DiskUtil.targetFile(dataId, group, tenant).exists()) {
            DUMP_LOG.warn("[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " + "lastModifiedNew={}", groupKey, md5, ConfigCacheService.getLastModifiedTs(groupKey), lastModifiedTs);
        } else if (!PropertyUtil.isDirectRead()) {
            DiskUtil.saveToDisk(dataId, group, tenant, content);
        }
        updateMd5(groupKey, md5, lastModifiedTs, encryptedDataKey);
        return true;
    } catch (IOException ioe) {
        DUMP_LOG.error("[dump-exception] save disk error. " + groupKey + ", " + ioe);
        if (ioe.getMessage() != null) {
            String errMsg = ioe.getMessage();
            if (NO_SPACE_CN.equals(errMsg) || NO_SPACE_EN.equals(errMsg) || errMsg.contains(DISK_QUATA_CN) || errMsg.contains(DISK_QUATA_EN)) {
                // Protect from disk full.
                FATAL_LOG.error("磁盘满自杀退出", ioe);
                System.exit(0);
            }
        }
        return false;
    } finally {
        releaseWriteLock(groupKey);
    }
}
Also used : CacheItem(com.alibaba.nacos.config.server.model.CacheItem) IOException(java.io.IOException)

Example 12 with CacheItem

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

the class ConfigCacheService method tryReadLock.

/**
 * Try to add read 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 - lock succeeded. Negative number - lock failed。
 */
public static int tryReadLock(String groupKey) {
    CacheItem groupItem = CACHE.get(groupKey);
    int result = (null == groupItem) ? 0 : (groupItem.rwLock.tryReadLock() ? 1 : -1);
    if (result < 0) {
        DEFAULT_LOG.warn("[read-lock] failed, {}, {}", result, groupKey);
    }
    return result;
}
Also used : CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Example 13 with CacheItem

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

the class ConfigCacheService method updateBetaMd5.

/**
 * Update Beta md5 value.
 *
 * @param groupKey       groupKey string value.
 * @param md5            md5 string value.
 * @param ips4Beta       ips4Beta List.
 * @param lastModifiedTs lastModifiedTs long value.
 */
public static void updateBetaMd5(String groupKey, String md5, List<String> ips4Beta, long lastModifiedTs, String encryptedDataKey) {
    CacheItem cache = makeSure(groupKey, encryptedDataKey, true);
    if (cache.md54Beta == null || !cache.md54Beta.equals(md5)) {
        cache.isBeta = true;
        cache.md54Beta = md5;
        cache.lastModifiedTs4Beta = lastModifiedTs;
        cache.ips4Beta = ips4Beta;
        NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey, true, ips4Beta));
    }
}
Also used : LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) CacheItem(com.alibaba.nacos.config.server.model.CacheItem)

Example 14 with CacheItem

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

the class ConfigCacheService method updateTagMd5.

/**
 * Update tag md5 value.
 *
 * @param groupKey       groupKey string value.
 * @param tag            tag string value.
 * @param md5            md5 string value.
 * @param lastModifiedTs lastModifiedTs long value.
 */
public static void updateTagMd5(String groupKey, String tag, String md5, long lastModifiedTs, String encryptedDataKey) {
    CacheItem cache = makeSure(groupKey, encryptedDataKey, false);
    if (cache.tagMd5 == null) {
        Map<String, String> tagMd5Tmp = new HashMap<>(1);
        tagMd5Tmp.put(tag, md5);
        cache.tagMd5 = tagMd5Tmp;
        if (cache.tagLastModifiedTs == null) {
            Map<String, Long> tagLastModifiedTsTmp = new HashMap<>(1);
            tagLastModifiedTsTmp.put(tag, lastModifiedTs);
            cache.tagLastModifiedTs = tagLastModifiedTsTmp;
        } else {
            cache.tagLastModifiedTs.put(tag, lastModifiedTs);
        }
        NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey, false, null, tag));
        return;
    }
    if (cache.tagMd5.get(tag) == null || !cache.tagMd5.get(tag).equals(md5)) {
        cache.tagMd5.put(tag, md5);
        cache.tagLastModifiedTs.put(tag, lastModifiedTs);
        NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey, false, null, tag));
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) 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