Search in sources :

Example 26 with OmDirectoryInfo

use of org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo in project ozone by apache.

the class OMFileRequest method checkSubDirectoryExists.

private static boolean checkSubDirectoryExists(OmKeyInfo omKeyInfo, OMMetadataManager metaMgr) throws IOException {
    // Check all dirTable cache for any sub paths.
    Table dirTable = metaMgr.getDirectoryTable();
    Iterator<Map.Entry<CacheKey<String>, CacheValue<OmDirectoryInfo>>> cacheIter = dirTable.cacheIterator();
    while (cacheIter.hasNext()) {
        Map.Entry<CacheKey<String>, CacheValue<OmDirectoryInfo>> entry = cacheIter.next();
        OmDirectoryInfo cacheOmDirInfo = entry.getValue().getCacheValue();
        if (cacheOmDirInfo == null) {
            continue;
        }
        if (isImmediateChild(cacheOmDirInfo.getParentObjectID(), omKeyInfo.getObjectID())) {
            // found a sub path directory
            return true;
        }
    }
    // Check dirTable entries for any sub paths.
    String seekDirInDB = metaMgr.getOzonePathKey(omKeyInfo.getObjectID(), "");
    TableIterator<String, ? extends Table.KeyValue<String, OmDirectoryInfo>> iterator = dirTable.iterator();
    iterator.seek(seekDirInDB);
    if (iterator.hasNext()) {
        Table.KeyValue<String, OmDirectoryInfo> entry = iterator.next();
        OmDirectoryInfo dirInfo = entry.getValue();
        return isImmediateChild(dirInfo.getParentObjectID(), omKeyInfo.getObjectID());
    }
    // no sub paths found
    return false;
}
Also used : Table(org.apache.hadoop.hdds.utils.db.Table) CacheValue(org.apache.hadoop.hdds.utils.db.cache.CacheValue) OmDirectoryInfo(org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo) Map(java.util.Map) CacheKey(org.apache.hadoop.hdds.utils.db.cache.CacheKey)

Example 27 with OmDirectoryInfo

use of org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo in project ozone by apache.

the class OMFileRequest method getParentID.

/**
 * Get parent id for the user given path.
 *
 * @param bucketId       bucket id
 * @param pathComponents fie path elements
 * @param keyName        user given key name
 * @param omMetadataManager   om metadata manager
 * @return lastKnownParentID
 * @throws IOException DB failure or parent not exists in DirectoryTable
 */
public static long getParentID(long bucketId, Iterator<Path> pathComponents, String keyName, OMMetadataManager omMetadataManager, String errMsg) throws IOException {
    long lastKnownParentId = bucketId;
    // If no sub-dirs then bucketID is the root/parent.
    if (!pathComponents.hasNext()) {
        return bucketId;
    }
    if (StringUtils.isBlank(errMsg)) {
        errMsg = "Failed to find parent directory of " + keyName;
    }
    OmDirectoryInfo omDirectoryInfo;
    while (pathComponents.hasNext()) {
        String nodeName = pathComponents.next().toString();
        boolean reachedLastPathComponent = !pathComponents.hasNext();
        String dbNodeName = omMetadataManager.getOzonePathKey(lastKnownParentId, nodeName);
        omDirectoryInfo = omMetadataManager.getDirectoryTable().get(dbNodeName);
        if (omDirectoryInfo != null) {
            if (reachedLastPathComponent) {
                throw new OMException("Can not create file: " + keyName + " as there is already directory in the given path", NOT_A_FILE);
            }
            lastKnownParentId = omDirectoryInfo.getObjectID();
        } else {
            // exists for committing the key, otherwise will fail the operation.
            if (!reachedLastPathComponent) {
                throw new OMException(errMsg, OMException.ResultCodes.DIRECTORY_NOT_FOUND);
            }
            break;
        }
    }
    return lastKnownParentId;
}
Also used : OmDirectoryInfo(org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo) OMException(org.apache.hadoop.ozone.om.exceptions.OMException)

Example 28 with OmDirectoryInfo

use of org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo in project ozone by apache.

the class OMKeyAclRequestWithFSO method validateAndUpdateCache.

@Override
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
    OmKeyInfo omKeyInfo = null;
    OzoneManagerProtocolProtos.OMResponse.Builder omResponse = onInit();
    OMClientResponse omClientResponse = null;
    IOException exception = null;
    OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
    boolean lockAcquired = false;
    String volume = null;
    String bucket = null;
    String key = null;
    boolean operationResult = false;
    Result result = null;
    try {
        ObjectParser objectParser = new ObjectParser(getPath(), OzoneManagerProtocolProtos.OzoneObj.ObjectType.KEY);
        volume = objectParser.getVolume();
        bucket = objectParser.getBucket();
        key = objectParser.getKey();
        // check Acl
        if (ozoneManager.getAclsEnabled()) {
            checkAcls(ozoneManager, OzoneObj.ResourceType.VOLUME, OzoneObj.StoreType.OZONE, IAccessAuthorizer.ACLType.WRITE_ACL, volume, bucket, key);
        }
        lockAcquired = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volume, bucket);
        OzoneFileStatus keyStatus = OMFileRequest.getOMKeyInfoIfExists(omMetadataManager, volume, bucket, key, 0);
        if (keyStatus == null) {
            throw new OMException("Key not found. Key:" + key, KEY_NOT_FOUND);
        }
        omKeyInfo = keyStatus.getKeyInfo();
        String dbKey = omKeyInfo.getPath();
        boolean isDirectory = keyStatus.isDirectory();
        operationResult = apply(omKeyInfo, trxnLogIndex);
        omKeyInfo.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
        // Update the modification time when updating ACLs of Key.
        long modificationTime = omKeyInfo.getModificationTime();
        if (getOmRequest().getAddAclRequest().hasObj() && operationResult) {
            modificationTime = getOmRequest().getAddAclRequest().getModificationTime();
        } else if (getOmRequest().getSetAclRequest().hasObj()) {
            modificationTime = getOmRequest().getSetAclRequest().getModificationTime();
        } else if (getOmRequest().getRemoveAclRequest().hasObj() && operationResult) {
            modificationTime = getOmRequest().getRemoveAclRequest().getModificationTime();
        }
        omKeyInfo.setModificationTime(modificationTime);
        // update cache.
        if (isDirectory) {
            Table<String, OmDirectoryInfo> dirTable = omMetadataManager.getDirectoryTable();
            dirTable.addCacheEntry(new CacheKey<>(dbKey), new CacheValue<>(Optional.of(OMFileRequest.getDirectoryInfo(omKeyInfo)), trxnLogIndex));
        } else {
            omMetadataManager.getKeyTable(getBucketLayout()).addCacheEntry(new CacheKey<>(dbKey), new CacheValue<>(Optional.of(omKeyInfo), trxnLogIndex));
        }
        omClientResponse = onSuccess(omResponse, omKeyInfo, operationResult, isDirectory);
        result = Result.SUCCESS;
    } catch (IOException ex) {
        result = Result.FAILURE;
        exception = ex;
        omClientResponse = onFailure(omResponse, ex);
    } finally {
        addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
        if (lockAcquired) {
            omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volume, bucket);
        }
    }
    OzoneObj obj = getObject();
    Map<String, String> auditMap = obj.toAuditMap();
    onComplete(result, operationResult, exception, trxnLogIndex, ozoneManager.getAuditLogger(), auditMap);
    return omClientResponse;
}
Also used : ObjectParser(org.apache.hadoop.ozone.om.request.util.ObjectParser) OzoneObj(org.apache.hadoop.ozone.security.acl.OzoneObj) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) IOException(java.io.IOException) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OmDirectoryInfo(org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo) OzoneFileStatus(org.apache.hadoop.ozone.om.helpers.OzoneFileStatus) OMException(org.apache.hadoop.ozone.om.exceptions.OMException)

Example 29 with OmDirectoryInfo

use of org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo in project ozone by apache.

the class OMKeyRenameRequestWithFSO method renameKey.

@SuppressWarnings("parameternumber")
private OMClientResponse renameKey(long toKeyParentId, long trxnLogIndex, OmKeyInfo fromKeyValue, boolean isRenameDirectory, String toKeyName, long modificationTime, OMResponse.Builder omResponse, OzoneManager ozoneManager) {
    String dbFromKey = fromKeyValue.getPath();
    String toKeyFileName = OzoneFSUtils.getFileName(toKeyName);
    fromKeyValue.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
    // Set toFileName
    fromKeyValue.setKeyName(toKeyFileName);
    fromKeyValue.setFileName(toKeyFileName);
    // Set toKeyObjectId
    fromKeyValue.setParentObjectID(toKeyParentId);
    // Set modification time
    fromKeyValue.setModificationTime(modificationTime);
    // destination dbKeyName
    String dbToKey = fromKeyValue.getPath();
    // Add to cache.
    // dbFromKey should be deleted, dbToKey should be added with newly updated
    // omKeyInfo.
    // Add from_key and to_key details into cache.
    OMMetadataManager metadataMgr = ozoneManager.getMetadataManager();
    if (isRenameDirectory) {
        Table<String, OmDirectoryInfo> dirTable = metadataMgr.getDirectoryTable();
        dirTable.addCacheEntry(new CacheKey<>(dbFromKey), new CacheValue<>(Optional.absent(), trxnLogIndex));
        dirTable.addCacheEntry(new CacheKey<>(dbToKey), new CacheValue<>(Optional.of(OMFileRequest.getDirectoryInfo(fromKeyValue)), trxnLogIndex));
    } else {
        Table<String, OmKeyInfo> keyTable = metadataMgr.getKeyTable(getBucketLayout());
        keyTable.addCacheEntry(new CacheKey<>(dbFromKey), new CacheValue<>(Optional.absent(), trxnLogIndex));
        keyTable.addCacheEntry(new CacheKey<>(dbToKey), new CacheValue<>(Optional.of(fromKeyValue), trxnLogIndex));
    }
    OMClientResponse omClientResponse = new OMKeyRenameResponseWithFSO(omResponse.setRenameKeyResponse(RenameKeyResponse.newBuilder()).build(), dbFromKey, dbToKey, fromKeyValue, isRenameDirectory, getBucketLayout());
    return omClientResponse;
}
Also used : OMKeyRenameResponseWithFSO(org.apache.hadoop.ozone.om.response.key.OMKeyRenameResponseWithFSO) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) OmDirectoryInfo(org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo)

Example 30 with OmDirectoryInfo

use of org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo in project ozone by apache.

the class OMDirectoryCreateRequestWithFSO method validateAndUpdateCache.

@Override
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
    CreateDirectoryRequest createDirectoryRequest = getOmRequest().getCreateDirectoryRequest();
    KeyArgs keyArgs = createDirectoryRequest.getKeyArgs();
    String volumeName = keyArgs.getVolumeName();
    String bucketName = keyArgs.getBucketName();
    String keyName = keyArgs.getKeyName();
    int numKeysCreated = 0;
    OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
    omResponse.setCreateDirectoryResponse(CreateDirectoryResponse.newBuilder());
    OMMetrics omMetrics = ozoneManager.getMetrics();
    omMetrics.incNumCreateDirectory();
    AuditLogger auditLogger = ozoneManager.getAuditLogger();
    OzoneManagerProtocolProtos.UserInfo userInfo = getOmRequest().getUserInfo();
    Map<String, String> auditMap = buildKeyArgsAuditMap(keyArgs);
    OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
    boolean acquiredLock = false;
    IOException exception = null;
    OMClientResponse omClientResponse = null;
    Result result = Result.FAILURE;
    List<OmDirectoryInfo> missingParentInfos;
    try {
        keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
        volumeName = keyArgs.getVolumeName();
        bucketName = keyArgs.getBucketName();
        // check Acl
        checkKeyAcls(ozoneManager, volumeName, bucketName, keyName, IAccessAuthorizer.ACLType.CREATE, OzoneObj.ResourceType.KEY);
        // Check if this is the root of the filesystem.
        if (keyName.length() == 0) {
            throw new OMException("Directory create failed. Cannot create " + "directory at root of the filesystem", OMException.ResultCodes.CANNOT_CREATE_DIRECTORY_AT_ROOT);
        }
        // acquire lock
        acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
        validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
        Path keyPath = Paths.get(keyName);
        // Need to check if any files exist in the given path, if they exist we
        // cannot create a directory with the given key.
        // Verify the path against directory table
        OMFileRequest.OMPathInfoWithFSO omPathInfo = OMFileRequest.verifyDirectoryKeysInPath(omMetadataManager, volumeName, bucketName, keyName, keyPath);
        OMFileRequest.OMDirectoryResult omDirectoryResult = omPathInfo.getDirectoryResult();
        if (omDirectoryResult == FILE_EXISTS || omDirectoryResult == FILE_EXISTS_IN_GIVENPATH) {
            throw new OMException("Unable to create directory: " + keyName + " in volume/bucket: " + volumeName + "/" + bucketName + " as " + "file:" + omPathInfo.getFileExistsInPath() + " already exists", FILE_ALREADY_EXISTS);
        } else if (omDirectoryResult == DIRECTORY_EXISTS_IN_GIVENPATH || omDirectoryResult == NONE) {
            // prepare all missing parents
            missingParentInfos = OMDirectoryCreateRequestWithFSO.getAllMissingParentDirInfo(ozoneManager, keyArgs, omPathInfo, trxnLogIndex);
            // prepare leafNode dir
            OmDirectoryInfo dirInfo = createDirectoryInfoWithACL(omPathInfo.getLeafNodeName(), keyArgs, omPathInfo.getLeafNodeObjectId(), omPathInfo.getLastKnownParentId(), trxnLogIndex, OzoneAclUtil.fromProtobuf(keyArgs.getAclsList()));
            OMFileRequest.addDirectoryTableCacheEntries(omMetadataManager, Optional.of(dirInfo), Optional.of(missingParentInfos), trxnLogIndex);
            // total number of keys created.
            numKeysCreated = missingParentInfos.size() + 1;
            result = OMDirectoryCreateRequest.Result.SUCCESS;
            omClientResponse = new OMDirectoryCreateResponseWithFSO(omResponse.build(), dirInfo, missingParentInfos, result, getBucketLayout());
        } else {
            result = Result.DIRECTORY_ALREADY_EXISTS;
            omResponse.setStatus(Status.DIRECTORY_ALREADY_EXISTS);
            omClientResponse = new OMDirectoryCreateResponseWithFSO(omResponse.build(), result);
        }
    } catch (IOException ex) {
        exception = ex;
        omClientResponse = new OMDirectoryCreateResponseWithFSO(createErrorOMResponse(omResponse, exception), result);
    } finally {
        addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
        if (acquiredLock) {
            omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
        }
    }
    auditLog(auditLogger, buildAuditMessage(OMAction.CREATE_DIRECTORY, auditMap, exception, userInfo));
    logResult(createDirectoryRequest, keyArgs, omMetrics, numKeysCreated, result, exception);
    return omClientResponse;
}
Also used : Path(java.nio.file.Path) AuditLogger(org.apache.hadoop.ozone.audit.AuditLogger) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) KeyArgs(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyArgs) IOException(java.io.IOException) CreateDirectoryRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.CreateDirectoryRequest) OMResponse(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse) OMMetrics(org.apache.hadoop.ozone.om.OMMetrics) OzoneManagerProtocolProtos(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos) OMDirectoryCreateResponseWithFSO(org.apache.hadoop.ozone.om.response.file.OMDirectoryCreateResponseWithFSO) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OmDirectoryInfo(org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo) OMException(org.apache.hadoop.ozone.om.exceptions.OMException)

Aggregations

OmDirectoryInfo (org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo)49 OmKeyInfo (org.apache.hadoop.ozone.om.helpers.OmKeyInfo)26 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)12 OmBucketInfo (org.apache.hadoop.ozone.om.helpers.OmBucketInfo)11 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)10 RepeatedOmKeyInfo (org.apache.hadoop.ozone.om.helpers.RepeatedOmKeyInfo)9 IOException (java.io.IOException)7 OMException (org.apache.hadoop.ozone.om.exceptions.OMException)7 OMMetadataManager (org.apache.hadoop.ozone.om.OMMetadataManager)6 Path (java.nio.file.Path)5 Path (org.apache.hadoop.fs.Path)4 Table (org.apache.hadoop.hdds.utils.db.Table)4 DirectoryDeletingService (org.apache.hadoop.ozone.om.DirectoryDeletingService)4 OMMetrics (org.apache.hadoop.ozone.om.OMMetrics)3 OzoneFileStatus (org.apache.hadoop.ozone.om.helpers.OzoneFileStatus)3 OMRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest)3 OMResponse (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse)3 Map (java.util.Map)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2