Couchbase server availability situation

Greetings to all,
I developed a windows service application with .Net 6.0. I am currently testing my application by creating various illegal situations. While the Couchbase database is running, my application starts to log on the file when the connection to the database is lost. When the connection is re-established, it continues its task. But if the database has fallen before my application starts, I cannot run the service, it gives the attached error. In fact, in such a case, I want it to start assigning logs again until the connection is established.

Thanks.

use WaitUntilReady to wait until the cluster is ready before trying to access the bucket.

Thaks for your answer.

The waitUntilReady command does not help in this sense because it is not known when the caouchbase database will be up. The service works when I change the code as below. This time, when I restore the database later, the system cannot catch it. So now the problem is that the system cannot catch whether the server is up or not.

Picture below shows changed code

Picture below shows when the server available and service is running without any problem

Picture below shows when the server not available and service is running with error

And picture below shows the error it gives whether the server gets up afterwards or not

As additional information, if I stop my windows service application and start it again, the service runs without any errors. But my problem is that when I start this service, couchbase database may not be ready or there may be any problem (such as network problem) and I need to get over this situation without closing and restarting the service again.

The waitUntilReady command does not help in this sense because it is not known when the caouchbase database will be up.

waitUntilReady will wait until the database is up. Just use a very long timeout. Here is a test case that is started when couchbase server is down, after I start the test case, I start the couchbase server after starting the testcase, and withing 36 seconds, waitUntilReady retturns, and processing can begin. ( The example uses a timeout of 24 hours). Or you could simply use a timeout of 24 hours on the operation. Although most applications will not want to wait that long, and would rather throw an exception up to the caller.

 @Test
  public void waitUntilReady() {
    long t0 = System.currentTimeMillis();
    ClusterEnvironment env = ClusterEnvironment.builder().build();
    String endpoint= "couchbase://localhost";
    String username = "Administrator";
    String password = "password";
    String bucketName = "my_bucket";
    Cluster cluster = Cluster.connect(endpoint, ClusterOptions.clusterOptions(username, password).environment(env));
    Bucket bucket = cluster.bucket(bucketName);
    bucket.waitUntilReady(Duration.parse("PT24H"));
    Collection collection = bucket.defaultCollection();
    collection.upsert("my_test_123", JsonObject.create().put("test","hello"));
    GetResult getResult = collection.get("my_test_123");
    assertEquals("hello", getResult.contentAsObject().get("test"));
    System.out.println("getResult.contentAsObject(): "+getResult.contentAsObject());
    System.out.println("Elapsed time: "+(System.currentTimeMillis()-t0)/1000 +" seconds");
  }

2022-12-05 10:01:59,904  WARN                   com.couchbase.endpoint: 462 - [com.couchbase.endpoint][EndpointConnectionFailedEvent][465us] Connect attempt 10 failed because of : finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8091 - Check server ports and cluster encryption setting. {"circuitBreaker":"DISABLED","coreId":"0x6dd2240700000001","remote":"localhost:8091","type":"MANAGER"}
  com.couchbase.client.core.endpoint.BaseEndpoint$2: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8091 - Check server ports and cluster encryption setting.
2022-12-05 10:02:00,426  WARN                   com.couchbase.endpoint: 462 - [com.couchbase.endpoint][EndpointConnectionFailedEvent][582us] Connect attempt 10 failed because of : finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8091 - Check server ports and cluster encryption setting. {"circuitBreaker":"DISABLED","coreId":"0x6dd2240700000001","remote":"localhost:8091","type":"MANAGER"}
  com.couchbase.client.core.endpoint.BaseEndpoint$2: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8091 - Check server ports and cluster encryption setting.
2022-12-05 10:02:04,649  INFO                       com.couchbase.core: 442 - [com.couchbase.core][BucketOpenedEvent][18s] Opened bucket "my_bucket" {"coreId":"0x6dd2240700000001"}
getResult.contentAsObject(): {"test":"hello"}
  Elapsed time: 36 seconds

Thanks for your answers,

Sorry my mistake, I should have told you what the app does. This service has to stand up anyway and keep a log for a task, if the database is active it will write to the database, if not to the file. When the database is active, it will transfer the data in the file to the database. In this case I cannot use the WaitUntilReady method. And also Iā€™m using the Dipendency Injection design pattern so here I leave the connection procedure entirely to the couchbase library. I do not have any connection procedure.

1 Like

Move the code that gets the exception when couchbase is not available out of the service initialization code into the code that writes to couchbase. There will be an exception there when couchbase is not available, which you will catch, and then fall-back to the write-to-file.