Uploaded image for project: 'Couchbase Documentation'
  1. Couchbase Documentation
  2. DOC-12365

.Net bucket creation can take some time : add logic to be sure bucket is ready

    XMLWordPrintable

Details

    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 :

      https://github.com/couchbase/couchbase-net-client/blob/c10fe9ef09beadb8512f696d764b7a770429e641/tests/Couchbase.CombinationTests/Utils/TestHelper.cs#L18-L38

      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.

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            richard.smedley Richard Smedley
            fabrice.leray Fabrice Leray
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty