001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.client.locking;
021
022import java.util.List;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.Abortable;
026import org.apache.hadoop.hbase.HBaseInterfaceAudience;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.NonceGenerator;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.yetus.audience.InterfaceStability;
032
033import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
034import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockRequest;
036import org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockService;
037import org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockType;
038
039/**
040 * Helper class to create "master locks" for namespaces, tables and regions.
041 * DEV-NOTE: At the moment this class is used only by the RS for MOB,
042 *           to prevent other MOB compaction to conflict.
043 *           The RS has already the stub of the LockService, so we have only one constructor that
044 *           takes the LockService stub. If in the future we are going to use this in other places
045 *           we should add a constructor that from conf or connection, creates the stub.
046 */
047@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
048@InterfaceStability.Evolving
049public class LockServiceClient {
050  private final LockService.BlockingInterface stub;
051  private final Configuration conf;
052  private final NonceGenerator ng;
053
054  public LockServiceClient(final Configuration conf, final LockService.BlockingInterface stub,
055      final NonceGenerator ng) {
056    this.conf = conf;
057    this.stub = stub;
058    this.ng = ng;
059  }
060
061  /**
062   * Create a new EntityLock object to acquire an exclusive or shared lock on a table.
063   * Internally, the table namespace will also be locked in shared mode.
064   */
065  public EntityLock tableLock(final TableName tableName, final boolean exclusive,
066      final String description, final Abortable abort) {
067    LockRequest lockRequest = buildLockRequest(exclusive ? LockType.EXCLUSIVE : LockType.SHARED,
068        tableName.getNameAsString(), null, null, description, ng.getNonceGroup(), ng.newNonce());
069    return new EntityLock(conf, stub, lockRequest, abort);
070  }
071
072  /**
073   * LocCreate a new EntityLock object to acquire exclusive lock on a namespace.
074   * Clients can not acquire shared locks on namespace.
075   */
076  public EntityLock namespaceLock(String namespace, String description, Abortable abort) {
077    LockRequest lockRequest = buildLockRequest(LockType.EXCLUSIVE,
078        namespace, null, null, description, ng.getNonceGroup(), ng.newNonce());
079    return new EntityLock(conf, stub, lockRequest, abort);
080  }
081
082  /**
083   * Create a new EntityLock object to acquire exclusive lock on multiple regions of same tables.
084   * Internally, the table and its namespace will also be locked in shared mode.
085   */
086  public EntityLock regionLock(List<RegionInfo> regionInfos, String description, Abortable abort) {
087    LockRequest lockRequest = buildLockRequest(LockType.EXCLUSIVE,
088        null, null, regionInfos, description, ng.getNonceGroup(), ng.newNonce());
089    return new EntityLock(conf, stub, lockRequest, abort);
090  }
091
092  @VisibleForTesting
093  public static LockRequest buildLockRequest(final LockType type,
094      final String namespace, final TableName tableName, final List<RegionInfo> regionInfos,
095      final String description, final long nonceGroup, final long nonce) {
096    final LockRequest.Builder builder = LockRequest.newBuilder()
097      .setLockType(type)
098      .setNonceGroup(nonceGroup)
099      .setNonce(nonce);
100    if (regionInfos != null) {
101      for (RegionInfo hri: regionInfos) {
102        builder.addRegionInfo(ProtobufUtil.toRegionInfo(hri));
103      }
104    } else if (namespace != null) {
105      builder.setNamespace(namespace);
106    } else if (tableName != null) {
107      builder.setTableName(ProtobufUtil.toProtoTableName(tableName));
108    }
109    return builder.setDescription(description).build();
110  }
111}