View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master.procedure;
20  
21  import java.io.IOException;
22  import java.io.InterruptedIOException;
23  import java.util.concurrent.CountDownLatch;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.client.VersionInfoUtil;
28  import org.apache.hadoop.hbase.procedure2.Procedure;
29  
30  /**
31   * Latch used by the Master to have the prepare() sync behaviour for old
32   * clients, that can only get exceptions in a synchronous way.
33   */
34  @InterfaceAudience.Private
35  @InterfaceStability.Evolving
36  public abstract class ProcedurePrepareLatch {
37    private static final NoopLatch noopLatch = new NoopLatch();
38  
39    public static ProcedurePrepareLatch createLatch() {
40      // don't use the latch if we have procedure support
41      return hasProcedureSupport() ? noopLatch : new CompatibilityLatch();
42    }
43  
44    public static boolean hasProcedureSupport() {
45      return VersionInfoUtil.currentClientHasMinimumVersion(1, 1);
46    }
47  
48    protected abstract void countDown(final Procedure proc);
49    public abstract void await() throws IOException;
50  
51    protected static void releaseLatch(final ProcedurePrepareLatch latch, final Procedure proc) {
52      if (latch != null) {
53        latch.countDown(proc);
54      }
55    }
56  
57    private static class NoopLatch extends ProcedurePrepareLatch {
58      protected void countDown(final Procedure proc) {}
59      public void await() throws IOException {}
60    }
61  
62    protected static class CompatibilityLatch extends ProcedurePrepareLatch {
63      private final CountDownLatch latch = new CountDownLatch(1);
64  
65      private IOException exception = null;
66  
67      protected void countDown(final Procedure proc) {
68        if (proc.hasException()) {
69          exception = proc.getException().unwrapRemoteException();
70        }
71        latch.countDown();
72      }
73  
74      public void await() throws IOException {
75        try {
76          latch.await();
77        } catch (InterruptedException e) {
78          throw (InterruptedIOException)new InterruptedIOException().initCause(e);
79        }
80  
81        if (exception != null) {
82          throw exception;
83        }
84      }
85    }
86  }