Migrating from SDK2 to SDK3 API

    +
    The 3.0 API breaks the existing 2.0 APIs in order to provide a number of improvements. Collections and Scopes are introduced. The Document class and structure has been completely removed from the API, and the returned value is now Result. Retry behavior is more proactive, and lazy bootstrapping moves all error handling to a single place. Individual behaviour changes across services are explained here.

    In following Microsoft recommendations on .NET, the API has also been changed to surface many of the APIs as async using Tasks.

    Fundamentals

    The Couchbase SDK team takes semantic versioning seriously, which means that API should not be broken in incompatible ways while staying on a certain major release. This has the benefit that most of the time upgrading the SDK should not cause much trouble, even when switching between minor versions (not just bugfix releases). The downside though is that significant improvements to the APIs are very often not possible, save as pure additions — which eventually lead to overloaded methods.

    To support new server releases and prepare the SDK for years to come, we have decided to increase the major version of each SDK and as a result take the opportunity to break APIs where we had to. As a result, migration from the previous major version to the new major version will take some time and effort — an effort to be counterbalanced by improvements to coding time, through the simpler API, and performance. The new API is built on years of hands-on experience with the current SDK as well as with a focus on simplicity, correctness, and performance.

    Before this guide dives into the language-specific technical component of the migration, it is important to understand the high level changes first. As a migration guide, this document assumes you are familiar with the previous generation of the SDK and does not re-introducing SDK 2.0 concepts. We recommend familiarizing yourself with the new SDK first by reading at least the getting started guide, and browsing through the other chapters a little.

    Terminology

    The concept of a Cluster and a Bucket remain the same, but a fundamental new layer is introduced into the API: Collections and their Scopes. Collections are logical data containers inside a Couchbase bucket that let you group similar data just like a Table does in a relational database — although documents inside a collection do not need to have the same structure. Scopes allow the grouping of collections into a namespace, which is very usfeul when you have multilpe tenants acessing the same bucket. Couchbase Server is including support for collections as a developer preview in version 6.5 — in a future release, it is hoped that collections will become a first class concept of the programming model. To prepare for this, the SDKs include the feature from SDK 3.0.

    In the previous SDK generation, particularly with the KeyValue API, the focus has been on the codified concept of a Document. Documents were read and written and had a certain structure, including the id/key, content, expiry (ttl), and so forth. While the server still operates on the logical concept of documents, we found that this model in practice didn’t work so well for client code in certain edge cases. As a result we have removed the Document class/structure completely from the API. The new API follows a clear scheme: each command takes required arguments explicitly, and an option block for all optional values. The returned value is always of type Result. This avoids method overloading bloat in certain languages, and has the added benefit of making it easy to grasp APIs evenly across services.

    As an example here is a KeyValue document fetch:

    IGetResult getResult = await collection.GetAsync("doc1");
    var docContent = getResult.ContentAs<dynamic>();

    Compared to the 2.x SDK, you will note that the result is now oriented around the result of the operation. Retrieval of the content of the document is accomplished through the ContentAs() method. Also, in most cases you do not need to check the result object returned as exceptions are thrown for most error conditions. Check the API reference for details.

    Compare this to a N1QL query:

    IQueryResult<dynamic> result = await cluster.QueryAsync<dynamic>("select 1 = 1", QueryOptions.Create().Timeout(TimeSpan.FromSeconds(3)));

    Since documents also fundamentally handled the serialization aspects of content, two new concepts are introduced: the Serializer and the Transcoder. Out of the box the SDKs ship with a JSON serializer which handles the encoding and decoding of JSON. You’ll find the serializer exposes the options for methods like N1QL queries and KeyValue subdocument operations,.

    The KV API extends the concept of the serializer to the Transcoder. Since you can also store non-JSON data inside a document, the Transcoder allows the writing of binary data as well. It handles the object/entity encoding and decoding, and if it happens to deal with JSON makes uses of the configured Serializer internally. See the Serialization and Transcoding section below for details.

    What to look out for

    The SDKs are more proactive in retrying with certain errors and in certain situations, within the timeout budget given by the user — as an example, temporary failures or locked documents are now being retried by default — making it even easier to program against certain error cases. This behavior is customizable in a RetryStrategy, which can be overridden on a per operation basis for maximum flexibility if you need it.

    Note, most of the bootstrap sequence is now lazy (happening behind the scenes). For example, opening a bucket is not raising an error anymore, but it will only show up once you perform an actual operation. The reason behind this is to spare the application developer the work of having to do error handling in more places than needed. A bucket can go down 2ms after you opened it, so you have to handle request failures anyway. By delaying the error into the operation result itself, there is only one place to do the error handling. There will still be situations why you want to check if the resource you are accessing is available before continuing the bootstrap; for this, we have the diagnostics and ping commands at each level which allow you to perform those checks eagerly.

    Language Specifics

    Now that you are familiar with the general theme of the migration, the next sections dive deep into the specifics. First, installation and configuration are covered, then we talk about exception handling, and then each service (i.e. Key/Value, Query,…​) is covered separately.

    Installation and Configuration

    The .NET SDK 3.x is available for download from the same resources as the previous generation 2.0 SDK:

    Please see the Release Notes for up-to-date information.

    Couchbase .NET SDK 3.0 targets the .NET Standard 2.0. This was chosen so .NET Full Framework 4.8 can be supported along with .NET Core 3.x and earlier and follows the suggested path for library authors. The goal is to use the latest and greatest .NET Core libraries available, but still allow fallback for developers still using .NET Full Framework as they progress towards .NET Standard compliance.

    Dependencies

    There are few dependency changes from SDK 2.x. SDK 3.0 and later no longer has any dependencies on .NET Framework 4.5.2. It is based on .NET Standard 2.0. One dependency addition is DnsClient, which was previously in SDK 2.x through a transitive dependency via the Couchbase.Extensions.DnsDiscovery project.

    Current dependencies:

    • DnsClient (>= 1.2.0)

    • Microsoft.CSharp (>= 4.5.0)

    • Microsoft.Extensions.Logging (>= 2.1.1)

    • Newtonsoft.Json (>= 11.0.2)

    • OpenTracing (>= 0.12.0)

    • System.Memory (>= 4.5.2)

    Note that if using the NuGet package these dependencies will all be handled for you by the NuGet Package Manager tool in Visual Studio or Visual Studio Code. See the Start Using document for information on adding it via NuGet.

    Configuring the Environment

    Configuration is essentially the same as SDK 2.x retaining capabilites with less tunable properties. Instead of using a ClientConfiguration object, you would use a ClusterOptions object. For example, to use a custom timeout for Key/Value (K/V) operations in SDK 2.x you would do something like this:

    // SDK 2.0 custom k/v timeout
     var config = new ClientConfiguration
    {
        DefaultOperationLifespan = (uint)TimeSpan.FromMilliseconds(5).TotalMilliseconds
    };

    You can perform the same custom K/V timeout in SDK 3.0, however, you will use the ClusterOptions class:

    // SDK 3.0 custom k/v timeout
     var options = new ClusterOptions
    {
        KvTimeout = TimeSpan.FromMilliseconds(5)
    };

    The configuration options are passed into the Cluster object via a constructor:

    var cluster = new Cluster("couchbase://localhost", options);

    Or by using one of the static Cluster.ConnectAsync(…​) methods:

    var cluster = await Cluster.ConnectAsync("couchbase://localhost", options);

    In order to free all resources associated with a cluster, simply call the .Dispose() method:

    cluster.Dispose();

    When creating a configuration, you may customize settings through the ClusterOptions or the connection string. See client configuration for further configuration details.

                // Will set the max http connections to 23
                var config = new ClusterOptions()
                {
                    UserName = "user",
                    Password = "pass",
                    MaxHttpConnections =  23
                };
    
                config.WithConnectionString("couchbase://localhost");
    
                cluster = await Cluster.ConnectAsync(config);

    The above has this equivalent with a connection string

                // Will set the max http connections to 23
                var config = new ClusterOptions()
                {
                    Password = "pass"
                };
    
                config.WithConnectionString("couchbase://user@localhost?MaxHttpConnections=23");
    
                cluster = await Cluster.ConnectAsync(config);

    The SDK offers a configuration API for customizing bootstrapping, timeouts, reliability and performance tuning. Configuration options have changed since the 2.x release. See the configuration section for specifics.

    Authentication

    Since SDK 2 supports Couchbase Server clusters older than 5.0, it had to support both Role-Based access control as well as bucket-level passwords. The minimum cluster version supported by SDK 3 is Server 5.0, which means that only RBAC is supported. This is why you can set the username and password when directly connecting:

    var cluster = await Cluster.ConnectAsync("couchbase://localhost", "user", "pass");

    This is just a shorthand for:

                var cluster = await Cluster.ConnectAsync("localhost", new ClusterOptions
                {
                    UserName = "user",
                    Password = "pass"
    
                });

    You may also use this approach to configure certificate-based authentication:

                cluster = await Cluster.ConnectAsync("127.0.0.1", new ClusterOptions
                {
                    EnableCertificateAuthentication = true
                });

    Connection Lifecycle

    From a high-level perspective, bootstrapping and shutdown is very similar to SDK 2. One notable difference is that the Collection is introduced and that the individual methods like bucket immediately return, and do not throw an exception. Compare SDK 2: the openBucket method would not work if it could not open the bucket.

    The reason behind this change is that even if a bucket can be opened, a millisecond later it may not be available any more. All this state has been moved into the actual operation so there is only a single place where the error handling needs to take place. This simplifies error handling and retry logic for an application.

    In SDK 2, you connected, opened a bucket, performed a KV op, and disconnected like this:

                var cluster = new Cluster(new ClientConfiguration
                {
                    Servers = new List<Uri> { new Uri("http://localhost:8091") }
                });
    
                var authenticator = new PasswordAuthenticator("user", "pass");
                cluster.Authenticate(authenticator);
                var bucket = cluster.OpenBucket("travel-sample");
    
                var result = bucket.Get<dynamic>("airline_10");
    
                bucket.Dispose();
                cluster.Dispose();

    Here is the SDK 3 equivalent:

                var cluster = await Cluster.ConnectAsync("127.0.0.1", "user", "pass");
                var bucket = await cluster.BucketAsync("travel-sample");
                var collection = bucket.DefaultCollection();
    
                var getResult = await collection.GetAsync("airline_10");
    
                cluster.Dispose();

    Collections will be generally available with an upcoming Couchbase Server release, but the SDK already encodes it in its API to be future-proof. If you are using a Couchbase Server version which does not support Collections such as 6.0, always use the defaultCollection() method to access the KV API; it will map to the full bucket.

    You’ll notice that BucketAsync(String) returns immediately, even if the bucket resources are not completely opened. This means that the subsequent Get operation may be dispatched even before the connection is opened in the background. The SDK will handle this case transparently, and reschedule the operation until the bucket is opened properly. This also means that if a bucket could not be opened (say, because no server was reachable) the operation will time out. Please check the logs to see the cause of the timeout. In this example case, you’ll see network socket connection failures.

    Also note you will now find Query, Search, and Analytics at the Cluster level. This is where they logically belong. If you are using Couchbase Server 6.5 or later, you will be able to perform cluster-level queries even if no bucket is open. If you are using an earlier version of the cluster you must open at least one bucket, otherwise cluster-level queries will fail.

    Async and Await by Default

    SDK 2 followed a pattern of having a Method() and a MethodAsync() as was the common approach in most C# code at the time. Subsequently, it has become more common in contemporary C# code for all methods to return a Task from all asynchronous methods. This is now considered to be the best practice. Read more about it in MSDN’s post Async All the Way. With this change, the SDK follows the same idioms in the base class library’s HttpClient. The application then waits as appropriate on these calls with either the await keyword or by calling the .Wait() method, depending on how it fits into the rest of the applciation.

    Exception Handling

    How to handle exceptions is unchanged from SDK 2. You should still use try/catch.

    There have been changes made in the following areas:

    • Exception hierachy and naming.

    • Proactive retry where possible.

    Exception hierachy

    The exception hierachy is now flat and unified under a CouchbaseException. Each CouchbaseException has an associated ErrorContext which is populated with as much information as possible and then dumped alongside the stack trace if an error happens.

    Here is an example of the error context if a N1QL query is performed with an invalid syntax (i.e. select 1= from):

          try {
              IQueryResult<dynamic> result = await cluster.QueryAsync<dynamic>("select 1");
          }
          catch (CouchbaseException ex) {
              Console.WriteLine(ex);
          }
    -----------------------Context Info---------------------------
    {"Statement":"[{\"statement\":\"select 1 = \",\"timeout\":\"3000ms\",\"client_context_id\":\"e3003393-e54b-4f5b-b620-f91903556282\"}]","ClientContextId":"e3003393-e54b-4f5b-b620-f91903556282","Parameters":"{\"Named\":{},\"Raw\":{},\"Positional\":[]}","HttpStatus":400,"QueryStatus":6,"Errors":[{"msg":"syntax error - at end of input","Code":3000,"Name":null,"Severity":0,"Temp":false}],"Message":null}

    The expectation is that the application catches the CouchbaseException and deals with it as an unexpected error (e.g. logging with subsequent bubbling up of the exception or failing). In addition to that, each method exposes exceptions that can be caught separately if needed. For example, a GetAsync() may throw a DocumentNotFoundException or a TimeoutException in addition to a more generic CouchbaseException. These exceptions extend CouchbaseException, but both the TimeoutException and the DocumentNotFoundException can be caught individually if specific logic should be executed to handle them.

    Proactive Retry

    One reason why the APIs do not expose a long list of exceptions is that the SDK now retries as many operations as it can if it can do so safely. This depends on the type of operation (idempotent or not), in which state of processing it is (already dispatched or not), and what the actual response code is if it arrived already. As a result, many transient cases — such as locked documents, or temporary failure — are now retried by default and should less often impact applications. It also means, when migrating to the new SDK API, you may observe a longer period of time until an error is returned by default.

    Operations are retried by default as described above with the default BestEffortRetryStrategy. Like in SDK 2 you can configure fail-fast retry strategies to not retry certain or all operations. The RetryStrategy interface has been extended heavily in SDK 3 — please see the error handling documentation.

    When migrating your SDK 2 exception handling code to SDK 3, make sure to wrap every call with a catch for CouchbaseException (or let it bubble up immediately). You can likely remove your user-level retry code for temporary failures, backpressure exception, and so on. One notable exception from this is the CasMismatchException, which is still thrown since it requires more app-level code to handle (most likely identical to SDK 2).

    Logging and Events

    Configuring and consuming logs has not greatly changed.

    The SDK is still compatible with multiple loggers and works well with the .NET Core Runtime abstraction interface in Microsoft.Extensions.Logging. The biggest impact you’ll see from it is that the log messages now look very structured and contain contextual information where possible.

    The logger may be configured from the ClusterOptions.

    using Microsoft.Extensions.Logging;
    …
          var loggerFactory = new LoggerFactory().AddConsole();
    
          var config = new ClusterOptions()
          {
              UserName = "user",
              Password = "pass",
              Logging = loggerFactory
          };
    
          config.WithConnectionString("couchbase://localhost");

    Migrating Services

    The following section discusses each service in detail and covers specific bits that have not been covered by the more generic sections above.

    Key Value

    The Key/Value (KV) API is now located under the Collection interface, so even if you do not use collections, the DefaultCollection() needs to be opened in order to access it.

    The following table describes the SDK 2 KV APIs and where they are now located in SDK 3:

    Table 1. SDK 2.x KV API vs. SDK 3.x KV API
    SDK 2 SDK 3

    Bucket.Upsert and Bucket.UpsertAsync

    Collection.UpsertAsync

    Bucket.Get and Bucket.GetAsync

    Collection.GetAsync

    Bucket.Exists and Bucket.ExistsAsync

    Collection.ExistsAsync

    Bucket.GetFromReplica and Bucket.GetFromReplicaAsync

    Collection.GetAnyReplicaAsync and Collection.GetAllReplicasAsync

    Bucket.GetAndLock and Bucket.GetAndLockAsync

    Collection.GetAndLockAsync

    Bucket.GetAndTouch and Bucket.GetAndTouchAsync

    Collection.GetAndTouchAsync

    Bucket.Insert and Bucket.InsertAsync

    Collection.InsertAsync

    Bucket.Upsert and Bucket.UpsertAsync

    Collection.UpsertAsync

    Bucket.Replace and Bucket.ReplaceAsync

    Collection.ReplaceAsync

    Bucket.Remove and Bucket.RemoveAsync

    Collection.RemoveAsync

    Bucket.Unlock and Bucket.UnlockAsync

    Collection.UnlockAsync

    Bucket.Touch and Bucket.TouchAsync

    Collection.TouchAsync

    Bucket.LookupIn

    Collection.LookupInAsync

    Bucket.MutateIn

    Collection.MutateInAsync

    Bucket.Increment, Bucket.IncrementAsync

    Bucket.Decrement and Bucket.DecrementAsync

    Collection.Binary.IncrementAsync and Collection.Binary.DecrementAsync

    Bucket.Append and Bucket.AppendAsync

    Collection.Binary.IncrementAsync.AppendAsync

    Bucket.Prepend and Bucket.PrependAsync

    Collection.Binary.IncrementAsync.PrependAsync

    There are two important API changes:

    • On the request side, overloads have been reduced and moved under a Options block

    • On the response side, the return types have been unified.

    The signatures now look very similar. The concept of the Document as a type is gone in SDK 3 and instead you need to pass in the properties explicitly. This makes it very clear what is returned, especially on the response side.

    Thus, the GetAsync method does not return a Document but a GetResult instead, and the UpsertAsync does not return a Document but a MutationResult. Each of those results only contain the field that the specific method can actually return, making it impossible to accidentally try to access the expiry on the Document after a mutation, for example.

    Instead of having many overloads, all optional params are now part of the Option block. All required params are still part of the method signature, making it clear what is required and what is not (or has default values applied if not overridden).

    The timeout can be overridden on every operation and now takes a Duration from java 8. Compare SDK 2 and SDK 3 custom timeout setting:

                // SDK 2 custom timeout
                IOperationResult getResult = bucket.Get<dynamic>("mydoc-id", TimeSpan.FromMilliseconds(2250));
                IGetResult getaResult = await collection.GetAsync("mydoc-id",
                    options => options.Timeout(TimeSpan.FromMilliseconds(2250)));

    Query

    N1QL querying is now available at the Cluster level instead of the bucket level, because you can also write N1QL queries that span multiple buckets. Querying is now async by default as discussed earlier. One related change is that query results come back in an async stream by default as well. To convert results to IEnumerable to iterate like you would in the 2.x SDK, you can call .ToEnumerable() on the results.

    Compare a simple N1QL query from SDK 2 with its SDK 3 equivalent:

          // SDK 2 simple query
          var query = new QueryRequest("SELECT * FROM `travel-sample` LIMIT 10");
          foreach (var row in bucket.Query<dynamic>(query))
          {
              Console.WriteLine(JsonConvert.SerializeObject(row));
          }
          try {
              var queryResult = await cluster.QueryAsync<dynamic>("SELECT `travel-sample`.* FROM `travel-sample` LIMIT 10",
                  parameter =>
                  {
                      parameter.Parameter("type", "airline");
                  }).ConfigureAwait(false);
    
              await foreach (var o in queryResult.ConfigureAwait(false))
              {
                  Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(o, Newtonsoft.Json.Formatting.None));
              }
          }
          catch (CouchbaseException) {
              // SDK 3 throws exceptions where possible
          }

    If you want to use prepared statements, the AdHoc() method is still available on the QueryOptions, alongside every other option that used to be exposed on the SDK 2 Query options.

    Much of the non-row metadata has been moved into a specific QueryMetaData section:

    Table 2. Query Metadata Changes
    SDK 2 SDK 3

    IQueryResult.Signature

    QueryResult.QueryMetaData.Signature

    IQueryResult.Metrics

    QueryResult.QueryMetaData.Metrics

    IQueryResult.Profile

    QueryResult.QueryMetaData.Profile

    IQueryResult.Success

    removed

    IQueryResult.Status

    QueryResult.QueryMetaData.status containing any warnings

    IQueryResult.Errors

    throws an Exception on QueryResult

    IQueryResult.RequestId

    QueryResult.QueryMetaData.RequestId

    IQueryResult.ClientContextId

    QueryResult.QueryMetaData.ClientContextId

    It is no longer necessary to check for a specific error in the stream: if an error happened during processing it will throw an exception at the top level of the query. The stream with terminate with an error as soon as one is received.