blob: de803281a8704bbbf8c23c68988213849a50df28 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package com.yahoo.vespa.hosted.provision.persistence;
import com.yahoo.path.Path;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.curator.recipes.CuratorCounter;
import com.yahoo.vespa.curator.transaction.CuratorOperation;
import com.yahoo.vespa.curator.transaction.CuratorTransaction;
import com.yahoo.vespa.curator.transaction.TransactionChanges;
/**
* CuratorTransaction wrapper which increments a counter, to signal invalidation of node repository caches.
*
* This class ensures a CuratorTransaction against the cached data (the node repository data) is
* accompanied by an increment of the data generation counter. An increment must occur <em>after</em>
* the write has completed, successfully or not. It is therefore placed in a {@code finally} block,
* wrapping the super class' {@link #commit()}.
* Likewise, {@link #prepare()} is also wrapped with an increment, in case it fails due to an inconsistent cache.
* The cache is invalid whenever the generation counter is higher than what the cache contents were read with.
* The usual locking for modifications of shared data is then enough to ensure the cache provides a
* consistent view of the shared data, with one exception: when incrementing the counter fails. This is
* assumed to be extremely rare, and the consequence is temporary neglect of cache invalidation.
*
* @author jonmv
*/
class CountingCuratorTransaction extends CuratorTransaction {
private final CuratorCounter counter;
public CountingCuratorTransaction(Curator curator, CuratorCounter counter) {
super(curator);
this.counter = counter;
}
@Override
public void prepare() {
try {
counter.get();
super.prepare();
}
finally {
counter.next();
}
}
@Override
public void commit() {
try {
super.commit();
}
finally {
counter.next();
}
}
@Override
public String toString() {
return "(" + super.toString() + "), INCREMENT " + counter;
}
}
|