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