Skip to content

Commit

Permalink
Add retry in parameter in the Throttling exception, and take into acc…
Browse files Browse the repository at this point in the history
…ount last execution duration
  • Loading branch information
zakariamaaraki committed Jun 2, 2024
1 parent 8dd3177 commit 52ba96e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public class CompilerThrottlingException extends MonitoredException {
public CompilerThrottlingException(String message) {
super(message, ErrorCode.THROTTLING_ERROR, ErrorType.WARNING, true, RETRY_IN);
}

public CompilerThrottlingException(String message, int retryIn ) {
super(message, ErrorCode.THROTTLING_ERROR, ErrorType.WARNING, true, retryIn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ResponseEntity<RemoteCodeCompilerResponse> execute(Execution execution) {
throttlingCounterMetric.increment();
String errorMessage = "Request has been throttled, service reached maximum resources usage";
log.warn(errorMessage);
throw new CompilerThrottlingException(errorMessage);
throw new CompilerThrottlingException(errorMessage, resources.lastExecutionDuration.intValue());
}

private ResponseEntity<RemoteCodeCompilerResponse> compileAndExecute(Execution execution) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
@Slf4j
@Component("compiled")
public class CompiledLanguagesExecutionStrategy extends ExecutionStrategy {
public class CompiledLanguagesExecutionStrategy extends ExecutionStrategyDecorator {

// Note: this value should not be updated, once update don't forget to update build.sh script used to build these images.
private static final String IMAGE_PREFIX_NAME = "compiler.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public abstract class ExecutionStrategy {

private static final long EXECUTION_TIME_OUT = 20000; // in ms

private final Resources resources;
protected final Resources resources;

private static final String TEST_CASE_ID_ENV_VARIABLE = "TEST_CASE_ID";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.cp.compiler.services.businesslogic.strategies;

import com.cp.compiler.executions.Execution;
import com.cp.compiler.models.ExecutionResponse;
import com.cp.compiler.services.platform.containers.ContainerService;
import com.cp.compiler.services.platform.resources.Resources;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class ExecutionStrategyDecorator extends ExecutionStrategy {

/**
* Instantiates a new Execution strategy.
*
* @param containerService the container service
* @param meterRegistry the meter registry
* @param resources the resources
*/
protected ExecutionStrategyDecorator(ContainerService containerService, MeterRegistry meterRegistry, Resources resources) {
super(containerService, meterRegistry, resources);
}

@Override
public ExecutionResponse run(Execution execution, boolean deleteImageAfterExecution) {

long startTime = System.nanoTime();

var executionResponse = super.run(execution, deleteImageAfterExecution);

long endTime = System.nanoTime();
long elapsedTime = endTime - startTime;

// Convert elapsed time to seconds
double elapsedTimeInMilliSeconds = (double) elapsedTime / 1_000_000;
log.info("Total execution duration took {} milliseconds", elapsedTimeInMilliSeconds);

// Update last execution time, used to suggest retry in case of a throttling.
resources.lastExecutionDuration.set(elapsedTimeInMilliSeconds);

return executionResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
@Slf4j
@Component("interpreted")
public class InterpretedLanguagesExecutionStrategy extends ExecutionStrategy {
public class InterpretedLanguagesExecutionStrategy extends ExecutionStrategyDecorator {

private final MeterRegistry meterRegistry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.cp.compiler.services.platform.resources;

import com.cp.compiler.contract.resources.AvailableResources;
import com.google.common.util.concurrent.AtomicDouble;

/**
* The interface Cpu resources.
*
* @author Zakaria Maaraki
*/
public interface Resources {


/**
* Used for retry after.
*/
AtomicDouble lastExecutionDuration = new AtomicDouble(-1);

/**
* Gets max cpus.
*
Expand Down

0 comments on commit 52ba96e

Please sign in to comment.