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.Closeable; 021import java.io.IOException; 022import java.util.HashSet; 023import java.util.Set; 024import java.util.concurrent.ExecutionException; 025import java.util.concurrent.ExecutorService; 026import java.util.concurrent.Executors; 027import java.util.concurrent.TimeUnit; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.ServerName; 030import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 031import org.apache.yetus.audience.InterfaceAudience; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 035import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; 036 037import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ClientMetaService; 038 039/** 040 * Thread safe utility that keeps master end points used by {@link MasterRegistry} up to date. This 041 * uses the RPC {@link ClientMetaService#getMasters} to fetch the latest list of registered masters. 042 * By default the refresh happens periodically (configured via 043 * {@link #PERIODIC_REFRESH_INTERVAL_SECS}). The refresh can also be triggered on demand via 044 * {@link #refreshNow()}. To prevent a flood of on-demand refreshes we expect that any attempts two 045 * should be spaced at least {@link #MIN_SECS_BETWEEN_REFRESHES} seconds apart. 046 */ 047@InterfaceAudience.Private 048public class MasterAddressRefresher implements Closeable { 049 private static final Logger LOG = LoggerFactory.getLogger(MasterAddressRefresher.class); 050 public static final String PERIODIC_REFRESH_INTERVAL_SECS = 051 "hbase.client.master_registry.refresh_interval_secs"; 052 private static final int PERIODIC_REFRESH_INTERVAL_SECS_DEFAULT = 300; 053 public static final String MIN_SECS_BETWEEN_REFRESHES = 054 "hbase.client.master_registry.min_secs_between_refreshes"; 055 private static final int MIN_SECS_BETWEEN_REFRESHES_DEFAULT = 60; 056 057 private final ExecutorService pool; 058 private final MasterRegistry registry; 059 private final long periodicRefreshMs; 060 private final long timeBetweenRefreshesMs; 061 private final Object refreshMasters = new Object(); 062 063 @Override 064 public void close() { 065 pool.shutdownNow(); 066 } 067 068 /** 069 * Thread that refreshes the master end points until it is interrupted via {@link #close()}. 070 * Multiple callers attempting to refresh at the same time synchronize on {@link #refreshMasters}. 071 */ 072 private class RefreshThread implements Runnable { 073 @Override 074 public void run() { 075 long lastRpcTs = 0; 076 while (!Thread.interrupted()) { 077 try { 078 // Spurious wake ups are okay, worst case we make an extra RPC call to refresh. We won't 079 // have duplicate refreshes because once the thread is past the wait(), notify()s are 080 // ignored until the thread is back to the waiting state. 081 synchronized (refreshMasters) { 082 refreshMasters.wait(periodicRefreshMs); 083 } 084 long currentTs = EnvironmentEdgeManager.currentTime(); 085 if (lastRpcTs != 0 && currentTs - lastRpcTs <= timeBetweenRefreshesMs) { 086 continue; 087 } 088 lastRpcTs = currentTs; 089 LOG.debug("Attempting to refresh master address end points."); 090 Set<ServerName> newMasters = new HashSet<>(registry.getMasters().get()); 091 registry.populateMasterStubs(newMasters); 092 LOG.debug("Finished refreshing master end points. {}", newMasters); 093 } catch (InterruptedException e) { 094 LOG.debug("Interrupted during wait, aborting refresh-masters-thread.", e); 095 break; 096 } catch (ExecutionException | IOException e) { 097 LOG.debug("Error populating latest list of masters.", e); 098 } 099 } 100 LOG.info("Master end point refresher loop exited."); 101 } 102 } 103 104 MasterAddressRefresher(Configuration conf, MasterRegistry registry) { 105 pool = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() 106 .setNameFormat("master-registry-refresh-end-points").setDaemon(true).build()); 107 periodicRefreshMs = TimeUnit.SECONDS.toMillis(conf.getLong(PERIODIC_REFRESH_INTERVAL_SECS, 108 PERIODIC_REFRESH_INTERVAL_SECS_DEFAULT)); 109 timeBetweenRefreshesMs = TimeUnit.SECONDS.toMillis(conf.getLong(MIN_SECS_BETWEEN_REFRESHES, 110 MIN_SECS_BETWEEN_REFRESHES_DEFAULT)); 111 Preconditions.checkArgument(periodicRefreshMs > 0); 112 Preconditions.checkArgument(timeBetweenRefreshesMs < periodicRefreshMs); 113 this.registry = registry; 114 pool.submit(new RefreshThread()); 115 } 116 117 /** 118 * Notifies the refresher thread to refresh the configuration. This does not guarantee a refresh. 119 * See class comment for details. 120 */ 121 void refreshNow() { 122 synchronized (refreshMasters) { 123 refreshMasters.notify(); 124 } 125 } 126}