Watches never trigger in FoundationDB

230 Views Asked by At

I playing around with watches functionality and struggling to get it work. The problem is that watch never fires, it simply not react to changes that I make for key in other transactions.

val key = new Tuple().add("watch-test").pack()
val watchExecuted = db.runAsync(tr => {
  tr.set(key, new Tuple().add(1).pack())
  tr.watch(key)
})

Thread.sleep(5000) // ensure that watch applied

db.run(tr => {
  tr.set(key, new Tuple().add(2).pack())
})

watchExecuted.get() // never finish

Is anybody have any idea why watches do not react on changes as it supposed to do?

1

There are 1 best solutions below

0
On BEST ANSWER

I think what's going on here is that your first transaction is never completing. It's maybe not obvious from the documentation, but runAsync won't return until the CompletableFuture returned in your function is ready. Because you are returning the watch future and not changing the value until after the transaction, it's never becoming ready and the transaction never ends.

If you replaced runAsync with run, I think it would work:

val watchExecuted = db.run(tr => {
  tr.set(key, new Tuple().add(1).pack())
  tr.watch(key)
})

If you wanted to use runAsync, then you would need to return your watch future wrapped in another object.

EDIT: or rather, if you want to use runAsync, you could return a CompletableFuture<CompletableFuture<Void>>:

var watchExecuted = db.runAsync(tr => {
  tr.set(key, new Tuple().add(1).pack())
  CompletableFuture.completedFuture(tr.watch(key))
});