check connection to event store

Hi,

I have credentials and want to check connection like to SQL

MS Sql example:

SqlConnection conn = new SqlConnection(_connectionString);

conn.Open();

conn.Close();

conn.Dispose();

Event store:

var connection = EventStoreConnection.Create(

ConnectionSettings.Create()

.SetDefaultUserCredentials(new UserCredentials(_userName, _password)),

ClusterSettings.Create()

.DiscoverClusterViaDns()

.SetClusterDns(_clusterDns)

.SetClusterGossipPort(_port)

.SetMaxDiscoverAttempts(3600));

connection.ConnectAsync().Wait();

//How to check connection, because now code doesn’t react to any credentials :frowning:

connection.Close();

connection.Dispose();

Oleg

Hi Oleg

When you say you want to check the connection to Event Store, are you wanting to check that the default credentials you are using are valid?

If that’s the case, the connection provides an event called AuthenticationFailed for this. For example, you could add a handler like this before connecting:

I try this code, bet doesn’t work :frowning:

var connection = EventStoreConnection.Create(

ConnectionSettings.Create()

.SetDefaultUserCredentials(new UserCredentials(_userName, _password)),

ClusterSettings.Create()

.DiscoverClusterViaDns()

.SetClusterDns(_clusterDns)

.SetClusterGossipPort(_port)

.SetMaxDiscoverAttempts(3600));

connection.AuthenticationFailed += (s, e) => {throw new NotAuthenticatedException(“Invalid credentials”);};

connection.Connected += (s, e) => {Log(“OK”);

connection.ConnectAsync().Wait();

connection.Close();

connection.Dispose();

Oleg

2016 m. spalis 31 d., pirmadienis 11:23:59 UTC+2, Hayley-Jean Campbell rašė:

Could you help me, because my code works only with ReadLine :frowning: How can I change my code to working code?

var connection = EventStoreConnection.Create(

ConnectionSettings.Create()

.SetDefaultUserCredentials(new UserCredentials(_userName, _password)),

ClusterSettings.Create()

.DiscoverClusterViaDns()

.SetClusterDns(_clusterDns)

.SetClusterGossipPort(_port)

.SetMaxDiscoverAttempts(3600));

connection.AuthenticationFailed += (s, e) => {throw new NotAuthenticatedException(“Invalid credentials”);};

connection.Connected += (s, e) => {Log(“OK”);

connection.ConnectAsync().Wait();

Console.ReadLine();

connection.Close();

connection.Dispose();

2016 m. spalis 31 d., pirmadienis 11:50:37 UTC+2, Oleg Makarenko rašė:

Hi Oleg,

You are running into issues because you are closing the connection directly after opening it, which is not how it was intended to be used.

The EventStoreConnection is designed to be used with one instance per application, not to be frequently created and destroyed (Some info about this is here)

What are you trying to achieve with this?

I have WebApi and I want to have health endpoint which check all dependencies, e.g. ES, SQL server etc. Then dashboard pings e.g. every 1h and I know WebApi full or partially alive. How can I implement this solution?

2016 m. spalis 31 d., pirmadienis 15:27:28 UTC+2, Hayley-Jean Campbell rašė:

Use startup code that lives for the life of the process. Are you using native Web API? OWIN? Something else?

OWIN

2016 m. spalis 31 d., pirmadienis 15:36:42 UTC+2, Andy Badera rašė:

Then use your Startup class to instantiate the connection. This is a resource that needs to live for the life of your process, not the life of a controller or within a single method, right?

Yes. You want to say that I don’t need to check ES connection? ES connection lives with WebApi service and when fired ClientClosedEventArgs I have to stop service?

Maybe you have startup OWIN implementation?

2016 m. spalis 31 d., pirmadienis 15:42:55 UTC+2, Andy Badera rašė:

Also there is a better way to check the health. Try hitting the uri
es:2113/gossip

I think about one case: could be authentication errors, but es:2113/gossip works. I want to say somebody change ES configuration and we don’t know about it.

2016 m. spalis 31 d., pirmadienis 15:50:34 UTC+2, Greg Young rašė:

I can’t speak to whether or not you need to check your connection. A health endpoint for this would call into your process-scoped connection, but it shouldn’t instantiate the connection within the controller or method your health check lives in.

I believe yes, you do need to manually reconnect if you receive a ClientClosed event. You can wire up this event handler in your OWIN Startup class.

Where I can read more about gossip endpoint, maybe there is nuget or what I have to check: is master isAlive?

2016 m. spalis 31 d., pirmadienis 16:12:20 UTC+2, Andy Badera rašė:

FWIW, we have implemented health check by reading the last event in the system (readAllEventsBackward). This checks that the connection itself is healthy (credentials are correct etc) and that there is something on the other end responding to it.

As others have mentioned, this would be on the process level connection object (in our case injected via Spring), to ensure that the connection object that “everyone else” is using is valid.

Cheers,

Kristian

It would be better to do a write. The reason why is a node will still
respond to a read without a cluster being made. Writes will however
fail.