couchbase upsert/insert silently failing with ttl

Viewed 353

i am trying to upsert 10 documents using spring boot. It is failing to upsert "few documents" with TTL.There is no error or exception. If i do not provide ttl then it is working as expected.

In addition to that, if i increase the ttl to a different value then also all the documents are getting created.

On the other hand, if i reduce the ttl then failing to insert few more docuemnts.

I tried to insert the failed document(single document out of 10) from another poc with the same ttl the document is getting created.

public Flux<JsonDocument> upsertAll(final List<JsonDocument> jsonDocuments) {

        return Flux
            .from(keys())
            .flatMap(key -> Flux
                .fromIterable(jsonDocuments)
                .parallel()
                .runOn(Schedulers.parallel())
                .flatMap(jsonDocument -> {
                    final String arg = String.format("upsertAll-%s", jsonDocument);

                    return Mono
                        .just(asyncBucket
                            .upsert(jsonDocument, 1000, TimeUnit.MILLISECONDS)
                            .doOnError(error -> log.error(jsonDocument.content(), error, "failed to upsert")))

                        .map(obs -> Tuples.of(obs, jsonDocument.content()))
                        .map(tuple2 -> log.observableHandler(tuple2))
                        .map(observable1 -> Tuples.of(observable1, jsonDocument.content()))
                        .flatMap(tuple2 -> log.monoHandler(tuple2))
                        ;
                })
                .sequential())
            ;
    }
List<JsonDocument> jsonDocuments = new LinkedList<>();
dbService.upsertAll(jsonDocuments)
            .subscribe();

some one please suggest how to resolve this issue.

1 Answers

Due to an oddity in the Couchbase server API, TTL values less than 30 days are treated differently than values greater than 30 days.

In order to get consistent behavior with Couchbase Java SDK 2.x, you'll need to adjust the TTL value before passing it to the SDK:

// adjust TTL for Couchbase Java SDK 2.x
public static int adjustTtl(int ttlSeconds) {
  return ttlSeconds < TimeUnit.DAYS.toSeconds(30)
      ? ttlSeconds
      : (int) (ttlSeconds + (System.currentTimeMillis() / 1000));
}

In Couchbase Java SDK 3.0.6 this is no longer required; just pass a Duration and the SDK will adjust the value behind the scenes if necessary.

Related