Search in sources :

Example 6 with FileName

use of org.apache.crail.metadata.FileName in project incubator-crail by apache.

the class NameNodeService method renameFile.

@Override
public short renameFile(RpcRequestMessage.RenameFileReq request, RpcResponseMessage.RenameRes response, RpcNameNodeState errorState) throws Exception {
    // check protocol
    if (!RpcProtocol.verifyProtocol(RpcProtocol.CMD_RENAME_FILE, request, response)) {
        return RpcErrors.ERR_PROTOCOL_MISMATCH;
    }
    // get params
    FileName srcFileHash = request.getSrcFileName();
    FileName dstFileHash = request.getDstFileName();
    // rpc
    AbstractNode srcParent = fileTree.retrieveParent(srcFileHash, errorState);
    if (errorState.getError() != RpcErrors.ERR_OK) {
        return errorState.getError();
    }
    if (srcParent == null) {
        return RpcErrors.ERR_GET_FILE_FAILED;
    }
    AbstractNode srcFile = fileTree.retrieveFile(srcFileHash, errorState);
    if (errorState.getError() != RpcErrors.ERR_OK) {
        return errorState.getError();
    }
    if (srcFile == null) {
        return RpcErrors.ERR_SRC_FILE_NOT_FOUND;
    }
    // directory block
    int index = CrailUtils.computeIndex(srcFile.getDirOffset());
    NameNodeBlockInfo srcBlock = srcParent.getBlock(index);
    if (srcBlock == null) {
        return RpcErrors.ERR_GET_FILE_FAILED;
    }
    // end
    response.setSrcParent(srcParent);
    response.setSrcFile(srcFile);
    response.setSrcBlock(srcBlock);
    AbstractNode dstParent = fileTree.retrieveParent(dstFileHash, errorState);
    if (errorState.getError() != RpcErrors.ERR_OK) {
        return errorState.getError();
    }
    if (dstParent == null) {
        return RpcErrors.ERR_GET_FILE_FAILED;
    }
    AbstractNode dstFile = fileTree.retrieveFile(dstFileHash, errorState);
    if (dstFile != null && !dstFile.getType().isDirectory()) {
        return RpcErrors.ERR_FILE_EXISTS;
    }
    if (dstFile != null && dstFile.getType().isDirectory()) {
        dstParent = dstFile;
    }
    srcFile = srcParent.removeChild(srcFile.getComponent());
    if (srcFile == null) {
        return RpcErrors.ERR_SRC_FILE_NOT_FOUND;
    }
    srcFile.rename(dstFileHash.getFileComponent());
    try {
        AbstractNode oldNode = dstParent.putChild(srcFile);
        if (oldNode != null && oldNode.getFd() != srcFile.getFd()) {
            appendToDeleteQueue(oldNode);
        }
        dstFile = srcFile;
    } catch (Exception e) {
        return RpcErrors.ERR_FILE_EXISTS;
    }
    // directory block
    index = CrailUtils.computeIndex(srcFile.getDirOffset());
    NameNodeBlockInfo dstBlock = dstParent.getBlock(index);
    if (dstBlock == null) {
        dstBlock = blockStore.getBlock(dstParent.getStorageClass(), dstParent.getLocationClass());
        if (dstBlock == null) {
            return RpcErrors.ERR_NO_FREE_BLOCKS;
        }
        if (!dstParent.addBlock(index, dstBlock)) {
            blockStore.addBlock(dstBlock);
            dstBlock = dstParent.getBlock(index);
            if (dstBlock == null) {
                blockStore.addBlock(srcBlock);
                return RpcErrors.ERR_CREATE_FILE_FAILED;
            }
        }
    }
    dstParent.incCapacity(CrailConstants.DIRECTORY_RECORD);
    // end
    response.setDstParent(dstParent);
    response.setDstFile(dstFile);
    response.setDstBlock(dstBlock);
    if (response.getDstParent().getCapacity() < response.getDstFile().getDirOffset() + CrailConstants.DIRECTORY_RECORD) {
        LOG.info("rename: parent capacity does not match dst file offset, capacity " + response.getDstParent().getCapacity() + ", offset " + response.getDstFile().getDirOffset() + ", capacity " + dstParent.getCapacity() + ", offset " + dstFile.getDirOffset());
    }
    if (CrailConstants.DEBUG) {
        LOG.info("renameFile: src-parent " + srcParent.getFd() + ", src-file " + srcFile.getFd() + ", dst-parent " + dstParent.getFd() + ", dst-fd " + dstFile.getFd());
    }
    return RpcErrors.ERR_OK;
}
Also used : FileName(org.apache.crail.metadata.FileName) IOException(java.io.IOException)

Example 7 with FileName

use of org.apache.crail.metadata.FileName in project incubator-crail by apache.

the class NameNodeService method getLocation.

@Override
public short getLocation(RpcRequestMessage.GetLocationReq request, RpcResponseMessage.GetLocationRes response, RpcNameNodeState errorState) throws Exception {
    // check protocol
    if (!RpcProtocol.verifyProtocol(RpcProtocol.CMD_GET_LOCATION, request, response)) {
        return RpcErrors.ERR_PROTOCOL_MISMATCH;
    }
    // get params
    FileName fileName = request.getFileName();
    long position = request.getPosition();
    // check params
    if (position < 0) {
        return RpcErrors.ERR_POSITION_NEGATIV;
    }
    // rpc
    AbstractNode fileInfo = fileTree.retrieveFile(fileName, errorState);
    if (errorState.getError() != RpcErrors.ERR_OK) {
        return errorState.getError();
    }
    if (fileInfo == null) {
        return RpcErrors.ERR_GET_FILE_FAILED;
    }
    int index = CrailUtils.computeIndex(position);
    if (index < 0) {
        return RpcErrors.ERR_POSITION_NEGATIV;
    }
    BlockInfo block = fileInfo.getBlock(index);
    if (block == null) {
        return RpcErrors.ERR_OFFSET_TOO_LARGE;
    }
    response.setBlockInfo(block);
    return RpcErrors.ERR_OK;
}
Also used : BlockInfo(org.apache.crail.metadata.BlockInfo) FileName(org.apache.crail.metadata.FileName)

Example 8 with FileName

use of org.apache.crail.metadata.FileName in project incubator-crail by apache.

the class NameNodeService method createFile.

@Override
public short createFile(RpcRequestMessage.CreateFileReq request, RpcResponseMessage.CreateFileRes response, RpcNameNodeState errorState) throws Exception {
    // check protocol
    if (!RpcProtocol.verifyProtocol(RpcProtocol.CMD_CREATE_FILE, request, response)) {
        return RpcErrors.ERR_PROTOCOL_MISMATCH;
    }
    // get params
    FileName fileHash = request.getFileName();
    CrailNodeType type = request.getFileType();
    boolean writeable = type.isDirectory() ? false : true;
    int storageClass = request.getStorageClass();
    int locationClass = request.getLocationClass();
    boolean enumerable = request.isEnumerable();
    // check params
    if (type.isContainer() && locationClass > 0) {
        return RpcErrors.ERR_DIR_LOCATION_AFFINITY_MISMATCH;
    }
    // rpc
    AbstractNode parentInfo = fileTree.retrieveParent(fileHash, errorState);
    if (errorState.getError() != RpcErrors.ERR_OK) {
        return errorState.getError();
    }
    if (parentInfo == null) {
        return RpcErrors.ERR_PARENT_MISSING;
    }
    if (!parentInfo.getType().isContainer()) {
        return RpcErrors.ERR_PARENT_NOT_DIR;
    }
    if (storageClass < 0) {
        storageClass = parentInfo.getStorageClass();
    }
    if (locationClass < 0) {
        locationClass = parentInfo.getLocationClass();
    }
    AbstractNode fileInfo = fileTree.createNode(fileHash.getFileComponent(), type, storageClass, locationClass, enumerable);
    try {
        AbstractNode oldNode = parentInfo.putChild(fileInfo);
        if (oldNode != null && oldNode.getFd() != fileInfo.getFd()) {
            appendToDeleteQueue(oldNode);
        }
    } catch (Exception e) {
        return RpcErrors.ERR_FILE_EXISTS;
    }
    fileTable.put(fileInfo.getFd(), fileInfo);
    NameNodeBlockInfo fileBlock = blockStore.getBlock(fileInfo.getStorageClass(), fileInfo.getLocationClass());
    if (fileBlock == null) {
        return RpcErrors.ERR_NO_FREE_BLOCKS;
    }
    if (!fileInfo.addBlock(0, fileBlock)) {
        return RpcErrors.ERR_ADD_BLOCK_FAILED;
    }
    NameNodeBlockInfo parentBlock = null;
    if (fileInfo.getDirOffset() >= 0) {
        int index = CrailUtils.computeIndex(fileInfo.getDirOffset());
        parentBlock = parentInfo.getBlock(index);
        if (parentBlock == null) {
            parentBlock = blockStore.getBlock(parentInfo.getStorageClass(), parentInfo.getLocationClass());
            if (parentBlock == null) {
                return RpcErrors.ERR_NO_FREE_BLOCKS;
            }
            if (!parentInfo.addBlock(index, parentBlock)) {
                blockStore.addBlock(parentBlock);
                parentBlock = parentInfo.getBlock(index);
                if (parentBlock == null) {
                    blockStore.addBlock(fileBlock);
                    return RpcErrors.ERR_CREATE_FILE_FAILED;
                }
            }
        }
        parentInfo.incCapacity(CrailConstants.DIRECTORY_RECORD);
    }
    if (writeable) {
        fileInfo.updateToken();
        response.shipToken(true);
    } else {
        response.shipToken(false);
    }
    response.setParentInfo(parentInfo);
    response.setFileInfo(fileInfo);
    response.setFileBlock(fileBlock);
    response.setDirBlock(parentBlock);
    if (CrailConstants.DEBUG) {
        LOG.info("createFile: fd " + fileInfo.getFd() + ", parent " + parentInfo.getFd() + ", writeable " + writeable + ", token " + fileInfo.getToken() + ", capacity " + fileInfo.getCapacity() + ", dirOffset " + fileInfo.getDirOffset());
    }
    return RpcErrors.ERR_OK;
}
Also used : CrailNodeType(org.apache.crail.CrailNodeType) FileName(org.apache.crail.metadata.FileName) IOException(java.io.IOException)

Example 9 with FileName

use of org.apache.crail.metadata.FileName in project incubator-crail by apache.

the class CoreDataStore method create.

public Upcoming<CrailNode> create(String path, CrailNodeType type, CrailStorageClass storageClass, CrailLocationClass locationClass, boolean enumerable) throws Exception {
    FileName name = new FileName(path);
    if (CrailConstants.DEBUG) {
        LOG.info("createNode: name " + path + ", type " + type + ", storageAffinity " + storageClass + ", locationAffinity " + locationClass);
    }
    RpcFuture<RpcCreateFile> fileRes = rpcConnection.createFile(name, type, storageClass.value(), locationClass.value(), enumerable);
    return new CreateNodeFuture(this, path, type, fileRes);
}
Also used : RpcCreateFile(org.apache.crail.rpc.RpcCreateFile) FileName(org.apache.crail.metadata.FileName)

Example 10 with FileName

use of org.apache.crail.metadata.FileName in project incubator-crail by apache.

the class CoreDataStore method delete.

public Upcoming<CrailNode> delete(String path, boolean recursive) throws Exception {
    FileName name = new FileName(path);
    if (CrailConstants.DEBUG) {
        LOG.info("delete: name " + path + ", recursive " + recursive);
    }
    RpcFuture<RpcDeleteFile> fileRes = rpcConnection.removeFile(name, recursive);
    return new DeleteNodeFuture(this, path, recursive, fileRes);
}
Also used : RpcDeleteFile(org.apache.crail.rpc.RpcDeleteFile) FileName(org.apache.crail.metadata.FileName)

Aggregations

FileName (org.apache.crail.metadata.FileName)12 IOException (java.io.IOException)3 BlockInfo (org.apache.crail.metadata.BlockInfo)2 RpcGetFile (org.apache.crail.rpc.RpcGetFile)2 FileNotFoundException (java.io.FileNotFoundException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 CrailNodeType (org.apache.crail.CrailNodeType)1 CrailConfiguration (org.apache.crail.conf.CrailConfiguration)1 CoreDataStore (org.apache.crail.core.CoreDataStore)1 DirectoryInputStream (org.apache.crail.core.DirectoryInputStream)1 DirectoryRecord (org.apache.crail.core.DirectoryRecord)1 DataNodeInfo (org.apache.crail.metadata.DataNodeInfo)1 FileInfo (org.apache.crail.metadata.FileInfo)1 RpcCreateFile (org.apache.crail.rpc.RpcCreateFile)1 RpcDeleteFile (org.apache.crail.rpc.RpcDeleteFile)1 RpcGetLocation (org.apache.crail.rpc.RpcGetLocation)1