aboutsummaryrefslogtreecommitdiffstats
path: root/zkfacade/src/main/java/com/yahoo/vespa/curator/stats/LockAttemptSamples.java
blob: 0e3837048cdb0f3c75f5cd0c10f6223887776dc9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator.stats;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

/**
 * Collection containing "interesting" {@code LockAttempt}s.
 *
 * @author hakon
 */
// @ThreadSafe
public class LockAttemptSamples {
    private final int maxSamples;

    /** Ensure atomic operations on this collection. */
    private final Object monitor = new Object();

    /** Keep at most one sample for each lock path. */
    private final Map<String, LockAttempt> byLockPath;

    /**
     * Priority queue containing all samples.  The head of this queue (peek()/poll())
     * returns the LockAttempt with the smallest duration.
     */
    private final PriorityQueue<LockAttempt> priorityQueue =
            new PriorityQueue<>(Comparator.comparing(LockAttempt::getStableTotalDuration));

    LockAttemptSamples(int maxSamples) {
        this.maxSamples = maxSamples;
        this.byLockPath = new HashMap<>(maxSamples);
    }

    int size() { return byLockPath.size(); }

    boolean maybeSample(LockAttempt lockAttempt) {
        final boolean added;
        synchronized (monitor) {
            if (shouldAdd(lockAttempt)) {
                byLockPath.put(lockAttempt.getLockPath(), lockAttempt);
                priorityQueue.add(lockAttempt);
                added = true;
            } else {
                added = false;
            }
        }

        if (added) {
            // Unnecessary to invoke under synchronized, although it means that some samples
            // may be without stack trace (just retry if that happens).
            lockAttempt.fillStackTrace();
        }

        return added;
    }

    private boolean shouldAdd(LockAttempt lockAttempt) {
        LockAttempt existingLockAttempt = byLockPath.get(lockAttempt.getLockPath());
        if (existingLockAttempt != null) {
            if (hasLongerDurationThan(lockAttempt, existingLockAttempt)) {
                byLockPath.remove(existingLockAttempt.getLockPath());
                priorityQueue.remove(existingLockAttempt);
                return true;
            }

            return false;
        }

        if (size() < maxSamples) {
            return true;
        }

        // peek() and poll() retrieves the smallest element.
        existingLockAttempt = priorityQueue.peek();  // cannot be null
        if (hasLongerDurationThan(lockAttempt, existingLockAttempt)) {
            priorityQueue.poll();
            byLockPath.remove(existingLockAttempt.getLockPath());
            return true;
        }

        return false;
    }

    List<LockAttempt> asList() {
        synchronized (monitor) {
            return List.copyOf(byLockPath.values());
        }
    }

    private static boolean hasLongerDurationThan(LockAttempt lockAttempt, LockAttempt otherLockAttempt) {
        // Use stable total duration to avoid messing up priority queue.
        return lockAttempt.getStableTotalDuration().compareTo(otherLockAttempt.getStableTotalDuration()) > 0;
    }
}