Skip to content

Commit

Permalink
feat: Retry options implementation across all APIs (#10183)
Browse files Browse the repository at this point in the history
* Custom Retry options changes in all APIs and RetryHandler changes
  • Loading branch information
anshuldavid13 committed Apr 19, 2023
1 parent 6e52ed9 commit cc384b5
Show file tree
Hide file tree
Showing 43 changed files with 189 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public sealed class CreateBucketOptions
/// </summary>
public Projection? Projection { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(BucketsResource.InsertRequest request)
{
if (PredefinedAcl != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Google LLC
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,11 @@ namespace Google.Cloud.Storage.V1
/// </summary>
public sealed class CreateHmacKeyOptions
{
/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(HmacKeysResource.CreateRequest request)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,11 @@ public sealed class CreateNotificationOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(NotificationsResource.InsertRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public sealed class DeleteBucketOptions
/// </remarks>
public bool? DeleteObjects { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(BucketsResource.DeleteRequest request)
{
if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Google LLC
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,11 @@ namespace Google.Cloud.Storage.V1
/// </summary>
public sealed class DeleteHmacKeyOptions
{
/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(ProjectsResource.HmacKeysResource.DeleteRequest request)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,11 @@ public sealed class DeleteNotificationOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(NotificationsResource.DeleteRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public sealed class DeleteObjectOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(ObjectsResource.DeleteRequest request)
{
// Note the use of ArgumentException here, as this will basically be the result of invalid
Expand All @@ -74,12 +79,10 @@ internal void ModifyRequest(ObjectsResource.DeleteRequest request)
if (Generation != null)
{
request.Generation = Generation;
RetryHandler.MarkAsRetriable(request);
}
if (IfGenerationMatch != null)
{
request.IfGenerationMatch = IfGenerationMatch;
RetryHandler.MarkAsRetriable(request);
}
if (IfGenerationNotMatch != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public sealed class GetBucketIamPolicyOptions
/// </summary>
public int? RequestedPolicyVersion { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(GetIamPolicyRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public sealed class GetBucketOptions
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Google LLC
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,11 @@ namespace Google.Cloud.Storage.V1
/// </summary>
public sealed class GetHmacKeyOptions
{
/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(ProjectsResource.HmacKeysResource.GetRequest request)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,11 @@ public sealed class GetNotificationOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(NotificationsResource.GetRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public sealed class GetObjectOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(GetRequest request)
{
// Note the use of ArgumentException here, as this will basically be the result of invalid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,11 @@ public sealed class GetStorageServiceAccountEmailOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(GetRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public sealed class ListBucketsOptions
/// </summary>
public string Fields { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

/// <summary>
/// Modifies the specified request for all non-null properties of this options object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Google LLC
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,11 @@ public sealed class ListHmacKeysOptions
/// </summary>
public bool? ShowDeletedKeys { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(ProjectsResource.HmacKeysResource.ListRequest request)
{
if (PageSize != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,11 @@ public sealed class ListNotificationsOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(ListRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public sealed class ListObjectsOptions
/// </summary>
public string EndOffset { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

/// <summary>
/// Modifies the specified request for all non-null properties of this options object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Google LLC
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,11 @@ public sealed class LockBucketRetentionPolicyOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(BucketsResource.LockRetentionPolicyRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public sealed class PatchBucketOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(PatchRequest request)
{
if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
Expand All @@ -67,7 +72,6 @@ internal void ModifyRequest(PatchRequest request)
if (IfMetagenerationMatch != null)
{
request.IfMetagenerationMatch = IfMetagenerationMatch;
RetryHandler.MarkAsRetriable(request);
}
if (IfMetagenerationNotMatch != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public sealed class PatchObjectOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(PatchRequest request)
{
// Note the use of ArgumentException here, as this will basically be the result of invalid
Expand Down Expand Up @@ -103,7 +108,6 @@ internal void ModifyRequest(PatchRequest request)
if (IfMetagenerationMatch != null)
{
request.IfMetagenerationMatch = IfMetagenerationMatch;
RetryHandler.MarkAsRetriable(request);
}
if (IfMetagenerationNotMatch != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ internal sealed class RetryHandler : IHttpUnsuccessfulResponseHandler
private RetryHandler(RetryOptions retryOptions, IScheduler scheduler) =>
(_retryOptions, _scheduler) = (retryOptions, scheduler);

// TODO: Will remove it once the implementation across all APIs is complete
internal static void MarkAsRetriable<TResponse>(StorageBaseServiceRequest<TResponse> request)
{
RetryHandler retryHandler = new RetryHandler(RetryOptions.IdempotentRetryOptions, SystemScheduler.Instance);

// Note: we can't use ModifyRequest, as the x-goog-api-client header is added later by ConfigurableMessageHandler.
// Additionally, that's only called once, and we may want to record the attempt number as well.
request.AddExecuteInterceptor(InvocationIdInterceptor.Instance);
request.AddUnsuccessfulResponseHandler(retryHandler);
}

/// <summary>
/// It marks the request as retriable with the retry options as provided.
/// In case null retry options or Never is passed, retry will not happen in case of failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public sealed class SetBucketIamPolicyOptions
/// </summary>
public string UserProject { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
public RetryOptions RetryOptions { get; set; }

internal void ModifyRequest(SetIamPolicyRequest request)
{
if (UserProject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ public sealed partial class StorageClientImpl : StorageClient
Object obj = options?.ExtraMetadata ?? new Object();
var request = Service.Objects.Rewrite(obj, sourceBucket, sourceObjectName, destinationBucket, destinationObjectName);

options?.ModifyRequest(request);
RetryOptions retryOptions = options?.RetryOptions ?? RetryOptions.MaybeIdempotent(options?.IfGenerationMatch);
MarkAsRetriable(request, retryOptions);

options?.ModifyRequest(request);
ApplyEncryptionKey(options?.EncryptionKey, options?.KmsKeyName, request);
request.ModifyRequest += (options?.SourceEncryptionKey ?? EncryptionKey).ModifyRequestForRewriteSource;
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ private BucketsResource.InsertRequest CreateCreateBucketRequest(string projectId
var bucketEntity = new Bucket { Name = bucket };
var request = Service.Buckets.Insert(bucketEntity, projectId);
options?.ModifyRequest(request);
RetryHandler.MarkAsRetriable(request);
RetryOptions retryOptions = options?.RetryOptions ?? RetryOptions.IdempotentRetryOptions;
MarkAsRetriable(request, retryOptions);
return request;
}

Expand All @@ -56,7 +57,8 @@ private BucketsResource.InsertRequest CreateCreateBucketRequest(string projectId
ValidateBucket(bucket, nameof(bucket));
var request = Service.Buckets.Insert(bucket, projectId);
options?.ModifyRequest(request);
RetryHandler.MarkAsRetriable(request);
RetryOptions retryOptions = options?.RetryOptions ?? RetryOptions.IdempotentRetryOptions;
MarkAsRetriable(request, retryOptions);
return request;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ private BucketsResource.DeleteRequest CreateDeleteBucketRequest(string bucket, D
{
var request = Service.Buckets.Delete(bucket);
options?.ModifyRequest(request);
RetryHandler.MarkAsRetriable(request);
RetryOptions retryOptions = options?.RetryOptions ?? RetryOptions.IdempotentRetryOptions;
MarkAsRetriable(request, retryOptions);
return request;
}
}
Expand Down
Loading

0 comments on commit cc384b5

Please sign in to comment.