Search in sources :

Example 76 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class DefaultBlockMaster method getUsedBytesOnTiers.

@Override
public Map<String, Long> getUsedBytesOnTiers() {
    Map<String, Long> ret = new HashMap<>();
    for (MasterWorkerInfo worker : mWorkers) {
        try (LockResource r = worker.lockWorkerMeta(EnumSet.of(WorkerMetaLockSection.USAGE), true)) {
            for (Map.Entry<String, Long> entry : worker.getUsedBytesOnTiers().entrySet()) {
                Long used = ret.get(entry.getKey());
                ret.put(entry.getKey(), (used == null ? 0L : used) + entry.getValue());
            }
        }
    }
    return ret;
}
Also used : LockResource(alluxio.resource.LockResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MasterWorkerInfo(alluxio.master.block.meta.MasterWorkerInfo) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 77 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class DefaultBlockMaster method workerHeartbeat.

@Override
public Command workerHeartbeat(long workerId, Map<String, Long> capacityBytesOnTiers, Map<String, Long> usedBytesOnTiers, List<Long> removedBlockIds, Map<BlockLocation, List<Long>> addedBlocks, Map<String, StorageList> lostStorage, List<Metric> metrics) {
    MasterWorkerInfo worker = mWorkers.getFirstByField(ID_INDEX, workerId);
    if (worker == null) {
        LOG.warn("Could not find worker id: {} for heartbeat.", workerId);
        return Command.newBuilder().setCommandType(CommandType.Register).build();
    }
    // Update the TS before the heartbeat so even if the worker heartbeat processing
    // is time-consuming or triggers GC, the worker does not get marked as lost
    // by the LostWorkerDetectionHeartbeatExecutor
    worker.updateLastUpdatedTimeMs();
    // The address is final, no need for locking
    processWorkerMetrics(worker.getWorkerAddress().getHost(), metrics);
    Command workerCommand = null;
    try (LockResource r = worker.lockWorkerMeta(EnumSet.of(WorkerMetaLockSection.USAGE, WorkerMetaLockSection.BLOCKS), false)) {
        worker.addLostStorage(lostStorage);
        if (capacityBytesOnTiers != null) {
            worker.updateCapacityBytes(capacityBytesOnTiers);
        }
        worker.updateUsedBytes(usedBytesOnTiers);
        // Technically, 'worker' should be confirmed to still be in the data structure. Lost worker
        // detection can remove it. However, we are intentionally ignoring this race, since the worker
        // will just re-register regardless.
        processWorkerRemovedBlocks(worker, removedBlockIds, false);
        processWorkerAddedBlocks(worker, addedBlocks);
        Set<Long> toRemoveBlocks = worker.getToRemoveBlocks();
        if (toRemoveBlocks.isEmpty()) {
            workerCommand = Command.newBuilder().setCommandType(CommandType.Nothing).build();
        } else {
            workerCommand = Command.newBuilder().setCommandType(CommandType.Free).addAllData(toRemoveBlocks).build();
        }
    }
    // Update the TS again
    worker.updateLastUpdatedTimeMs();
    // Should not reach here
    Preconditions.checkNotNull(workerCommand, "Worker heartbeat response command is null!");
    return workerCommand;
}
Also used : LockResource(alluxio.resource.LockResource) Command(alluxio.grpc.Command) MasterWorkerInfo(alluxio.master.block.meta.MasterWorkerInfo)

Example 78 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class DefaultBlockMaster method removeBlocks.

@Override
public void removeBlocks(Collection<Long> blockIds, boolean delete) throws UnavailableException {
    try (JournalContext journalContext = createJournalContext()) {
        for (long blockId : blockIds) {
            Set<Long> workerIds;
            try (LockResource r = lockBlock(blockId)) {
                Optional<BlockMeta> block = mBlockStore.getBlock(blockId);
                if (!block.isPresent()) {
                    continue;
                }
                List<BlockLocation> locations = mBlockStore.getLocations(blockId);
                workerIds = new HashSet<>(locations.size());
                for (BlockLocation loc : locations) {
                    workerIds.add(loc.getWorkerId());
                }
                // processWorkerRemovedBlocks
                if (delete) {
                    // Make sure blockId is removed from mLostBlocks when the block metadata is deleted.
                    // Otherwise blockId in mLostBlock can be dangling index if the metadata is gone.
                    mLostBlocks.remove(blockId);
                    mBlockStore.removeBlock(blockId);
                    JournalEntry entry = JournalEntry.newBuilder().setDeleteBlock(DeleteBlockEntry.newBuilder().setBlockId(blockId)).build();
                    journalContext.append(entry);
                }
            }
            // workerRegister should be changed to address this race condition.
            for (long workerId : workerIds) {
                MasterWorkerInfo worker = mWorkers.getFirstByField(ID_INDEX, workerId);
                if (worker != null) {
                    try (LockResource r = worker.lockWorkerMeta(EnumSet.of(WorkerMetaLockSection.BLOCKS), false)) {
                        worker.updateToRemovedBlock(true, blockId);
                    }
                }
            }
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) JournalContext(alluxio.master.journal.JournalContext) MasterWorkerInfo(alluxio.master.block.meta.MasterWorkerInfo) BlockLocation(alluxio.proto.meta.Block.BlockLocation) BlockMeta(alluxio.proto.meta.Block.BlockMeta) JournalEntry(alluxio.proto.journal.Journal.JournalEntry)

Example 79 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class StateLockManagerTest method testGraceMode_Timeout.

@Test
public void testGraceMode_Timeout() throws Throwable {
    configureInterruptCycle(false);
    // The state-lock instance.
    StateLockManager stateLockManager = new StateLockManager();
    // Start a thread that owns the state-lock in shared mode.
    StateLockingThread sharedHolderThread = new StateLockingThread(stateLockManager, false);
    sharedHolderThread.start();
    sharedHolderThread.waitUntilStateLockAcquired();
    // Expect timeout when the lock is held in shared mode.
    mExpected.expect(TimeoutException.class);
    stateLockManager.lockExclusive(new StateLockOptions(StateLockOptions.GraceMode.TIMEOUT, 10, 0, 100));
    // Exit the shared holder.
    sharedHolderThread.unlockExit();
    sharedHolderThread.join();
    // Create an exclusive owner of the state-lock.
    StateLockingThread exclusiveHolderThread = new StateLockingThread(stateLockManager, true);
    exclusiveHolderThread.start();
    exclusiveHolderThread.waitUntilStateLockAcquired();
    // Expect timeout when the lock is held in exclusive mode.
    mExpected.expect(TimeoutException.class);
    stateLockManager.lockExclusive(new StateLockOptions(StateLockOptions.GraceMode.TIMEOUT, 10, 0, 100));
    // Exit the exclusive holder.
    exclusiveHolderThread.unlockExit();
    exclusiveHolderThread.join();
    // Now the lock can be acquired within the grace-cycle.
    try (LockResource lr = stateLockManager.lockExclusive(new StateLockOptions(StateLockOptions.GraceMode.TIMEOUT, 10, 0, 100))) {
    // Acquired within the grace-cycle with no active holder.
    }
}
Also used : LockResource(alluxio.resource.LockResource) Test(org.junit.Test)

Example 80 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BackupLeaderRole method scheduleLocalBackup.

/**
 * Schedule backup on this master.
 */
private void scheduleLocalBackup(BackupPRequest request, StateLockOptions stateLockOptions) {
    LOG.info("Scheduling backup at the backup-leader.");
    mLocalBackupFuture = mExecutorService.submit(() -> {
        try (LockResource stateLockResource = mStateLockManager.lockExclusive(stateLockOptions)) {
            mBackupTracker.updateState(BackupState.Running);
            AlluxioURI backupUri = takeBackup(request, mBackupTracker.getEntryCounter());
            mBackupTracker.updateBackupUri(backupUri);
            mBackupTracker.updateState(BackupState.Completed);
        } catch (Exception e) {
            LOG.error("Local backup failed at the backup-leader.", e);
            mBackupTracker.updateError(new BackupException(String.format("Local backup failed: %s", e.getMessage()), e));
        }
    });
}
Also used : BackupException(alluxio.exception.BackupException) LockResource(alluxio.resource.LockResource) BackupAbortedException(alluxio.exception.BackupAbortedException) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) BackupException(alluxio.exception.BackupException) BackupDelegationException(alluxio.exception.BackupDelegationException) AlluxioURI(alluxio.AlluxioURI)

Aggregations

LockResource (alluxio.resource.LockResource)116 IOException (java.io.IOException)14 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)13 HashMap (java.util.HashMap)12 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)11 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)11 Map (java.util.Map)11 AlluxioURI (alluxio.AlluxioURI)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)10 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)9 ArrayList (java.util.ArrayList)9 MasterWorkerInfo (alluxio.master.block.meta.MasterWorkerInfo)8 InvalidPathException (alluxio.exception.InvalidPathException)7 NotFoundException (alluxio.exception.status.NotFoundException)7 List (java.util.List)7 Lock (java.util.concurrent.locks.Lock)7 ConcurrentHashSet (alluxio.collections.ConcurrentHashSet)6 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)6 UnavailableException (alluxio.exception.status.UnavailableException)6