Skip to content

Commit

Permalink
feat: Configure the number of replay cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdayuan1 committed Jul 8, 2024
1 parent e1d1653 commit be6a6b1
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 179 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.arextest.schedule.model;

import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum CaseProviderEnum {
ROLLING(0, "Rolling"),
PINNED(1, "Pinned"),
AUTO_PINNED(2, "AutoPinned");

private final int code;
private final String name;
private static final Map<Integer, CaseProviderEnum> codeToProviderMap = new HashMap<>();

static {
for (CaseProviderEnum provider : values()) {
codeToProviderMap.put(provider.code, provider);
}
}

public static CaseProviderEnum fromCode(int code) {
return codeToProviderMap.get(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ReplayActionCaseItem {
/**
* old data fallback to default value
*
* @see CaseProvider
* @see CaseProviderEnum
*/
private int caseProviderCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.arextest.schedule.plan.builder.impl;

import com.arextest.schedule.model.AppServiceDescriptor;
import com.arextest.schedule.model.CaseProvider;
import com.arextest.schedule.model.CaseProviderEnum;
import com.arextest.schedule.model.CaseSourceEnvType;
import com.arextest.schedule.model.ReplayActionItem;
import com.arextest.schedule.model.deploy.DeploymentVersion;
Expand Down Expand Up @@ -122,7 +122,7 @@ public List<ReplayActionItem> buildReplayActionList(BuildReplayPlanRequest reque
abstract List<ReplayActionItem> getReplayActionList(BuildReplayPlanRequest request,
PlanContext planContext);

protected int queryCaseCountByAction(ReplayActionItem actionItem, CaseProvider provider) {
protected int queryCaseCountByAction(ReplayActionItem actionItem, CaseProviderEnum provider) {
return replayCaseRemoteLoadService.queryCaseCount(actionItem, provider.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.arextest.model.replay.QueryMockCacheResponseType;
import com.arextest.model.response.ResponseStatusType;
import com.arextest.schedule.client.HttpWepServiceApiClient;
import com.arextest.schedule.model.CaseProvider;
import com.arextest.schedule.model.CaseProviderEnum;
import com.arextest.schedule.model.ReplayActionCaseItem;
import java.util.Optional;
import javax.annotation.Resource;
Expand Down Expand Up @@ -33,8 +33,8 @@ public boolean prepareCache(ReplayActionCaseItem caseItem) {
request.setRecordId(recordId);
QueryMockCacheResponseType response;

String provider = Optional.ofNullable(CaseProvider.fromCode(caseItem.getCaseProviderCode()))
.orElse(CaseProvider.ROLLING)
String provider = Optional.ofNullable(CaseProviderEnum.fromCode(caseItem.getCaseProviderCode()))
.orElse(CaseProviderEnum.ROLLING)
.getName();
request.setSourceProvider(provider);

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.arextest.schedule.service;

import com.arextest.common.config.ApplicationDefaultConfig;
import com.arextest.schedule.common.CommonConstant;
import com.arextest.schedule.dao.mongodb.ReplayActionCaseItemRepository;
import com.arextest.schedule.dao.mongodb.ReplayPlanActionRepository;
import com.arextest.schedule.dao.mongodb.ReplayPlanRepository;
import com.arextest.schedule.model.AppServiceDescriptor;
import com.arextest.schedule.model.AppServiceOperationDescriptor;
import com.arextest.schedule.model.CaseProvider;
import com.arextest.schedule.model.CaseProviderEnum;
import com.arextest.schedule.model.CaseSendStatusType;
import com.arextest.schedule.model.CompareProcessStatusType;
import com.arextest.schedule.model.LogType;
Expand All @@ -16,7 +17,6 @@
import com.arextest.schedule.model.ReplayPlan;
import com.arextest.schedule.model.ReplayStatusType;
import com.arextest.schedule.model.deploy.ServiceInstance;
import com.arextest.schedule.model.plan.BuildReplayPlanType;
import com.arextest.schedule.plan.PlanContext;
import com.arextest.schedule.plan.PlanContextCreator;
import com.arextest.schedule.planexecution.PlanExecutionContextProvider;
Expand Down Expand Up @@ -45,6 +45,11 @@
@Slf4j
@SuppressWarnings({"rawtypes", "unchecked"})
public class PlanConsumePrepareService {
private static final String CASE_COUNT_LIMIT_KEY = ".case.count.limit.";
private static final String CASE_COUNT_LIMIT_APPS_KEY = "use.case.count.limit.apps";
private static final String NO_TIME_LIMIT_KEY = "no.time.limit.";
private static final String ALL = "ALL";
private static final int ZERO_CASE_COUNT_LIMIT = 0;
@Resource
private MetricService metricService;
@Resource
Expand Down Expand Up @@ -73,6 +78,8 @@ public class PlanConsumePrepareService {
private ReplayNoiseIdentify replayNoiseIdentify;
@Resource
private ReplayStorageService replayStorageService;
@Resource
private ApplicationDefaultConfig defaultConfig;

public int preparePlan(ReplayPlan replayPlan) {
if (replayPlan.getPlanCreateMillis() == 0) {
Expand All @@ -95,9 +102,6 @@ public int preparePlan(ReplayPlan replayPlan) {
}

private int prepareAllActions(List<ReplayActionItem> replayActionItems) {
BuildReplayPlanType planType = BuildReplayPlanType.findByValue(
replayActionItems.get(0).getParent().getReplayPlanType());

int planLoadSize = 0;
for (ReplayActionItem action : replayActionItems) {
int actionLoadSize = 0;
Expand All @@ -109,25 +113,21 @@ private int prepareAllActions(List<ReplayActionItem> replayActionItems) {

if (!CollectionUtils.isEmpty(action.getCaseItemList())) {
actionLoadSize += loadPinnedCases(action);
} else if (planType == BuildReplayPlanType.MIXED) {
actionLoadSize += loadCasesByProvider(action, CaseProvider.AUTO_PINED);
actionLoadSize += loadCasesByProvider(action, CaseProvider.ROLLING);
} else {
actionLoadSize += loadCasesByProvider(action, CaseProvider.AUTO_PINED);
actionLoadSize += loadCasesByProvider(action, CaseProviderEnum.AUTO_PINNED);
actionLoadSize += loadCasesByProvider(action, CaseProviderEnum.ROLLING);
}
planLoadSize += actionLoadSize;
action.setReplayCaseCount(actionLoadSize);
progressEvent.onActionCaseLoaded(action);
}
// if no case saved, fallback to rolling source
return planLoadSize == 0 ? prepareAllActionsRollingFallback(replayActionItems)
: planLoadSize;
return planLoadSize;
}

private int prepareAllActionsRollingFallback(List<ReplayActionItem> replayActionItems) {
int planSavedCaseSize = 0;
for (ReplayActionItem action : replayActionItems) {
int actionLoadSize = loadCasesByProvider(action, CaseProvider.ROLLING);
int actionLoadSize = loadCasesByProvider(action, CaseProviderEnum.ROLLING);

action.setReplayCaseCount(actionLoadSize);
progressEvent.onActionCaseLoaded(action);
Expand All @@ -137,7 +137,7 @@ private int prepareAllActionsRollingFallback(List<ReplayActionItem> replayAction
return planSavedCaseSize;
}

private void caseItemPostProcess(List<ReplayActionCaseItem> cases, CaseProvider provider) {
private void caseItemPostProcess(List<ReplayActionCaseItem> cases, CaseProviderEnum provider) {
if (CollectionUtils.isEmpty(cases)) {
return;
}
Expand All @@ -155,7 +155,7 @@ private void caseItemPostProcess(List<ReplayActionCaseItem> cases, CaseProvider
* else if caseCountLimit < CommonConstant.MAX_PAGE_SIZE or recording data size < request page
* size, Only need to query once by page
*/
public int loadCasesByProvider(ReplayActionItem replayActionItem, CaseProvider provider) {
public int loadCasesByProvider(ReplayActionItem replayActionItem, CaseProviderEnum provider) {
List<OperationTypeData> operationTypes = replayActionItem.getOperationTypes();
int totalCount = 0;

Expand All @@ -173,20 +173,20 @@ public int loadCasesByProvider(ReplayActionItem replayActionItem, CaseProvider p
* Find data by paging according to operationType
* ps: providerName: Rolling, operationType: DubboProvider
*/
private int loadCaseWithOperationType(ReplayActionItem replayActionItem, CaseProvider provider,
private int loadCaseWithOperationType(ReplayActionItem replayActionItem, CaseProviderEnum provider,
OperationTypeData operationTypeData) {
final ReplayPlan replayPlan = replayActionItem.getParent();
long beginTimeMills = replayPlan.getCaseSourceFrom().getTime();
// need all auto pined cases
if (provider.equals(CaseProvider.AUTO_PINED)) {
beginTimeMills = 0;
}

long endTimeMills = operationTypeData.getLastRecordTime();
if (endTimeMills == 0) {
endTimeMills = replayPlan.getCaseSourceTo().getTime();
// Get the limit on the number of replay cases
int caseCountLimit = getCaseCountLimit(replayPlan, provider.getName());
if (caseCountLimit == ZERO_CASE_COUNT_LIMIT) {
return ZERO_CASE_COUNT_LIMIT;
}
int caseCountLimit = replayPlan.getCaseCountLimit();

// Rolling data needs to be judged whether to pull all
long beginTimeMills = getBeginTimeMills(replayPlan, provider);
long endTimeMills = getEndTimeMills(replayPlan, operationTypeData);

int pageSize = Math.min(caseCountLimit, CommonConstant.MAX_PAGE_SIZE);

// The task is pulled up to obtain the recorded case
Expand Down Expand Up @@ -215,6 +215,63 @@ private int loadCaseWithOperationType(ReplayActionItem replayActionItem, CasePro
return count;
}

/**
* Get the limit on the number of replay cases
* @param plan
* @param providerName
* @return
*/
private int getCaseCountLimit(ReplayPlan plan, String providerName) {
String configApps = defaultConfig.getConfigAsString(CASE_COUNT_LIMIT_APPS_KEY, ALL);
// If the app list in the configuration contains all apps or contains currently scheduled apps
if (StringUtils.equalsIgnoreCase(configApps, ALL)
|| StringUtils.contains(configApps, plan.getAppId())) {
// Get the use case number limit from configuration
Integer limitValue = defaultConfig.getConfigAsInt(
providerName + CASE_COUNT_LIMIT_KEY + plan.getReplayPlanType());
// If no use case limit is set in the configuration, the use case limit in the plan is used
if (limitValue == null) {
return plan.getCaseCountLimit();
}

return limitValue;
}

return plan.getCaseCountLimit();
}

/**
* Determine the replay start time of rolling cases through configuration
* @param replayPlan
* @param provider
* @return
*/
private long getBeginTimeMills(ReplayPlan replayPlan, CaseProviderEnum provider) {
// need all auto pined or rolling cases
if (provider.equals(CaseProviderEnum.AUTO_PINNED) ||
(provider.equals(CaseProviderEnum.ROLLING) &&
defaultConfig.getConfigAsBoolean(NO_TIME_LIMIT_KEY + replayPlan.getReplayPlanType(),
false))) {
return 0;
}
return replayPlan.getCaseSourceFrom().getTime();
}

/**
* Determine the replay end time of rolling cases through configuration
* @param replayPlan
* @param operationTypeData
* @return
*/
private long getEndTimeMills(ReplayPlan replayPlan, OperationTypeData operationTypeData) {
// Job wake-up time
long endTimeMills = operationTypeData.getLastRecordTime();
if (endTimeMills == 0) {
endTimeMills = replayPlan.getCaseSourceTo().getTime();
}
return endTimeMills;
}

private int loadPinnedCases(ReplayActionItem replayActionItem) {
List<ReplayActionCaseItem> caseItemList = replayActionItem.getCaseItemList();
int size = 0;
Expand All @@ -237,7 +294,7 @@ private int loadPinnedCases(ReplayActionItem replayActionItem) {
}
}
ReplayParentBinder.setupCaseItemParent(caseItemList, replayActionItem);
caseItemPostProcess(caseItemList, CaseProvider.PINNED);
caseItemPostProcess(caseItemList, CaseProviderEnum.PINNED);
replayActionCaseItemRepository.save(caseItemList);
replayActionItem.setReplayCaseCount(size);
return size;
Expand Down
Loading

0 comments on commit be6a6b1

Please sign in to comment.