Details
-
Improvement
-
Resolution: Unresolved
-
Major
-
None
Description
Page https://docs.couchbase.com/dotnet-sdk/current/howtos/provisioning-cluster-resources.html and the associated sample code are not OK because bucket creation can take some time (bucket creation is an asynchronous non-blocking operation and can last some seconds).
Even with await keyword, we get the response from the request for a bucket creation BUT not for the creation being already done (when the method returns, bucket creation is in progress, not completed yet):
await bucketMgr.CreateBucketAsync
|
This can lead to subsequent errors in the code because of bucket being not ready yet.
This is why it is MANDATORY to add this kind of utility method (this should appear in our documentation AND code sample):
/// <summary> |
/// Checks if a Bucket exists as an Http resource. |
/// As the presence of the resource does not guarantee it is ready to use, |
/// this method also attempts to bootstrap against the bucket. |
/// </summary> |
/// <param name="bucketName">The Bucket's name.</param> |
/// <param name="limit">Number of times to retry.</param> |
/// <returns>True or false depending on if the Bucket could be accessed.</returns> |
static async Task<bool> WaitUntilBucketIsAvailable(string bucketName, int limit = 10) |
{
|
var retryCount = 0; |
while (retryCount < limit) |
{
|
Console.WriteLine($"Retry count: {retryCount}"); |
try |
{
|
await _cluster.Buckets.GetBucketAsync(bucketName).ConfigureAwait(false); |
Console.WriteLine("Bucket exists as an HTTP resource."); |
await _cluster.BucketAsync(bucketName).ConfigureAwait(false); |
Console.WriteLine("Bucket is available to use."); |
return true; |
}
|
catch (Exception) |
{
|
retryCount++;
|
await Task.Delay(LinearBackoff(retryCount)).ConfigureAwait(false); |
}
|
}
|
Console.WriteLine($"Could not retrieve Bucket within {limit} retries."); |
return false; |
}
|
static TimeSpan LinearBackoff(int retryCount) |
{
|
return TimeSpan.FromMilliseconds(retryCount * 200); |
}
|
}
|
Note that this kind of helper method already exists in our .Net SDK test code and therefore MUST be present inside our documentation, see :
Also this API doc page should state that bucket creation is async and, even with await call, this is a non blocking operation and bucket creation check needs to be performed.