View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  
26  /**
27   * An interface for an application-specific lock.
28   */
29  @InterfaceAudience.Private
30  public interface InterProcessLock {
31  
32    /**
33     * Acquire the lock, waiting indefinitely until the lock is released or
34     * the thread is interrupted.
35     * @throws IOException If there is an unrecoverable error releasing the lock
36     * @throws InterruptedException If current thread is interrupted while
37     *                              waiting for the lock
38     */
39    public void acquire() throws IOException, InterruptedException;
40  
41    /**
42     * Acquire the lock within a wait time.
43     * @param timeoutMs The maximum time (in milliseconds) to wait for the lock,
44     *                  -1 to wait indefinitely
45     * @return True if the lock was acquired, false if waiting time elapsed
46     *         before the lock was acquired
47     * @throws IOException If there is an unrecoverable error talking talking
48     *                     (e.g., when talking to a lock service) when acquiring
49     *                     the lock
50     * @throws InterruptedException If the thread is interrupted while waiting to
51     *                              acquire the lock
52     */
53    public boolean tryAcquire(long timeoutMs)
54    throws IOException, InterruptedException;
55  
56    /**
57     * Release the lock.
58     * @throws IOException If there is an unrecoverable error releasing the lock
59     * @throws InterruptedException If the thread is interrupted while releasing
60     *                              the lock
61     */
62    public void release() throws IOException, InterruptedException;
63  
64    /**
65     * If supported, attempts to reap all the locks of this type by forcefully
66     * deleting the locks. Lock reaping is different than coordinated lock revocation
67     * in that, there is no coordination, and the behavior is undefined if the
68     * lock holder is still alive.
69     * @throws IOException If there is an unrecoverable error reaping the locks
70     */
71    public void reapAllLocks() throws IOException;
72  
73    /**
74     * An interface for objects that process lock metadata.
75     */
76    public static interface MetadataHandler {
77  
78      /**
79       * Called after lock metadata is successfully read from a distributed
80       * lock service. This method may contain any procedures for, e.g.,
81       * printing the metadata in a humanly-readable format.
82       * @param metadata The metadata
83       */
84      public void handleMetadata(byte[] metadata);
85    }
86  }