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.rsgroup;
019
020import java.util.ArrayList;
021import java.util.List;
022import java.util.stream.Collectors;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.client.BalanceRequest;
025import org.apache.hadoop.hbase.client.BalanceResponse;
026import org.apache.hadoop.hbase.net.Address;
027import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
028import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
029import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
030import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.BalanceRSGroupRequest;
031import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.BalanceRSGroupResponse;
032import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos;
033import org.apache.hadoop.hbase.protobuf.generated.TableProtos;
034import org.apache.yetus.audience.InterfaceAudience;
035
036@InterfaceAudience.Private
037final class RSGroupProtobufUtil {
038  private RSGroupProtobufUtil() {
039  }
040
041  static void populateBalanceRSGroupResponse(BalanceRSGroupResponse.Builder responseBuilder,
042    BalanceResponse response) {
043    responseBuilder.setBalanceRan(response.isBalancerRan())
044      .setMovesCalculated(response.getMovesCalculated())
045      .setMovesExecuted(response.getMovesExecuted());
046  }
047
048  static BalanceResponse toBalanceResponse(BalanceRSGroupResponse response) {
049    return BalanceResponse.newBuilder().setBalancerRan(response.getBalanceRan())
050      .setMovesExecuted(response.hasMovesExecuted() ? response.getMovesExecuted() : 0)
051      .setMovesCalculated(response.hasMovesCalculated() ? response.getMovesCalculated() : 0)
052      .build();
053  }
054
055  static BalanceRSGroupRequest createBalanceRSGroupRequest(String groupName,
056    BalanceRequest request) {
057    return BalanceRSGroupRequest.newBuilder().setRSGroupName(groupName)
058      .setDryRun(request.isDryRun()).setIgnoreRit(request.isIgnoreRegionsInTransition()).build();
059  }
060
061  static BalanceRequest toBalanceRequest(BalanceRSGroupRequest request) {
062    return BalanceRequest.newBuilder().setDryRun(request.hasDryRun() && request.getDryRun())
063      .setIgnoreRegionsInTransition(request.hasIgnoreRit() && request.getIgnoreRit()).build();
064  }
065
066  static RSGroupInfo toGroupInfo(RSGroupProtos.RSGroupInfo proto) {
067    RSGroupInfo rsGroupInfo = new RSGroupInfo(proto.getName());
068    for (HBaseProtos.ServerName el : proto.getServersList()) {
069      rsGroupInfo.addServer(Address.fromParts(el.getHostName(), el.getPort()));
070    }
071    for (TableProtos.TableName pTableName : proto.getTablesList()) {
072      rsGroupInfo.addTable(ProtobufUtil.toTableName(pTableName));
073    }
074    proto.getConfigurationList()
075      .forEach(pair -> rsGroupInfo.setConfiguration(pair.getName(), pair.getValue()));
076    return rsGroupInfo;
077  }
078
079  static RSGroupProtos.RSGroupInfo toProtoGroupInfo(RSGroupInfo pojo) {
080    List<TableProtos.TableName> tables = new ArrayList<>(pojo.getTables().size());
081    for (TableName arg : pojo.getTables()) {
082      tables.add(ProtobufUtil.toProtoTableName(arg));
083    }
084    List<HBaseProtos.ServerName> hostports = new ArrayList<>(pojo.getServers().size());
085    for (Address el : pojo.getServers()) {
086      hostports.add(HBaseProtos.ServerName.newBuilder().setHostName(el.getHostname())
087        .setPort(el.getPort()).build());
088    }
089    List<
090      NameStringPair> configuration =
091        pojo
092          .getConfiguration().entrySet().stream().map(entry -> NameStringPair.newBuilder()
093            .setName(entry.getKey()).setValue(entry.getValue()).build())
094          .collect(Collectors.toList());
095    return RSGroupProtos.RSGroupInfo.newBuilder().setName(pojo.getName()).addAllServers(hostports)
096      .addAllTables(tables).addAllConfiguration(configuration).build();
097  }
098}