aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/NodeMergeStats.java
blob: 97112e01aedc45f05f15358f6b07d1f77677f177 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vespa.clustercontroller.core.hostinfo.StorageNode;

/**
 * @author hakonhall
 */
public class NodeMergeStats {

    /**
     * Constructor that sets values to zero if not present.
     */
    public NodeMergeStats(StorageNode storageNodePojo) {
        this.nodeIndex = storageNodePojo.getIndex();

        StorageNode.OutstandingMergeOps mergeOps = storageNodePojo.getOutstandingMergeOpsOrNull();
        if (mergeOps == null) {
            mergeOps = new StorageNode.OutstandingMergeOps();
        }
        syncing = createAmount(mergeOps.getSyncingOrNull());
        copyingIn = createAmount(mergeOps.getCopyingInOrNull());
        movingOut = createAmount(mergeOps.getMovingOutOrNull());
        copyingOut = createAmount(mergeOps.getCopyingOutOrNull());
    }

    private static Amount createAmount(StorageNode.Buckets bucketOrNull) {
        if (bucketOrNull == null) {
            return new Amount();
        }
        return new Amount(bucketOrNull.getBuckets());
    }

    static public class Amount {
        private long buckets;

        Amount() { this(0); }
        Amount(long buckets) { this.buckets = buckets; }

        public void set(Amount other) {
            buckets = other.buckets;
        }

        public long getBuckets() {
            return buckets;
        }

        /**
         * Logically, add (factor * amount) to this object.
         */
        void scaledAdd(int factor, Amount amount) {
            buckets += factor * amount.buckets;
        }

        public boolean equals(Object other) {
            if (!(other instanceof Amount)) {
                return false;
            }
            Amount otherAmount = (Amount) other;
            return buckets == otherAmount.buckets;
        }

        public int hashCode() {
                return (int)buckets;
        }

        public String toString() {
            return String.format("{buckets = %d}", buckets);
        }
    }

    private final Amount syncing;
    private final Amount copyingIn;
    private final Amount movingOut;
    private final Amount copyingOut;
    private int nodeIndex;

    /**
     * An instance with all 0 amounts.
     */
    public NodeMergeStats(int index) {
        this(index, new Amount(), new Amount(), new Amount(), new Amount());
    }

    NodeMergeStats(int index, Amount syncing, Amount copyingIn, Amount movingOut, Amount copyingOut) {
        this.nodeIndex = index;
        this.syncing = syncing;
        this.copyingIn = copyingIn;
        this.movingOut = movingOut;
        this.copyingOut = copyingOut;
    }

    public void set(NodeMergeStats stats) {
        nodeIndex = stats.nodeIndex;
        syncing.set(stats.syncing);
        copyingIn.set(stats.copyingIn);
        movingOut.set(stats.movingOut);
        copyingOut.set(stats.copyingOut);
    }

    int getNodeIndex() { return nodeIndex; }
    public Amount getSyncing() { return syncing; }
    public Amount getCopyingIn() { return copyingIn; }
    public Amount getMovingOut() { return movingOut; }
    public Amount getCopyingOut() { return copyingOut; }

    void add(NodeMergeStats stats) {
        scaledAdd(1, stats);
    }

    void subtract(NodeMergeStats stats) {
        scaledAdd(-1, stats);
    }

    /**
     * Logically, adds (factor * stats) to this object. factor of 1 is normal add, -1 is subtraction.
     */
    private void scaledAdd(int factor, NodeMergeStats stats) {
        syncing.scaledAdd(factor, stats.syncing);
        copyingIn.scaledAdd(factor, stats.copyingIn);
        movingOut.scaledAdd(factor, stats.movingOut);
        copyingOut.scaledAdd(factor, stats.copyingOut);
    }

    @Override
    public int hashCode() {
        return (int) (syncing.buckets +
                copyingIn.buckets * 31 +
                movingOut.buckets * 17 +
                copyingOut.buckets * 7);
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof NodeMergeStats)) {
            return false;
        }

        NodeMergeStats otherStats = (NodeMergeStats) other;
        return nodeIndex == otherStats.nodeIndex &&
                syncing.equals(otherStats.syncing) &&
                copyingIn.equals(otherStats.copyingIn) &&
                movingOut.equals(otherStats.movingOut) &&
                copyingOut.equals(otherStats.copyingOut);
    }

    public String toString() {
        return String.format("{index = %d, syncing = %s, copyingIn = %s, movingOut = %s, copyingOut = %s}",
                nodeIndex, syncing, copyingIn, movingOut, copyingOut);
    }
}