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;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Optional;
023import java.util.concurrent.CompletableFuture;
024import org.apache.hadoop.hbase.HRegionLocation;
025import org.apache.hadoop.hbase.RegionLocations;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.regionserver.HRegionServer;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Connection registry implementation for region server.
032 */
033@InterfaceAudience.Private
034public class RegionServerRegistry implements ConnectionRegistry {
035
036  private final HRegionServer regionServer;
037
038  public RegionServerRegistry(HRegionServer regionServer) {
039    this.regionServer = regionServer;
040  }
041
042  @Override
043  public CompletableFuture<RegionLocations> getMetaRegionLocations() {
044    CompletableFuture<RegionLocations> future = new CompletableFuture<>();
045    Optional<List<HRegionLocation>> locs =
046      regionServer.getMetaRegionLocationCache().getMetaRegionLocations();
047    if (locs.isPresent()) {
048      List<HRegionLocation> list = locs.get();
049      if (list.isEmpty()) {
050        future.completeExceptionally(new IOException("no meta location available"));
051      } else {
052        future.complete(new RegionLocations(list));
053      }
054    } else {
055      future.completeExceptionally(new IOException("no meta location available"));
056    }
057    return future;
058  }
059
060  @Override
061  public CompletableFuture<String> getClusterId() {
062    return CompletableFuture.completedFuture(regionServer.getClusterId());
063  }
064
065  @Override
066  public CompletableFuture<ServerName> getActiveMaster() {
067    CompletableFuture<ServerName> future = new CompletableFuture<>();
068    Optional<ServerName> activeMaster = regionServer.getActiveMaster();
069    if (activeMaster.isPresent()) {
070      future.complete(activeMaster.get());
071    } else {
072      future.completeExceptionally(new IOException("no active master available"));
073    }
074    return future;
075  }
076
077  @Override
078  public String getConnectionString() {
079    return "short-circuit";
080  }
081
082  @Override
083  public void close() {
084    // nothing
085  }
086}