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;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.Abortable;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.NonceGenerator;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.apache.yetus.audience.InterfaceStability;
031
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockRequest;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockService;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockType;
036
037/**
038 * Helper class to create "master locks" for namespaces, tables and regions.
039 * DEV-NOTE: At the moment this class is used only by the RS for MOB,
040 *           to prevent other MOB compaction to conflict.
041 *           The RS has already the stub of the LockService, so we have only one constructor that
042 *           takes the LockService stub. If in the future we are going to use this in other places
043 *           we should add a constructor that from conf or connection, creates the stub.
044 */
045@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
046@InterfaceStability.Evolving
047public class LockServiceClient {
048  private final LockService.BlockingInterface stub;
049  private final Configuration conf;
050  private final NonceGenerator ng;
051
052  public LockServiceClient(final Configuration conf, final LockService.BlockingInterface stub,
053      final NonceGenerator ng) {
054    this.conf = conf;
055    this.stub = stub;
056    this.ng = ng;
057  }
058
059  /**
060   * Create a new EntityLock object to acquire an exclusive or shared lock on a table.
061   * Internally, the table namespace will also be locked in shared mode.
062   */
063  public EntityLock tableLock(final TableName tableName, final boolean exclusive,
064      final String description, final Abortable abort) {
065    LockRequest lockRequest = buildLockRequest(exclusive ? LockType.EXCLUSIVE : LockType.SHARED,
066        tableName.getNameAsString(), null, null, description, ng.getNonceGroup(), ng.newNonce());
067    return new EntityLock(conf, stub, lockRequest, abort);
068  }
069
070  /**
071   * LocCreate a new EntityLock object to acquire exclusive lock on a namespace.
072   * Clients can not acquire shared locks on namespace.
073   */
074  public EntityLock namespaceLock(String namespace, String description, Abortable abort) {
075    LockRequest lockRequest = buildLockRequest(LockType.EXCLUSIVE,
076        namespace, null, null, description, ng.getNonceGroup(), ng.newNonce());
077    return new EntityLock(conf, stub, lockRequest, abort);
078  }
079
080  /**
081   * Create a new EntityLock object to acquire exclusive lock on multiple regions of same tables.
082   * Internally, the table and its namespace will also be locked in shared mode.
083   */
084  public EntityLock regionLock(List<RegionInfo> regionInfos, String description, Abortable abort) {
085    LockRequest lockRequest = buildLockRequest(LockType.EXCLUSIVE,
086        null, null, regionInfos, description, ng.getNonceGroup(), ng.newNonce());
087    return new EntityLock(conf, stub, lockRequest, abort);
088  }
089
090  @InterfaceAudience.Private
091  public static LockRequest buildLockRequest(final LockType type,
092      final String namespace, final TableName tableName, final List<RegionInfo> regionInfos,
093      final String description, final long nonceGroup, final long nonce) {
094    final LockRequest.Builder builder = LockRequest.newBuilder()
095      .setLockType(type)
096      .setNonceGroup(nonceGroup)
097      .setNonce(nonce);
098    if (regionInfos != null) {
099      for (RegionInfo hri: regionInfos) {
100        builder.addRegionInfo(ProtobufUtil.toRegionInfo(hri));
101      }
102    } else if (namespace != null) {
103      builder.setNamespace(namespace);
104    } else if (tableName != null) {
105      builder.setTableName(ProtobufUtil.toProtoTableName(tableName));
106    }
107    return builder.setDescription(description).build();
108  }
109}