Search in sources :

Example 6 with ConfigInfoWrapper

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

the class EmbeddedStoragePersistServiceImpl method convertChangeConfig.

@Override
public List<ConfigInfoWrapper> convertChangeConfig(List<Map<String, Object>> list) {
    List<ConfigInfoWrapper> configs = new ArrayList<ConfigInfoWrapper>();
    for (Map<String, Object> map : list) {
        String dataId = (String) map.get("data_id");
        String group = (String) map.get("group_id");
        String tenant = (String) map.get("tenant_id");
        String content = (String) map.get("content");
        long mTime = ((Timestamp) map.get("gmt_modified")).getTime();
        ConfigInfoWrapper config = new ConfigInfoWrapper();
        config.setDataId(dataId);
        config.setGroup(group);
        config.setTenant(tenant);
        config.setContent(content);
        config.setLastModified(mTime);
        configs.add(config);
    }
    return configs;
}
Also used : ArrayList(java.util.ArrayList) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) Timestamp(java.sql.Timestamp)

Example 7 with ConfigInfoWrapper

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

the class ConfigServletInnerTest method testDoGetConfigV2.

@Test
public void testDoGetConfigV2() throws Exception {
    final MockedStatic<DiskUtil> diskUtil = Mockito.mockStatic(DiskUtil.class);
    final MockedStatic<ConfigCacheService> configCacheService = Mockito.mockStatic(ConfigCacheService.class);
    final MockedStatic<PropertyUtil> propertyUtil = Mockito.mockStatic(PropertyUtil.class);
    when(ConfigCacheService.tryReadLock(anyString())).thenReturn(1);
    // isBeta: false
    CacheItem cacheItem = new CacheItem("test");
    cacheItem.setBeta(false);
    List<String> ips4Beta = new ArrayList<>();
    ips4Beta.add("localhost");
    cacheItem.setIps4Beta(ips4Beta);
    when(ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem);
    // if tag is blank and direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    ConfigInfoWrapper configInfoWrapper = new ConfigInfoWrapper();
    configInfoWrapper.setDataId("test");
    configInfoWrapper.setGroup("test");
    configInfoWrapper.setContent("tag is blank and direct read is true");
    when(persistService.findConfigInfo(anyString(), anyString(), anyString())).thenReturn(configInfoWrapper);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("localhost:8080");
    request.addHeader(CLIENT_APPNAME_HEADER, "test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    String actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("tag is blank and direct read is true", response.getContentAsString());
    // if tag is blank and direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    response = new MockHttpServletResponse();
    File file = tempFolder.newFile("test.txt");
    when(DiskUtil.targetFile(anyString(), anyString(), anyString())).thenReturn(file);
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("", response.getContentAsString());
    // if tag is not blank and direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    ConfigInfoTagWrapper configInfoTagWrapper = new ConfigInfoTagWrapper();
    configInfoTagWrapper.setDataId("test");
    configInfoTagWrapper.setGroup("test");
    configInfoTagWrapper.setContent("tag is not blank and direct read is true");
    when(persistService.findConfigInfo4Tag(anyString(), anyString(), anyString(), anyString())).thenReturn(configInfoTagWrapper);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("tag is not blank and direct read is true", response.getContentAsString());
    // if tag is not blank and direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    response = new MockHttpServletResponse();
    when(DiskUtil.targetTagFile(anyString(), anyString(), anyString(), anyString())).thenReturn(file);
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("", response.getContentAsString());
    // if use auto tag and direct read is true
    when(PropertyUtil.isDirectRead()).thenReturn(true);
    Map<String, String> tagMd5 = new HashMap<>();
    tagMd5.put("auto-tag-test", "auto-tag-test");
    cacheItem.setTagMd5(tagMd5);
    request.addHeader("Vipserver-Tag", "auto-tag-test");
    configInfoTagWrapper.setContent("auto tag mode and direct read is true");
    when(persistService.findConfigInfo4Tag(anyString(), anyString(), anyString(), eq("auto-tag-test"))).thenReturn(configInfoTagWrapper);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("auto tag mode and direct read is true", response.getContentAsString());
    // if use auto tag and direct read is false
    when(PropertyUtil.isDirectRead()).thenReturn(false);
    response = new MockHttpServletResponse();
    actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost");
    Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue);
    Assert.assertEquals("", response.getContentAsString());
    diskUtil.close();
    configCacheService.close();
    propertyUtil.close();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ConfigInfoTagWrapper(com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) PropertyUtil(com.alibaba.nacos.config.server.utils.PropertyUtil) DiskUtil(com.alibaba.nacos.config.server.utils.DiskUtil) CacheItem(com.alibaba.nacos.config.server.model.CacheItem) File(java.io.File) ConfigCacheService(com.alibaba.nacos.config.server.service.ConfigCacheService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with ConfigInfoWrapper

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

the class HistoryControllerTest method testGetDataIds.

@Test
public void testGetDataIds() throws Exception {
    ConfigInfoWrapper configInfoWrapper = new ConfigInfoWrapper();
    configInfoWrapper.setDataId("test");
    configInfoWrapper.setGroup("test");
    configInfoWrapper.setContent("test");
    List<ConfigInfoWrapper> configInfoWrappers = new ArrayList<>();
    configInfoWrappers.add(configInfoWrapper);
    when(persistService.queryConfigInfoByNamespace("test")).thenReturn(configInfoWrappers);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HISTORY_CONTROLLER_PATH + "/configs").param("tenant", "test");
    String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
    List resConfigInfoWrappers = JacksonUtils.toObj(actualValue, List.class);
    ConfigInfoWrapper resConfigInfoWrapper = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get(0).toString(), ConfigInfoWrapper.class);
    Assert.assertEquals(configInfoWrappers.size(), resConfigInfoWrappers.size());
    Assert.assertEquals(configInfoWrapper.getDataId(), resConfigInfoWrapper.getDataId());
    Assert.assertEquals(configInfoWrapper.getGroup(), resConfigInfoWrapper.getGroup());
    Assert.assertEquals(configInfoWrapper.getContent(), resConfigInfoWrapper.getContent());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) Test(org.junit.Test)

Example 9 with ConfigInfoWrapper

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

the class DumpChangeProcessor method process.

@Override
public boolean process(NacosTask task) {
    LogUtil.DEFAULT_LOG.warn("quick start; startTime:{},endTime:{}", startTime, endTime);
    LogUtil.DEFAULT_LOG.warn("updateMd5 start");
    long startUpdateMd5 = System.currentTimeMillis();
    List<ConfigInfoWrapper> updateMd5List = persistService.listAllGroupKeyMd5();
    LogUtil.DEFAULT_LOG.warn("updateMd5 count:{}", updateMd5List.size());
    for (ConfigInfoWrapper config : updateMd5List) {
        final String groupKey = GroupKey2.getKey(config.getDataId(), config.getGroup());
        ConfigCacheService.updateMd5(groupKey, config.getMd5(), config.getLastModified(), config.getEncryptedDataKey());
    }
    long endUpdateMd5 = System.currentTimeMillis();
    LogUtil.DEFAULT_LOG.warn("updateMd5 done,cost:{}", endUpdateMd5 - startUpdateMd5);
    LogUtil.DEFAULT_LOG.warn("deletedConfig start");
    long startDeletedConfigTime = System.currentTimeMillis();
    List<ConfigInfo> configDeleted = persistService.findDeletedConfig(startTime, endTime);
    LogUtil.DEFAULT_LOG.warn("deletedConfig count:{}", configDeleted.size());
    for (ConfigInfo configInfo : configDeleted) {
        if (persistService.findConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant()) == null) {
            ConfigCacheService.remove(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
        }
    }
    long endDeletedConfigTime = System.currentTimeMillis();
    LogUtil.DEFAULT_LOG.warn("deletedConfig done,cost:{}", endDeletedConfigTime - startDeletedConfigTime);
    LogUtil.DEFAULT_LOG.warn("changeConfig start");
    final long startChangeConfigTime = System.currentTimeMillis();
    List<ConfigInfoWrapper> changeConfigs = persistService.findChangeConfig(startTime, endTime);
    LogUtil.DEFAULT_LOG.warn("changeConfig count:{}", changeConfigs.size());
    for (ConfigInfoWrapper cf : changeConfigs) {
        ConfigCacheService.dumpChange(cf.getDataId(), cf.getGroup(), cf.getTenant(), cf.getContent(), cf.getLastModified(), cf.getEncryptedDataKey());
        final String content = cf.getContent();
        final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
        LogUtil.DEFAULT_LOG.info("[dump-change-ok] {}, {}, length={}, md5={}", GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5);
    }
    ConfigCacheService.reloadConfig();
    long endChangeConfigTime = System.currentTimeMillis();
    LogUtil.DEFAULT_LOG.warn("changeConfig done,cost:{}", endChangeConfigTime - startChangeConfigTime);
    return true;
}
Also used : ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Aggregations

ConfigInfoWrapper (com.alibaba.nacos.config.server.model.ConfigInfoWrapper)9 Timestamp (java.sql.Timestamp)5 ArrayList (java.util.ArrayList)5 File (java.io.File)2 IOException (java.io.IOException)2 List (java.util.List)2 Test (org.junit.Test)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 CacheItem (com.alibaba.nacos.config.server.model.CacheItem)1 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)1 ConfigInfoTagWrapper (com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper)1 ConfigCacheService (com.alibaba.nacos.config.server.service.ConfigCacheService)1 DumpChangeProcessor (com.alibaba.nacos.config.server.service.dump.processor.DumpChangeProcessor)1 DumpAllTask (com.alibaba.nacos.config.server.service.dump.task.DumpAllTask)1 DumpChangeTask (com.alibaba.nacos.config.server.service.dump.task.DumpChangeTask)1 DiskUtil (com.alibaba.nacos.config.server.utils.DiskUtil)1 PropertyUtil (com.alibaba.nacos.config.server.utils.PropertyUtil)1 FileInputStream (java.io.FileInputStream)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1