-
Notifications
You must be signed in to change notification settings - Fork 3.4k
HBASE-23257: Track clusterID in stand by masters (#798) #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
hbase-server/src/main/java/org/apache/hadoop/hbase/master/CachedClusterId.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hadoop.hbase.master; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.hbase.ClusterId; | ||
import org.apache.hadoop.hbase.util.FSUtils; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; | ||
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; | ||
|
||
/** | ||
* Caches the cluster ID of the cluster. For standby masters, this is used to serve the client | ||
* RPCs that fetch the cluster ID. ClusterID is only created by an active master if one does not | ||
* already exist. Standby masters just read the information from the file system. This class is | ||
* thread-safe. | ||
* | ||
* TODO: Make it a singleton without affecting concurrent junit tests. | ||
*/ | ||
@InterfaceAudience.Private | ||
public class CachedClusterId { | ||
|
||
public static final Logger LOG = LoggerFactory.getLogger(CachedClusterId.class); | ||
private static final int MAX_FETCH_TIMEOUT_MS = 10000; | ||
|
||
private Path rootDir; | ||
private FileSystem fs; | ||
|
||
// When true, indicates that a FileSystem fetch of ClusterID is in progress. This is used to | ||
// avoid multiple fetches from FS and let only one thread fetch the information. | ||
AtomicBoolean fetchInProgress = new AtomicBoolean(false); | ||
|
||
// When true, it means that the cluster ID has been fetched successfully from fs. | ||
private AtomicBoolean isClusterIdSet = new AtomicBoolean(false); | ||
// Immutable once set and read multiple times. | ||
private ClusterId clusterId; | ||
|
||
// cache stats for testing. | ||
private AtomicInteger cacheMisses = new AtomicInteger(0); | ||
|
||
public CachedClusterId(Configuration conf) throws IOException { | ||
rootDir = FSUtils.getRootDir(conf); | ||
fs = rootDir.getFileSystem(conf); | ||
} | ||
|
||
/** | ||
* Succeeds only once, when setting to a non-null value. Overwrites are not allowed. | ||
*/ | ||
private void setClusterId(ClusterId id) { | ||
if (id == null || isClusterIdSet.get()) { | ||
return; | ||
} | ||
clusterId = id; | ||
isClusterIdSet.set(true); | ||
} | ||
|
||
/** | ||
* Returns a cached copy of the cluster ID. null if the cache is not populated. | ||
*/ | ||
private String getClusterId() { | ||
if (!isClusterIdSet.get()) { | ||
return null; | ||
} | ||
// It is ok to read without a lock since clusterId is immutable once set. | ||
return clusterId.toString(); | ||
} | ||
|
||
/** | ||
* Attempts to fetch the cluster ID from the file system. If no attempt is already in progress, | ||
* synchronously fetches the cluster ID and sets it. If an attempt is already in progress, | ||
* returns right away and the caller is expected to wait for the fetch to finish. | ||
* @return true if the attempt is done, false if another thread is already fetching it. | ||
*/ | ||
private boolean attemptFetch() { | ||
if (fetchInProgress.compareAndSet(false, true)) { | ||
// A fetch is not in progress, so try fetching the cluster ID synchronously and then notify | ||
// the waiting threads. | ||
try { | ||
cacheMisses.incrementAndGet(); | ||
setClusterId(FSUtils.getClusterId(fs, rootDir)); | ||
} catch (IOException e) { | ||
LOG.warn("Error fetching cluster ID", e); | ||
} finally { | ||
Preconditions.checkState(fetchInProgress.compareAndSet(true, false)); | ||
synchronized (fetchInProgress) { | ||
fetchInProgress.notifyAll(); | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void waitForFetchToFinish() throws InterruptedException { | ||
synchronized (fetchInProgress) { | ||
while (fetchInProgress.get()) { | ||
// We don't want the fetches to block forever, for example if there are bugs | ||
// of missing notifications. | ||
fetchInProgress.wait(MAX_FETCH_TIMEOUT_MS); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fetches the ClusterId from FS if it is not cached locally. Atomically updates the cached | ||
* copy and is thread-safe. Optimized to do a single fetch when there are multiple threads are | ||
* trying get from a clean cache. | ||
* | ||
* @return ClusterId by reading from FileSystem or null in any error case or cluster ID does | ||
* not exist on the file system. | ||
*/ | ||
public String getFromCacheOrFetch() { | ||
String id = getClusterId(); | ||
if (id != null) { | ||
return id; | ||
} | ||
if (!attemptFetch()) { | ||
// A fetch is in progress. | ||
try { | ||
waitForFetchToFinish(); | ||
} catch (InterruptedException e) { | ||
// pass and return whatever is in the cache. | ||
} | ||
} | ||
return getClusterId(); | ||
} | ||
|
||
@VisibleForTesting | ||
public int getCacheStats() { | ||
return cacheMisses.get(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
hbase-server/src/test/java/org/apache/hadoop/hbase/TestCachedClusterId.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext; | ||
import org.apache.hadoop.hbase.MultithreadedTestUtil.TestThread; | ||
import org.apache.hadoop.hbase.master.CachedClusterId; | ||
import org.apache.hadoop.hbase.master.HMaster; | ||
import org.apache.hadoop.hbase.testclassification.MediumTests; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
@Category(MediumTests.class) | ||
public class TestCachedClusterId { | ||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestCachedClusterId.class); | ||
|
||
private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); | ||
|
||
private static String clusterId; | ||
private static HMaster activeMaster; | ||
private static HMaster standByMaster; | ||
|
||
private static class GetClusterIdThread extends TestThread { | ||
CachedClusterId cachedClusterId; | ||
public GetClusterIdThread(TestContext ctx, CachedClusterId clusterId) { | ||
super(ctx); | ||
cachedClusterId = clusterId; | ||
} | ||
|
||
@Override | ||
public void doWork() throws Exception { | ||
assertEquals(clusterId, cachedClusterId.getFromCacheOrFetch()); | ||
} | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
TEST_UTIL.startMiniCluster(1); | ||
activeMaster = TEST_UTIL.getHBaseCluster().getMaster(); | ||
clusterId = activeMaster.getClusterId(); | ||
standByMaster = TEST_UTIL.getHBaseCluster().startMaster().getMaster(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
TEST_UTIL.shutdownMiniCluster(); | ||
} | ||
|
||
@Test | ||
public void testClusterIdMatch() { | ||
assertEquals(clusterId, standByMaster.getClusterId()); | ||
} | ||
|
||
@Test | ||
public void testMultiThreadedGetClusterId() throws Exception { | ||
Configuration conf = TEST_UTIL.getConfiguration(); | ||
CachedClusterId cachedClusterId = new CachedClusterId(conf); | ||
TestContext context = new TestContext(conf); | ||
int numThreads = 100; | ||
for (int i = 0; i < numThreads; i++) { | ||
context.addThread(new GetClusterIdThread(context, cachedClusterId)); | ||
} | ||
context.startThreads(); | ||
context.stop(); | ||
int cacheMisses = cachedClusterId.getCacheStats(); | ||
assertEquals(cacheMisses, 1); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: A bit wasteful to have this just for a test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree... wish Java had C++ like macros that we could use to inject DEBUG only code :D.
This code also exists in the master, I'll have to cleanup from there. Can I do it as a follow up?