csharp-driver icon indicating copy to clipboard operation
csharp-driver copied to clipboard

Change session/cluster initialization to happen in the background CSHARP-916

Open joao-r-reis opened this issue 5 years ago • 1 comments

Currently cluster.Connect() starts the initialization task of the control connection, then creates the session, then initializes the session.

This PR changes this behavior so that cluster.Connect() creates the session object and returns it to the user immediately. The session initialization task is started in the background which will start the cluster initialization if it's not initialized yet.

If a user attempts to do something with the session, cluster or cluster.Metadata that requires the initialization to be finished, the method will await/block until the initialization task is complete.

I also implemented CSHARP-698 on this PR. It uses the reconnection policy to keep retrying the control connection initialization in the background. **After an initialization attempt fails, all method calls (session.Execute, etc.) will throw the original exception until a new attempt is started. When a new attempt is started, the methods will block again until the current attempt finishes.

This PR also adds Session.Connect() for users that want to wait for the initialization to be finished.

Several API changes are required in order for this to be a good experience for the user.

The Cluster.Metadata property no longer blocks. Also introduced a IMetadata interface and a IMetadataSnapshotProvider that declares some Metadata methods which do not block.

ILoadBalancingPolicy.Initialize is called by the initialization method but a ICluster instance is passed to that method which means that if the user attempts to access Metadata it will deadlock because it requires the initialization task to be finished. This PR changes this method so that it receives a IMetadataSnapshotProvider instance instead (which is inherited by IMetadata). It also changes the method to support async (returns Task).

A lot of methods from ICluster and ISession are moved to Metadata. This simplifies the driver's API because these methods and properties were just wrappers of those in Metadata so it makes more sense to be on the IMetadata interface only.

Also updated the upgrade guide so it's easier to get the overall picture of these API changes by reading the guide.

joao-r-reis avatar Jun 23 '20 19:06 joao-r-reis

This PR doesn't unify Session and Cluster yet but a lot of these changes were made with the unification in mind which will come in a following PR.

I'm opening this PR before fixing the test code in order to allow for an earlier review of the API changes/design @jorgebay

joao-r-reis avatar Jun 23 '20 19:06 joao-r-reis