Search in sources :

Example 6 with InstanceConfig

use of com.ctrip.framework.apollo.biz.entity.InstanceConfig in project apollo by ctripcorp.

the class InstanceConfigController method getByReleasesNotIn.

@GetMapping("/by-namespace-and-releases-not-in")
public List<InstanceDTO> getByReleasesNotIn(@RequestParam("appId") String appId, @RequestParam("clusterName") String clusterName, @RequestParam("namespaceName") String namespaceName, @RequestParam("releaseIds") String releaseIds) {
    Set<Long> releaseIdSet = RELEASES_SPLITTER.splitToList(releaseIds).stream().map(Long::parseLong).collect(Collectors.toSet());
    List<Release> releases = releaseService.findByReleaseIds(releaseIdSet);
    if (CollectionUtils.isEmpty(releases)) {
        throw new NotFoundException("releases not found for %s", releaseIds);
    }
    Set<String> releaseKeys = releases.stream().map(Release::getReleaseKey).collect(Collectors.toSet());
    List<InstanceConfig> instanceConfigs = instanceService.findInstanceConfigsByNamespaceWithReleaseKeysNotIn(appId, clusterName, namespaceName, releaseKeys);
    Multimap<Long, InstanceConfig> instanceConfigMap = HashMultimap.create();
    Set<String> otherReleaseKeys = Sets.newHashSet();
    for (InstanceConfig instanceConfig : instanceConfigs) {
        instanceConfigMap.put(instanceConfig.getInstanceId(), instanceConfig);
        otherReleaseKeys.add(instanceConfig.getReleaseKey());
    }
    List<Instance> instances = instanceService.findInstancesByIds(instanceConfigMap.keySet());
    if (CollectionUtils.isEmpty(instances)) {
        return Collections.emptyList();
    }
    List<InstanceDTO> instanceDTOs = BeanUtils.batchTransform(InstanceDTO.class, instances);
    List<Release> otherReleases = releaseService.findByReleaseKeys(otherReleaseKeys);
    Map<String, ReleaseDTO> releaseMap = Maps.newHashMap();
    for (Release release : otherReleases) {
        // unset configurations to save space
        release.setConfigurations(null);
        ReleaseDTO releaseDTO = BeanUtils.transform(ReleaseDTO.class, release);
        releaseMap.put(release.getReleaseKey(), releaseDTO);
    }
    for (InstanceDTO instanceDTO : instanceDTOs) {
        Collection<InstanceConfig> configs = instanceConfigMap.get(instanceDTO.getId());
        List<InstanceConfigDTO> configDTOs = configs.stream().map(instanceConfig -> {
            InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO();
            instanceConfigDTO.setRelease(releaseMap.get(instanceConfig.getReleaseKey()));
            instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime());
            instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig.getDataChangeLastModifiedTime());
            return instanceConfigDTO;
        }).collect(Collectors.toList());
        instanceDTO.setConfigs(configDTOs);
    }
    return instanceDTOs;
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) Multimap(com.google.common.collect.Multimap) BeanUtils(com.ctrip.framework.apollo.common.utils.BeanUtils) Strings(com.google.common.base.Strings) HashMultimap(com.google.common.collect.HashMultimap) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) Instance(com.ctrip.framework.apollo.biz.entity.Instance) Map(java.util.Map) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) GetMapping(org.springframework.web.bind.annotation.GetMapping) InstanceService(com.ctrip.framework.apollo.biz.service.InstanceService) Pageable(org.springframework.data.domain.Pageable) Splitter(com.google.common.base.Splitter) Collection(java.util.Collection) ReleaseService(com.ctrip.framework.apollo.biz.service.ReleaseService) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) Release(com.ctrip.framework.apollo.biz.entity.Release) Page(org.springframework.data.domain.Page) Maps(com.google.common.collect.Maps) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) CollectionUtils(org.springframework.util.CollectionUtils) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Collections(java.util.Collections) Instance(com.ctrip.framework.apollo.biz.entity.Instance) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 7 with InstanceConfig

use of com.ctrip.framework.apollo.biz.entity.InstanceConfig in project apollo by ctripcorp.

the class InstanceConfigController method getByRelease.

@GetMapping("/by-release")
public PageDTO<InstanceDTO> getByRelease(@RequestParam("releaseId") long releaseId, Pageable pageable) {
    Release release = releaseService.findOne(releaseId);
    if (release == null) {
        throw new NotFoundException(String.format("release not found for %s", releaseId));
    }
    Page<InstanceConfig> instanceConfigsPage = instanceService.findActiveInstanceConfigsByReleaseKey(release.getReleaseKey(), pageable);
    List<InstanceDTO> instanceDTOs = Collections.emptyList();
    if (instanceConfigsPage.hasContent()) {
        Multimap<Long, InstanceConfig> instanceConfigMap = HashMultimap.create();
        Set<String> otherReleaseKeys = Sets.newHashSet();
        for (InstanceConfig instanceConfig : instanceConfigsPage.getContent()) {
            instanceConfigMap.put(instanceConfig.getInstanceId(), instanceConfig);
            otherReleaseKeys.add(instanceConfig.getReleaseKey());
        }
        Set<Long> instanceIds = instanceConfigMap.keySet();
        List<Instance> instances = instanceService.findInstancesByIds(instanceIds);
        if (!CollectionUtils.isEmpty(instances)) {
            instanceDTOs = BeanUtils.batchTransform(InstanceDTO.class, instances);
        }
        for (InstanceDTO instanceDTO : instanceDTOs) {
            Collection<InstanceConfig> configs = instanceConfigMap.get(instanceDTO.getId());
            List<InstanceConfigDTO> configDTOs = configs.stream().map(instanceConfig -> {
                InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO();
                // to save some space
                instanceConfigDTO.setRelease(null);
                instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime());
                instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig.getDataChangeLastModifiedTime());
                return instanceConfigDTO;
            }).collect(Collectors.toList());
            instanceDTO.setConfigs(configDTOs);
        }
    }
    return new PageDTO<>(instanceDTOs, pageable, instanceConfigsPage.getTotalElements());
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) Multimap(com.google.common.collect.Multimap) BeanUtils(com.ctrip.framework.apollo.common.utils.BeanUtils) Strings(com.google.common.base.Strings) HashMultimap(com.google.common.collect.HashMultimap) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) Instance(com.ctrip.framework.apollo.biz.entity.Instance) Map(java.util.Map) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) GetMapping(org.springframework.web.bind.annotation.GetMapping) InstanceService(com.ctrip.framework.apollo.biz.service.InstanceService) Pageable(org.springframework.data.domain.Pageable) Splitter(com.google.common.base.Splitter) Collection(java.util.Collection) ReleaseService(com.ctrip.framework.apollo.biz.service.ReleaseService) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) Release(com.ctrip.framework.apollo.biz.entity.Release) Page(org.springframework.data.domain.Page) Maps(com.google.common.collect.Maps) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) CollectionUtils(org.springframework.util.CollectionUtils) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Collections(java.util.Collections) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) Instance(com.ctrip.framework.apollo.biz.entity.Instance) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 8 with InstanceConfig

use of com.ctrip.framework.apollo.biz.entity.InstanceConfig in project apollo by ctripcorp.

the class InstanceConfigControllerTest method getByRelease.

@Test
public void getByRelease() throws Exception {
    long someReleaseId = 1;
    long someInstanceId = 1;
    long anotherInstanceId = 2;
    String someReleaseKey = "someKey";
    Release someRelease = new Release();
    someRelease.setReleaseKey(someReleaseKey);
    String someAppId = "someAppId";
    String anotherAppId = "anotherAppId";
    String someCluster = "someCluster";
    String someDataCenter = "someDC";
    String someConfigAppId = "someConfigAppId";
    String someConfigNamespace = "someNamespace";
    String someIp = "someIp";
    Date someReleaseDeliveryTime = new Date();
    Date anotherReleaseDeliveryTime = new Date();
    when(releaseService.findOne(someReleaseId)).thenReturn(someRelease);
    InstanceConfig someInstanceConfig = assembleInstanceConfig(someInstanceId, someConfigAppId, someConfigNamespace, someReleaseKey, someReleaseDeliveryTime);
    InstanceConfig anotherInstanceConfig = assembleInstanceConfig(anotherInstanceId, someConfigAppId, someConfigNamespace, someReleaseKey, anotherReleaseDeliveryTime);
    List<InstanceConfig> instanceConfigs = Lists.newArrayList(someInstanceConfig, anotherInstanceConfig);
    Page<InstanceConfig> instanceConfigPage = new PageImpl<>(instanceConfigs, pageable, instanceConfigs.size());
    when(instanceService.findActiveInstanceConfigsByReleaseKey(someReleaseKey, pageable)).thenReturn(instanceConfigPage);
    Instance someInstance = assembleInstance(someInstanceId, someAppId, someCluster, someDataCenter, someIp);
    Instance anotherInstance = assembleInstance(anotherInstanceId, anotherAppId, someCluster, someDataCenter, someIp);
    List<Instance> instances = Lists.newArrayList(someInstance, anotherInstance);
    Set<Long> instanceIds = Sets.newHashSet(someInstanceId, anotherInstanceId);
    when(instanceService.findInstancesByIds(instanceIds)).thenReturn(instances);
    PageDTO<InstanceDTO> result = instanceConfigController.getByRelease(someReleaseId, pageable);
    assertEquals(2, result.getContent().size());
    InstanceDTO someInstanceDto = null;
    InstanceDTO anotherInstanceDto = null;
    for (InstanceDTO instanceDTO : result.getContent()) {
        if (instanceDTO.getId() == someInstanceId) {
            someInstanceDto = instanceDTO;
        } else if (instanceDTO.getId() == anotherInstanceId) {
            anotherInstanceDto = instanceDTO;
        }
    }
    verifyInstance(someInstance, someInstanceDto);
    verifyInstance(anotherInstance, anotherInstanceDto);
    assertEquals(1, someInstanceDto.getConfigs().size());
    assertEquals(someReleaseDeliveryTime, someInstanceDto.getConfigs().get(0).getReleaseDeliveryTime());
    assertEquals(1, anotherInstanceDto.getConfigs().size());
    assertEquals(anotherReleaseDeliveryTime, anotherInstanceDto.getConfigs().get(0).getReleaseDeliveryTime());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Instance(com.ctrip.framework.apollo.biz.entity.Instance) Date(java.util.Date) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) Release(com.ctrip.framework.apollo.biz.entity.Release) Test(org.junit.Test)

Example 9 with InstanceConfig

use of com.ctrip.framework.apollo.biz.entity.InstanceConfig in project apollo by ctripcorp.

the class InstanceConfigControllerTest method assembleInstanceConfig.

private InstanceConfig assembleInstanceConfig(long instanceId, String configAppId, String configNamespaceName, String releaseKey, Date releaseDeliveryTime) {
    InstanceConfig instanceConfig = new InstanceConfig();
    instanceConfig.setInstanceId(instanceId);
    instanceConfig.setConfigAppId(configAppId);
    instanceConfig.setConfigNamespaceName(configNamespaceName);
    instanceConfig.setReleaseKey(releaseKey);
    instanceConfig.setDataChangeLastModifiedTime(new Date());
    instanceConfig.setReleaseDeliveryTime(releaseDeliveryTime);
    return instanceConfig;
}
Also used : InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) Date(java.util.Date)

Example 10 with InstanceConfig

use of com.ctrip.framework.apollo.biz.entity.InstanceConfig in project apollo by ctripcorp.

the class InstanceConfigAuditUtil method doAudit.

void doAudit(InstanceConfigAuditModel auditModel) {
    String instanceCacheKey = assembleInstanceKey(auditModel.getAppId(), auditModel.getClusterName(), auditModel.getIp(), auditModel.getDataCenter());
    Long instanceId = instanceCache.getIfPresent(instanceCacheKey);
    if (instanceId == null) {
        instanceId = prepareInstanceId(auditModel);
        instanceCache.put(instanceCacheKey, instanceId);
    }
    // load instance config release key from cache, and check if release key is the same
    String instanceConfigCacheKey = assembleInstanceConfigKey(instanceId, auditModel.getConfigAppId(), auditModel.getConfigNamespace());
    String cacheReleaseKey = instanceConfigReleaseKeyCache.getIfPresent(instanceConfigCacheKey);
    // if release key is the same, then skip audit
    if (cacheReleaseKey != null && Objects.equals(cacheReleaseKey, auditModel.getReleaseKey())) {
        return;
    }
    instanceConfigReleaseKeyCache.put(instanceConfigCacheKey, auditModel.getReleaseKey());
    // if release key is not the same or cannot find in cache, then do audit
    InstanceConfig instanceConfig = instanceService.findInstanceConfig(instanceId, auditModel.getConfigAppId(), auditModel.getConfigNamespace());
    if (instanceConfig != null) {
        if (!Objects.equals(instanceConfig.getReleaseKey(), auditModel.getReleaseKey())) {
            instanceConfig.setConfigClusterName(auditModel.getConfigClusterName());
            instanceConfig.setReleaseKey(auditModel.getReleaseKey());
            instanceConfig.setReleaseDeliveryTime(auditModel.getOfferTime());
        } else if (offerTimeAndLastModifiedTimeCloseEnough(auditModel.getOfferTime(), instanceConfig.getDataChangeLastModifiedTime())) {
            // when releaseKey is the same, optimize to reduce writes if the record was updated not long ago
            return;
        }
        // we need to update no matter the release key is the same or not, to ensure the
        // last modified time is updated each day
        instanceConfig.setDataChangeLastModifiedTime(auditModel.getOfferTime());
        instanceService.updateInstanceConfig(instanceConfig);
        return;
    }
    instanceConfig = new InstanceConfig();
    instanceConfig.setInstanceId(instanceId);
    instanceConfig.setConfigAppId(auditModel.getConfigAppId());
    instanceConfig.setConfigClusterName(auditModel.getConfigClusterName());
    instanceConfig.setConfigNamespaceName(auditModel.getConfigNamespace());
    instanceConfig.setReleaseKey(auditModel.getReleaseKey());
    instanceConfig.setReleaseDeliveryTime(auditModel.getOfferTime());
    instanceConfig.setDataChangeCreatedTime(auditModel.getOfferTime());
    try {
        instanceService.createInstanceConfig(instanceConfig);
    } catch (DataIntegrityViolationException ex) {
    // concurrent insertion, safe to ignore
    }
}
Also used : InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)14 Test (org.junit.Test)7 Date (java.util.Date)6 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)5 Instance (com.ctrip.framework.apollo.biz.entity.Instance)5 Release (com.ctrip.framework.apollo.biz.entity.Release)5 InstanceDTO (com.ctrip.framework.apollo.common.dto.InstanceDTO)4 Rollback (org.springframework.test.annotation.Rollback)4 Pageable (org.springframework.data.domain.Pageable)3 InstanceService (com.ctrip.framework.apollo.biz.service.InstanceService)2 ReleaseService (com.ctrip.framework.apollo.biz.service.ReleaseService)2 InstanceConfigDTO (com.ctrip.framework.apollo.common.dto.InstanceConfigDTO)2 PageDTO (com.ctrip.framework.apollo.common.dto.PageDTO)2 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)2 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)2 BeanUtils (com.ctrip.framework.apollo.common.utils.BeanUtils)2 Splitter (com.google.common.base.Splitter)2 Strings (com.google.common.base.Strings)2 HashMultimap (com.google.common.collect.HashMultimap)2 Maps (com.google.common.collect.Maps)2