summaryrefslogtreecommitdiffstats
path: root/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java
blob: 299fceb8d7f5e46530833d3d18d3baf63a64bd94 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator;

import com.google.common.util.concurrent.UncheckedTimeoutException;
import com.yahoo.path.Path;
import com.yahoo.transaction.Mutex;
import org.apache.curator.framework.recipes.locks.InterProcessLock;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * A cluster-wide re-entrant mutex which is released on (the last symmetric) close.
 *
 * Re-entrancy is limited to the instance of this. To ensure re-entrancy callers should access the lock through
 * {@link Curator#lock(Path, Duration)} instead of constructing this directly.
 *
 * @author bratseth
 */
public class Lock implements Mutex {

    private final ReentrantLock lock;
    private final InterProcessLock mutex;
    private final String lockPath;

    public Lock(String lockPath, Curator curator) {
        this.lockPath = lockPath;
        this.lock = new ReentrantLock(true);
        mutex = curator.createMutex(lockPath);
    }

    /** Take the lock with the given timeout. This may be called multiple times from the same thread - each matched by a close */
    public void acquire(Duration timeout) throws UncheckedTimeoutException {
        ThreadLockInfo threadLockInfo = getThreadLockInfo();
        threadLockInfo.invokingAcquire(timeout);
        try {
            if ( ! mutex.acquire(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
                threadLockInfo.acquireTimedOut();

                throw new UncheckedTimeoutException("Timed out after waiting " + timeout +
                        " to acquire lock '" + lockPath + "'");
            }
            threadLockInfo.lockAcquired();

            if ( ! lock.tryLock()) { // Should be available to only this thread, while holding the above mutex.
                release();
                threadLockInfo.failedToAcquireReentrantLock();
                throw new IllegalStateException("InterProcessMutex acquired, but guarded lock held by someone else, for lock '" + lockPath + "'");
            }
        }
        catch (UncheckedTimeoutException | IllegalStateException e) {
            throw e;
        }
        catch (Exception e) {
            throw new RuntimeException("Exception acquiring lock '" + lockPath + "'", e);
        }
    }

    @Override
    public void close() {
        try {
            lock.unlock();
        }
        finally {
            release();
        }
    }

    private void release() {
        getThreadLockInfo().lockReleased();
        try {
            mutex.release();
        }
        catch (Exception e) {
            throw new RuntimeException("Exception releasing lock '" + lockPath + "'");
        }
    }

    private ThreadLockInfo getThreadLockInfo() {
        return ThreadLockInfo.getCurrentThreadLockInfo(lockPath, lock);
    }
}