|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hbase.master; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | +import java.util.concurrent.atomic.AtomicInteger; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.hadoop.fs.FileSystem; |
| 26 | +import org.apache.hadoop.fs.Path; |
| 27 | +import org.apache.hadoop.hbase.ClusterId; |
| 28 | +import org.apache.hadoop.hbase.util.FSUtils; |
| 29 | +import org.apache.yetus.audience.InterfaceAudience; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | +import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; |
| 33 | +import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; |
| 34 | + |
| 35 | +/** |
| 36 | + * Caches the cluster ID of the cluster. For standby masters, this is used to serve the client |
| 37 | + * RPCs that fetch the cluster ID. ClusterID is only created by an active master if one does not |
| 38 | + * already exist. Standby masters just read the information from the file system. This class is |
| 39 | + * thread-safe. |
| 40 | + * |
| 41 | + * TODO: Make it a singleton without affecting concurrent junit tests. |
| 42 | + */ |
| 43 | +@InterfaceAudience.Private |
| 44 | +public class CachedClusterId { |
| 45 | + |
| 46 | + public static final Logger LOG = LoggerFactory.getLogger(CachedClusterId.class); |
| 47 | + private static final int MAX_FETCH_TIMEOUT_MS = 10000; |
| 48 | + |
| 49 | + private Path rootDir; |
| 50 | + private FileSystem fs; |
| 51 | + |
| 52 | + // When true, indicates that a FileSystem fetch of ClusterID is in progress. This is used to |
| 53 | + // avoid multiple fetches from FS and let only one thread fetch the information. |
| 54 | + AtomicBoolean fetchInProgress = new AtomicBoolean(false); |
| 55 | + |
| 56 | + // When true, it means that the cluster ID has been fetched successfully from fs. |
| 57 | + private AtomicBoolean isClusterIdSet = new AtomicBoolean(false); |
| 58 | + // Immutable once set and read multiple times. |
| 59 | + private ClusterId clusterId; |
| 60 | + |
| 61 | + // cache stats for testing. |
| 62 | + private AtomicInteger cacheMisses = new AtomicInteger(0); |
| 63 | + |
| 64 | + public CachedClusterId(Configuration conf) throws IOException { |
| 65 | + rootDir = FSUtils.getRootDir(conf); |
| 66 | + fs = rootDir.getFileSystem(conf); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Succeeds only once, when setting to a non-null value. Overwrites are not allowed. |
| 71 | + */ |
| 72 | + private void setClusterId(ClusterId id) { |
| 73 | + if (id == null || isClusterIdSet.get()) { |
| 74 | + return; |
| 75 | + } |
| 76 | + clusterId = id; |
| 77 | + isClusterIdSet.set(true); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns a cached copy of the cluster ID. null if the cache is not populated. |
| 82 | + */ |
| 83 | + private String getClusterId() { |
| 84 | + if (!isClusterIdSet.get()) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + // It is ok to read without a lock since clusterId is immutable once set. |
| 88 | + return clusterId.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Attempts to fetch the cluster ID from the file system. If no attempt is already in progress, |
| 93 | + * synchronously fetches the cluster ID and sets it. If an attempt is already in progress, |
| 94 | + * returns right away and the caller is expected to wait for the fetch to finish. |
| 95 | + * @return true if the attempt is done, false if another thread is already fetching it. |
| 96 | + */ |
| 97 | + private boolean attemptFetch() { |
| 98 | + if (fetchInProgress.compareAndSet(false, true)) { |
| 99 | + // A fetch is not in progress, so try fetching the cluster ID synchronously and then notify |
| 100 | + // the waiting threads. |
| 101 | + try { |
| 102 | + cacheMisses.incrementAndGet(); |
| 103 | + setClusterId(FSUtils.getClusterId(fs, rootDir)); |
| 104 | + } catch (IOException e) { |
| 105 | + LOG.warn("Error fetching cluster ID", e); |
| 106 | + } finally { |
| 107 | + Preconditions.checkState(fetchInProgress.compareAndSet(true, false)); |
| 108 | + synchronized (fetchInProgress) { |
| 109 | + fetchInProgress.notifyAll(); |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + private void waitForFetchToFinish() throws InterruptedException { |
| 118 | + synchronized (fetchInProgress) { |
| 119 | + while (fetchInProgress.get()) { |
| 120 | + // We don't want the fetches to block forever, for example if there are bugs |
| 121 | + // of missing notifications. |
| 122 | + fetchInProgress.wait(MAX_FETCH_TIMEOUT_MS); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Fetches the ClusterId from FS if it is not cached locally. Atomically updates the cached |
| 129 | + * copy and is thread-safe. Optimized to do a single fetch when there are multiple threads are |
| 130 | + * trying get from a clean cache. |
| 131 | + * |
| 132 | + * @return ClusterId by reading from FileSystem or null in any error case or cluster ID does |
| 133 | + * not exist on the file system. |
| 134 | + */ |
| 135 | + public String getFromCacheOrFetch() { |
| 136 | + String id = getClusterId(); |
| 137 | + if (id != null) { |
| 138 | + return id; |
| 139 | + } |
| 140 | + if (!attemptFetch()) { |
| 141 | + // A fetch is in progress. |
| 142 | + try { |
| 143 | + waitForFetchToFinish(); |
| 144 | + } catch (InterruptedException e) { |
| 145 | + // pass and return whatever is in the cache. |
| 146 | + } |
| 147 | + } |
| 148 | + return getClusterId(); |
| 149 | + } |
| 150 | + |
| 151 | + @VisibleForTesting |
| 152 | + public int getCacheStats() { |
| 153 | + return cacheMisses.get(); |
| 154 | + } |
| 155 | +} |
0 commit comments