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;
}
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;
}
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);
}
}
}
}
}
}
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.
}
}
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));
}
});
}
Aggregations