Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

punam.kumari_185226 avatar image
punam.kumari_185226 asked ·

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 characters needed characters left characters exceeded

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 ·

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 characters needed characters left characters exceeded

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

Erick Ramirez avatar image
Erick Ramirez answered ·

@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 characters needed characters left characters exceeded

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

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 · ·

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 · ·

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
Rebecca Mills avatar image
Rebecca Mills answered ·

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 characters needed characters left characters exceeded

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

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

0 Likes 0 · ·