aboutsummaryrefslogtreecommitdiffstats
path: root/zkfacade/src/main/java/com/yahoo/vespa/curator/stats/RecordedLockAttempts.java
blob: 52ac2c067eba1f4bbe93a921e8c2b5d6173a9b71 (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
// 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.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Contains information about the lock attempts made by a thread between a start and end.
 *
 * <p>Any thread is allowed access the public methods of an instance.</p>
 *
 * @author hakon
 */
public class RecordedLockAttempts {
    private static final int MAX_TOP_LEVEL_LOCK_ATTEMPTS = 100;

    private final String recordId;
    private final Instant startInstant;
    private volatile Instant endInstant = null;
    private final ConcurrentLinkedQueue<LockAttempt> lockAttempts = new ConcurrentLinkedQueue<>();

    static RecordedLockAttempts startRecording(String recordId) {
        return new RecordedLockAttempts(recordId);
    }

    private RecordedLockAttempts(String recordId) {
        this.recordId = recordId;
        startInstant = Instant.now();
    }

    /** Note: A LockAttempt may have nested lock attempts. */
    void addTopLevelLockAttempt(LockAttempt lockAttempt) {
        // guard against recordings that are too long - to cap the memory used
        if (lockAttempts.size() < MAX_TOP_LEVEL_LOCK_ATTEMPTS) {
            lockAttempts.add(lockAttempt);
        }
    }

    void stopRecording() {
        endInstant = Instant.now();
    }

    public String recordId() { return recordId; }
    public Instant startInstant() { return startInstant; }
    public Instant endInstant() { return endInstant == null ? Instant.now() : endInstant; }
    public Duration duration() { return Duration.between(startInstant, endInstant()); }
    public List<LockAttempt> lockAttempts() { return List.copyOf(lockAttempts); }
}