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 */
018package org.apache.hadoop.hbase.master.procedure;
019
020import java.io.IOException;
021import java.io.InterruptedIOException;
022import java.util.concurrent.CountDownLatch;
023import org.apache.hadoop.hbase.client.VersionInfoUtil;
024import org.apache.hadoop.hbase.procedure2.Procedure;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.apache.yetus.audience.InterfaceStability;
027
028/**
029 * Latch used by the Master to have the prepare() sync behaviour for old clients, that can only get
030 * exceptions in a synchronous way.
031 */
032@InterfaceAudience.Private
033@InterfaceStability.Evolving
034public abstract class ProcedurePrepareLatch {
035  private static final NoopLatch noopLatch = new NoopLatch();
036
037  /**
038   * Create a latch if the client does not have async proc support. This uses the default 1.1
039   * version.
040   * @return a CompatibilityLatch or a NoopLatch if the client has async proc support
041   */
042  public static ProcedurePrepareLatch createLatch() {
043    // don't use the latch if we have procedure support (default 1.1)
044    return createLatch(1, 1);
045  }
046
047  /**
048   * Create a latch if the client does not have async proc support
049   * @param major major version with async proc support
050   * @param minor minor version with async proc support
051   * @return a CompatibilityLatch or a NoopLatch if the client has async proc support
052   */
053  public static ProcedurePrepareLatch createLatch(int major, int minor) {
054    // don't use the latch if we have procedure support
055    return hasProcedureSupport(major, minor) ? noopLatch : new CompatibilityLatch();
056  }
057
058  /**
059   * Creates a latch which blocks.
060   */
061  public static ProcedurePrepareLatch createBlockingLatch() {
062    return new CompatibilityLatch();
063  }
064
065  /**
066   * Returns the singleton latch which does nothing.
067   */
068  public static ProcedurePrepareLatch getNoopLatch() {
069    return noopLatch;
070  }
071
072  private static boolean hasProcedureSupport(int major, int minor) {
073    // Note: this won't work if the shortcut similar to the one in HRegionServer is used
074    // without the corresponding version handling.
075    return VersionInfoUtil.currentClientHasMinimumVersion(major, minor);
076  }
077
078  protected abstract void countDown(final Procedure proc);
079
080  public abstract void await() throws IOException;
081
082  public static void releaseLatch(final ProcedurePrepareLatch latch, final Procedure proc) {
083    if (latch != null) {
084      latch.countDown(proc);
085    }
086  }
087
088  private static class NoopLatch extends ProcedurePrepareLatch {
089    @Override
090    protected void countDown(final Procedure proc) {
091    }
092
093    @Override
094    public void await() throws IOException {
095    }
096  }
097
098  protected static class CompatibilityLatch extends ProcedurePrepareLatch {
099    private final CountDownLatch latch = new CountDownLatch(1);
100
101    private IOException exception = null;
102
103    @Override
104    protected void countDown(final Procedure proc) {
105      if (proc.hasException()) {
106        exception = MasterProcedureUtil.unwrapRemoteIOException(proc);
107      }
108      latch.countDown();
109    }
110
111    @Override
112    public void await() throws IOException {
113      try {
114        latch.await();
115      } catch (InterruptedException e) {
116        throw (InterruptedIOException) new InterruptedIOException().initCause(e);
117      }
118
119      if (exception != null) {
120        throw exception;
121      }
122    }
123  }
124}