Skip to content

Ind schedule task #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import org.evomaster.client.java.controller.api.dto.ActionDto;
import org.evomaster.client.java.controller.api.dto.BootTimeInfoDto;
import org.evomaster.client.java.controller.api.dto.UnitsInfoDto;
import org.evomaster.client.java.controller.api.dto.problem.rpc.ScheduleTaskInvocationDto;
import org.evomaster.client.java.controller.internal.SutController;
import org.evomaster.client.java.instrumentation.*;
import org.evomaster.client.java.instrumentation.object.ClassToSchema;
import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;
import org.evomaster.client.java.instrumentation.staticstate.ObjectiveRecorder;
import org.evomaster.client.java.instrumentation.staticstate.UnitsInfoRecorder;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -75,6 +74,26 @@ public final void newActionSpecificHandler(ActionDto dto){
));
}

@Override
public void newScheduleActionSpecificHandler(ScheduleTaskInvocationDto dto) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be marked withfinal to avoid users overriding it?

List<String> inputVariables = new ArrayList<>();
if (dto.requestParams != null && (!dto.requestParams.isEmpty())){
inputVariables.addAll(dto.requestParams.stream().map(e-> e.stringValue).collect(Collectors.toList()));
} else if (dto.requestParamsAsStrings != null){
inputVariables.addAll(dto.requestParamsAsStrings);
}

ExecutionTracer.setAction(new Action(
dto.index,
dto.taskName,
inputVariables,
// might handle external service later, then add empty collection for the moment.
new HashMap<>(),
new HashMap<>(),
new ArrayList<>()
));
}

@Override
public final UnitsInfoDto getUnitsInfoDto(){
return getUnitsInfoDto(UnitsInfoRecorder.getInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.evomaster.client.java.controller.api.dto.ActionDto;
import org.evomaster.client.java.controller.api.dto.BootTimeInfoDto;
import org.evomaster.client.java.controller.api.dto.UnitsInfoDto;
import org.evomaster.client.java.controller.api.dto.problem.rpc.ScheduleTaskInvocationDto;
import org.evomaster.client.java.instrumentation.*;
import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;
import org.evomaster.client.java.instrumentation.staticstate.ObjectiveRecorder;
Expand Down Expand Up @@ -455,6 +456,29 @@ public final void newActionSpecificHandler(ActionDto dto) {
}
}

@Override
public void newScheduleActionSpecificHandler(ScheduleTaskInvocationDto dto) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be marked withfinal to avoid users overriding it?

if (isInstrumentationActivated()) {

List<String> inputVariables = new ArrayList<>();
if (dto.requestParams != null && (!dto.requestParams.isEmpty())){
inputVariables.addAll(dto.requestParams.stream().map(e-> e.stringValue).collect(Collectors.toList()));
} else if (dto.requestParamsAsStrings != null){
inputVariables.addAll(dto.requestParamsAsStrings);
}

serverController.setAction(new Action(
dto.index,
dto.taskName,
inputVariables,
// might handle external service later, then add empty collection for the moment.
new HashMap<>(),
new HashMap<>(),
new ArrayList<>()
));
}
}

@Override
public final UnitsInfoDto getUnitsInfoDto(){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ public final void newScheduleAction(ScheduleTaskInvocationDto dto, boolean query
this.actionIndex = dto.index;

resetExtraHeuristics();

newScheduleActionSpecificHandler(dto);
}

public final void executeHandleLocalAuthenticationSetup(RPCActionDto dto, ActionResponseDto responseDto){
Expand All @@ -956,8 +958,8 @@ public final void invokeScheduleTasks(List<ScheduleTaskInvocationDto> dtos, Sche
newScheduleAction(dto, queryFromDatabase);
invokeScheduleTask(dto, responseDto);
}catch (Exception e){
// now we execute all schedule tasks
SimpleLogger.warn(e.getMessage());
// we stop executing the following schedule task
throw new RuntimeException(e);
}
}
assert dtos.size() == responseDto.results.size();
Expand Down Expand Up @@ -1192,6 +1194,7 @@ private RPCType getRPCType(RPCActionDto dto){

public abstract void newActionSpecificHandler(ActionDto dto);

public abstract void newScheduleActionSpecificHandler(ScheduleTaskInvocationDto dto);

/**
* Check if bytecode instrumentation is on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ abstract class ApiWsStructureMutator : StructureMutator() {
addInitializingSqlActions(individual, mutatedGenes, sampler)
addInitializingMongoDbActions(individual, mutatedGenes, sampler)
addInitializingHostnameResolutionActions(individual, mutatedGenes, sampler)
// TODO if we handle schedule actions with structure mutator
}

private fun <T: ApiWsIndividual> addInitializingMongoDbActions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ class RPCExternalServiceAction(

companion object{
fun getRPCExternalServiceActionName(apiDto: MockRPCExternalServiceDto, index: Int) =
getRPCExternalServiceActionName(apiDto.interfaceFullName, apiDto.functionName, apiDto.requestRules?.get(index), apiDto.responseFullTypesWithGeneric?.get(index)?:apiDto.responseTypes[index])


getRPCExternalServiceActionName(
apiDto.interfaceFullName,
apiDto.functionName,
apiDto.requestRules?.run{
if (this.size > index) get(index)
else throw IllegalArgumentException("request rules are unspecified for function ${apiDto.functionName} from interface ${apiDto.interfaceFullName}") },
apiDto.responseFullTypesWithGeneric?.run{ if (this.size > index) get(index) else null }?:apiDto.responseTypes[index])

fun getRPCExternalServiceActionName(interfaceName: String, functionName: String, requestRuleIdentifier: String?, responseClassType: String) = "$interfaceName$EXACTION_NAME_SEPARATOR$functionName$EXACTION_NAME_SEPARATOR${requestRuleIdentifier?:"ANY"}$EXACTION_NAME_SEPARATOR$responseClassType"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class RestIndividual(
sqlSize: Int = 0,
mongoSize: Int = 0,
dnsSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions,mainSize,sqlSize,mongoSize,dnsSize, 0)
scheduleSize : Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions,mainSize,sqlSize,mongoSize,dnsSize, scheduleSize)
): ApiWsIndividual(sampleType, trackOperator, index, allActions,
childTypeVerifier = EnterpriseChildTypeVerifier(RestCallAction::class.java,RestResourceCalls::class.java),
groups) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.evomaster.core.search.impact.impactinfocollection.ImpactsOfAction
import org.evomaster.core.search.impact.impactinfocollection.ImpactsOfIndividual
import org.evomaster.core.search.impact.impactinfocollection.InitializationGroupedActionsImpacts
import org.evomaster.core.search.impact.impactinfocollection.value.numeric.IntegerGeneImpact
import kotlin.reflect.KClass

/**
* created by manzhang on 2021/10/21
Expand Down Expand Up @@ -56,7 +57,7 @@ class ResourceImpactOfIndividual : ImpactsOfIndividual {
this.anySqlTableSizeImpact = anySqlTableSizeImpact
}

constructor(individual: RestIndividual, initActionTypes: List<String>, abstractInitializationGeneToMutate: Boolean, fitnessValue: FitnessValue?)
constructor(individual: RestIndividual, initActionTypes: List<KClass<*>>, abstractInitializationGeneToMutate: Boolean, fitnessValue: FitnessValue?)
: super(individual, initActionTypes, abstractInitializationGeneToMutate, fitnessValue) {
resourceSizeImpact = mutableMapOf<String, IntegerGeneImpact>().apply {
individual.seeResource(RestIndividual.ResourceFilter.ALL).forEach { r->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ class RPCFitness : ApiWsFitness<RPCIndividual>() {
val additionalInfoForScheduleTask = dto.additionalInfoList.subList(0, scheduleTasksResults.size)
// TODO man might need to handle additional targets for schedule task later

val additionalInfoForMainTask= dto.additionalInfoList.subList(scheduleTasksResults.size, dto.additionalInfoList.size)
handleResponseTargets(fv, individual.seeAllActions().filterIsInstance<RPCCallAction>(), rpcActionResults, additionalInfoForMainTask)
if (rpcActionResults.isNotEmpty()){
val startingIndexForMainAction = scheduleTasksResults.size
if (startingIndexForMainAction > dto.additionalInfoList.size)
throw IllegalStateException("missing ${rpcActionResults.size} additionalInfo for RPC Actions, starting Index: $startingIndexForMainAction, and total: ${dto.additionalInfoList.size}")
val additionalInfoForMainTask= dto.additionalInfoList.subList(scheduleTasksResults.size, dto.additionalInfoList.size)
handleResponseTargets(fv, individual.seeAllActions().filterIsInstance<RPCCallAction>(), rpcActionResults, additionalInfoForMainTask)
}


if (config.isEnabledTaintAnalysis()) {
Lazy.assert { (scheduleTasksResults.size + rpcActionResults.size) == dto.additionalInfoList.size }
Expand Down Expand Up @@ -133,11 +139,12 @@ class RPCFitness : ApiWsFitness<RPCIndividual>() {
// }
// }

/**
* @return if all tasks have been successfully executed
*/
private fun executeScheduleTasks(firstIndex : Int, tasks : List<ScheduleTaskAction>, actionResults : MutableList<ActionResult>) : Boolean{
searchTimeController.waitForRateLimiter()

val taskResults = tasks.map { ScheduleTaskActionResult(it.getLocalId()) }
actionResults.addAll(taskResults)
val taskDtos = tasks.mapIndexed { index, scheduleTaskAction -> rpcHandler.transformScheduleTaskInvocationDto(scheduleTaskAction).apply {
this.index = firstIndex + index
}
Expand All @@ -147,16 +154,24 @@ class RPCFitness : ApiWsFitness<RPCIndividual>() {
val response = rc.invokeScheduleTasksAndGetResults(command)

if (response != null){
if (taskResults.size == response.results.size){
taskResults.forEachIndexed { index, taskResult ->
val resultDto = response.results[index]
taskResult.setResultBasedOnDto(resultDto)
if (response.results.size > tasks.size)
throw IllegalStateException("Received more responses (ie, ${response.results.size}) than tasks (ie, ${tasks.size})")

response.results.forEachIndexed { index, resultDto ->
val taskResult = ScheduleTaskActionResult(tasks[index].getLocalId())
taskResult.setResultBasedOnDto(resultDto)
taskResult.stopping = resultDto.status == ExecutionStatusDto.FAILED
actionResults.add(taskResult)
if (taskResult.stopping){
log.warn("fail to execute the task at $index (in total ${tasks.size}), and the task name is ${tasks[index].taskName}")
return false
}
}
}else{
log.warn("no response when executing schedule tasks")
}
val ok = (response != null && response.results.none { it.status == ExecutionStatusDto.FAILED })
taskResults.last().stopping = !ok
return ok

return response != null && response.results.size == tasks.size
}

private fun executeNewAction(action: RPCCallAction, index: Int, actionResults: MutableList<ActionResult>) : Boolean{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ class RPCSampler: ApiWsSampler<RPCIndividual>() {
* sample a schedule task action from [scheduleActionCluster] at random
* @param noSeedProbability specifies a probability which does not apply seeded one
*/
fun sampleRandomScheduleTaskAction(noSeedProbability: Double = 0.05) : ScheduleTaskAction {
private fun sampleRandomScheduleTaskAction(noSeedProbability: Double = 0.05) : ScheduleTaskAction {
if (scheduleActionCluster.isEmpty())
throw IllegalStateException("cannot sample schedule action with empty cluster")
val action = randomness.choose(scheduleActionCluster).copy() as ScheduleTaskAction
action.doInitialize(randomness)
rpcHandler.scheduleActionWithRandomSeeded(action, noSeedProbability)
Expand All @@ -124,15 +126,16 @@ class RPCSampler: ApiWsSampler<RPCIndividual>() {
}.toMutableList()

val leftlen = config.maxTestSize - len
if (leftlen > 0 && randomness.nextBoolean(config.probOfSamplingScheduleTask)){
val scheduleTaskSize = if (scheduleActionCluster.isNotEmpty() && leftlen > 0 && randomness.nextBoolean(config.probOfSamplingScheduleTask)){
val slen = randomness.nextInt(1, leftlen)
val scheduleActions = (0 until slen).map {
sampleRandomScheduleTaskAction()
}
actions.addAll(0, scheduleActions)
}
scheduleActions.size
}else 0

val ind = createRPCIndividual(sampleType = SampleType.RANDOM, actions)
val ind = createRPCIndividual(sampleType = SampleType.RANDOM, actions, scheduleTaskSize)
ind.doGlobalInitialize(searchGlobalState)
return ind
}
Expand All @@ -150,7 +153,6 @@ class RPCSampler: ApiWsSampler<RPCIndividual>() {
adHocInitialIndividuals.forEach {
it.doGlobalInitialize(searchGlobalState)
}
adHocInitialIndividuals
}

override fun initSeededTests(infoDto: SutInfoDto?) {
Expand Down Expand Up @@ -211,13 +213,15 @@ class RPCSampler: ApiWsSampler<RPCIndividual>() {
)
}

private fun createRPCIndividual(sampleType: SampleType, actions : MutableList<ActionComponent>) : RPCIndividual{
private fun createRPCIndividual(sampleType: SampleType, actions : MutableList<ActionComponent>, scheduleTaskSize : Int = 0) : RPCIndividual{
// enable tracking in rpc
return RPCIndividual(
sampleType = sampleType,
trackOperator = if(config.trackingEnabled()) this else null,
index = if (config.trackingEnabled()) time.evaluatedIndividuals else -1,
allActions=actions
allActions=actions,
mainSize = actions.size - scheduleTaskSize,
scheduleTaskSize = scheduleTaskSize
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ class RPCStructureMutator : ApiWsStructureMutator() {

private fun mutateForRandomType(individual: RPCIndividual, mutatedGenes: MutatedGeneSpecification?) {

/*
TODO
here we implement strategies to support structure mutation on main action,
as we support the schedule task now, also need to be able to apply structure mutation on schedule task
*/
val size = individual.seeMainExecutableActions().size
if ((size + 1 < config.maxTestSize) && (size == 1 || randomness.nextBoolean())){
if ((size + 1 < config.maxTestSize) && (size <= 1 || randomness.nextBoolean())){
// add
val sampledAction = sampler.sampleRandomAction()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.evomaster.core.problem.rest.RestCallAction
import org.evomaster.core.problem.rest.RestCallResult
import org.evomaster.core.problem.rest.RestIndividual
import org.evomaster.core.problem.rest.resource.ResourceImpactOfIndividual
import org.evomaster.core.scheduletask.ScheduleTaskAction
import org.evomaster.core.search.action.*
import org.evomaster.core.search.action.ActionFilter.*
import org.evomaster.core.search.service.monitor.ProcessMonitorExcludeField
Expand Down Expand Up @@ -108,7 +109,7 @@ class EvaluatedIndividual<T>(
trackOperator = trackOperator,
index = index,
impactInfo = if ((config.isEnabledImpactCollection())) {
val initActionTypes = individual.seeInitializingActions().groupBy { it::class.java.name }.keys.toList()
val initActionTypes = individual.seeInitializingActions().groupBy { it::class }.keys.toList()
if (individual is RestIndividual && config.isEnabledResourceDependency())
ResourceImpactOfIndividual(individual, initActionTypes, config.abstractInitializationGeneToMutate, fitness)
else
Expand Down Expand Up @@ -988,7 +989,7 @@ class EvaluatedIndividual<T>(
return !invalid
}
private fun initializingActionClasses(): List<KClass<*>> {
return listOf(MongoDbAction::class, SqlAction::class)
return listOf(MongoDbAction::class, SqlAction::class, ScheduleTaskAction::class)
}

fun hasAnyPotentialFault() = this.fitness.hasAnyPotentialFault(this.individual.searchGlobalState!!.idMapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,30 @@ open class ImpactsOfIndividual(
val impactsOfStructure: ActionStructureImpact = ActionStructureImpact("StructureSize")
) {

constructor(individual: Individual, initActionTypes: List<String>, abstractInitializationGeneToMutate: Boolean, fitnessValue: FitnessValue?) : this(
initActionImpacts = initActionTypes.associateWith {
constructor(individual: Individual, initActionTypes: List<KClass<*>>, abstractInitializationGeneToMutate: Boolean, fitnessValue: FitnessValue?) : this(
initActionImpacts = initActionTypes.map { it.java.name }.associateWith {
InitializationGroupedActionsImpacts(
abstractInitializationGeneToMutate
)
}.toMutableMap(),
fixedMainActionImpacts = individual.seeFixedMainActions().map { a -> ImpactsOfAction(a) }.toMutableList(),
dynamicMainActionImpacts = individual.seeDynamicMainActions().map { a-> ImpactsOfAction(a) }.toMutableList()
) {
if (individual.seeActions(ActionFilter.NO_INIT).isEmpty())
if (individual.seeActions(ActionFilter.NO_INIT).isEmpty()
// It allows to have tests which are only composed of schedule actions,
// then we need to put it as part of main action later
&& individual.seeActions(ActionFilter.ONLY_SCHEDULE_TASK).isEmpty())
throw IllegalArgumentException("there is no main action")

if (fitnessValue != null) {
impactsOfStructure.updateStructure(individual, fitnessValue)
}
val scheduleTasks = individual.seeActions(ActionFilter.ONLY_SCHEDULE_TASK)
if (scheduleTasks.isNotEmpty()){
scheduleTasks.groupBy { it::class.java.name }.forEach { (t, u) ->
initActionImpacts[t]?.initInitializationActions(listOf(u), 0)?:throw IllegalStateException("InitializationGroupedActionsImpacts is not created for $t")
}
}
}

companion object {
Expand Down Expand Up @@ -234,7 +243,8 @@ open class ImpactsOfIndividual(
//for fixed action
val fixed = individual.seeFixedMainActions()
if ((fixed.isNotEmpty() && fixed.size != fixedMainActionImpacts.size) ||
(fixed.isEmpty() && !noneActionIndividual()))
(fixed.isEmpty() && individual.seeActions(ActionFilter.ONLY_SCHEDULE_TASK).isEmpty() // TODO Man: need a better way to handle schedule task and main action
&& !noneActionIndividual()))
throw IllegalArgumentException("inconsistent size of actions and impacts")

fixed.forEach { action ->
Expand Down
Loading