Skip to content

Commit 52ba96e

Browse files
Add retry in parameter in the Throttling exception, and take into account last execution duration
1 parent 8dd3177 commit 52ba96e

File tree

7 files changed

+58
-5
lines changed

7 files changed

+58
-5
lines changed

src/main/java/com/cp/compiler/exceptions/CompilerThrottlingException.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public class CompilerThrottlingException extends MonitoredException {
2222
public CompilerThrottlingException(String message) {
2323
super(message, ErrorCode.THROTTLING_ERROR, ErrorType.WARNING, true, RETRY_IN);
2424
}
25+
26+
public CompilerThrottlingException(String message, int retryIn ) {
27+
super(message, ErrorCode.THROTTLING_ERROR, ErrorType.WARNING, true, retryIn);
28+
}
2529
}

src/main/java/com/cp/compiler/services/api/CompilerProxy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public ResponseEntity<RemoteCodeCompilerResponse> execute(Execution execution) {
109109
throttlingCounterMetric.increment();
110110
String errorMessage = "Request has been throttled, service reached maximum resources usage";
111111
log.warn(errorMessage);
112-
throw new CompilerThrottlingException(errorMessage);
112+
throw new CompilerThrottlingException(errorMessage, resources.lastExecutionDuration.intValue());
113113
}
114114

115115
private ResponseEntity<RemoteCodeCompilerResponse> compileAndExecute(Execution execution) {

src/main/java/com/cp/compiler/services/businesslogic/strategies/CompiledLanguagesExecutionStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
@Slf4j
3232
@Component("compiled")
33-
public class CompiledLanguagesExecutionStrategy extends ExecutionStrategy {
33+
public class CompiledLanguagesExecutionStrategy extends ExecutionStrategyDecorator {
3434

3535
// Note: this value should not be updated, once update don't forget to update build.sh script used to build these images.
3636
private static final String IMAGE_PREFIX_NAME = "compiler.";

src/main/java/com/cp/compiler/services/businesslogic/strategies/ExecutionStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public abstract class ExecutionStrategy {
5353

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

56-
private final Resources resources;
56+
protected final Resources resources;
5757

5858
private static final String TEST_CASE_ID_ENV_VARIABLE = "TEST_CASE_ID";
5959

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.cp.compiler.services.businesslogic.strategies;
2+
3+
import com.cp.compiler.executions.Execution;
4+
import com.cp.compiler.models.ExecutionResponse;
5+
import com.cp.compiler.services.platform.containers.ContainerService;
6+
import com.cp.compiler.services.platform.resources.Resources;
7+
import io.micrometer.core.instrument.MeterRegistry;
8+
import lombok.extern.slf4j.Slf4j;
9+
10+
@Slf4j
11+
public abstract class ExecutionStrategyDecorator extends ExecutionStrategy {
12+
13+
/**
14+
* Instantiates a new Execution strategy.
15+
*
16+
* @param containerService the container service
17+
* @param meterRegistry the meter registry
18+
* @param resources the resources
19+
*/
20+
protected ExecutionStrategyDecorator(ContainerService containerService, MeterRegistry meterRegistry, Resources resources) {
21+
super(containerService, meterRegistry, resources);
22+
}
23+
24+
@Override
25+
public ExecutionResponse run(Execution execution, boolean deleteImageAfterExecution) {
26+
27+
long startTime = System.nanoTime();
28+
29+
var executionResponse = super.run(execution, deleteImageAfterExecution);
30+
31+
long endTime = System.nanoTime();
32+
long elapsedTime = endTime - startTime;
33+
34+
// Convert elapsed time to seconds
35+
double elapsedTimeInMilliSeconds = (double) elapsedTime / 1_000_000;
36+
log.info("Total execution duration took {} milliseconds", elapsedTimeInMilliSeconds);
37+
38+
// Update last execution time, used to suggest retry in case of a throttling.
39+
resources.lastExecutionDuration.set(elapsedTimeInMilliSeconds);
40+
41+
return executionResponse;
42+
}
43+
}

src/main/java/com/cp/compiler/services/businesslogic/strategies/InterpretedLanguagesExecutionStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
@Slf4j
2121
@Component("interpreted")
22-
public class InterpretedLanguagesExecutionStrategy extends ExecutionStrategy {
22+
public class InterpretedLanguagesExecutionStrategy extends ExecutionStrategyDecorator {
2323

2424
private final MeterRegistry meterRegistry;
2525

src/main/java/com/cp/compiler/services/platform/resources/Resources.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.cp.compiler.services.platform.resources;
22

33
import com.cp.compiler.contract.resources.AvailableResources;
4+
import com.google.common.util.concurrent.AtomicDouble;
45

56
/**
67
* The interface Cpu resources.
78
*
89
* @author Zakaria Maaraki
910
*/
1011
public interface Resources {
11-
12+
13+
/**
14+
* Used for retry after.
15+
*/
16+
AtomicDouble lastExecutionDuration = new AtomicDouble(-1);
17+
1218
/**
1319
* Gets max cpus.
1420
*

0 commit comments

Comments
 (0)