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.yetus.audience.InterfaceAudience;
028
029/**
030 * A {@link ConnectionRegistry} implementation used at server side, where we could use the
031 * {@link ConnectionRegistryEndpoint} directly, without any rpcs.
032 */
033@InterfaceAudience.Private
034class ShortCircuitConnectionRegistry implements ConnectionRegistry {
035
036  private final ConnectionRegistryEndpoint endpoint;
037
038  public ShortCircuitConnectionRegistry(ConnectionRegistryEndpoint endpoint) {
039    this.endpoint = endpoint;
040  }
041
042  @Override
043  public CompletableFuture<RegionLocations> getMetaRegionLocations() {
044    CompletableFuture<RegionLocations> future = new CompletableFuture<>();
045    List<HRegionLocation> locs = endpoint.getMetaLocations();
046    if (locs.isEmpty()) {
047      future.completeExceptionally(new IOException("no meta location available"));
048    } else {
049      future.complete(new RegionLocations(locs));
050    }
051    return future;
052  }
053
054  @Override
055  public CompletableFuture<String> getClusterId() {
056    return CompletableFuture.completedFuture(endpoint.getClusterId());
057  }
058
059  @Override
060  public CompletableFuture<ServerName> getActiveMaster() {
061    CompletableFuture<ServerName> future = new CompletableFuture<>();
062    Optional<ServerName> activeMaster = endpoint.getActiveMaster();
063    if (activeMaster.isPresent()) {
064      future.complete(activeMaster.get());
065    } else {
066      future.completeExceptionally(new IOException("no active master available"));
067    }
068    return future;
069  }
070
071  @Override
072  public String getConnectionString() {
073    return "short-circuit";
074  }
075
076  @Override
077  public void close() {
078    // nothing
079  }
080}