1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
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  
32  
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      
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  }