Skip to content

Commit 19df621

Browse files
lqiu96blakeli0suztomo
authored
chore: Update LRO help section (#9720)
* chore: Update LRO help section * chore: Update wording * chore: Apply suggestions from code review Co-authored-by: Blake Li <[email protected]> * chore: Resolve merge conflicts * chore: Address PR comments * chore: Add more info to README * Update README.md Co-authored-by: Tomo Suzuki <[email protected]> * chore: Address PR comments * chore: Address PR comments * Apply suggestions from code review Co-authored-by: Blake Li <[email protected]> --------- Co-authored-by: Blake Li <[email protected]> Co-authored-by: Tomo Suzuki <[email protected]>
1 parent 1ecb036 commit 19df621

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

README.md

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,81 @@ public CloudTasksClient getService() throws IOException {
384384
## Long Running Operations
385385
386386
Long running operations (LROs) are often used for API calls that are expected to
387-
take a long time to complete (e.g. provisioning a GCE instance or a Dataflow pipeline).
388-
The initial API call creates an "operation" on the server and returns an operation ID
389-
to track its progress.
387+
take a long time to complete (i.e. provisioning a GCE instance or a Dataflow pipeline).
388+
The initial API call creates an "operation" on the server and returns an Operation ID
389+
to track its progress. LRO RPCs have the suffix `Async` appended to the call name
390+
(i.e. `clusterControllerClient.createClusterAsync()`)
390391
391-
Our generated gRPC clients provide a nice interface for starting the operation and
392+
Our generated clients provide a nice interface for starting the operation and
392393
then waiting for the operation to complete. This is accomplished by returning an
393394
[`OperationFuture`](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.longrunning.OperationFuture).
394-
When you call `get()` on the `OperationFuture` we poll the operation endpoint to
395-
check on the operation. These polling operations have a default timeout that
396-
varies from service to service and will throw a `java.util.concurrent.CancellationException`
397-
with the message: `Task was cancelled.` after that timeout has been reached.
395+
When calling `get()` on the `OperationFuture`, the client library will poll the operation to
396+
check the operation's status.
398397
399-
### Configuring LRO Timeouts
398+
For example, take a sample `createCluster` Operation in google-cloud-dataproc v4.20.0:
399+
```java
400+
try (ClusterControllerClient clusterControllerClient = ClusterControllerClient.create()) {
401+
CreateClusterRequest request =
402+
CreateClusterRequest.newBuilder()
403+
.setProjectId("{PROJECT_ID}")
404+
.setRegion("{REGION}")
405+
.setCluster(Cluster.newBuilder().build())
406+
.setRequestId("{REQUEST_ID}")
407+
.setActionOnFailedPrimaryWorkers(FailureAction.forNumber(0))
408+
.build();
409+
OperationFuture<Cluster, ClusterOperationMetadata> future =
410+
clusterControllerClient.createClusterOperationCallable().futureCall(request);
411+
// Do something.
412+
Cluster response = future.get();
413+
} catch (CancellationException e) {
414+
// Exceeded the default RPC timeout without the Operation completing.
415+
// Library is no longer polling for the Operation status. Consider
416+
// increasing the timeout.
417+
}
418+
```
419+
420+
### LRO Timeouts
421+
The polling operations have a default timeout that varies from service to service.
422+
The library will throw a `java.util.concurrent.CancellationException` with the message:
423+
`Task was cancelled.` if the timeout exceeds the operation. A `CancellationException`
424+
does not mean that the backend GCP Operation was cancelled. This exception is thrown from the
425+
client library when it has exceeded the total timeout without receiving a successful status from the operation.
426+
Our client libraries respect the configured values set in the OperationTimedPollAlgorithm for each RPC.
427+
428+
Note: The client library handles the Operation's polling mechanism for you. By default, there is no need
429+
to manually poll the status yourself.
400430
431+
### Default LRO Values
432+
Each LRO RPC has a pre-configured default values. You can find these values by
433+
searching in each Client's `StubSettings`'s class. The default LRO settings are initialized
434+
inside the `initDefaults()` method in the nested Builder class.
435+
436+
For example, in google-cloud-aiplatform v3.24.0, the default [OperationTimedPollAlgorithm](https://github.com/googleapis/google-cloud-java/blob/9ae786d1acdc7354adf86b78691570668caa293d/java-aiplatform/google-cloud-aiplatform/src/main/java/com/google/cloud/aiplatform/v1/stub/EndpointServiceStubSettings.java#L755-L765)
437+
has these default values:
438+
```java
439+
OperationTimedPollAlgorithm.create(
440+
RetrySettings.newBuilder()
441+
.setInitialRetryDelay(Duration.ofMillis(5000L))
442+
.setRetryDelayMultiplier(1.5)
443+
.setMaxRetryDelay(Duration.ofMillis(45000L))
444+
.setInitialRpcTimeout(Duration.ZERO)
445+
.setRpcTimeoutMultiplier(1.0)
446+
.setMaxRpcTimeout(Duration.ZERO)
447+
.setTotalTimeout(Duration.ofMillis(300000L))
448+
.build())
449+
```
450+
Both retries and LROs share the same RetrySettings class. Note the corresponding link:
451+
- Total Timeout (Max Time allowed for polling): 5 minutes
452+
- Initial Retry Delay (Initial delay before first poll): 5 seconds
453+
- Max Retry Delay (Maximum delay between each poll): 45 seconds
454+
- Retry Delay Multiplier (Multiplier value to increase the poll delay): 1.5
455+
456+
The RPC Timeout values have no use in LROs and can be omitted or set to the default values
457+
(`Duration.ZERO` for Timeouts or `1.0` for the multiplier).
458+
459+
### Configuring LRO Timeouts
460+
To configure the LRO values, create an OperationTimedPollAlgorithm object and update the
461+
RPC's polling algorithm. For example:
401462
```java
402463
ClusterControllerSettings.Builder settingsBuilder = ClusterControllerSettings.newBuilder();
403464
TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create(
@@ -415,6 +476,9 @@ settingsBuilder.createClusterOperationSettings()
415476
ClusterControllerClient clusterControllerClient = ClusterControllerClient.create(settingsBuilder.build());
416477
```
417478
479+
Note: The configuration above *only* modifies the LRO values for the `createClusterOperation` RPC.
480+
The other RPCs in the Client will still use each RPC's pre-configured LRO values.
481+
418482
## Managing Dependencies
419483
420484
If you are using more than one Google Cloud client library, we recommend you use one of

0 commit comments

Comments
 (0)