-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* http performance improvements
- Loading branch information
Showing
6 changed files
with
191 additions
and
8 deletions.
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
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
94 changes: 94 additions & 0 deletions
94
server/src/main/java/io/orkes/conductor/execution/tasks/OrkesRestTemplateProvider.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,94 @@ | ||
/* | ||
* Copyright 2020 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 io.orkes.conductor.execution.tasks; | ||
|
||
import com.netflix.conductor.tasks.http.HttpTask; | ||
import com.netflix.conductor.tasks.http.providers.RestTemplateProvider; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Provider for a customized RestTemplateBuilder. This class provides a default {@link RestTemplateBuilder} which can be | ||
* configured or extended as needed. | ||
*/ | ||
@Component | ||
@Primary | ||
public class OrkesRestTemplateProvider implements RestTemplateProvider { | ||
|
||
@AllArgsConstructor | ||
private static class RestTemplateHolder { | ||
RestTemplate restTemplate; | ||
HttpComponentsClientHttpRequestFactory requestFactory; | ||
int readTimeout; | ||
int connectTimeout; | ||
} | ||
|
||
private final ThreadLocal<RestTemplateHolder> threadLocalRestTemplate; | ||
private final int defaultReadTimeout; | ||
private final int defaultConnectTimeout; | ||
|
||
@Autowired | ||
public OrkesRestTemplateProvider(@Value("${conductor.tasks.http.readTimeout:250ms}") Duration readTimeout, | ||
@Value("${conductor.tasks.http.connectTimeout:250ms}") Duration connectTimeout, | ||
Optional<ClientHttpRequestInterceptor> interceptor) { | ||
this.defaultReadTimeout = (int) readTimeout.toMillis(); | ||
this.defaultConnectTimeout = (int) connectTimeout.toMillis(); | ||
this.threadLocalRestTemplate = ThreadLocal.withInitial(() -> | ||
{ | ||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); | ||
requestFactory.setReadTimeout(defaultReadTimeout); | ||
requestFactory.setConnectTimeout(defaultConnectTimeout); | ||
restTemplate.setRequestFactory(requestFactory); | ||
interceptor.ifPresent(it -> addInterceptor(restTemplate, it)); | ||
return new RestTemplateHolder(restTemplate, requestFactory, defaultReadTimeout, defaultConnectTimeout); | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public RestTemplate getRestTemplate(HttpTask.Input input) { | ||
RestTemplateHolder holder = threadLocalRestTemplate.get(); | ||
RestTemplate restTemplate = holder.restTemplate; | ||
HttpComponentsClientHttpRequestFactory requestFactory = holder.requestFactory; | ||
|
||
int newReadTimeout = Optional.ofNullable(input.getReadTimeOut()).orElse(defaultReadTimeout); | ||
int newConnectTimeout = Optional.ofNullable(input.getConnectionTimeOut()).orElse(defaultConnectTimeout); | ||
if (newReadTimeout != holder.readTimeout || newConnectTimeout != holder.connectTimeout) { | ||
holder.readTimeout = newReadTimeout; | ||
holder.connectTimeout = newConnectTimeout; | ||
requestFactory.setReadTimeout(newReadTimeout); | ||
requestFactory.setConnectTimeout(newConnectTimeout); | ||
} | ||
|
||
return restTemplate; | ||
} | ||
|
||
private void addInterceptor(RestTemplate restTemplate, ClientHttpRequestInterceptor interceptor) { | ||
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); | ||
if (!interceptors.contains(interceptor)) { | ||
interceptors.add(interceptor); | ||
} | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
server/src/main/java/io/orkes/conductor/rest/WorkflowResourceSync.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,91 @@ | ||
package io.orkes.conductor.rest; | ||
|
||
import com.google.common.util.concurrent.Uninterruptibles; | ||
import com.netflix.conductor.common.metadata.workflow.StartWorkflowRequest; | ||
import com.netflix.conductor.common.run.Workflow; | ||
import com.netflix.conductor.service.WorkflowService; | ||
import io.orkes.conductor.common.model.WorkflowRun; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.ArrayList; | ||
import java.util.concurrent.*; | ||
|
||
import static com.netflix.conductor.rest.config.RequestMappingConstants.WORKFLOW; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
@RestController | ||
@RequestMapping(WORKFLOW) | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class WorkflowResourceSync { | ||
|
||
public static final String REQUEST_ID_KEY = "_X-Request-Id"; | ||
|
||
private final WorkflowService workflowService; | ||
|
||
private final ScheduledExecutorService executionMonitor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2); | ||
|
||
@PostConstruct | ||
public void startMonitor() { | ||
log.info("Starting execution monitors"); | ||
} | ||
|
||
@PostMapping(value = "execute/{name}/{version}", produces = APPLICATION_JSON_VALUE) | ||
@Operation(summary = "Execute a workflow synchronously", tags = "workflow-resource") | ||
@SneakyThrows | ||
public WorkflowRun executeWorkflow( | ||
@PathVariable("name") String name, | ||
@PathVariable(value = "version", required = false) Integer version, | ||
@RequestParam(value = "requestId", required = true) String requestId, | ||
@RequestParam(value = "waitUntilTaskRef", required = false) String waitUntilTaskRef, | ||
@RequestBody StartWorkflowRequest request) { | ||
|
||
request.setName(name); | ||
request.setVersion(version); | ||
String workflowId = workflowService.startWorkflow(request); | ||
request.getInput().put(REQUEST_ID_KEY, requestId); | ||
Workflow workflow = workflowService.getExecutionStatus(workflowId, true); | ||
if(workflow.getStatus().isTerminal() || workflow.getTasks().stream().anyMatch(t -> t.getReferenceTaskName().equalsIgnoreCase(waitUntilTaskRef))) { | ||
return toWorkflowRun(workflow); | ||
} | ||
int maxTimeInMilis = 5_000; //5 sec | ||
int sleepTime = 100; //millis | ||
int loopCount = maxTimeInMilis / sleepTime; | ||
for (int i = 0; i < loopCount; i++) { | ||
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); | ||
workflow = workflowService.getExecutionStatus(workflowId, true); | ||
if(workflow.getStatus().isTerminal() || workflow.getTasks().stream().anyMatch(t -> t.getReferenceTaskName().equalsIgnoreCase(waitUntilTaskRef))) { | ||
return toWorkflowRun(workflow); | ||
} | ||
} | ||
workflow = workflowService.getExecutionStatus(workflowId, true); | ||
return toWorkflowRun(workflow); | ||
} | ||
|
||
public static WorkflowRun toWorkflowRun(Workflow workflow) { | ||
WorkflowRun run = new WorkflowRun(); | ||
|
||
run.setWorkflowId(workflow.getWorkflowId()); | ||
run.setRequestId((String) workflow.getInput().get(REQUEST_ID_KEY)); | ||
run.setCorrelationId(workflow.getCorrelationId()); | ||
run.setInput(workflow.getInput()); | ||
run.setCreatedBy(workflow.getCreatedBy()); | ||
run.setCreateTime(workflow.getCreateTime()); | ||
run.setOutput(workflow.getOutput()); | ||
run.setTasks(new ArrayList<>()); | ||
workflow.getTasks().forEach(task -> run.getTasks().add(task)); | ||
run.setPriority(workflow.getPriority()); | ||
if(workflow.getUpdateTime() != null) { | ||
run.setUpdateTime(workflow.getUpdateTime()); | ||
} | ||
run.setStatus(Workflow.WorkflowStatus.valueOf(workflow.getStatus().name())); | ||
run.setVariables(workflow.getVariables()); | ||
|
||
return run; | ||
} | ||
} |