question

punam.kumari_185226 avatar image
punam.kumari_185226 asked Erick Ramirez edited

C# driver Reconnection Policy not working

Working on a single node cluster and creating a cassandra connection as shown below. I am expecting that if connection will be failed the first time, it will retry after 5s then 2 min, and so on. Even though Cassandra server is up after a few seconds, the _session variable is null. Do I have to call NewSchedule function of Reconnection policy?

_cassandraCluster =
  Cluster.Builder()
    .WithSessionName("Cassandra")
    .AddContactPoint("127.0.0.1").WithPort(9042)
    .WithRetryPolicy(new CassandraCustomRetryPolicy(3, 3, 2))
    .WithReconnectionPolicy(new FixedReconnectionPolicy(400, 5000, 2 * 60000, 15 * 60000))
    .Build();
_session = _cassandraCluster.connect();
return _session;
csharp driver
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

joao.reis avatar image
joao.reis answered joao.reis edited

Hi, the Cluster.Connect() method never returns null so if you are seeing the session variable with a null value then my guess is that the snippet that you pasted here does not accurately represent the driver initialization code on your application.

In any case, the reconnection policy is only used AFTER the session is initialized. If the driver is not able to initialize the session then an exception will be thrown and you will have to manually retry the initialization attempt in case you want to keep the application running.

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Rebecca Mills avatar image
Rebecca Mills answered punam.kumari_185226 commented

Hi @punam.kumari_185226 what I can tell you is that you need to use the Connect() method to create a new session. So as opposed to this:

_session = _cassandraCluster.build();

Use this:

_session = _cassandraCluster.Connect();


1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

punam.kumari_185226 avatar image punam.kumari_185226 commented ·

Hi @Rebbaca Mills.. Its connect only. some typing mistakes. Thanks

0 Likes 0 ·
Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez commented

@punam.kumari_185226 I haven't coded with C# before so I'm not familiar with it but it looks to me like your test isn't valid because there's only 1 node in the cluster.

When the driver tries to create a connection and the node is down, my hypothesis is that the driver blacklists the node and doesn't try to establish connections further. Assuming you've tried to enable logging, what errors did the driver log? That will give you clues as to what's happening.

It appears you're using a custom retry policy. What does your custom policy do and have you confirmed that it works? Maybe try to simplify your configuration and use the default policies first and make sure you have logging enabled.

If the driver never manages to connect, confirm that there's connectivity between your application and the node. In particular, check that the CQL port is bound to localhost. Assuming it, then the application must be running on the same server as Cassandra for it to work. Cheers!

10 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

punam.kumari_185226 avatar image punam.kumari_185226 commented ·

Documentation says that it tries to connect to the down nodes at interval given in reconnection policy. Please check the documentation https://docs.datastax.com/en/devapp/doc/devapp/driversReconnectionPolicies.html .

"To prevent intermediate network devices like routers and firewalls from disconnecting the drivers from a node, an OPTIONS request is sent to a connection at a constant interval, also known as a heartbeat. If the connection becomes idle and the node does not respond to the heartbeat in a given amount of time, the node is marked down. Once this occurs, the driver waits a specified amount of time based on the reconnection policy before attempting to reconnect to the node."


And I am just logging errors in Retry Policy. Not much changes there.


0 Likes 0 ·
Erick Ramirez avatar image Erick Ramirez ♦♦ punam.kumari_185226 commented ·

Care to share those errors? And what does your custom retry policy do?

How are you causing the outage and how do you know that your test is valid? Cheers!

0 Likes 0 ·
punam.kumari_185226 avatar image punam.kumari_185226 Erick Ramirez ♦♦ commented ·

Testing the outage by shutting down the Cassandra server and try to connect from C# client. And I start the server immediately before the interval given in Reconnection Policy. After some time I check the session object and its still null.

And I am assuming Retry Policy is for query failure and Reconnection Policy for server connection failure. Am I right?


0 Likes 0 ·
Show more comments
Show more comments