Search in sources :

Example 16 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent in project ignite by apache.

the class IgfsInputStreamImpl method close.

/**
 * {@inheritDoc}
 */
@Override
public synchronized void close() throws IOException {
    if (!closed) {
        try {
            if (secReader != null) {
                // Close secondary input stream.
                secReader.close();
                // Ensuring local cache futures completion.
                for (IgniteInternalFuture<byte[]> fut : locCache.values()) {
                    try {
                        fut.get();
                    } catch (IgniteCheckedException ignore) {
                    // No-op.
                    }
                }
                // Ensuring pending evicted futures completion.
                while (!pendingFuts.isEmpty()) {
                    pendingFutsLock.lock();
                    try {
                        pendingFutsCond.await(100, TimeUnit.MILLISECONDS);
                    } catch (InterruptedException ignore) {
                    // No-op.
                    } finally {
                        pendingFutsLock.unlock();
                    }
                }
            }
        } catch (Exception e) {
            throw new IOException("File to close the file: " + path, e);
        } finally {
            closed = true;
            IgfsLocalMetrics metrics = igfsCtx.metrics();
            metrics.addReadBytesTime(bytes, time);
            metrics.decrementFilesOpenedForRead();
            locCache.clear();
            GridEventStorageManager evts = igfsCtx.kernalContext().event();
            if (evts.isRecordable(EVT_IGFS_FILE_CLOSED_READ))
                evts.record(new IgfsEvent(path, igfsCtx.localNode(), EVT_IGFS_FILE_CLOSED_READ, bytes()));
        }
    }
}
Also used : GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgfsEvent(org.apache.ignite.events.IgfsEvent) IOException(java.io.IOException) IgfsCorruptedFileException(org.apache.ignite.igfs.IgfsCorruptedFileException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) EOFException(java.io.EOFException) IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException)

Example 17 with IgfsEvent

use of org.apache.ignite.events.IgfsEvent 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

IgfsEvent (org.apache.ignite.events.IgfsEvent)17 ArrayList (java.util.ArrayList)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Event (org.apache.ignite.events.Event)9 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteException (org.apache.ignite.IgniteException)6 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)5 IgfsException (org.apache.ignite.igfs.IgfsException)4 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)3 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)3 IgfsConcurrentModificationException (org.apache.ignite.igfs.IgfsConcurrentModificationException)3 IgfsDirectoryNotEmptyException (org.apache.ignite.igfs.IgfsDirectoryNotEmptyException)3 IgfsParentNotDirectoryException (org.apache.ignite.igfs.IgfsParentNotDirectoryException)3 IgfsPath (org.apache.ignite.igfs.IgfsPath)3