19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static junit .framework .TestCase .assertNotNull ;
21
21
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 ;
22
29
import java .io .ByteArrayOutputStream ;
23
30
import java .io .IOException ;
24
31
import java .io .PrintStream ;
32
+ import java .util .ArrayList ;
33
+ import java .util .List ;
34
+ import java .util .concurrent .TimeUnit ;
25
35
import org .junit .After ;
26
36
import org .junit .Before ;
27
37
import org .junit .BeforeClass ;
31
41
32
42
@ RunWith (JUnit4 .class )
33
43
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 " );
35
45
private ByteArrayOutputStream bout ;
36
46
private PrintStream out ;
37
47
private PrintStream originalPrintStream ;
@@ -49,11 +59,64 @@ public static void checkRequirements() {
49
59
}
50
60
51
61
@ Before
52
- public void setUp () {
62
+ public void setUp () throws IOException , InterruptedException {
53
63
bout = new ByteArrayOutputStream ();
54
64
out = new PrintStream (bout );
55
65
originalPrintStream = System .out ;
56
66
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
+ }
57
120
}
58
121
59
122
@ After
0 commit comments