001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.master.procedure;
020
021import java.io.IOException;
022import java.io.InterruptedIOException;
023import java.util.concurrent.CountDownLatch;
024
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.yetus.audience.InterfaceStability;
027import org.apache.hadoop.hbase.client.VersionInfoUtil;
028import org.apache.hadoop.hbase.procedure2.Procedure;
029
030/**
031 * Latch used by the Master to have the prepare() sync behaviour for old
032 * clients, that can only get exceptions in a synchronous way.
033 */
034@InterfaceAudience.Private
035@InterfaceStability.Evolving
036public abstract class ProcedurePrepareLatch {
037  private static final NoopLatch noopLatch = new NoopLatch();
038
039  /**
040   * Create a latch if the client does not have async proc support.
041   * This uses the default 1.1 version.
042   * @return a CompatibilityLatch or a NoopLatch if the client has async proc support
043   */
044  public static ProcedurePrepareLatch createLatch() {
045    // don't use the latch if we have procedure support (default 1.1)
046    return createLatch(1, 1);
047  }
048
049  /**
050   * Create a latch if the client does not have async proc support
051   * @param major major version with async proc support
052   * @param minor minor version with async proc support
053   * @return a CompatibilityLatch or a NoopLatch if the client has async proc support
054   */
055  public static ProcedurePrepareLatch createLatch(int major, int minor) {
056    // don't use the latch if we have procedure support
057    return hasProcedureSupport(major, minor) ? noopLatch : new CompatibilityLatch();
058  }
059
060  /**
061   * Creates a latch which blocks.
062   */
063  public static ProcedurePrepareLatch createBlockingLatch() {
064    return new CompatibilityLatch();
065  }
066
067  /**
068   * Returns the singleton latch which does nothing.
069   */
070  public static ProcedurePrepareLatch getNoopLatch() {
071    return noopLatch;
072  }
073
074  private static boolean hasProcedureSupport(int major, int minor) {
075    // Note: this won't work if the shortcut similar to the one in HRegionServer is used
076    //       without the corresponding version handling.
077    return VersionInfoUtil.currentClientHasMinimumVersion(major, minor);
078  }
079
080  protected abstract void countDown(final Procedure proc);
081  public abstract void await() throws IOException;
082
083  public static void releaseLatch(final ProcedurePrepareLatch latch, final Procedure proc) {
084    if (latch != null) {
085      latch.countDown(proc);
086    }
087  }
088
089  private static class NoopLatch extends ProcedurePrepareLatch {
090    @Override
091    protected void countDown(final Procedure proc) {}
092    @Override
093    public void await() throws IOException {}
094  }
095
096  protected static class CompatibilityLatch extends ProcedurePrepareLatch {
097    private final CountDownLatch latch = new CountDownLatch(1);
098
099    private IOException exception = null;
100
101    @Override
102    protected void countDown(final Procedure proc) {
103      if (proc.hasException()) {
104        exception = MasterProcedureUtil.unwrapRemoteIOException(proc);
105      }
106      latch.countDown();
107    }
108
109    @Override
110    public void await() throws IOException {
111      try {
112        latch.await();
113      } catch (InterruptedException e) {
114        throw (InterruptedIOException)new InterruptedIOException().initCause(e);
115      }
116
117      if (exception != null) {
118        throw exception;
119      }
120    }
121  }
122}