Search in sources :

Example 6 with IgfsPath

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

the class IgfsMetaManager method generateCreateEvents.

/**
     * Generate events for created file or directory.
     *
     * @param createdPaths Created paths.
     * @param file Whether file was created.
     */
private void generateCreateEvents(List<IgfsPath> createdPaths, boolean file) {
    if (evts.isRecordable(EventType.EVT_IGFS_DIR_CREATED)) {
        for (int i = 0; i < createdPaths.size() - 1; i++) IgfsUtils.sendEvents(igfsCtx.kernalContext(), createdPaths.get(i), EventType.EVT_IGFS_DIR_CREATED);
    }
    IgfsPath leafPath = createdPaths.get(createdPaths.size() - 1);
    if (file) {
        IgfsUtils.sendEvents(igfsCtx.kernalContext(), leafPath, EventType.EVT_IGFS_FILE_CREATED);
        IgfsUtils.sendEvents(igfsCtx.kernalContext(), leafPath, EventType.EVT_IGFS_FILE_OPENED_WRITE);
    } else
        IgfsUtils.sendEvents(igfsCtx.kernalContext(), leafPath, EventType.EVT_IGFS_DIR_CREATED);
}
Also used : IgfsPath(org.apache.ignite.igfs.IgfsPath)

Example 7 with IgfsPath

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

the class IgfsMetaManager method onSuccessCreate.

/**
     * A delegate method that performs file creation in the synchronization task.
     *
     * @param fs File system.
     * @param path Path.
     * @param simpleCreate "Simple create" flag.
     * @param props Properties..
     * @param overwrite Overwrite flag.
     * @param bufSize Buffer size.
     * @param replication Replication factor.
     * @param blockSize Block size.
     * @param affKey Affinity key.
     * @param infos Map from paths to corresponding infos.
     * @param pendingEvts A non-null collection the events are to be accumulated in.
     * @param t1 A signle-object tuple to hold the created output stream.
     * @return Output stream descriptor.
     * @throws Exception On error.
     */
IgfsCreateResult onSuccessCreate(IgfsSecondaryFileSystem fs, IgfsPath path, boolean simpleCreate, @Nullable final Map<String, String> props, boolean overwrite, int bufSize, short replication, long blockSize, IgniteUuid affKey, Map<IgfsPath, IgfsEntryInfo> infos, final Deque<IgfsEvent> pendingEvts, final T1<OutputStream> t1) throws Exception {
    validTxState(true);
    assert !infos.isEmpty();
    // Determine the first existing parent.
    IgfsPath parentPath = null;
    for (IgfsPath curPath : infos.keySet()) {
        if (parentPath == null || curPath.isSubDirectoryOf(parentPath))
            parentPath = curPath;
    }
    assert parentPath != null;
    IgfsEntryInfo parentInfo = infos.get(parentPath);
    // Delegate to the secondary file system.
    OutputStream out = simpleCreate ? fs.create(path, overwrite) : fs.create(path, bufSize, overwrite, replication, blockSize, props);
    t1.set(out);
    IgfsPath parent0 = path.parent();
    assert parent0 != null : "path.parent() is null (are we creating ROOT?): " + path;
    // If some of the parent directories were missing, synchronize again.
    if (!parentPath.equals(parent0)) {
        parentInfo = synchronize(fs, parentPath, parentInfo, parent0, true, null);
        // Fire notification about missing directories creation.
        if (evts.isRecordable(EventType.EVT_IGFS_DIR_CREATED)) {
            IgfsPath evtPath = parent0;
            while (!parentPath.equals(evtPath)) {
                pendingEvts.addFirst(new IgfsEvent(evtPath, locNode, EventType.EVT_IGFS_DIR_CREATED));
                evtPath = evtPath.parent();
                // If this fails, then ROOT does not exist.
                assert evtPath != null;
            }
        }
    }
    // Get created file info.
    IgfsFile status = fs.info(path);
    if (status == null)
        throw fsException("Failed to open output stream to the file created in " + "the secondary file system because it no longer exists: " + path);
    else if (status.isDirectory())
        throw fsException("Failed to open output stream to the file created in " + "the secondary file system because the path points to a directory: " + path);
    IgfsEntryInfo newInfo = IgfsUtils.createFile(IgniteUuid.randomUuid(), igfsCtx.configuration().getBlockSize(), status.length(), affKey, createFileLockId(false), igfsCtx.igfs().evictExclude(path, false), status.properties(), status.accessTime(), status.modificationTime());
    // Add new file info to the listing optionally removing the previous one.
    assert parentInfo != null;
    IgniteUuid oldId = putIfAbsentNonTx(parentInfo.id(), path.name(), newInfo);
    if (oldId != null) {
        IgfsEntryInfo oldInfo = info(oldId);
        // Otherwise cache is in inconsistent state.
        assert oldInfo != null;
        // The contact is that we cannot overwrite a file locked for writing:
        if (oldInfo.lockId() != null)
            throw fsException("Failed to overwrite file (file is opened for writing) [path=" + path + ", fileId=" + oldId + ", lockId=" + oldInfo.lockId() + ']');
        // Remove the old one.
        id2InfoPrj.remove(oldId);
        id2InfoPrj.invoke(parentInfo.id(), new IgfsMetaDirectoryListingRemoveProcessor(path.name(), parentInfo.listing().get(path.name()).fileId()));
        // Put new one.
        createNewEntry(newInfo, parentInfo.id(), path.name());
        igfsCtx.data().delete(oldInfo);
    }
    // Record CREATE event if needed.
    if (oldId == null && evts.isRecordable(EventType.EVT_IGFS_FILE_CREATED))
        pendingEvts.add(new IgfsEvent(path, locNode, EventType.EVT_IGFS_FILE_CREATED));
    return new IgfsCreateResult(newInfo, out);
}
Also used : IgfsPath(org.apache.ignite.igfs.IgfsPath) IgfsEvent(org.apache.ignite.events.IgfsEvent) IgniteUuid(org.apache.ignite.lang.IgniteUuid) OutputStream(java.io.OutputStream) IgfsMetaDirectoryListingRemoveProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaDirectoryListingRemoveProcessor) IgfsFile(org.apache.ignite.igfs.IgfsFile)

Example 8 with IgfsPath

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

the class IgfsMetaManager method renameDual.

/**
     * Rename path in DUAL mode.
     *
     * @param fs Secondary file system.
     * @param src Source path.
     * @param dest Destination path.
     * @return Operation result.
     * @throws IgniteCheckedException If failed.
     */
public boolean renameDual(final IgfsSecondaryFileSystem fs, final IgfsPath src, final IgfsPath dest) throws IgniteCheckedException {
    if (busyLock.enterBusy()) {
        try {
            assert fs != null;
            assert src != null;
            assert dest != null;
            if (src.parent() == null)
                // Root directory cannot be renamed.
                return false;
            // Events to fire (can be done outside of a transaction).
            final Collection<IgfsEvent> pendingEvts = new LinkedList<>();
            SynchronizationTask<Boolean> task = new SynchronizationTask<Boolean>() {

                @Override
                public Boolean onSuccess(Map<IgfsPath, IgfsEntryInfo> infos) throws Exception {
                    IgfsEntryInfo srcInfo = infos.get(src);
                    IgfsEntryInfo srcParentInfo = infos.get(src.parent());
                    IgfsEntryInfo destInfo = infos.get(dest);
                    IgfsEntryInfo destParentInfo = dest.parent() != null ? infos.get(dest.parent()) : null;
                    // Source path and destination (or destination parent) must exist.
                    if (srcInfo == null)
                        throw fsException(new IgfsPathNotFoundException("Failed to rename " + "(source path not found): " + src));
                    if (destInfo == null && destParentInfo == null)
                        throw fsException(new IgfsPathNotFoundException("Failed to rename " + "(destination path not found): " + dest));
                    // Delegate to the secondary file system.
                    fs.rename(src, dest);
                    // Rename was successful, perform compensation in the local file system.
                    if (destInfo == null)
                        moveNonTx(srcInfo.id(), src.name(), srcParentInfo.id(), dest.name(), destParentInfo.id());
                    else {
                        // Move.
                        if (destInfo.isFile())
                            throw fsException("Failed to rename the path in the local file system " + "because destination path already exists and it is a file: " + dest);
                        else
                            moveNonTx(srcInfo.id(), src.name(), srcParentInfo.id(), src.name(), destInfo.id());
                    }
                    // Record event if needed.
                    if (srcInfo.isFile()) {
                        if (evts.isRecordable(EventType.EVT_IGFS_FILE_RENAMED))
                            pendingEvts.add(new IgfsEvent(src, destInfo == null ? dest : new IgfsPath(dest, src.name()), locNode, EventType.EVT_IGFS_FILE_RENAMED));
                    } else if (evts.isRecordable(EventType.EVT_IGFS_DIR_RENAMED))
                        pendingEvts.add(new IgfsEvent(src, dest, locNode, EventType.EVT_IGFS_DIR_RENAMED));
                    return true;
                }

                @Override
                public Boolean onFailure(@Nullable Exception err) throws IgniteCheckedException {
                    U.error(log, "Path rename in DUAL mode failed [source=" + src + ", destination=" + dest + ']', err);
                    throw new IgniteCheckedException("Failed to rename the path due to secondary file system " + "exception: " + src, err);
                }
            };
            try {
                return synchronizeAndExecute(task, fs, false, src, dest);
            } finally {
                for (IgfsEvent evt : pendingEvts) evts.record(evt);
            }
        } finally {
            busyLock.leaveBusy();
        }
    } else
        throw new IllegalStateException("Failed to rename in DUAL mode because Grid is stopping [src=" + src + ", dest=" + dest + ']');
}
Also used : IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException) 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) IgfsPath(org.apache.ignite.igfs.IgfsPath) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgfsEvent(org.apache.ignite.events.IgfsEvent) Map(java.util.Map) HashMap(java.util.HashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) TreeMap(java.util.TreeMap) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with IgfsPath

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

the class IgfsMetaManager method mkdirsDual.

/**
     * Create directory in DUAL mode.
     *
     * @param fs Secondary file system.
     * @param path Path to create.
     * @param props Properties to be applied.
     * @return {@code True} in case rename was successful.
     * @throws IgniteCheckedException If directory creation failed.
     */
public boolean mkdirsDual(final IgfsSecondaryFileSystem fs, final IgfsPath path, final Map<String, String> props) throws IgniteCheckedException {
    if (busyLock.enterBusy()) {
        try {
            assert fs != null;
            assert path != null;
            if (path.parent() == null)
                // No additional handling for root directory is needed.
                return true;
            // Events to fire (can be done outside of a transaction).
            final Deque<IgfsEvent> pendingEvts = new LinkedList<>();
            SynchronizationTask<Boolean> task = new SynchronizationTask<Boolean>() {

                @Override
                public Boolean onSuccess(Map<IgfsPath, IgfsEntryInfo> infos) throws Exception {
                    fs.mkdirs(path, props);
                    assert !infos.isEmpty();
                    // Now perform synchronization again starting with the last created parent.
                    IgfsPath parentPath = null;
                    for (IgfsPath curPath : infos.keySet()) {
                        if (parentPath == null || curPath.isSubDirectoryOf(parentPath))
                            parentPath = curPath;
                    }
                    assert parentPath != null;
                    IgfsEntryInfo parentPathInfo = infos.get(parentPath);
                    synchronize(fs, parentPath, parentPathInfo, path, true, null);
                    if (evts.isRecordable(EventType.EVT_IGFS_DIR_CREATED)) {
                        IgfsPath evtPath = path;
                        while (!parentPath.equals(evtPath)) {
                            pendingEvts.addFirst(new IgfsEvent(evtPath, locNode, EventType.EVT_IGFS_DIR_CREATED));
                            evtPath = evtPath.parent();
                            // If this fails, then ROOT does not exist.
                            assert evtPath != null;
                        }
                    }
                    return true;
                }

                @Override
                public Boolean onFailure(@Nullable Exception err) throws IgniteCheckedException {
                    U.error(log, "Directory creation in DUAL mode failed [path=" + path + ", properties=" + props + ']', err);
                    throw new IgniteCheckedException("Failed to create the path due to secondary file system " + "exception: " + path, err);
                }
            };
            try {
                return synchronizeAndExecute(task, fs, false, path.parent());
            } finally {
                for (IgfsEvent evt : pendingEvts) evts.record(evt);
            }
        } finally {
            busyLock.leaveBusy();
        }
    } else
        throw new IllegalStateException("Failed to create directory 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) IgfsPath(org.apache.ignite.igfs.IgfsPath) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgfsEvent(org.apache.ignite.events.IgfsEvent) Map(java.util.Map) HashMap(java.util.HashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) TreeMap(java.util.TreeMap) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with IgfsPath

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

the class IgfsImpl method await.

/** {@inheritDoc} */
@Override
public void await(IgfsPath... paths) {
    assert paths != null;
    for (Map.Entry<IgfsPath, IgfsFileWorkerBatch> workerEntry : workerMap.entrySet()) {
        IgfsPath workerPath = workerEntry.getKey();
        boolean await = false;
        for (IgfsPath path : paths) {
            if (workerPath.isSubDirectoryOf(path) || F.eq(workerPath, path)) {
                await = true;
                break;
            }
        }
        if (await) {
            IgfsFileWorkerBatch batch = workerEntry.getValue();
            if (batch != null) {
                try {
                    // batches on current path and all it's known children.
                    if (batch.finishing())
                        batch.await();
                } catch (IgniteCheckedException ignore) {
                // No-op.
                }
            }
        }
    }
}
Also used : IgfsPath(org.apache.ignite.igfs.IgfsPath) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

IgfsPath (org.apache.ignite.igfs.IgfsPath)161 IgfsOutputStream (org.apache.ignite.igfs.IgfsOutputStream)23 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 HashMap (java.util.HashMap)13 IgniteException (org.apache.ignite.IgniteException)13 IgniteFileSystem (org.apache.ignite.IgniteFileSystem)13 IgfsFile (org.apache.ignite.igfs.IgfsFile)13 IgfsException (org.apache.ignite.igfs.IgfsException)12 IgniteUuid (org.apache.ignite.lang.IgniteUuid)11 IgfsBlockLocation (org.apache.ignite.igfs.IgfsBlockLocation)10 IgfsInputStream (org.apache.ignite.igfs.IgfsInputStream)10 Map (java.util.Map)9 Path (org.apache.hadoop.fs.Path)9 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)9 FileNotFoundException (java.io.FileNotFoundException)6 OutputStream (java.io.OutputStream)6 IgfsDirectoryNotEmptyException (org.apache.ignite.igfs.IgfsDirectoryNotEmptyException)6 IgfsParentNotDirectoryException (org.apache.ignite.igfs.IgfsParentNotDirectoryException)6