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;
}
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();
}
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());
}
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;
}
Aggregations