Search in sources :

Example 1 with IgfsSecondaryFileSystemPositionedReadable

use of org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable 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 2 with IgfsSecondaryFileSystemPositionedReadable

use of org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable in project ignite by apache.

the class IgfsMetaManager method appendDual.

/**
 * Append to a file in DUAL mode.
 *
 * @param fs File system.
 * @param path Path.
 * @param bufSize Buffer size.
 * @param create Create flag.
 * @return Output stream descriptor.
 * @throws IgniteCheckedException If output stream open for append has failed.
 */
public IgfsCreateResult appendDual(final IgfsSecondaryFileSystem fs, final IgfsPath path, final int bufSize, final boolean create) throws IgniteCheckedException {
    if (busyLock.enterBusy()) {
        try {
            assert fs != null;
            assert path != null;
            // Events to fire (can be done outside of a transaction).
            final Deque<IgfsEvent> pendingEvts = new LinkedList<>();
            SynchronizationTask<IgfsCreateResult> task = new SynchronizationTask<IgfsCreateResult>() {

                /**
                 * Container for the secondary file system output stream.
                 */
                private final T1<OutputStream> outT1 = new T1<>(null);

                @Override
                public IgfsCreateResult onSuccess(Map<IgfsPath, IgfsEntryInfo> infos) throws Exception {
                    validTxState(true);
                    final IgfsEntryInfo info = infos.get(path);
                    final IgfsEntryInfo lockedInfo;
                    if (info == null)
                        return onSuccessCreate(fs, path, true, /*simpleCreate*/
                        null, false, /*overwrite*/
                        bufSize, (short) 0, 0, null, infos, pendingEvts, outT1);
                    else {
                        if (info.isDirectory())
                            throw fsException("Failed to open output stream to the file in the " + "secondary file system because the path points to a directory: " + path);
                        outT1.set(fs.append(path, bufSize, false, null));
                        // Synchronize file ending.
                        long len = info.length();
                        int blockSize = info.blockSize();
                        int remainder = (int) (len % blockSize);
                        if (remainder > 0) {
                            int blockIdx = (int) (len / blockSize);
                            try (IgfsSecondaryFileSystemPositionedReadable reader = fs.open(path, bufSize)) {
                                IgniteInternalFuture<byte[]> fut = igfsCtx.data().dataBlock(info, path, blockIdx, reader);
                                assert fut != null;
                                fut.get();
                            }
                        }
                        if (info.lockId() != null) {
                            throw fsException("Failed to open file (file is opened for writing) [path=" + path + ", fileId=" + info.id() + ", lockId=" + info.lockId() + ']');
                        }
                        // Set lock and return.
                        lockedInfo = invokeLock(info.id(), false);
                    }
                    if (evts.isRecordable(EventType.EVT_IGFS_FILE_OPENED_WRITE))
                        pendingEvts.add(new IgfsEvent(path, locNode, EventType.EVT_IGFS_FILE_OPENED_WRITE));
                    return new IgfsCreateResult(lockedInfo, outT1.get());
                }

                @Override
                public IgfsCreateResult onFailure(@Nullable Exception err) throws IgniteCheckedException {
                    U.closeQuiet(outT1.get());
                    U.error(log, "File append in DUAL mode failed [path=" + path + ", bufferSize=" + bufSize + ']', err);
                    throw new IgniteCheckedException("Failed to append to the file due to secondary file " + "system exception: " + path, err);
                }
            };
            try {
                return synchronizeAndExecute(task, fs, !create, /*strict*/
                path);
            } finally {
                for (IgfsEvent evt : pendingEvts) evts.record(evt);
            }
        } finally {
            busyLock.leaveBusy();
        }
    } else
        throw new IllegalStateException("Failed to append to file in DUAL mode because Grid is stopping: " + path);
}
Also used : LinkedList(java.util.LinkedList) 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) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgfsEvent(org.apache.ignite.events.IgfsEvent) IgfsSecondaryFileSystemPositionedReadable(org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable) T1(org.apache.ignite.internal.util.typedef.T1) Map(java.util.Map) HashMap(java.util.HashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) TreeMap(java.util.TreeMap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 IgfsException (org.apache.ignite.igfs.IgfsException)2 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)2 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)2 IgfsSecondaryFileSystemPositionedReadable (org.apache.ignite.igfs.secondary.IgfsSecondaryFileSystemPositionedReadable)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)1 IgfsEvent (org.apache.ignite.events.IgfsEvent)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