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 committed Oct 15, 2024
1 parent 4ad0415 commit 0c0fe21
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class DataStorageVO {
private List<String> allowedCidrs;
private Long regionId;
private Long fileShareMountId;
private boolean mountExactPath;
private boolean sensitive;
private List<ToolFingerprint> toolsToMount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,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 @@ -490,6 +491,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());

if (dataStorage instanceof AbstractAWSDataStorage) {
Expand Down Expand Up @@ -584,6 +586,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 @@ -187,6 +187,7 @@ enum FolderParameters {
DATASTORAGE_ALLOWED_CIDRS,
DATASTORAGE_REGION_ID,
DATASTORAGE_FILE_SHARE_MOUNT_ID,
DATASTORAGE_MOUNT_EXACT_PATH,
DATASTORAGE_SENSITIVE,
DATASTORAGE_S3_KMS_KEY_ARN,
DATASTORAGE_S3_USE_ASSUMED_CREDS,
Expand Down Expand Up @@ -304,6 +305,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,15 +38,15 @@ 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);

public AbstractDataStorage convertToDataStorage(DataStorageVO vo, final CloudProvider provider) {
DataStorageType type = determineStorageType(vo, provider);
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());
storage.setDescription(vo.getDescription());
Expand Down Expand Up @@ -75,6 +75,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 @@ -91,6 +92,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));
return storage;
case AZ:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public final class NFSHelper {
private static final String SMB_SCHEME = "//";
private static final String PATH_SEPARATOR = "/";
private static final String NFS_HOST_DELIMITER = ":/";
private static final String LUSTRE_MOUNTS_DELIMITER = "_";

private NFSHelper() {

Expand Down Expand Up @@ -100,6 +101,16 @@ 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, flat)
: normalizePath(mountPath, flat);
}

static String getNFSMountOption(final AbstractCloudRegion cloudRegion,
final AbstractCloudRegionCredentials credentials,
final String defaultOptions, final String protocol) {
Expand Down Expand Up @@ -142,4 +153,20 @@ static void deleteFolderIfEmpty(final File folder) throws IOException {
FileUtils.deleteDirectory(folder);
}
}

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, 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.epam.pipeline.entity.datastorage.AbstractDataStorage;
import com.epam.pipeline.entity.datastorage.DataStorageException;
import com.epam.pipeline.entity.datastorage.FileShareMount;
import com.epam.pipeline.entity.datastorage.MountType;
import com.epam.pipeline.entity.datastorage.nfs.NFSDataStorage;
import com.epam.pipeline.entity.region.AbstractCloudRegion;
import com.epam.pipeline.entity.region.AbstractCloudRegionCredentials;
Expand All @@ -25,11 +24,13 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.epam.pipeline.manager.datastorage.providers.nfs.NFSHelper.formatNfsPath;
import static com.epam.pipeline.manager.datastorage.providers.nfs.NFSHelper.getNfsRootPath;
import static com.epam.pipeline.manager.datastorage.providers.nfs.NFSHelper.normalizeMountPath;

@Service
public class NFSStorageMounter {
Expand Down Expand Up @@ -66,8 +67,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 @@ -80,7 +80,10 @@ public synchronized File mount(final NFSDataStorage dataStorage) {
final String mountOptions = NFSHelper.getNFSMountOption(cloudRegion, credentials,
dataStorage.getMountOptions(), 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 @@ -95,20 +98,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 All @@ -123,17 +127,27 @@ public synchronized void unmountNFSIfEmpty(AbstractDataStorage storage) {
}
}

private File getStorageMountRoot(final NFSDataStorage dataStorage, final FileShareMount fileShareMount) {
final String storageMountPath = MountType.LUSTRE == fileShareMount.getMountType()
? normalizeLustrePath(getNfsRootPath(dataStorage.getPath()))
: normalizePath(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 = MountType.LUSTRE == fileShareMount.getMountType()
? normalizeLustrePath(fileShareMount.getMountRoot())
: normalizePath(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 @@ -44,6 +44,7 @@
allowed_cidrs,
region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand All @@ -70,6 +71,7 @@
:ALLOWED_CIDRS,
:REGION_ID,
:FILE_SHARE_MOUNT_ID,
:MOUNT_EXACT_PATH,
:SENSITIVE,
:S3_KMS_KEY_ARN,
:S3_USE_ASSUMED_CREDS,
Expand Down Expand Up @@ -130,6 +132,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -165,6 +168,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -202,6 +206,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 @@ -240,6 +245,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -276,6 +282,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -312,6 +319,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -348,6 +356,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -384,6 +393,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -421,6 +431,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -457,6 +468,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down Expand Up @@ -515,6 +527,7 @@
d.s3_use_assumed_creds,
d.s3_temp_creds_role,
d.mount_status,
d.mount_exact_path,
c.folder_id,
c.parent_id AS parent_folder_id
FROM pipeline.datastorage d
Expand Down Expand Up @@ -545,6 +558,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 s3_kms_key_arn,
null as s3_use_assumed_creds,
Expand Down Expand Up @@ -582,6 +596,7 @@
d.allowed_cidrs,
d.region_id as region_id,
d.file_share_mount_id,
d.mount_exact_path,
d.sensitive,
d.s3_kms_key_arn,
d.s3_use_assumed_creds,
Expand Down Expand Up @@ -613,6 +628,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 s3_kms_key_arn,
null as s3_use_assumed_creds,
Expand Down Expand Up @@ -728,6 +744,7 @@
allowed_cidrs,
region_id as region_id,
file_share_mount_id,
mount_exact_path,
sensitive,
s3_kms_key_arn,
s3_use_assumed_creds,
Expand Down
Loading

0 comments on commit 0c0fe21

Please sign in to comment.