This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #926 from Netflix/http_task_mapper
added http task mapper
- Loading branch information
Showing
7 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/main/java/com/netflix/conductor/core/execution/mapper/HTTPTaskMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2018 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.conductor.core.execution.mapper; | ||
|
||
import com.netflix.conductor.common.metadata.tasks.Task; | ||
import com.netflix.conductor.common.metadata.tasks.TaskDef; | ||
import com.netflix.conductor.common.metadata.workflow.TaskType; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowDef; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowTask; | ||
import com.netflix.conductor.common.run.Workflow; | ||
import com.netflix.conductor.core.execution.ParametersUtils; | ||
import com.netflix.conductor.core.execution.TerminateWorkflowException; | ||
import com.netflix.conductor.dao.MetadataDAO; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* An implementation of {@link TaskMapper} to map a {@link WorkflowTask} of type {@link TaskType#HTTP} | ||
* to a {@link Task} of type {@link TaskType#HTTP} with {@link Task.Status#SCHEDULED} | ||
*/ | ||
public class HTTPTaskMapper implements TaskMapper { | ||
|
||
public static final Logger logger = LoggerFactory.getLogger(com.netflix.conductor.core.execution.mapper.HTTPTaskMapper.class); | ||
|
||
private final ParametersUtils parametersUtils; | ||
private final MetadataDAO metadataDAO; | ||
|
||
public HTTPTaskMapper(ParametersUtils parametersUtils, MetadataDAO metadataDAO) { | ||
this.parametersUtils = parametersUtils; | ||
this.metadataDAO = metadataDAO; | ||
} | ||
|
||
/** | ||
* This method maps a {@link WorkflowTask} of type {@link TaskType#HTTP} | ||
* to a {@link Task} in a {@link Task.Status#SCHEDULED} state | ||
* | ||
* @param taskMapperContext: A wrapper class containing the {@link WorkflowTask}, {@link WorkflowDef}, {@link Workflow} and a string representation of the TaskId | ||
* @return a List with just one HTTP task | ||
* @throws TerminateWorkflowException In case if the task definition does not exist | ||
*/ | ||
@Override | ||
public List<Task> getMappedTasks(TaskMapperContext taskMapperContext) throws TerminateWorkflowException { | ||
|
||
logger.debug("TaskMapperContext {} in HTTPTaskMapper", taskMapperContext); | ||
|
||
WorkflowTask taskToSchedule = taskMapperContext.getTaskToSchedule(); | ||
Workflow workflowInstance = taskMapperContext.getWorkflowInstance(); | ||
String taskId = taskMapperContext.getTaskId(); | ||
int retryCount = taskMapperContext.getRetryCount(); | ||
|
||
TaskDef taskDefinition = Optional.ofNullable(taskMapperContext.getTaskDefinition()) | ||
.orElseGet(() -> Optional.ofNullable(metadataDAO.getTaskDef(taskToSchedule.getName())) | ||
.orElseThrow(() -> { | ||
String reason = String.format("Invalid task specified. Cannot find task by name %s in the task definitions", taskToSchedule.getName()); | ||
return new TerminateWorkflowException(reason); | ||
})); | ||
|
||
Map<String, Object> input = parametersUtils.getTaskInputV2(taskToSchedule.getInputParameters(), workflowInstance, taskId, taskDefinition); | ||
|
||
Task httpTask = new Task(); | ||
httpTask.setTaskType(taskToSchedule.getType()); | ||
httpTask.setTaskDefName(taskToSchedule.getName()); | ||
httpTask.setReferenceTaskName(taskToSchedule.getTaskReferenceName()); | ||
httpTask.setWorkflowInstanceId(workflowInstance.getWorkflowId()); | ||
httpTask.setWorkflowType(workflowInstance.getWorkflowName()); | ||
httpTask.setCorrelationId(workflowInstance.getCorrelationId()); | ||
httpTask.setScheduledTime(System.currentTimeMillis()); | ||
httpTask.setTaskId(taskId); | ||
httpTask.setInputData(input); | ||
httpTask.setStatus(Task.Status.SCHEDULED); | ||
httpTask.setRetryCount(retryCount); | ||
httpTask.setCallbackAfterSeconds(taskToSchedule.getStartDelay()); | ||
httpTask.setWorkflowTask(taskToSchedule); | ||
httpTask.setRateLimitPerFrequency(taskDefinition.getRateLimitPerFrequency()); | ||
httpTask.setRateLimitFrequencyInSeconds(taskDefinition.getRateLimitFrequencyInSeconds()); | ||
return Collections.singletonList(httpTask); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
core/src/main/java/com/netflix/conductor/core/execution/mapper/TaskMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/** | ||
/* | ||
* Copyright 2018 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
core/src/test/java/com/netflix/conductor/core/execution/mapper/HTTPTaskMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2018 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.conductor.core.execution.mapper; | ||
|
||
import com.netflix.conductor.common.metadata.tasks.Task; | ||
import com.netflix.conductor.common.metadata.tasks.TaskDef; | ||
import com.netflix.conductor.common.metadata.workflow.TaskType; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowDef; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowTask; | ||
import com.netflix.conductor.common.run.Workflow; | ||
import com.netflix.conductor.core.execution.ParametersUtils; | ||
import com.netflix.conductor.core.execution.TerminateWorkflowException; | ||
import com.netflix.conductor.core.utils.IDGenerator; | ||
import com.netflix.conductor.dao.MetadataDAO; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class HTTPTaskMapperTest { | ||
|
||
private ParametersUtils parametersUtils; | ||
private MetadataDAO metadataDAO; | ||
private HTTPTaskMapper httpTaskMapper; | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Before | ||
public void setUp() { | ||
parametersUtils = mock(ParametersUtils.class); | ||
metadataDAO = mock(MetadataDAO.class); | ||
httpTaskMapper = new HTTPTaskMapper(parametersUtils, metadataDAO); | ||
} | ||
|
||
@Test | ||
public void getMappedTasks() { | ||
//Given | ||
WorkflowTask taskToSchedule = new WorkflowTask(); | ||
taskToSchedule.setName("http_task"); | ||
taskToSchedule.setType(TaskType.HTTP.name()); | ||
taskToSchedule.setTaskDefinition(new TaskDef("http_task")); | ||
String taskId = IDGenerator.generate(); | ||
String retriedTaskId = IDGenerator.generate(); | ||
|
||
Workflow workflow = new Workflow(); | ||
WorkflowDef workflowDef = new WorkflowDef(); | ||
workflow.setWorkflowDefinition(workflowDef); | ||
|
||
TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() | ||
.withWorkflowDefinition(workflowDef) | ||
.withWorkflowInstance(workflow) | ||
.withTaskDefinition(new TaskDef()) | ||
.withTaskToSchedule(taskToSchedule) | ||
.withTaskInput(new HashMap<>()) | ||
.withRetryCount(0) | ||
.withRetryTaskId(retriedTaskId) | ||
.withTaskId(taskId) | ||
.build(); | ||
|
||
//when | ||
List<Task> mappedTasks = httpTaskMapper.getMappedTasks(taskMapperContext); | ||
|
||
//Then | ||
assertEquals(1, mappedTasks.size()); | ||
assertEquals(TaskType.HTTP.name(), mappedTasks.get(0).getTaskType()); | ||
} | ||
|
||
@Test | ||
public void getMappedTasksException() { | ||
//Given | ||
WorkflowTask taskToSchedule = new WorkflowTask(); | ||
taskToSchedule.setName("http_task"); | ||
taskToSchedule.setType(TaskType.HTTP.name()); | ||
String taskId = IDGenerator.generate(); | ||
String retriedTaskId = IDGenerator.generate(); | ||
|
||
Workflow workflow = new Workflow(); | ||
WorkflowDef workflowDef = new WorkflowDef(); | ||
workflow.setWorkflowDefinition(workflowDef); | ||
|
||
TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() | ||
.withWorkflowDefinition(workflowDef) | ||
.withWorkflowInstance(workflow) | ||
.withTaskToSchedule(taskToSchedule) | ||
.withTaskInput(new HashMap<>()) | ||
.withRetryCount(0) | ||
.withRetryTaskId(retriedTaskId) | ||
.withTaskId(taskId) | ||
.build(); | ||
|
||
//then | ||
expectedException.expect(TerminateWorkflowException.class); | ||
expectedException.expectMessage(String.format("Invalid task specified. Cannot find task by name %s in the task definitions", taskToSchedule.getName())); | ||
//when | ||
httpTaskMapper.getMappedTasks(taskMapperContext); | ||
} | ||
} |