Search in sources :

Example 21 with LockResource

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

the class MountTable method getMountPoint.

/**
 * Returns the closest ancestor mount point the given path is nested under.
 *
 * @param uri an Alluxio path URI
 * @return mount point the given Alluxio path is nested under
 * @throws InvalidPathException if an invalid path is encountered
 */
public String getMountPoint(AlluxioURI uri) throws InvalidPathException {
    String path = uri.getPath();
    String lastMount = ROOT;
    try (LockResource r = new LockResource(mReadLock)) {
        for (Map.Entry<String, MountInfo> entry : mState.getMountTable().entrySet()) {
            String mount = entry.getKey();
            // of the current alluxioPath and the alluxioPath is a prefix of the path
            if (!mount.equals(ROOT) && PathUtils.hasPrefix(path, mount) && PathUtils.hasPrefix(mount, lastMount)) {
                lastMount = mount;
            }
        }
        return lastMount;
    }
}
Also used : LockResource(alluxio.resource.LockResource) MountInfo(alluxio.master.file.meta.options.MountInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with LockResource

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

the class MountTable method add.

/**
 * Mounts the given UFS path at the given Alluxio path. The Alluxio path should not be nested
 * under an existing mount point.
 *
 * @param journalContext the journal context
 * @param alluxioUri an Alluxio path URI
 * @param ufsUri a UFS path URI
 * @param mountId the mount id
 * @param options the mount options
 * @throws FileAlreadyExistsException if the mount point already exists
 * @throws InvalidPathException if an invalid path is encountered
 */
public void add(Supplier<JournalContext> journalContext, AlluxioURI alluxioUri, AlluxioURI ufsUri, long mountId, MountPOptions options) throws FileAlreadyExistsException, InvalidPathException {
    String alluxioPath = alluxioUri.getPath().isEmpty() ? "/" : alluxioUri.getPath();
    LOG.info("Mounting {} at {}", ufsUri, alluxioPath);
    try (LockResource r = new LockResource(mWriteLock)) {
        if (mState.getMountTable().containsKey(alluxioPath)) {
            throw new FileAlreadyExistsException(ExceptionMessage.MOUNT_POINT_ALREADY_EXISTS.getMessage(alluxioPath));
        }
        // or suffix of any existing mount path.
        for (Map.Entry<String, MountInfo> entry : mState.getMountTable().entrySet()) {
            AlluxioURI mountedUfsUri = entry.getValue().getUfsUri();
            if ((ufsUri.getScheme() == null || ufsUri.getScheme().equals(mountedUfsUri.getScheme())) && (ufsUri.getAuthority().toString().equals(mountedUfsUri.getAuthority().toString()))) {
                String ufsPath = ufsUri.getPath().isEmpty() ? "/" : ufsUri.getPath();
                String mountedUfsPath = mountedUfsUri.getPath().isEmpty() ? "/" : mountedUfsUri.getPath();
                if (PathUtils.hasPrefix(ufsPath, mountedUfsPath)) {
                    throw new InvalidPathException(ExceptionMessage.MOUNT_POINT_PREFIX_OF_ANOTHER.getMessage(mountedUfsUri.toString(), ufsUri.toString()));
                }
                if (PathUtils.hasPrefix(mountedUfsPath, ufsPath)) {
                    throw new InvalidPathException(ExceptionMessage.MOUNT_POINT_PREFIX_OF_ANOTHER.getMessage(ufsUri.toString(), mountedUfsUri.toString()));
                }
            }
        }
        Map<String, String> properties = options.getPropertiesMap();
        mState.applyAndJournal(journalContext, AddMountPointEntry.newBuilder().addAllProperties(properties.entrySet().stream().map(entry -> StringPairEntry.newBuilder().setKey(entry.getKey()).setValue(entry.getValue()).build()).collect(Collectors.toList())).setAlluxioPath(alluxioPath).setMountId(mountId).setReadOnly(options.getReadOnly()).setShared(options.getShared()).setUfsPath(ufsUri.toString()).build());
    }
}
Also used : CloseableIterator(alluxio.resource.CloseableIterator) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) DeleteMountPointEntry(alluxio.proto.journal.File.DeleteMountPointEntry) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) JournalEntry(alluxio.proto.journal.Journal.JournalEntry) PathUtils(alluxio.util.io.PathUtils) InvalidPathException(alluxio.exception.InvalidPathException) CloseableResource(alluxio.resource.CloseableResource) GrpcUtils(alluxio.grpc.GrpcUtils) AddMountPointEntry(alluxio.proto.journal.File.AddMountPointEntry) MountInfo(alluxio.master.file.meta.options.MountInfo) AlluxioURI(alluxio.AlluxioURI) Map(java.util.Map) DelegatingJournaled(alluxio.master.journal.DelegatingJournaled) MountPOptions(alluxio.grpc.MountPOptions) NoSuchElementException(java.util.NoSuchElementException) Nullable(javax.annotation.Nullable) Journaled(alluxio.master.journal.Journaled) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) IdUtils(alluxio.util.IdUtils) ExceptionMessage(alluxio.exception.ExceptionMessage) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) Throwables(com.google.common.base.Throwables) ThreadSafe(javax.annotation.concurrent.ThreadSafe) CheckpointName(alluxio.master.journal.checkpoint.CheckpointName) GuardedBy(javax.annotation.concurrent.GuardedBy) NotFoundException(alluxio.exception.status.NotFoundException) LockResource(alluxio.resource.LockResource) Collectors(java.util.stream.Collectors) AccessControlException(alluxio.exception.AccessControlException) File(alluxio.proto.journal.File) StringPairEntry(alluxio.proto.journal.File.StringPairEntry) List(java.util.List) Lock(java.util.concurrent.locks.Lock) UnderFileSystem(alluxio.underfs.UnderFileSystem) Journal(alluxio.proto.journal.Journal) UfsManager(alluxio.underfs.UfsManager) Collections(java.util.Collections) JournalContext(alluxio.master.journal.JournalContext) UnavailableException(alluxio.exception.status.UnavailableException) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) LockResource(alluxio.resource.LockResource) MountInfo(alluxio.master.file.meta.options.MountInfo) HashMap(java.util.HashMap) Map(java.util.Map) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Example 23 with LockResource

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

the class MountTable method checkUnderWritableMountPoint.

/**
 * Checks to see if a write operation is allowed for the specified Alluxio path, by determining
 * if it is under a readonly mount point.
 *
 * @param alluxioUri an Alluxio path URI
 * @throws InvalidPathException if the Alluxio path is invalid
 * @throws AccessControlException if the Alluxio path is under a readonly mount point
 */
public void checkUnderWritableMountPoint(AlluxioURI alluxioUri) throws InvalidPathException, AccessControlException {
    try (LockResource r = new LockResource(mReadLock)) {
        // This will re-acquire the read lock, but that is allowed.
        String mountPoint = getMountPoint(alluxioUri);
        MountInfo mountInfo = mState.getMountTable().get(mountPoint);
        if (mountInfo.getOptions().getReadOnly()) {
            throw new AccessControlException(ExceptionMessage.MOUNT_READONLY, alluxioUri, mountPoint);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) AccessControlException(alluxio.exception.AccessControlException) MountInfo(alluxio.master.file.meta.options.MountInfo)

Example 24 with LockResource

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

the class ActiveSyncManager method stopSyncAndJournal.

/**
 * Stop active sync on a URI and journal the remove entry.
 *
 * @param rpcContext the master rpc or no-op context
 * @param syncPoint sync point to be stopped
 */
public void stopSyncAndJournal(RpcContext rpcContext, AlluxioURI syncPoint) throws InvalidPathException {
    if (!isSyncPoint(syncPoint)) {
        throw new InvalidPathException(String.format("%s is not a sync point", syncPoint));
    }
    try (LockResource r = new LockResource(mLock)) {
        MountTable.Resolution resolution = mMountTable.resolve(syncPoint);
        LOG.debug("stop syncPoint {}", syncPoint.getPath());
        final long mountId = resolution.getMountId();
        RemoveSyncPointEntry removeSyncPoint = File.RemoveSyncPointEntry.newBuilder().setSyncpointPath(syncPoint.toString()).setMountId(mountId).build();
        applyAndJournal(rpcContext, removeSyncPoint);
        try {
            stopSyncInternal(syncPoint);
        } catch (Throwable e) {
            LOG.warn("Stop sync failed on {}", syncPoint, e);
            // revert state;
            AddSyncPointEntry addSyncPoint = File.AddSyncPointEntry.newBuilder().setSyncpointPath(syncPoint.toString()).build();
            applyAndJournal(rpcContext, addSyncPoint);
            recoverFromStopSync(syncPoint);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) AddSyncPointEntry(alluxio.proto.journal.File.AddSyncPointEntry) RemoveSyncPointEntry(alluxio.proto.journal.File.RemoveSyncPointEntry) MountTable(alluxio.master.file.meta.MountTable) InvalidPathException(alluxio.exception.InvalidPathException)

Example 25 with LockResource

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

the class ActiveSyncManager method startSyncAndJournal.

/**
 * Start active sync on a URI and journal the add entry.
 *
 * @param rpcContext the master rpc or no-op context
 * @param syncPoint sync point to be start
 */
public void startSyncAndJournal(RpcContext rpcContext, AlluxioURI syncPoint) throws InvalidPathException {
    try (LockResource r = new LockResource(mLock)) {
        MountTable.Resolution resolution = mMountTable.resolve(syncPoint);
        long mountId = resolution.getMountId();
        try (CloseableResource<UnderFileSystem> ufsResource = resolution.acquireUfsResource()) {
            if (!ufsResource.get().supportsActiveSync()) {
                throw new UnsupportedOperationException("Active Syncing is not supported on this UFS type: " + ufsResource.get().getUnderFSType());
            }
        }
        if (isUnderSyncPoint(syncPoint)) {
            throw new InvalidPathException("URI " + syncPoint + " is already a sync point");
        }
        AddSyncPointEntry addSyncPoint = AddSyncPointEntry.newBuilder().setSyncpointPath(syncPoint.toString()).setMountId(mountId).build();
        applyAndJournal(rpcContext, addSyncPoint);
        try {
            startSyncInternal(syncPoint, resolution);
        } catch (Throwable e) {
            LOG.warn("Start sync failed on {}", syncPoint, e);
            // revert state;
            RemoveSyncPointEntry removeSyncPoint = File.RemoveSyncPointEntry.newBuilder().setSyncpointPath(syncPoint.toString()).build();
            applyAndJournal(rpcContext, removeSyncPoint);
            recoverFromStartSync(syncPoint, resolution.getMountId());
            throw e;
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource) AddSyncPointEntry(alluxio.proto.journal.File.AddSyncPointEntry) RemoveSyncPointEntry(alluxio.proto.journal.File.RemoveSyncPointEntry) MountTable(alluxio.master.file.meta.MountTable) UnderFileSystem(alluxio.underfs.UnderFileSystem) InvalidPathException(alluxio.exception.InvalidPathException)

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