aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ContentNodeStats.java
blob: 260cd2ea4556a9febb738cc4a10776dce22bfea1 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright Vespa.ai. 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;

import java.util.*;

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

    private final int nodeIndex;
    private Map<String, BucketSpaceStats> bucketSpaces = new HashMap<>();

    public static class BucketSpaceStats {
        private int invalidCount;
        private long bucketsTotal;
        private long bucketsPending;

        private BucketSpaceStats() {
            this.invalidCount = 1;
            this.bucketsTotal = 0;
            this.bucketsPending = 0;
        }

        private BucketSpaceStats(long bucketsTotal, long bucketsPending, boolean invalid) {
            this.invalidCount = (invalid ? 1 : 0);
            this.bucketsTotal = bucketsTotal;
            this.bucketsPending = bucketsPending;
        }

        public static BucketSpaceStats invalid() {
            return new BucketSpaceStats();
        }

        public static BucketSpaceStats invalid(long bucketsTotal, long bucketsPending) {
            return new BucketSpaceStats(bucketsTotal, bucketsPending, true);
        }

        public static BucketSpaceStats of(long bucketsTotal, long bucketsPending) {
            return new BucketSpaceStats(bucketsTotal, bucketsPending, false);
        }

        public static BucketSpaceStats empty() {
            return new BucketSpaceStats(0, 0, false);
        }

        public long getBucketsTotal() {
            return bucketsTotal;
        }

        public long getBucketsPending() {
            return bucketsPending;
        }

        public boolean mayHaveBucketsPending(double minMergeCompletionRatio) {
            if (invalidCount > 0) {
                return true;
            }
            if (bucketsTotal == 0) {
                return bucketsPending > 0;
            }
            return calcMergeCompletionRatio() < minMergeCompletionRatio;
        }

        private double calcMergeCompletionRatio() {
            return ((double) Math.max(0, (bucketsTotal - bucketsPending)) / (double) bucketsTotal);
        }

        public boolean valid() {
            return invalidCount == 0;
        }

        public void merge(BucketSpaceStats rhs, int factor) {
            this.invalidCount += (factor * rhs.invalidCount);
            this.bucketsTotal += (factor * rhs.bucketsTotal);
            this.bucketsPending += (factor * rhs.bucketsPending);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            BucketSpaceStats that = (BucketSpaceStats) o;
            return invalidCount == that.invalidCount &&
                    bucketsTotal == that.bucketsTotal &&
                    bucketsPending == that.bucketsPending;
        }

        @Override
        public int hashCode() {
            return Objects.hash(invalidCount, bucketsTotal, bucketsPending);
        }

        @Override
        public String toString() {
            return "{bucketsTotal=" + bucketsTotal + ", bucketsPending=" + bucketsPending + ", invalidCount=" + invalidCount + "}";
        }
    }

    public ContentNodeStats(StorageNode storageNode) {
        this.nodeIndex = storageNode.getIndex();
        for (StorageNode.BucketSpaceStats stats : storageNode.getBucketSpacesStats()) {
            if (stats.valid()) {
                this.bucketSpaces.put(stats.getName(),
                        BucketSpaceStats.of(stats.getBucketStats().getTotal(),
                                stats.getBucketStats().getPending()));
            } else {
                this.bucketSpaces.put(stats.getName(), BucketSpaceStats.invalid());
            }
        }
    }

    public ContentNodeStats(int index) {
        this(index, new HashMap<>());
    }

    public ContentNodeStats(int index, Map<String, BucketSpaceStats> bucketSpaces) {
        this.nodeIndex = index;
        this.bucketSpaces = bucketSpaces;
    }

    public int getNodeIndex() { return nodeIndex; }

    public void add(ContentNodeStats stats) {
        merge(stats, 1);
    }

    public void subtract(ContentNodeStats stats) {
        merge(stats, -1);
    }

    private void merge(ContentNodeStats stats, int factor) {
        for (Map.Entry<String, BucketSpaceStats> entry : stats.bucketSpaces.entrySet()) {
            BucketSpaceStats statsToUpdate = bucketSpaces.get(entry.getKey());
            if (statsToUpdate == null && factor == 1) {
                statsToUpdate = BucketSpaceStats.empty();
                bucketSpaces.put(entry.getKey(), statsToUpdate);
            }
            if (statsToUpdate != null) {
                statsToUpdate.merge(entry.getValue(), factor);
            }
        }
    }

    public BucketSpaceStats getBucketSpace(String bucketSpace) {
        return bucketSpaces.get(bucketSpace);
    }

    public Map<String, BucketSpaceStats> getBucketSpaces() {
        return bucketSpaces;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ContentNodeStats that = (ContentNodeStats) o;
        return nodeIndex == that.nodeIndex &&
                Objects.equals(bucketSpaces, that.bucketSpaces);
    }

    @Override
    public int hashCode() {
        return Objects.hash(nodeIndex, bucketSpaces);
    }

    @Override
    public String toString() {
        return String.format("{index=%d, bucketSpaces=[%s]}",
                nodeIndex, Arrays.toString(bucketSpaces.entrySet().toArray()));
    }
}