Skip to content

Commit

Permalink
Issue #3745: Add mountExactPath to AbstractDataStorage to be able to …
Browse files Browse the repository at this point in the history
…choose mount strategy for storage, if mountExactPath is true, will try to mount storage by exact path and not by fileShareMount path (#3746)
  • Loading branch information
SilinPavel authored Oct 15, 2024
1 parent d0e3f8b commit 8345348
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class DataStorageVO {
private List<String> allowedCidrs;
private Long regionId;
private Long fileShareMountId;
private boolean mountExactPath;
private boolean sensitive;
private List<ToolFingerprint> toolsToMount;
private Boolean mountDisabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public enum DataStorageParameters {
// NFS specific fields
MOUNT_OPTIONS,
FILE_SHARE_MOUNT_ID,
MOUNT_EXACT_PATH,

// cloud specific fields
REGION_ID,
Expand Down Expand Up @@ -532,6 +533,7 @@ static MapSqlParameterSource getParameters(final AbstractDataStorage dataStorage
params.addValue(SHARED.name(), dataStorage.isShared());
params.addValue(MOUNT_OPTIONS.name(), dataStorage.getMountOptions());
params.addValue(FILE_SHARE_MOUNT_ID.name(), dataStorage.getFileShareMountId());
params.addValue(MOUNT_EXACT_PATH.name(), dataStorage.isMountExactPath());
params.addValue(SENSITIVE.name(), dataStorage.isSensitive());
params.addValue(MOUNT_DISABLED.name(), dataStorage.isMountDisabled());

Expand Down Expand Up @@ -636,6 +638,7 @@ private static AbstractDataStorage parseDataStorage(ResultSet rs) throws SQLExce
allowedCidrs,
regionId,
fileShareMountId,
rs.getBoolean(MOUNT_EXACT_PATH.name()),
rs.getString(S3_KMS_KEY_ARN.name()),
rs.getString(S3_TEMP_CREDS_ROLE.name()),
rs.getBoolean(S3_USE_ASSUMED_CREDS.name()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ enum FolderParameters {
DATASTORAGE_ALLOWED_CIDRS,
DATASTORAGE_REGION_ID,
DATASTORAGE_FILE_SHARE_MOUNT_ID,
DATASTORAGE_MOUNT_EXACT_PATH,
DATASTORAGE_SENSITIVE,
DATASTORAGE_MOUNT_DISABLED,
DATASTORAGE_S3_KMS_KEY_ARN,
Expand Down Expand Up @@ -307,6 +308,7 @@ static ResultSetExtractor<Collection<Folder>> getFolderExtractor(boolean withMet
allowedCidrs,
regionId,
fileShareMountId,
rs.getBoolean(DATASTORAGE_MOUNT_EXACT_PATH.name()),
rs.getString(DATASTORAGE_S3_KMS_KEY_ARN.name()),
rs.getString(DATASTORAGE_S3_TEMP_CREDS_ROLE.name()),
rs.getBoolean(DATASTORAGE_S3_USE_ASSUMED_CREDS.name()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static AbstractDataStorageFactory getDefaultDataStorageFactory() {
public abstract AbstractDataStorage convertToDataStorage(
Long id, String name, String path, DataStorageType type,
StoragePolicy policy, String mountOptions, String mountPoint,
List<String> allowedCidrs, Long regionId, Long fileShareMountId,
List<String> allowedCidrs, Long regionId, Long fileShareMountId, boolean mountExactPath,
String kmsKey, String tempRole, boolean useAssumedCreds, String mountStatus,
Set<String> masks, Long sourceStorageId);

Expand All @@ -47,7 +47,7 @@ public AbstractDataStorage convertToDataStorage(DataStorageVO vo, final CloudPro
AbstractDataStorage storage =
convertToDataStorage(vo.getId(), vo.getName(), vo.getPath(), type, vo.getStoragePolicy(),
vo.getMountOptions(), vo.getMountPoint(), vo.getAllowedCidrs(),
vo.getRegionId(), vo.getFileShareMountId(),
vo.getRegionId(), vo.getFileShareMountId(), vo.isMountExactPath(),
vo.getKmsKeyArn(), vo.getTempCredentialsRole(), vo.isUseAssumedCredentials(),
NFSStorageMountStatus.ACTIVE.name(), vo.getLinkingMasks(), vo.getSourceStorageId());
storage.setDescription(vo.getDescription());
Expand Down Expand Up @@ -77,6 +77,7 @@ public AbstractDataStorage convertToDataStorage(final Long id, final String name
final StoragePolicy policy, final String mountOptions,
final String mountPoint, final List<String> allowedCidrs,
final Long regionId, final Long fileShareMountId,
final boolean mountExactPath,
final String kmsKey, final String tempRole,
final boolean useAssumedCreds,
final String mountStatus,
Expand All @@ -97,6 +98,7 @@ public AbstractDataStorage convertToDataStorage(final Long id, final String name
NFSDataStorage storage = new NFSDataStorage(id, name, path, policy, mountPoint);
storage.setMountOptions(mountOptions);
storage.setFileShareMountId(fileShareMountId);
storage.setMountExactPath(mountExactPath);
storage.setMountStatus(NFSStorageMountStatus.fromName(mountStatus));
resultStorage = storage;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ public static String getNfsRootPath(final String path) {
}

public static String normalizeMountPath(final MountType mountType, final String mountPath) {
return normalizeMountPath(mountType, mountPath, false);
}

public static String normalizeMountPath(final MountType mountType, final String mountPath, final boolean flat) {
return MountType.LUSTRE == mountType
? normalizeLustrePath(mountPath)
: normalizePath(mountPath);
? normalizeLustrePath(mountPath, flat)
: normalizePath(mountPath, flat);
}

public static List<String> findIpAddresses(final FileShareMount fileShareMount) {
Expand Down Expand Up @@ -179,12 +183,20 @@ static void deleteFolderIfEmpty(final File folder) throws IOException {
}
}

private static String normalizePath(final String nfsPath) {
return nfsPath.replace(":", "/");
private static String normalizePath(final String nfsPath, boolean flat) {
if (flat) {
return nfsPath.replace(":", PATH_SEPARATOR)
.replace(PATH_SEPARATOR, "_");
}
return nfsPath.replace(":", PATH_SEPARATOR);
}

private static String normalizeLustrePath(final String nfsPath) {
return nfsPath.replaceAll(":/", "/").replace(":", LUSTRE_MOUNTS_DELIMITER);
private static String normalizeLustrePath(final String nfsPath, boolean flat) {
if (flat) {
return nfsPath.replaceAll(NFS_HOST_DELIMITER, PATH_SEPARATOR).replace(":", LUSTRE_MOUNTS_DELIMITER)
.replace(PATH_SEPARATOR, "_");
}
return nfsPath.replaceAll(NFS_HOST_DELIMITER, PATH_SEPARATOR).replace(":", LUSTRE_MOUNTS_DELIMITER);
}

private static List<String> determineIpAddresses(final String mountRoot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -70,8 +71,7 @@ public NFSStorageMounter(final MessageHelper messageHelper,
public synchronized File mount(final NFSDataStorage dataStorage) {
try {
final FileShareMount fileShareMount = shareMountManager.load(dataStorage.getFileShareMountId());
final File mntDir = getStorageMountRoot(dataStorage, fileShareMount);
final File rootMount = getShareRootMount(fileShareMount);
final File rootMount = getShareRootMount(dataStorage, fileShareMount);
if (!rootMount.exists()) {
Assert.isTrue(rootMount.mkdirs(), messageHelper.getMessage(
MessageConstants.ERROR_DATASTORAGE_NFS_MOUNT_DIRECTORY_NOT_CREATED));
Expand All @@ -87,7 +87,10 @@ public synchronized File mount(final NFSDataStorage dataStorage) {
final String mountOptions = NFSHelper.getNFSMountOption(cloudRegion, credentials,
defaultMountOptions, protocol);

final String rootNfsPath = formatNfsPath(fileShareMount.getMountRoot(), protocol);
final String rootNfsPath = formatNfsPath(
dataStorage.isMountExactPath() ? dataStorage.getPath() : fileShareMount.getMountRoot(),
protocol
);

final String mountCmd = String.format(NFS_MOUNT_CMD_PATTERN, protocol, mountOptions,
rootNfsPath, rootMount.getAbsolutePath());
Expand All @@ -102,20 +105,21 @@ public synchronized File mount(final NFSDataStorage dataStorage) {
dataStorage.getPath()), e);
}
}
String storageName = getStorageName(dataStorage.getPath());
return new File(mntDir, storageName);
return getStorageMountPath(dataStorage, fileShareMount);
} catch (IOException e) {
throw new DataStorageException(messageHelper.getMessage(
messageHelper.getMessage(MessageConstants.ERROR_DATASTORAGE_NFS_MOUNT, dataStorage.getName(),
dataStorage.getPath())), e);
}
}

public synchronized void unmountNFSIfEmpty(AbstractDataStorage storage) {
public synchronized void unmountNFSIfEmpty(NFSDataStorage storage) {
final FileShareMount fileShareMount = shareMountManager.load(storage.getFileShareMountId());
final File rootMount = getShareRootMount(fileShareMount);
final List<AbstractDataStorage> remaining = dataStorageDao.loadDataStoragesByFileShareMountID(
storage.getFileShareMountId());
final File rootMount = getShareRootMount(storage, fileShareMount);
// if mount exact path, storage will be mounted to the unique dir
final List<AbstractDataStorage> remaining = storage.isMountExactPath()
? Collections.singletonList(storage)
: dataStorageDao.loadDataStoragesByFileShareMountID(storage.getFileShareMountId());
LOGGER.debug("Remaining NFS: " + remaining.stream().map(AbstractDataStorage::getPath)
.collect(Collectors.joining(";")) + " related with current file share mount");

Expand Down Expand Up @@ -143,14 +147,27 @@ public void chown(final File file, final PipelineUser user, final Integer seed,
}
}

private File getStorageMountRoot(final NFSDataStorage dataStorage, final FileShareMount fileShareMount) {
final String storageMountPath = normalizeMountPath(fileShareMount.getMountType(),
getNfsRootPath(dataStorage.getPath()));
return Paths.get(rootMountPoint, storageMountPath).toFile();
private File getStorageMountPath(final NFSDataStorage storage, final FileShareMount fileShareMount) {
if (storage.isMountExactPath()) {
final String flatStorageMountPAth =
normalizeMountPath(fileShareMount.getMountType(), storage.getPath(), true);
return Paths.get(rootMountPoint, flatStorageMountPAth).toFile();
} else {
final String storageMountPath = normalizeMountPath(fileShareMount.getMountType(),
getNfsRootPath(storage.getPath()));
return Paths.get(rootMountPoint, storageMountPath, getStorageName(storage.getPath())).toFile();
}
}

private File getShareRootMount(final FileShareMount fileShareMount) {
final String shareMountPath = normalizeMountPath(fileShareMount.getMountType(), fileShareMount.getMountRoot());
private File getShareRootMount(final NFSDataStorage storage, final FileShareMount fileShareMount) {
final String shareMountPath;
if (storage.isMountExactPath()) {
shareMountPath = normalizeMountPath(
fileShareMount.getMountType(), storage.getPath(), true);
} else {
shareMountPath = normalizeMountPath(
fileShareMount.getMountType(), fileShareMount.getMountRoot());
}
return Paths.get(rootMountPoint, shareMountPath).toFile();
}

Expand Down
17 changes: 17 additions & 0 deletions api/src/main/resources/dao/datastorage-dao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
allowed_cidrs,
region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -75,6 +76,7 @@
:ALLOWED_CIDRS,
:REGION_ID,
:FILE_SHARE_MOUNT_ID,
:MOUNT_EXACT_PATH,
:SENSITIVE,
:MOUNT_DISABLED,
:S3_KMS_KEY_ARN,
Expand Down Expand Up @@ -140,6 +142,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -179,6 +182,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -220,6 +224,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -261,6 +266,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -301,6 +307,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -341,6 +348,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -381,6 +389,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -421,6 +430,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -462,6 +472,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -502,6 +513,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down Expand Up @@ -566,6 +578,7 @@
d.s3_temp_creds_role,
d.mount_status,
d.source_datastorage_id,
d.mount_exact_path,
d.masking_rules,
c.folder_id,
c.parent_id AS parent_folder_id
Expand Down Expand Up @@ -598,6 +611,7 @@
null as allowed_cidrs,
null as region_id,
null as file_share_mount_id,
null as mount_exact_path,
null as sensitive,
null as mount_disabled,
null as s3_kms_key_arn,
Expand Down Expand Up @@ -639,6 +653,7 @@
d.allowed_cidrs,
d.region_id as region_id,
d.file_share_mount_id,
d.mount_exact_path,
d.sensitive,
d.mount_disabled,
d.s3_kms_key_arn,
Expand Down Expand Up @@ -674,6 +689,7 @@
null as allowed_cidrs,
null as region_id,
null as file_share_mount_id,
null as mount_exact_path,
null as sensitive,
null as mount_disabled,
null as s3_kms_key_arn,
Expand Down Expand Up @@ -812,6 +828,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
mount_disabled,
s3_kms_key_arn,
Expand Down
Loading

0 comments on commit 8345348

Please sign in to comment.