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 */
019package org.apache.hadoop.hbase;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.hadoop.hbase.client.Connection;
026import org.apache.hadoop.hbase.client.Get;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.client.Scan;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
031import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos;
032import org.apache.hadoop.hbase.rsgroup.RSGroupInfo;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * Read rs group information from  <code>hbase:rsgroup</code>.
038 */
039@InterfaceAudience.Private
040public final class RSGroupTableAccessor {
041
042  //Assigned before user tables
043  private static final TableName RSGROUP_TABLE_NAME =
044      TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "rsgroup");
045  private static final byte[] META_FAMILY_BYTES = Bytes.toBytes("m");
046  private static final byte[] META_QUALIFIER_BYTES = Bytes.toBytes("i");
047
048  private RSGroupTableAccessor() {
049  }
050
051  public static boolean isRSGroupsEnabled(Connection connection) throws IOException {
052    return connection.getAdmin().tableExists(RSGROUP_TABLE_NAME);
053  }
054
055  public static List<RSGroupInfo> getAllRSGroupInfo(Connection connection)
056      throws IOException {
057    try (Table rsGroupTable = connection.getTable(RSGROUP_TABLE_NAME)) {
058      List<RSGroupInfo> rsGroupInfos = new ArrayList<>();
059      for (Result result : rsGroupTable.getScanner(new Scan())) {
060        RSGroupInfo rsGroupInfo = getRSGroupInfo(result);
061        if (rsGroupInfo != null) {
062          rsGroupInfos.add(rsGroupInfo);
063        }
064      }
065      return rsGroupInfos;
066    }
067  }
068
069  private static RSGroupInfo getRSGroupInfo(Result result) throws IOException {
070    byte[] rsGroupInfo = result.getValue(META_FAMILY_BYTES, META_QUALIFIER_BYTES);
071    if (rsGroupInfo == null) {
072      return null;
073    }
074    RSGroupProtos.RSGroupInfo proto =
075        RSGroupProtos.RSGroupInfo.parseFrom(rsGroupInfo);
076    return ProtobufUtil.toGroupInfo(proto);
077  }
078
079  public static RSGroupInfo getRSGroupInfo(Connection connection, byte[] rsGroupName)
080      throws IOException {
081    try (Table rsGroupTable = connection.getTable(RSGROUP_TABLE_NAME)){
082      Result result = rsGroupTable.get(new Get(rsGroupName));
083      return getRSGroupInfo(result);
084    }
085  }
086}