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