Search in sources :

Example 1 with IgfsPathIsDirectoryException

use of org.apache.ignite.igfs.IgfsPathIsDirectoryException in project ignite by apache.

the class IgfsMetaManager method create.

/**
     * Create a file.
     *
     * @param path Path.
     * @param dirProps Directory properties.
     * @param overwrite Overwrite flag.
     * @param blockSize Block size.
     * @param affKey Affinity key.
     * @param evictExclude Evict exclude flag.
     * @param fileProps File properties.
     * @param secondaryCtx Secondary file system create context.
     * @return @return Operation result.
     * @throws IgniteCheckedException If failed.
     */
IgfsCreateResult create(final IgfsPath path, Map<String, String> dirProps, final boolean overwrite, final int blockSize, @Nullable final IgniteUuid affKey, final boolean evictExclude, @Nullable Map<String, String> fileProps, @Nullable IgfsSecondaryFileSystemCreateContext secondaryCtx) throws IgniteCheckedException {
    validTxState(false);
    while (true) {
        if (busyLock.enterBusy()) {
            OutputStream secondaryOut = null;
            try {
                // Prepare path IDs.
                IgfsPathIds pathIds = pathIds(path);
                // Prepare lock IDs.
                Set<IgniteUuid> lockIds = new TreeSet<>(PATH_ID_SORTING_COMPARATOR);
                pathIds.addExistingIds(lockIds, relaxed);
                pathIds.addSurrogateIds(lockIds);
                // In overwrite mode we also lock ID of potential replacement as well as trash ID.
                IgniteUuid overwriteId = IgniteUuid.randomUuid();
                IgniteUuid trashId = IgfsUtils.randomTrashId();
                if (overwrite) {
                    lockIds.add(overwriteId);
                    // Trash ID is only added if we suspect conflict.
                    if (pathIds.allExists())
                        lockIds.add(trashId);
                }
                // Start TX.
                try (GridNearTxLocal tx = startTx()) {
                    Map<IgniteUuid, IgfsEntryInfo> lockInfos = lockIds(lockIds);
                    if (secondaryCtx != null && isRetryForSecondary(pathIds, lockInfos))
                        continue;
                    if (!pathIds.verifyIntegrity(lockInfos, relaxed))
                        // Directory structure changed concurrently. So we simply re-try.
                        continue;
                    if (pathIds.allExists()) {
                        // All participants found.
                        IgfsEntryInfo oldInfo = lockInfos.get(pathIds.lastId());
                        // Check: is it a file?
                        if (!oldInfo.isFile())
                            throw new IgfsPathIsDirectoryException("Failed to create a file: " + path);
                        // Check: can we overwrite it?
                        if (!overwrite)
                            throw new IgfsPathAlreadyExistsException("Failed to create a file: " + path);
                        // Check if file already opened for write.
                        if (oldInfo.lockId() != null)
                            throw new IgfsException("File is already opened for write: " + path);
                        // At this point file can be re-created safely.
                        // Add existing to trash listing.
                        IgniteUuid oldId = pathIds.lastId();
                        id2InfoPrj.invoke(trashId, new IgfsMetaDirectoryListingAddProcessor(IgfsUtils.composeNameForTrash(path, oldId), new IgfsListingEntry(oldInfo)));
                        // Replace ID in parent directory.
                        String name = pathIds.lastPart();
                        IgniteUuid parentId = pathIds.lastParentId();
                        id2InfoPrj.invoke(parentId, new IgfsMetaDirectoryListingReplaceProcessor(name, overwriteId));
                        // Create the file.
                        IgniteUuid newLockId = createFileLockId(false);
                        long newAccessTime;
                        long newModificationTime;
                        Map<String, String> newProps;
                        long newLen;
                        int newBlockSize;
                        if (secondaryCtx != null) {
                            secondaryOut = secondaryCtx.create();
                            newAccessTime = 0L;
                            newModificationTime = 0L;
                            newProps = null;
                        } else {
                            newAccessTime = System.currentTimeMillis();
                            newModificationTime = newAccessTime;
                            newProps = fileProps;
                        }
                        newLen = 0L;
                        newBlockSize = blockSize;
                        IgfsEntryInfo newInfo = invokeAndGet(overwriteId, new IgfsMetaFileCreateProcessor(newAccessTime, newModificationTime, newProps, newBlockSize, affKey, newLockId, evictExclude, newLen));
                        // Prepare result and commit.
                        tx.commit();
                        IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EventType.EVT_IGFS_FILE_OPENED_WRITE);
                        return new IgfsCreateResult(newInfo, secondaryOut);
                    } else {
                        // Create file and parent folders.
                        T1<OutputStream> secondaryOutHolder = null;
                        if (secondaryCtx != null)
                            secondaryOutHolder = new T1<>();
                        IgfsPathsCreateResult res;
                        try {
                            res = createFile(pathIds, lockInfos, dirProps, fileProps, blockSize, affKey, evictExclude, secondaryCtx, secondaryOutHolder);
                        } finally {
                            if (secondaryOutHolder != null)
                                secondaryOut = secondaryOutHolder.get();
                        }
                        if (res == null)
                            continue;
                        // Commit.
                        tx.commit();
                        // Generate events.
                        generateCreateEvents(res.createdPaths(), true);
                        return new IgfsCreateResult(res.info(), secondaryOut);
                    }
                }
            } catch (IgniteException | IgniteCheckedException e) {
                U.closeQuiet(secondaryOut);
                throw e;
            } catch (Exception e) {
                U.closeQuiet(secondaryOut);
                throw new IgniteCheckedException("Create failed due to unexpected exception: " + path, e);
            } finally {
                busyLock.leaveBusy();
            }
        } else
            throw new IllegalStateException("Failed to mkdir because Grid is stopping. [path=" + path + ']');
    }
}
Also used : OutputStream(java.io.OutputStream) IgfsPathAlreadyExistsException(org.apache.ignite.igfs.IgfsPathAlreadyExistsException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TreeSet(java.util.TreeSet) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) T1(org.apache.ignite.internal.util.typedef.T1) IgfsMetaDirectoryListingAddProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaDirectoryListingAddProcessor) IgfsMetaFileCreateProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaFileCreateProcessor) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgfsPathAlreadyExistsException(org.apache.ignite.igfs.IgfsPathAlreadyExistsException) IgfsParentNotDirectoryException(org.apache.ignite.igfs.IgfsParentNotDirectoryException) IgfsPathIsDirectoryException(org.apache.ignite.igfs.IgfsPathIsDirectoryException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgfsDirectoryNotEmptyException(org.apache.ignite.igfs.IgfsDirectoryNotEmptyException) IgfsException(org.apache.ignite.igfs.IgfsException) IgfsConcurrentModificationException(org.apache.ignite.igfs.IgfsConcurrentModificationException) IgfsPathIsNotDirectoryException(org.apache.ignite.igfs.IgfsPathIsNotDirectoryException) IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgfsException(org.apache.ignite.igfs.IgfsException) IgfsMetaDirectoryListingReplaceProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaDirectoryListingReplaceProcessor) IgfsPathIsDirectoryException(org.apache.ignite.igfs.IgfsPathIsDirectoryException)

Example 2 with IgfsPathIsDirectoryException

use of org.apache.ignite.igfs.IgfsPathIsDirectoryException in project ignite by apache.

the class IgfsImpl method open.

/** {@inheritDoc} */
@Override
public IgfsInputStream open(final IgfsPath path, final int bufSize, final int seqReadsBeforePrefetch) {
    A.notNull(path, "path");
    A.ensure(bufSize >= 0, "bufSize >= 0");
    A.ensure(seqReadsBeforePrefetch >= 0, "seqReadsBeforePrefetch >= 0");
    return safeOp(new Callable<IgfsInputStream>() {

        @Override
        public IgfsInputStream call() throws Exception {
            if (log.isDebugEnabled())
                log.debug("Open file for reading [path=" + path + ", bufSize=" + bufSize + ']');
            int bufSize0 = bufSize == 0 ? cfg.getBufferSize() : bufSize;
            IgfsMode mode = resolveMode(path);
            switch(mode) {
                case PRIMARY:
                    {
                        IgfsEntryInfo info = meta.infoForPath(path);
                        if (info == null)
                            throw new IgfsPathNotFoundException("File not found: " + path);
                        if (!info.isFile())
                            throw new IgfsPathIsDirectoryException("Failed to open file (not a file): " + path);
                        // Input stream to read data from grid cache with separate blocks.
                        IgfsInputStreamImpl os = new IgfsInputStreamImpl(igfsCtx, path, info, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, null, info.length(), info.blockSize(), info.blocksCount(), false);
                        IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
                        return os;
                    }
                case DUAL_ASYNC:
                case DUAL_SYNC:
                    {
                        assert IgfsUtils.isDualMode(mode);
                        IgfsSecondaryInputStreamDescriptor desc = meta.openDual(secondaryFs, path, bufSize0);
                        IgfsEntryInfo info = desc.info();
                        IgfsInputStreamImpl os = new IgfsInputStreamImpl(igfsCtx, path, info, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, desc.reader(), info.length(), info.blockSize(), info.blocksCount(), false);
                        IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
                        return os;
                    }
                case PROXY:
                    {
                        assert secondaryFs != null;
                        IgfsFile info = info(path);
                        if (info == null)
                            throw new IgfsPathNotFoundException("File not found: " + path);
                        if (!info.isFile())
                            throw new IgfsPathIsDirectoryException("Failed to open file (not a file): " + path);
                        IgfsSecondaryFileSystemPositionedReadable secReader = new IgfsLazySecondaryFileSystemPositionedReadable(secondaryFs, path, bufSize);
                        long len = info.length();
                        int blockSize = info.blockSize() > 0 ? info.blockSize() : cfg.getBlockSize();
                        long blockCnt = len / blockSize;
                        if (len % blockSize != 0)
                            blockCnt++;
                        IgfsInputStream os = new IgfsInputStreamImpl(igfsCtx, path, null, cfg.getPrefetchBlocks(), seqReadsBeforePrefetch, secReader, info.length(), blockSize, blockCnt, true);
                        IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EVT_IGFS_FILE_OPENED_READ);
                        return os;
                    }
                default:
                    assert false : "Unexpected mode " + mode;
                    return null;
            }
        }
    });
}
Also used : IgfsInputStream(org.apache.ignite.igfs.IgfsInputStream) IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException) IgfsPathIsDirectoryException(org.apache.ignite.igfs.IgfsPathIsDirectoryException) IgfsInvalidPathException(org.apache.ignite.igfs.IgfsInvalidPathException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgfsException(org.apache.ignite.igfs.IgfsException) IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException) IgfsMode(org.apache.ignite.igfs.IgfsMode) IgfsSecondaryFileSystemPositionedReadable(org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable) IgfsFile(org.apache.ignite.igfs.IgfsFile) IgfsPathIsDirectoryException(org.apache.ignite.igfs.IgfsPathIsDirectoryException)

Example 3 with IgfsPathIsDirectoryException

use of org.apache.ignite.igfs.IgfsPathIsDirectoryException in project ignite by apache.

the class IgfsMetaManager method append.

/**
     * Append routine.
     *
     * @param path Path.
     * @param dirProps Directory properties.
     * @param create Create flag.
     * @param blockSize Block size.
     * @param affKey Affinity key.
     * @param evictExclude Evict exclude flag.
     * @param fileProps File properties.
     * @return Resulting info.
     * @throws IgniteCheckedException If failed.
     */
IgfsEntryInfo append(final IgfsPath path, Map<String, String> dirProps, final boolean create, final int blockSize, @Nullable final IgniteUuid affKey, final boolean evictExclude, @Nullable Map<String, String> fileProps) throws IgniteCheckedException {
    validTxState(false);
    while (true) {
        if (busyLock.enterBusy()) {
            try {
                // Prepare path IDs.
                IgfsPathIds pathIds = pathIds(path);
                // Fail-fast: create flag is not specified and some paths are missing.
                if (!pathIds.allExists() && !create)
                    throw new IgfsPathNotFoundException("Failed to append because file is not found: " + path);
                // Prepare lock IDs.
                Set<IgniteUuid> lockIds = new TreeSet<>(PATH_ID_SORTING_COMPARATOR);
                pathIds.addExistingIds(lockIds, relaxed);
                pathIds.addSurrogateIds(lockIds);
                // Start TX.
                try (GridNearTxLocal tx = startTx()) {
                    Map<IgniteUuid, IgfsEntryInfo> lockInfos = lockIds(lockIds);
                    if (!pathIds.verifyIntegrity(lockInfos, relaxed))
                        // Directory structure changed concurrently. So we simply re-try.
                        continue;
                    if (pathIds.allExists()) {
                        // All participants are found. Simply open the stream.
                        IgfsEntryInfo info = lockInfos.get(pathIds.lastId());
                        // Check: is it a file?
                        if (!info.isFile())
                            throw new IgfsPathIsDirectoryException("Failed to open file for write." + path);
                        // Check if file already opened for write.
                        if (info.lockId() != null)
                            throw new IgfsException("File is already opened for write: " + path);
                        // At this point we can open the stream safely.
                        info = invokeLock(info.id(), false);
                        tx.commit();
                        IgfsUtils.sendEvents(igfsCtx.kernalContext(), path, EventType.EVT_IGFS_FILE_OPENED_WRITE);
                        return info;
                    } else {
                        // Create file and parent folders.
                        IgfsPathsCreateResult res = createFile(pathIds, lockInfos, dirProps, fileProps, blockSize, affKey, evictExclude, null, null);
                        if (res == null)
                            continue;
                        // Commit.
                        tx.commit();
                        // Generate events.
                        generateCreateEvents(res.createdPaths(), true);
                        return res.info();
                    }
                }
            } finally {
                busyLock.leaveBusy();
            }
        } else
            throw new IllegalStateException("Failed to append for file because Grid is stopping:" + path);
    }
}
Also used : IgfsException(org.apache.ignite.igfs.IgfsException) TreeSet(java.util.TreeSet) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException) IgfsPathIsDirectoryException(org.apache.ignite.igfs.IgfsPathIsDirectoryException)

Aggregations

IgfsException (org.apache.ignite.igfs.IgfsException)3 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)3 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)3 TreeSet (java.util.TreeSet)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)2 IgniteUuid (org.apache.ignite.lang.IgniteUuid)2 OutputStream (java.io.OutputStream)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)1 IgfsConcurrentModificationException (org.apache.ignite.igfs.IgfsConcurrentModificationException)1 IgfsDirectoryNotEmptyException (org.apache.ignite.igfs.IgfsDirectoryNotEmptyException)1 IgfsFile (org.apache.ignite.igfs.IgfsFile)1 IgfsInputStream (org.apache.ignite.igfs.IgfsInputStream)1 IgfsInvalidPathException (org.apache.ignite.igfs.IgfsInvalidPathException)1 IgfsMode (org.apache.ignite.igfs.IgfsMode)1 IgfsParentNotDirectoryException (org.apache.ignite.igfs.IgfsParentNotDirectoryException)1 IgfsPathAlreadyExistsException (org.apache.ignite.igfs.IgfsPathAlreadyExistsException)1 IgfsPathIsNotDirectoryException (org.apache.ignite.igfs.IgfsPathIsNotDirectoryException)1