blob: 14550844a824b28cf620e25616c0487360f8c1d6 (
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 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 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();
}
}
}
|