Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
chore: added cleanup option in the setup ListOperationIT (#536)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
munkhuushmgl committed May 8, 2021
1 parent a18442b commit 753d4af
Showing 1 changed file with 65 additions and 2 deletions.
Expand Up @@ -19,9 +19,19 @@
import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.api.client.util.ExponentialBackOff;
import com.google.api.gax.rpc.ResourceExhaustedException;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.LocationName;
import com.google.longrunning.ListOperationsRequest;
import com.google.longrunning.Operation;
import com.google.longrunning.OperationsClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -31,7 +41,7 @@

@RunWith(JUnit4.class)
public class ListOperationStatusTest {
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;
Expand All @@ -49,11 +59,64 @@ public static void checkRequirements() {
}

@Before
public void setUp() {
public void setUp() throws IOException, InterruptedException {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);

// if the LRO status count more than 300, delete half of operations.
try (AutoMlClient client = AutoMlClient.create()) {
OperationsClient operationsClient = client.getOperationsClient();
LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
ListOperationsRequest listRequest =
ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
List<String> operationFullPathsToBeDeleted = new ArrayList<>();
for (Operation operation : operationsClient.listOperations(listRequest).iterateAll()) {
// collect unused operation into the list.
// Filter: deleting already done operations.
if (operation.getDone() && !operation.hasError()) {
operationFullPathsToBeDeleted.add(operation.getName());
}
}

if (operationFullPathsToBeDeleted.size() > 300) {
System.out.println("Cleaning up...");


for (String operationFullPath :
operationFullPathsToBeDeleted.subList(0, operationFullPathsToBeDeleted.size() / 2)) {
// retry_interval * (random value in range [1 - rand_factor, 1 + rand_factor])
ExponentialBackOff exponentialBackOff = new ExponentialBackOff.Builder()
.setInitialIntervalMillis(60000)
.setMaxElapsedTimeMillis(300000)
.setRandomizationFactor(0.5)
.setMultiplier(1.1)
.setMaxIntervalMillis(80000)
.build();

// delete unused operations.
try {
operationsClient.deleteOperation(operationFullPath);
} catch (ResourceExhaustedException ex) {
// exponential back off and retry.
long backOffInMillis = exponentialBackOff.nextBackOffMillis();
System.out.printf("Backing off for %d milliseconds "
+ "due to Resource exhaustion.\n", backOffInMillis);
if (backOffInMillis < 0) {
break;
}
System.out.println("Backing off" + backOffInMillis);
TimeUnit.MILLISECONDS.sleep(backOffInMillis);
} catch (Exception ex) {
throw ex;
}
}
} else {
// Clear the list since we wont anything with the list.
operationFullPathsToBeDeleted.clear();
}
}
}

@After
Expand Down

0 comments on commit 753d4af

Please sign in to comment.