Skip to content

Commit 6a1b920

Browse files
authored
chore: added cleanup option in the setup ListOperationIT (#536)
* chore: added cleanup option in the setup ListOperationIT * lint * chore: added try/catch * chore: reverted project ID * chore: added filter * chore: added filter errror * chore: added sout for deleted items * chore: removed log message from setup * chore: added exponential backkoff * chore: decreased multiplier to 1.1 and setMaxelapsedTime 3min * chore: added some helpful comments * chore: lint
1 parent b98be07 commit 6a1b920

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

automl/snippets/src/test/java/com/example/automl/ListOperationStatusTest.java

+65-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.assertNotNull;
2121

22+
import com.google.api.client.util.ExponentialBackOff;
23+
import com.google.api.gax.rpc.ResourceExhaustedException;
24+
import com.google.cloud.automl.v1.AutoMlClient;
25+
import com.google.cloud.automl.v1.LocationName;
26+
import com.google.longrunning.ListOperationsRequest;
27+
import com.google.longrunning.Operation;
28+
import com.google.longrunning.OperationsClient;
2229
import java.io.ByteArrayOutputStream;
2330
import java.io.IOException;
2431
import java.io.PrintStream;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
import java.util.concurrent.TimeUnit;
2535
import org.junit.After;
2636
import org.junit.Before;
2737
import org.junit.BeforeClass;
@@ -31,7 +41,7 @@
3141

3242
@RunWith(JUnit4.class)
3343
public class ListOperationStatusTest {
34-
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
44+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3545
private ByteArrayOutputStream bout;
3646
private PrintStream out;
3747
private PrintStream originalPrintStream;
@@ -49,11 +59,64 @@ public static void checkRequirements() {
4959
}
5060

5161
@Before
52-
public void setUp() {
62+
public void setUp() throws IOException, InterruptedException {
5363
bout = new ByteArrayOutputStream();
5464
out = new PrintStream(bout);
5565
originalPrintStream = System.out;
5666
System.setOut(out);
67+
68+
// if the LRO status count more than 300, delete half of operations.
69+
try (AutoMlClient client = AutoMlClient.create()) {
70+
OperationsClient operationsClient = client.getOperationsClient();
71+
LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
72+
ListOperationsRequest listRequest =
73+
ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
74+
List<String> operationFullPathsToBeDeleted = new ArrayList<>();
75+
for (Operation operation : operationsClient.listOperations(listRequest).iterateAll()) {
76+
// collect unused operation into the list.
77+
// Filter: deleting already done operations.
78+
if (operation.getDone() && !operation.hasError()) {
79+
operationFullPathsToBeDeleted.add(operation.getName());
80+
}
81+
}
82+
83+
if (operationFullPathsToBeDeleted.size() > 300) {
84+
System.out.println("Cleaning up...");
85+
86+
87+
for (String operationFullPath :
88+
operationFullPathsToBeDeleted.subList(0, operationFullPathsToBeDeleted.size() / 2)) {
89+
// retry_interval * (random value in range [1 - rand_factor, 1 + rand_factor])
90+
ExponentialBackOff exponentialBackOff = new ExponentialBackOff.Builder()
91+
.setInitialIntervalMillis(60000)
92+
.setMaxElapsedTimeMillis(300000)
93+
.setRandomizationFactor(0.5)
94+
.setMultiplier(1.1)
95+
.setMaxIntervalMillis(80000)
96+
.build();
97+
98+
// delete unused operations.
99+
try {
100+
operationsClient.deleteOperation(operationFullPath);
101+
} catch (ResourceExhaustedException ex) {
102+
// exponential back off and retry.
103+
long backOffInMillis = exponentialBackOff.nextBackOffMillis();
104+
System.out.printf("Backing off for %d milliseconds "
105+
+ "due to Resource exhaustion.\n", backOffInMillis);
106+
if (backOffInMillis < 0) {
107+
break;
108+
}
109+
System.out.println("Backing off" + backOffInMillis);
110+
TimeUnit.MILLISECONDS.sleep(backOffInMillis);
111+
} catch (Exception ex) {
112+
throw ex;
113+
}
114+
}
115+
} else {
116+
// Clear the list since we wont anything with the list.
117+
operationFullPathsToBeDeleted.clear();
118+
}
119+
}
57120
}
58121

59122
@After

0 commit comments

Comments
 (0)