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