in ozhera-log/log-manager/src/main/java/org/apache/ozhera/log/manager/service/impl/MilogConfigNacosServiceImpl.java [304:398]
private synchronized MilogSpaceData dealSpaceConfigByRule(
String motorRoomEn, Long spaceId, Long storeId, Long tailId, Integer type, String changeType) {
MilogSpaceData existConfig = getSpaceConfigNacosProvider(motorRoomEn).getConfig(spaceId);
// new configuration
if (null == existConfig || OperateEnum.ADD_OPERATE.getCode().equals(type)) {
// The configuration is not configured yet, initialize the configuration
if (null == existConfig || CollectionUtils.isEmpty(existConfig.getSpaceConfig())) {
existConfig = new MilogSpaceData();
existConfig.setMilogSpaceId(spaceId);
List<SinkConfig> spaceConfigs = Lists.newArrayList();
spaceConfigs.add(assembleSinkConfig(storeId, tailId, motorRoomEn));
existConfig.setSpaceConfig(spaceConfigs);
} else {
List<SinkConfig> spaceConfig = existConfig.getSpaceConfig();
SinkConfig currentStoreConfig = spaceConfig.stream()
.filter(sinkConfig -> sinkConfig.getLogstoreId().equals(storeId))
.findFirst()
.orElse(null);
existConfig.setMilogSpaceId(spaceId);
if (null != currentStoreConfig) {
List<LogtailConfig> logtailConfigs = currentStoreConfig.getLogtailConfigs();
if (CollectionUtils.isEmpty(logtailConfigs)) {
logtailConfigs = Lists.newArrayList();
}
logtailConfigs.add(assembleLogTailConfigs(tailId));
currentStoreConfig.setLogtailConfigs(logtailConfigs);
} else {
//New addition to the log Store
spaceConfig.add(assembleSinkConfig(storeId, tailId, motorRoomEn));
}
}
}
// Delete configuration -- Delete log-tail
if (OperateEnum.DELETE_OPERATE.getCode().equals(type) && !LOG_STORE.equalsIgnoreCase(changeType)) {
if (null != existConfig) {
List<SinkConfig> spaceConfig = existConfig.getSpaceConfig();
SinkConfig currentStoreConfig = spaceConfig.stream()
.filter(sinkConfig -> sinkConfig.getLogstoreId().equals(storeId))
.findFirst()
.orElse(null);
if (null != currentStoreConfig) {
List<LogtailConfig> logTailConfigs = currentStoreConfig.getLogtailConfigs();
List<LogtailConfig> logtailConfigList = new ArrayList<>(logTailConfigs);
if (null != tailId && CollectionUtils.isNotEmpty(logTailConfigs) &&
logTailConfigs.stream().anyMatch(config -> config.getLogtailId().equals(tailId))) {
logtailConfigList.removeIf(logtailConfig -> logtailConfig.getLogtailId().equals(tailId));
}
currentStoreConfig.setLogtailConfigs(logtailConfigList);
}
}
}
// Delete configuration -- Delete log-tail
if (OperateEnum.DELETE_OPERATE.getCode().equals(type) && LOG_STORE.equalsIgnoreCase(changeType)) {
if (null != existConfig) {
List<SinkConfig> sinkConfigListDelStore = existConfig.getSpaceConfig().stream()
.filter(sinkConfig -> !storeId.equals(sinkConfig.getLogstoreId()))
.collect(Collectors.toList());
existConfig.setSpaceConfig(sinkConfigListDelStore);
}
}
// Modify the configuration -- find a specific tail under this store to make changes
if (OperateEnum.UPDATE_OPERATE.getCode().equals(type)) {
if (null != existConfig) {
List<SinkConfig> spaceConfig = existConfig.getSpaceConfig();
//Compare whether the store has changed
SinkConfig newSinkConfig = assembleSinkConfig(storeId, tailId, motorRoomEn);
SinkConfig currentStoreConfig = spaceConfig.stream()
.filter(sinkConfig -> sinkConfig.getLogstoreId().equals(storeId))
.findFirst()
.orElse(null);
if (null != currentStoreConfig) {
if (!newSinkConfig.equals(currentStoreConfig)) {
currentStoreConfig.updateStoreParam(newSinkConfig);
}
// Find the specific tail under the old store
LogtailConfig filterLogTailConfig = currentStoreConfig.getLogtailConfigs().stream()
.filter(logTailConfig -> Objects.equals(tailId, logTailConfig.getLogtailId()))
.findFirst()
.orElse(null);
if (null != filterLogTailConfig) {
BeanUtil.copyProperties(assembleLogTailConfigs(tailId), filterLogTailConfig);
} else {
log.info("query logtailConfig no designed config,tailId:{},insert", tailId);
currentStoreConfig.getLogtailConfigs().add(assembleLogTailConfigs(tailId));
}
} else {
//Does not exist, new
//New addition to the logstore
spaceConfig.add(assembleSinkConfig(storeId, tailId, motorRoomEn));
}
}
}
return existConfig;
}