use of java.util.concurrent.locks.ReentrantLock in project hadoop by apache.
the class TestCleanerTask method testProcessEvictableResource.
@Test
public void testProcessEvictableResource() throws Exception {
FileSystem fs = mock(FileSystem.class);
CleanerMetrics metrics = mock(CleanerMetrics.class);
SCMStore store = mock(SCMStore.class);
CleanerTask task = createSpiedTask(fs, store, metrics, new ReentrantLock());
// mock an evictable resource
when(store.isResourceEvictable(isA(String.class), isA(FileStatus.class))).thenReturn(true);
FileStatus status = mock(FileStatus.class);
when(status.getPath()).thenReturn(new Path(ROOT + "/a/b/c/abc"));
when(store.removeResource(isA(String.class))).thenReturn(true);
// rename succeeds
when(fs.rename(isA(Path.class), isA(Path.class))).thenReturn(true);
// delete returns true
when(fs.delete(isA(Path.class), anyBoolean())).thenReturn(true);
// process the resource
task.processSingleResource(status);
// the directory should be renamed
verify(fs).rename(eq(status.getPath()), isA(Path.class));
// metrics should record a deleted file
verify(metrics).reportAFileDelete();
verify(metrics, never()).reportAFileProcess();
}
use of java.util.concurrent.locks.ReentrantLock in project hadoop by apache.
the class TestCleanerTask method testProcessFreshResource.
@Test
public void testProcessFreshResource() throws Exception {
FileSystem fs = mock(FileSystem.class);
CleanerMetrics metrics = mock(CleanerMetrics.class);
SCMStore store = mock(SCMStore.class);
CleanerTask task = createSpiedTask(fs, store, metrics, new ReentrantLock());
// mock a resource that is not evictable
when(store.isResourceEvictable(isA(String.class), isA(FileStatus.class))).thenReturn(false);
FileStatus status = mock(FileStatus.class);
when(status.getPath()).thenReturn(new Path(ROOT + "/a/b/c/abc"));
// process the resource
task.processSingleResource(status);
// the directory should not be renamed
verify(fs, never()).rename(eq(status.getPath()), isA(Path.class));
// metrics should record a processed file (but not delete)
verify(metrics).reportAFileProcess();
verify(metrics, never()).reportAFileDelete();
}
use of java.util.concurrent.locks.ReentrantLock in project hbase by apache.
the class AssignmentManager method retrySendRegionOpen.
/**
* At master failover, for pending_open region, make sure
* sendRegionOpen RPC call is sent to the target regionserver
*/
private void retrySendRegionOpen(final RegionState regionState) {
this.executorService.submit(new EventHandler(server, EventType.M_MASTER_RECOVERY) {
@Override
public void process() throws IOException {
HRegionInfo hri = regionState.getRegion();
ServerName serverName = regionState.getServerName();
ReentrantLock lock = locker.acquireLock(hri.getEncodedName());
try {
for (int i = 1; i <= maximumAttempts; i++) {
if (!serverManager.isServerOnline(serverName) || server.isStopped() || server.isAborted()) {
// No need any more
return;
}
try {
if (!regionState.equals(regionStates.getRegionState(hri))) {
// Region is not in the expected state any more
return;
}
List<ServerName> favoredNodes = ServerName.EMPTY_SERVER_LIST;
if (shouldAssignFavoredNodes(hri)) {
FavoredNodesManager fnm = ((MasterServices) server).getFavoredNodesManager();
favoredNodes = fnm.getFavoredNodesWithDNPort(hri);
}
serverManager.sendRegionOpen(serverName, hri, favoredNodes);
// we're done
return;
} catch (Throwable t) {
if (t instanceof RemoteException) {
t = ((RemoteException) t).unwrapRemoteException();
}
if (t instanceof FailedServerException && i < maximumAttempts) {
// retry too soon. Retry after the failed_server_expiry time
try {
Configuration conf = this.server.getConfiguration();
long sleepTime = 1 + conf.getInt(RpcClient.FAILED_SERVER_EXPIRY_KEY, RpcClient.FAILED_SERVER_EXPIRY_DEFAULT);
if (LOG.isDebugEnabled()) {
LOG.debug(serverName + " is on failed server list; waiting " + sleepTime + "ms", t);
}
Thread.sleep(sleepTime);
continue;
} catch (InterruptedException ie) {
LOG.warn("Failed to assign " + hri.getRegionNameAsString() + " since interrupted", ie);
regionStates.updateRegionState(hri, State.FAILED_OPEN);
Thread.currentThread().interrupt();
return;
}
}
if (serverManager.isServerOnline(serverName) && t instanceof java.net.SocketTimeoutException) {
// reset the try count
i--;
} else {
LOG.info("Got exception in retrying sendRegionOpen for " + regionState + "; try=" + i + " of " + maximumAttempts, t);
}
Threads.sleep(100);
}
}
// Run out of attempts
regionStates.updateRegionState(hri, State.FAILED_OPEN);
} finally {
lock.unlock();
}
}
});
}
use of java.util.concurrent.locks.ReentrantLock in project hbase by apache.
the class AssignmentManager method unassign.
/**
* Unassigns the specified region.
* <p>
* Updates the RegionState and sends the CLOSE RPC unless region is being
* split by regionserver; then the unassign fails (silently) because we
* presume the region being unassigned no longer exists (its been split out
* of existence). TODO: What to do if split fails and is rolled back and
* parent is revivified?
* <p>
* If a RegionPlan is already set, it will remain.
*
* @param region server to be unassigned
* @param dest the destination server of the region
*/
public void unassign(HRegionInfo region, ServerName dest) {
// TODO: Method needs refactoring. Ugly buried returns throughout. Beware!
LOG.debug("Starting unassign of " + region.getRegionNameAsString() + " (offlining), current state: " + regionStates.getRegionState(region));
String encodedName = region.getEncodedName();
// Grab the state of this region and synchronize on it
// We need a lock here as we're going to do a put later and we don't want multiple states
// creation
ReentrantLock lock = locker.acquireLock(encodedName);
RegionState state = regionStates.getRegionTransitionState(encodedName);
try {
if (state == null || state.isFailedClose()) {
if (state == null) {
// Region is not in transition.
// We can unassign it only if it's not SPLIT/MERGED.
state = regionStates.getRegionState(encodedName);
if (state != null && state.isUnassignable()) {
LOG.info("Attempting to unassign " + state + ", ignored");
// Offline region will be reassigned below
return;
}
if (state == null || state.getServerName() == null) {
// We don't know where the region is, offline it.
// No need to send CLOSE RPC
LOG.warn("Attempting to unassign a region not in RegionStates " + region.getRegionNameAsString() + ", offlined");
regionOffline(region);
return;
}
}
state = regionStates.updateRegionState(region, State.PENDING_CLOSE);
} else if (state.isFailedOpen()) {
// The region is not open yet
regionOffline(region);
return;
} else {
LOG.debug("Attempting to unassign " + region.getRegionNameAsString() + " but it is " + "already in transition (" + state.getState());
return;
}
unassign(region, state.getServerName(), dest);
} finally {
lock.unlock();
// Region is expected to be reassigned afterwards
if (!replicasToClose.contains(region) && regionStates.isRegionInState(region, State.OFFLINE)) {
assign(region);
}
}
}
use of java.util.concurrent.locks.ReentrantLock in project hbase by apache.
the class SnapshotFileCache method getSnapshotsInProgress.
@VisibleForTesting
List<String> getSnapshotsInProgress(final SnapshotManager snapshotManager) throws IOException {
List<String> snapshotInProgress = Lists.newArrayList();
// only add those files to the cache, but not to the known snapshots
Path snapshotTmpDir = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOT_TMP_DIR_NAME);
// only add those files to the cache, but not to the known snapshots
FileStatus[] running = FSUtils.listStatus(fs, snapshotTmpDir);
if (running != null) {
for (FileStatus run : running) {
ReentrantLock lock = null;
if (snapshotManager != null) {
lock = snapshotManager.getLocks().acquireLock(run.getPath().getName());
}
try {
snapshotInProgress.addAll(fileInspector.filesUnderSnapshot(run.getPath()));
} catch (CorruptedSnapshotException e) {
// See HBASE-16464
if (e.getCause() instanceof FileNotFoundException) {
// If the snapshot is corrupt, we will delete it
fs.delete(run.getPath(), true);
LOG.warn("delete the " + run.getPath() + " due to exception:", e.getCause());
} else {
throw e;
}
} finally {
if (lock != null) {
lock.unlock();
}
}
}
}
return snapshotInProgress;
}
Aggregations