aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CountingCuratorTransaction.java
blob: 155158c51ee0237f44eae3c03734236cac3fa6fd (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.persistence;

import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.recipes.CuratorCounter;
import com.yahoo.vespa.curator.transaction.CuratorTransaction;

/**
 * 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;
    }

}