Skip to content

Commit

Permalink
Feat/mock compare ignore (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryhh authored Feb 21, 2024
1 parent d0921bc commit b13f318
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 5 deletions.
2 changes: 1 addition & 1 deletion arex-storage-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.10</version>
<version>1.1.11</version>
</parent>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion arex-storage-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.10</version>
<version>1.1.11</version>
</parent>

<profiles>
Expand Down
2 changes: 1 addition & 1 deletion arex-storage-web-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.10</version>
<version>1.1.11</version>
</parent>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.arextest.storage.model;

public interface Constants{

String APP_ID = "appId";

String REPLAY_SCHEDULE_CONFIG_COLLECTION_NAME = "ReplayScheduleConfig";

String CONFIG_COMPARISON_ENCRYPTION_COLLECTION_NAME = "ConfigComparisonEncryption";

String CONFIG_COMPARISON_EXCLUSIONS_COLLECTION_NAME = "ConfigComparisonExclusions";

String CONFIG_COMPARISON_IGNORE_CATEGORY_COLLECTION_NAME = "ConfigComparisonIgnoreCategory";

String CONFIG_COMPARISON_LIST_SORT_COLLECTION_NAME = "ConfigComparisonListSort";

String CONFIG_COMPARISON_REFERENCE_COLLECTION_NAME = "ConfigComparisonReference";

// region: fieldNames of Config
String COMPARE_CONFIG_TYPE = "compareConfigType";
String OPERATION_ID = "operationId";
String FS_INTERFACE_ID = "fsInterfaceId";
String DEPENDENCY_ID = "dependencyId";
String DATA_CHANGE_CREATE_TIME = "dataChangeCreateTime";
String DATA_CHANGE_UPDATE_TIME = "dataChangeUpdateTime";
String EXPIRATION_TYPE = "expirationType";
String EXPIRATION_TIME = "expirationTime";
// endregion

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.arextest.storage.model.event;

import org.springframework.context.ApplicationEvent;

public class ApplicationCreationEvent extends ApplicationEvent {

public ApplicationCreationEvent(String appId) {
super(appId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import com.arextest.config.model.vo.UpdateApplicationRequest;
import com.arextest.config.repository.impl.ApplicationConfigurationRepositoryImpl;
import com.arextest.config.repository.impl.ApplicationOperationConfigurationRepositoryImpl;
import com.arextest.config.repository.impl.ServiceCollectConfigurationRepositoryImpl;
import com.arextest.storage.cache.CacheKeyUtils;
import com.arextest.storage.model.Constants;
import com.arextest.storage.model.event.ApplicationCreationEvent;
import com.arextest.storage.repository.ProviderNames;
import com.arextest.storage.service.MockSourceEditionService;
import com.arextest.storage.service.config.impl.ServiceCollectConfigurableHandler;
import com.arextest.storage.utils.RandomUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoDatabase;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -29,6 +33,8 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
Expand Down Expand Up @@ -61,6 +67,13 @@ public class ApplicationService {

@Resource
private ServiceCollectConfigurableHandler serviceCollectConfigurableHandler;
@Resource
private ApplicationEventPublisher applicationEventPublisher;
@Resource
private ServiceCollectConfigurationRepositoryImpl serviceCollectConfigurationRepository;

@Resource
private MongoDatabase mongoDatabase;

public AddApplicationResponse addApplication(AddApplicationRequest request) {
AddApplicationResponse response = new AddApplicationResponse();
Expand All @@ -86,6 +99,7 @@ public AddApplicationResponse addApplication(AddApplicationRequest request) {

boolean success = applicationConfigurationRepository.insert(applicationConfiguration);
serviceCollectConfigurableHandler.createFromGlobalDefault(appId);
applicationEventPublisher.publishEvent(new ApplicationCreationEvent(appId));
response.setAppId(appId);
response.setSuccess(success);
return response;
Expand Down Expand Up @@ -123,7 +137,17 @@ public boolean deleteApplication(DeleteApplicationRequest request) {
applicationOperationRepository.removeByAppId(appId);

// remove App
applicationConfigurationRepository.removeByAppId(request.getAppId());
applicationConfigurationRepository.removeByAppId(appId);

// remove RecordServiceConfig
serviceCollectConfigurationRepository.removeByAppId(appId);

// remove ReplayScheduleConfig
mongoDatabase.getCollection(Constants.REPLAY_SCHEDULE_CONFIG_COLLECTION_NAME)
.deleteMany(new Document(Constants.APP_ID, appId));

// remove the config about comparison
removeComparisonConfig(appId);
return true;
}

Expand Down Expand Up @@ -176,4 +200,21 @@ public Set<String> getAppOwnersCache(String appId) {
return null;
}
}

private void removeComparisonConfig(String appId) {
// remove the config about comparison
List<String> COMPARISON_CONFIG_COLLECTIONS = Arrays.asList(
Constants.CONFIG_COMPARISON_ENCRYPTION_COLLECTION_NAME,
Constants.CONFIG_COMPARISON_EXCLUSIONS_COLLECTION_NAME,
Constants.CONFIG_COMPARISON_IGNORE_CATEGORY_COLLECTION_NAME,
Constants.CONFIG_COMPARISON_LIST_SORT_COLLECTION_NAME,
Constants.CONFIG_COMPARISON_REFERENCE_COLLECTION_NAME
);
COMPARISON_CONFIG_COLLECTIONS.forEach(collection -> {
Document document = new Document();
document.put(Constants.APP_ID, appId);
document.put("compareConfigType", 0);
mongoDatabase.getCollection(collection).deleteMany(document);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.arextest.config.model.dto.application.ApplicationDescription;
import com.arextest.config.repository.ConfigRepositoryProvider;
import com.arextest.config.repository.impl.ApplicationConfigurationRepositoryImpl;
import com.arextest.storage.model.event.ApplicationCreationEvent;
import com.arextest.storage.service.config.AbstractConfigurableHandler;
import com.arextest.storage.service.config.provider.ApplicationDescriptionProvider;
import java.util.Collections;
Expand All @@ -13,6 +14,7 @@
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
Expand All @@ -27,6 +29,9 @@ public class ApplicationConfigurableHandler extends AbstractConfigurableHandler<
@Resource
private ApplicationConfigurationRepositoryImpl applicationConfigurationRepository;

@Resource
private ApplicationEventPublisher applicationEventPublisher;

protected ApplicationConfigurableHandler(
@Autowired ConfigRepositoryProvider<ApplicationConfiguration> repositoryProvider) {
super(repositoryProvider);
Expand Down Expand Up @@ -62,6 +67,7 @@ protected List<ApplicationConfiguration> createFromGlobalDefault(String appId) {
applicationConfiguration.setRecordedCaseCount(0);
applicationConfiguration.setStatus(StatusType.RECORD.getMask() | StatusType.REPLAY.getMask());
this.insert(applicationConfiguration);
applicationEventPublisher.publishEvent(new ApplicationCreationEvent(appId));
return Collections.singletonList(applicationConfiguration);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.arextest.storage.service.event;

import com.arextest.storage.model.Constants;
import com.arextest.storage.model.event.ApplicationCreationEvent;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

@Service
public class ApplicationCreationEventListener implements
ApplicationListener<ApplicationCreationEvent> {

@Resource
private MongoDatabase mongoDatabase;

@Resource
private CompareConfiguration compareConfiguration;

private static final String IGNORE_CATEGORY_DETAIL = "ignoreCategoryDetail";

@Override
public void onApplicationEvent(@NonNull ApplicationCreationEvent event) {
String appId = (String) event.getSource();
// create default mock compare ignore config
createDefaultMockCompareIgnoreConfig(appId);
}


private void createDefaultMockCompareIgnoreConfig(String appId) {
List<CategoryDetail> ignoredCategoryTypes = compareConfiguration.getIgnoredCategoryTypes();
if (CollectionUtils.isEmpty(ignoredCategoryTypes)) {
return;
}

List<Document> mockCompareIgnoreConfig = new ArrayList<>();
for (CategoryDetail category : ignoredCategoryTypes) {
if (category == null || category.getOperationType().isEmpty()) {
continue;
}
long currentTimeMillis = System.currentTimeMillis();
Document document = new Document();
document.put(Constants.APP_ID, appId);
// to see com.arextest.web.model.contract.contracts.common.enums.CompareConfigType in arex-api
// it represents the config of comparison, which is main entrance
document.put(Constants.COMPARE_CONFIG_TYPE, 0);
document.put(Constants.OPERATION_ID, null);
document.put(Constants.DEPENDENCY_ID, null);
document.put(Constants.FS_INTERFACE_ID, null);
document.put(Constants.DATA_CHANGE_CREATE_TIME, currentTimeMillis);
document.put(Constants.DATA_CHANGE_UPDATE_TIME, currentTimeMillis);
// to see com.arextest.web.model.contract.contracts.common.enums.ExpirationType in arex-api
// it represents the config pinned forever use it
document.put(Constants.EXPIRATION_TYPE, 0);
document.put(Constants.EXPIRATION_TIME, new Date());
document.put(IGNORE_CATEGORY_DETAIL, category);
mockCompareIgnoreConfig.add(document);
}

if (CollectionUtils.isNotEmpty(mockCompareIgnoreConfig)) {
for (Document document : mockCompareIgnoreConfig) {
Bson filter = Filters.and(
Filters.eq(Constants.APP_ID, document.get(Constants.APP_ID)),
Filters.eq(Constants.COMPARE_CONFIG_TYPE, document.get(Constants.COMPARE_CONFIG_TYPE)),
Filters.eq(Constants.OPERATION_ID, document.get(Constants.OPERATION_ID)),
Filters.eq(Constants.DEPENDENCY_ID, document.get(Constants.DEPENDENCY_ID)),
Filters.eq(Constants.FS_INTERFACE_ID, document.get(Constants.FS_INTERFACE_ID)),
Filters.eq(IGNORE_CATEGORY_DETAIL, document.get(IGNORE_CATEGORY_DETAIL))
);

Bson update = Updates.combine(
Updates.set(Constants.APP_ID, document.get(Constants.APP_ID)),
Updates.set(Constants.COMPARE_CONFIG_TYPE, document.get(Constants.COMPARE_CONFIG_TYPE)),
Updates.set(Constants.OPERATION_ID, document.get(Constants.OPERATION_ID)),
Updates.set(Constants.DEPENDENCY_ID, document.get(Constants.DEPENDENCY_ID)),
Updates.set(Constants.FS_INTERFACE_ID, document.get(Constants.FS_INTERFACE_ID)),
Updates.set(IGNORE_CATEGORY_DETAIL, document.get(IGNORE_CATEGORY_DETAIL)),
Updates.set(Constants.DATA_CHANGE_CREATE_TIME,
document.get(Constants.DATA_CHANGE_CREATE_TIME)),
Updates.set(Constants.DATA_CHANGE_UPDATE_TIME,
document.get(Constants.DATA_CHANGE_UPDATE_TIME)),
Updates.set(Constants.EXPIRATION_TYPE, document.get(Constants.EXPIRATION_TYPE)),
Updates.set(Constants.EXPIRATION_TIME, document.get(Constants.EXPIRATION_TIME))
);

mongoDatabase.getCollection(Constants.CONFIG_COMPARISON_IGNORE_CATEGORY_COLLECTION_NAME)
.updateOne(filter, update, new UpdateOptions().upsert(true));
}
}
}

@Configuration
@ConfigurationProperties(prefix = "arex.config.default.compare")
@Getter
@Setter
public static class CompareConfiguration {

private List<CategoryDetail> ignoredCategoryTypes;

}

@Getter
@Setter
public static class CategoryDetail {

private String operationType;
private String operationName;
}


}
3 changes: 3 additions & 0 deletions arex-storage-web-api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ arex:
allowTimeOfDayTo: '23:59'
sampleRate: 1
timeMock: true
compare:
# to insert default ignoredCategoryTypes when create app
ignoredCategoryTypes: [ { operationType: Redis }, { operationType: QMessageConsumer }, { operationType: DynamicClass } ]
prometheus:
port: 20093
common:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,5 @@
<url>https://github.com/arextest/arex-storage</url>


<version>1.1.10</version>
<version>1.1.11</version>
</project>

0 comments on commit b13f318

Please sign in to comment.