Search in sources :

Example 1 with ReentrantLock

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();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SCMStore(org.apache.hadoop.yarn.server.sharedcachemanager.store.SCMStore) FileSystem(org.apache.hadoop.fs.FileSystem) CleanerMetrics(org.apache.hadoop.yarn.server.sharedcachemanager.metrics.CleanerMetrics) Test(org.junit.Test)

Example 2 with ReentrantLock

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();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SCMStore(org.apache.hadoop.yarn.server.sharedcachemanager.store.SCMStore) FileSystem(org.apache.hadoop.fs.FileSystem) CleanerMetrics(org.apache.hadoop.yarn.server.sharedcachemanager.metrics.CleanerMetrics) Test(org.junit.Test)

Example 3 with ReentrantLock

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();
            }
        }
    });
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Configuration(org.apache.hadoop.conf.Configuration) EventHandler(org.apache.hadoop.hbase.executor.EventHandler) FavoredNodesManager(org.apache.hadoop.hbase.favored.FavoredNodesManager) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) IOException(java.io.IOException) FailedServerException(org.apache.hadoop.hbase.ipc.FailedServerException) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ServerName(org.apache.hadoop.hbase.ServerName) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RemoteException(org.apache.hadoop.ipc.RemoteException)

Example 4 with ReentrantLock

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);
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 5 with ReentrantLock

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;
}
Also used : Path(org.apache.hadoop.fs.Path) ReentrantLock(java.util.concurrent.locks.ReentrantLock) FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) CorruptedSnapshotException(org.apache.hadoop.hbase.snapshot.CorruptedSnapshotException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10