Search in sources :

Example 16 with GridNearTxLocal

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal 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 17 with GridNearTxLocal

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.

the class IgfsMetaManager method lock.

/**
     * Lock the file explicitly outside of transaction.
     *
     * @param fileId File ID to lock.
     * @param del If file is being locked for delete.
     * @return Locked file info or {@code null} if file cannot be locked or doesn't exist.
     * @throws IgniteCheckedException If the file with such id does not exist, or on another failure.
     */
@Nullable
public IgfsEntryInfo lock(IgniteUuid fileId, boolean del) throws IgniteCheckedException {
    if (busyLock.enterBusy()) {
        try {
            validTxState(false);
            assert fileId != null;
            try (GridNearTxLocal tx = startTx()) {
                // Lock file ID for this transaction.
                IgfsEntryInfo oldInfo = info(fileId);
                if (oldInfo == null)
                    return null;
                if (oldInfo.lockId() != null)
                    // The file is already locked, we cannot lock it.
                    return null;
                IgfsEntryInfo newInfo = invokeLock(fileId, del);
                tx.commit();
                return newInfo;
            } catch (GridClosureException e) {
                throw U.cast(e);
            }
        } finally {
            busyLock.leaveBusy();
        }
    } else
        throw new IllegalStateException("Failed to obtain lock because Grid is stopping: " + fileId);
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with GridNearTxLocal

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.

the class IgfsMetaManager method mkdirs.

/**
     * Mkdirs implementation.
     *
     * @param path The path to create.
     * @param props The properties to use for created directories.
     * @return True if a directory was created during the operation.
     * @throws IgniteCheckedException If a non-directory file exists on the requested path, and in case of other errors.
     */
boolean mkdirs(final IgfsPath path, final Map<String, String> props) throws IgniteCheckedException {
    validTxState(false);
    while (true) {
        if (busyLock.enterBusy()) {
            try {
                // Prepare path IDs.
                IgfsPathIds pathIds = pathIds(path);
                // Prepare lock IDs. Essentially, they consist of two parts: existing IDs and potential new IDs.
                Set<IgniteUuid> lockIds = new TreeSet<>(PATH_ID_SORTING_COMPARATOR);
                pathIds.addExistingIds(lockIds, relaxed);
                pathIds.addSurrogateIds(lockIds);
                // Start TX.
                try (GridNearTxLocal tx = startTx()) {
                    final Map<IgniteUuid, IgfsEntryInfo> lockInfos = lockIds(lockIds);
                    if (!pathIds.verifyIntegrity(lockInfos, relaxed))
                        // Directory structure changed concurrently. So we simply re-try.
                        continue;
                    // Check if the whole structure is already in place.
                    if (pathIds.allExists()) {
                        if (lockInfos.get(pathIds.lastExistingId()).isDirectory())
                            return false;
                        else
                            throw new IgfsParentNotDirectoryException("Failed to create directory (parent " + "element is not a directory)");
                    }
                    IgfsPathsCreateResult res = createDirectory(pathIds, lockInfos, props);
                    if (res == null)
                        continue;
                    // Commit TX.
                    tx.commit();
                    generateCreateEvents(res.createdPaths(), false);
                    // We are done.
                    return true;
                }
            } finally {
                busyLock.leaveBusy();
            }
        } else
            throw new IllegalStateException("Failed to mkdir because Grid is stopping. [path=" + path + ']');
    }
}
Also used : IgfsParentNotDirectoryException(org.apache.ignite.igfs.IgfsParentNotDirectoryException) TreeSet(java.util.TreeSet) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)

Example 19 with GridNearTxLocal

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.

the class IgfsMetaManager method sampling.

/**
     * Set sampling flag.
     *
     * @param val Sampling flag state or {@code null} to clear sampling state and mark it as "not set".
     * @return {@code True} if sampling mode was actually changed by this call.
     * @throws IgniteCheckedException If failed.
     */
public boolean sampling(Boolean val) throws IgniteCheckedException {
    if (busyLock.enterBusy()) {
        try {
            validTxState(false);
            try (GridNearTxLocal tx = startTx()) {
                Object prev = val != null ? metaCache.getAndPut(sampling, val) : metaCache.getAndRemove(sampling);
                tx.commit();
                return !F.eq(prev, val);
            }
        } finally {
            busyLock.leaveBusy();
        }
    } else
        throw new IllegalStateException("Failed to set sampling flag because Grid is stopping.");
}
Also used : GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)

Example 20 with GridNearTxLocal

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal in project ignite by apache.

the class IgfsMetaManager method updateTimes.

/**
     * Update times.
     *
     * @param path Path.
     * @param accessTime Access time.
     * @param modificationTime Modification time.
     * @param secondaryFs Secondary file system.
     * @throws IgniteCheckedException If failed.
     */
public void updateTimes(IgfsPath path, long modificationTime, long accessTime, IgfsSecondaryFileSystem secondaryFs) throws IgniteCheckedException {
    while (true) {
        if (busyLock.enterBusy()) {
            try {
                validTxState(false);
                // Prepare path IDs.
                IgfsPathIds pathIds = pathIds(path);
                // Prepare lock IDs.
                Set<IgniteUuid> lockIds = new TreeSet<>(PATH_ID_SORTING_COMPARATOR);
                pathIds.addExistingIds(lockIds, relaxed);
                // Start TX.
                try (GridNearTxLocal tx = startTx()) {
                    Map<IgniteUuid, IgfsEntryInfo> lockInfos = lockIds(lockIds);
                    if (secondaryFs != null && isRetryForSecondary(pathIds, lockInfos))
                        continue;
                    if (!pathIds.verifyIntegrity(lockInfos, relaxed))
                        // Directory structure changed concurrently. So we re-try.
                        continue;
                    if (pathIds.allExists()) {
                        // All files are in place. Update both primary and secondary file systems.
                        if (secondaryFs != null)
                            secondaryFs.setTimes(path, modificationTime, accessTime);
                        IgniteUuid targetId = pathIds.lastExistingId();
                        IgfsEntryInfo targetInfo = lockInfos.get(targetId);
                        id2InfoPrj.invoke(targetId, new IgfsMetaUpdateTimesProcessor(accessTime == -1 ? targetInfo.accessTime() : accessTime, modificationTime == -1 ? targetInfo.modificationTime() : modificationTime));
                        tx.commit();
                        return;
                    } else {
                        // Propagate call to the secondary FS, as we might haven't cache this part yet.
                        if (secondaryFs != null) {
                            secondaryFs.setTimes(path, modificationTime, accessTime);
                            return;
                        } else
                            throw new IgfsPathNotFoundException("Failed to update times (path not found): " + path);
                    }
                }
            } catch (IgniteException | IgniteCheckedException e) {
                throw e;
            } catch (Exception e) {
                throw new IgniteCheckedException("setTimes failed due to unexpected exception: " + path, e);
            } finally {
                busyLock.leaveBusy();
            }
        } else
            throw new IllegalStateException("Failed to update times because Grid is stopping: " + path);
    }
}
Also used : GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgfsPathNotFoundException(org.apache.ignite.igfs.IgfsPathNotFoundException) 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) IgfsMetaUpdateTimesProcessor(org.apache.ignite.internal.processors.igfs.meta.IgfsMetaUpdateTimesProcessor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TreeSet(java.util.TreeSet) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException)

Aggregations

GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)48 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 IgniteUuid (org.apache.ignite.lang.IgniteUuid)10 IgniteException (org.apache.ignite.IgniteException)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 GridClosureException (org.apache.ignite.internal.util.lang.GridClosureException)7 TreeSet (java.util.TreeSet)6 UUID (java.util.UUID)6 Callable (java.util.concurrent.Callable)6 Nullable (org.jetbrains.annotations.Nullable)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 IgfsException (org.apache.ignite.igfs.IgfsException)5 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)5 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 IgfsPathAlreadyExistsException (org.apache.ignite.igfs.IgfsPathAlreadyExistsException)4 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)4 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)4