summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/SuperModelGenerationCounter.java
blob: 744e5e63adac651929dc7a961e3ee1d6ba12c63a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.yahoo.path.Path;
import com.yahoo.transaction.AbstractTransaction;
import com.yahoo.transaction.Transaction;
import com.yahoo.vespa.config.GenerationCounter;
import com.yahoo.vespa.curator.recipes.CuratorCounter;
import com.yahoo.vespa.curator.Curator;

/**
 * Distributed global generation counter for the super model.
 *
 * @author lulf
 * @since 5.9
 */
public class SuperModelGenerationCounter implements GenerationCounter {

    private static final Path counterPath = Path.fromString("/config/v2/RPC/superModelGeneration");
    private final CuratorCounter counter;

    public SuperModelGenerationCounter(Curator curator) {
        this.counter =  new CuratorCounter(curator, counterPath.getAbsolute());
    }

    /**
     * Increment counter and return next value. This method is thread safe and provides an atomic value
     * across zookeeper clusters.
     *
     * @return incremented counter value.
     */
    public synchronized long increment() {
        return counter.next();
    }

    /**
     * @return current counter value.
     */
    public synchronized long get() {
        return counter.get();
    }

    /** Returns a transaction which increments this */
    public IncrementTransaction incrementTransaction() {
        return new IncrementTransaction(counter);
    }
    
    /** An increment transaction */
    public static class IncrementTransaction extends AbstractTransaction {

        /** Creates a counting curator transaction containing a single increment operation */
        public IncrementTransaction(CuratorCounter counter) {
            add(new IncrementOperation(counter));
        }

        @Override
        public void prepare() { }

        @Override
        public void commit() {
            for (Operation operation : operations())
                ((IncrementOperation)operation).commit();
        }

        @Override
        public void rollbackOrLog() {
            for (Operation operation : operations())
                ((IncrementOperation)operation).rollback();
        }

        public static class IncrementOperation implements Transaction.Operation {

            private final CuratorCounter counter;

            public IncrementOperation(CuratorCounter counter) {
                this.counter = counter;
            }

            public void commit() {
                counter.next();
            }

            public void rollback() {
                // ok; we're just losing a generation number
            }

            public String toString() { return "increment " + counterPath + " operation"; }

        }

    }
}