aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/ClusterStateHistory.java
blob: a6d82c2fbeb912751ef3fcf6edf95b77393c43ee (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Tracks a configurable max number of cluster state history entries, pruning old entries
 * as newer entries are added. To save memory, state diffs are computed once and cached as
 * a string upon adding a new state bundle. Only the most recent bundle is retained in its
 * entirety.
 */
public class ClusterStateHistory {

    private final LinkedList<ClusterStateHistoryEntry> stateHistory = new LinkedList<>();
    private int maxHistoryEntryCount = 50;
    private ClusterStateBundle prevStateBundle = null;

    /**
     * Sets limit on how many cluster states can be kept in the in-memory queue. Once
     * the list exceeds this limit, the oldest state is repeatedly removed until the limit
     * is no longer exceeded.
     */
    void setMaxHistoryEntryCount(final int maxHistoryEntryCount) {
        this.maxHistoryEntryCount = maxHistoryEntryCount;
    }

    List<ClusterStateHistoryEntry> getHistory() {
        return Collections.unmodifiableList(stateHistory);
    }

    public void add(ClusterStateBundle currentClusterState, long currentTimeMs) {
        if (prevStateBundle != null) {
            stateHistory.addFirst(ClusterStateHistoryEntry.makeSuccessor(currentClusterState, prevStateBundle, currentTimeMs));
        } else {
            stateHistory.addFirst(ClusterStateHistoryEntry.makeFirstEntry(currentClusterState, currentTimeMs));
        }
        prevStateBundle = currentClusterState;
        while (stateHistory.size() > maxHistoryEntryCount) {
            stateHistory.removeLast();
        }
    }

}