Search in sources :

Example 26 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class HadoopIgfs20FileSystemAbstractSelfTest method testMultithreadedDelete.

/**
     * Test concurrent deletion of the same directory with advanced structure.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings("TooBroadScope")
public void testMultithreadedDelete() throws Exception {
    final Path dir = new Path(new Path(primaryFsUri), "/dir");
    fs.mkdir(dir, FsPermission.getDefault(), true);
    int depth = 3;
    int entryCnt = 5;
    Deque<IgniteBiTuple<Integer, Path>> queue = new ArrayDeque<>();
    queue.add(F.t(0, dir));
    while (!queue.isEmpty()) {
        IgniteBiTuple<Integer, Path> t = queue.pollFirst();
        int curDepth = t.getKey();
        Path curPath = t.getValue();
        if (curDepth < depth) {
            int newDepth = curDepth + 1;
            // Create directories.
            for (int i = 0; i < entryCnt; i++) {
                Path subDir = new Path(curPath, "dir-" + newDepth + "-" + i);
                fs.mkdir(subDir, FsPermission.getDefault(), true);
                queue.addLast(F.t(newDepth, subDir));
            }
        } else {
            // Create files.
            for (int i = 0; i < entryCnt; i++) {
                Path file = new Path(curPath, "file " + i);
                fs.create(file, EnumSet.noneOf(CreateFlag.class), Options.CreateOpts.perms(FsPermission.getDefault())).close();
            }
        }
    }
    final AtomicBoolean err = new AtomicBoolean();
    multithreaded(new Runnable() {

        @Override
        public void run() {
            try {
                U.awaitQuiet(barrier);
                fs.delete(dir, true);
            } catch (FileNotFoundException ignore) {
            // No-op.
            } catch (IOException ignore) {
                err.set(true);
            }
        }
    }, THREAD_CNT);
    // Ensure there were no errors.
    assert !err.get();
    // Ensure the directory was actually deleted.
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            fs.getFileStatus(dir);
            return null;
        }
    }, FileNotFoundException.class, null);
}
Also used : Path(org.apache.hadoop.fs.Path) IgfsPath(org.apache.ignite.igfs.IgfsPath) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) FileNotFoundException(java.io.FileNotFoundException) PathExistsException(org.apache.hadoop.fs.PathExistsException) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) IOException(java.io.IOException) PathIsNotEmptyDirectoryException(org.apache.hadoop.fs.PathIsNotEmptyDirectoryException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 27 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class HadoopIgfs20FileSystemAbstractSelfTest method testMultithreadedMkdirs.

/**
     * Test concurrent creation of multiple directories.
     *
     * @throws Exception If failed.
     */
public void testMultithreadedMkdirs() throws Exception {
    final Path dir = new Path(new Path("igfs:///"), "/dir");
    fs.mkdir(dir, FsPermission.getDefault(), true);
    final int depth = 3;
    final int entryCnt = 5;
    final AtomicBoolean err = new AtomicBoolean();
    multithreaded(new Runnable() {

        @Override
        public void run() {
            Deque<IgniteBiTuple<Integer, Path>> queue = new ArrayDeque<>();
            queue.add(F.t(0, dir));
            U.awaitQuiet(barrier);
            while (!queue.isEmpty()) {
                IgniteBiTuple<Integer, Path> t = queue.pollFirst();
                int curDepth = t.getKey();
                Path curPath = t.getValue();
                if (curDepth <= depth) {
                    int newDepth = curDepth + 1;
                    // Create directories.
                    for (int i = 0; i < entryCnt; i++) {
                        Path subDir = new Path(curPath, "dir-" + newDepth + "-" + i);
                        try {
                            fs.mkdir(subDir, FsPermission.getDefault(), true);
                        } catch (IOException ignore) {
                            err.set(true);
                        }
                        queue.addLast(F.t(newDepth, subDir));
                    }
                }
            }
        }
    }, THREAD_CNT);
    // Ensure there were no errors.
    assert !err.get();
    // Ensure correct folders structure.
    Deque<IgniteBiTuple<Integer, Path>> queue = new ArrayDeque<>();
    queue.add(F.t(0, dir));
    while (!queue.isEmpty()) {
        IgniteBiTuple<Integer, Path> t = queue.pollFirst();
        int curDepth = t.getKey();
        Path curPath = t.getValue();
        if (curDepth <= depth) {
            int newDepth = curDepth + 1;
            // Create directories.
            for (int i = 0; i < entryCnt; i++) {
                Path subDir = new Path(curPath, "dir-" + newDepth + "-" + i);
                assertNotNull(fs.getFileStatus(subDir));
                queue.add(F.t(newDepth, subDir));
            }
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) IgfsPath(org.apache.ignite.igfs.IgfsPath) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IOException(java.io.IOException) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) ArrayDeque(java.util.ArrayDeque) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 28 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridIndexingSpiAbstractSelfTest method testSpi.

/**
     * @throws Exception If failed.
     */
public void testSpi() throws Exception {
    IgniteH2Indexing spi = getIndexing();
    IgniteCache<Integer, BinaryObject> cacheA = ignite0.createCache(cacheACfg());
    IgniteCache<Integer, BinaryObject> cacheB = ignite0.createCache(cacheBCfg());
    assertFalse(spi.queryLocalSql(spi.schema(typeAA.cacheName()), "select * from A.A", null, Collections.emptySet(), typeAA.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), "select * from A.B", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select * from B.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select * from B.A, A.B, A.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
    try {
        spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select aa.*, ab.*, ba.* from A.A aa, A.B ab, B.A ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext();
        fail("Enumerations of aliases in select block must be prohibited");
    } catch (IgniteCheckedException ignored) {
    // all fine
    }
    assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), "select ab.* from A.B ab", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select   ba.*   from B.A  as ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
    cacheA.put(1, aa("A", 1, "Vasya", 10).build());
    cacheA.put(1, ab(1, "Vasya", 20, "Some text about Vasya goes here.").build());
    cacheB.put(1, ba(2, "Petya", 25, true).build());
    cacheB.put(1, ba(2, "Kolya", 25, true).build());
    cacheA.put(2, aa("A", 2, "Valera", 19).build());
    cacheA.put(3, aa("A", 3, "Borya", 18).build());
    cacheA.put(4, ab(4, "Vitalya", 20, "Very Good guy").build());
    // Query data.
    Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), "from a order by age", null, Collections.emptySet(), typeAA.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), "select aa.* from a aa order by aa.age", null, Collections.emptySet(), typeAA.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), "from b order by name", null, Collections.emptySet(), typeAB.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), "select bb.* from b as bb order by bb.name", null, Collections.emptySet(), typeAB.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeBA.cacheName()), "from a", null, Collections.emptySet(), typeBA.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(ba(2, "Kolya", 25, true).build(), value(res.next()));
    assertFalse(res.hasNext());
    // Text queries
    Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> txtRes = spi.queryLocalText(spi.schema(typeAB.cacheName()), "good", typeAB.name(), null);
    assertTrue(txtRes.hasNext());
    assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(txtRes.next()));
    assertFalse(txtRes.hasNext());
    // Fields query
    GridQueryFieldsResult fieldsRes = spi.queryLocalSqlFields(spi.schema("A"), "select a.a.name n1, a.a.age a1, b.a.name n2, " + "b.a.age a2 from a.a, b.a where a.a.id = b.a.id ", Collections.emptySet(), null, false, 0, null);
    String[] aliases = { "N1", "A1", "N2", "A2" };
    Object[] vals = { "Valera", 19, "Kolya", 25 };
    IgniteSpiCloseableIterator<List<?>> it = fieldsRes.iterator();
    assertTrue(it.hasNext());
    List<?> fields = it.next();
    assertEquals(4, fields.size());
    int i = 0;
    for (Object f : fields) {
        assertEquals(aliases[i], fieldsRes.metaData().get(i).fieldName());
        assertEquals(vals[i++], f);
    }
    assertFalse(it.hasNext());
    // Remove
    cacheA.remove(2);
    cacheB.remove(1);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class IgniteNode method loadConfiguration.

/**
     * @param springCfgPath Spring configuration file path.
     * @return Tuple with grid configuration and Spring application context.
     * @throws Exception If failed.
     */
public static IgniteBiTuple<IgniteConfiguration, ? extends ApplicationContext> loadConfiguration(String springCfgPath) throws Exception {
    URL url;
    try {
        url = new URL(springCfgPath);
    } catch (MalformedURLException e) {
        url = IgniteUtils.resolveIgniteUrl(springCfgPath);
        if (url == null)
            throw new IgniteCheckedException("Spring XML configuration path is invalid: " + springCfgPath + ". Note that this path should be either absolute or a relative local file system path, " + "relative to META-INF in classpath or valid URL to IGNITE_HOME.", e);
    }
    GenericApplicationContext springCtx;
    try {
        springCtx = new GenericApplicationContext();
        new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));
        springCtx.refresh();
    } catch (BeansException e) {
        throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e);
    }
    Map<String, IgniteConfiguration> cfgMap;
    try {
        cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class);
    } catch (BeansException e) {
        throw new Exception("Failed to instantiate bean [type=" + IgniteConfiguration.class + ", err=" + e.getMessage() + ']', e);
    }
    if (cfgMap == null || cfgMap.isEmpty())
        throw new Exception("Failed to find ignite configuration in: " + url);
    return new IgniteBiTuple<>(cfgMap.values().iterator().next(), springCtx);
}
Also used : MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) UrlResource(org.springframework.core.io.UrlResource) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BeansException(org.springframework.beans.BeansException) BeansException(org.springframework.beans.BeansException)

Example 30 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridDhtAtomicCache method updateAllAsyncInternal0.

/**
     * Executes local update after preloader fetched values.
     *
     * @param nodeId Node ID.
     * @param req Update request.
     * @param completionCb Completion callback.
     */
private void updateAllAsyncInternal0(UUID nodeId, GridNearAtomicAbstractUpdateRequest req, UpdateReplyClosure completionCb) {
    ClusterNode node = ctx.discovery().node(nodeId);
    if (node == null) {
        U.warn(msgLog, "Skip near update request, node originated update request left [" + "futId=" + req.futureId() + ", node=" + nodeId + ']');
        return;
    }
    GridNearAtomicUpdateResponse res = new GridNearAtomicUpdateResponse(ctx.cacheId(), nodeId, req.futureId(), req.partition(), false, ctx.deploymentEnabled());
    assert !req.returnValue() || (req.operation() == TRANSFORM || req.size() == 1);
    GridDhtAtomicAbstractUpdateFuture dhtFut = null;
    boolean remap = false;
    String taskName = ctx.kernalContext().task().resolveTaskName(req.taskNameHash());
    IgniteCacheExpiryPolicy expiry = null;
    try {
        // If batch store update is enabled, we need to lock all entries.
        // First, need to acquire locks on cache entries, then check filter.
        List<GridDhtCacheEntry> locked = null;
        Collection<IgniteBiTuple<GridDhtCacheEntry, GridCacheVersion>> deleted = null;
        try {
            GridDhtPartitionTopology top = topology();
            top.readLock();
            try {
                if (top.stopping()) {
                    res.addFailedKeys(req.keys(), new IgniteCheckedException("Failed to perform cache operation " + "(cache is stopped): " + name()));
                    completionCb.apply(req, res);
                    return;
                }
                // external transaction or explicit lock.
                if (req.topologyLocked() || !needRemap(req.topologyVersion(), top.topologyVersion())) {
                    ctx.shared().database().ensureFreeSpace(ctx.memoryPolicy());
                    locked = lockEntries(req, req.topologyVersion());
                    boolean hasNear = ctx.discovery().cacheNearNode(node, name());
                    // Assign next version for update inside entries lock.
                    GridCacheVersion ver = ctx.versions().next(top.topologyVersion());
                    if (hasNear)
                        res.nearVersion(ver);
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("Assigned update version [futId=" + req.futureId() + ", writeVer=" + ver + ']');
                    }
                    assert ver != null : "Got null version for update request: " + req;
                    boolean sndPrevVal = !top.rebalanceFinished(req.topologyVersion());
                    dhtFut = createDhtFuture(ver, req);
                    expiry = expiryPolicy(req.expiry());
                    GridCacheReturn retVal = null;
                    if (// Several keys ...
                    req.size() > 1 && writeThrough() && // and store is enabled ...
                    !req.skipStore() && // and this is not local store ...
                    !ctx.store().isLocal() && // (conflict resolver should be used for local store)
                    !// and no DR.
                    ctx.dr().receiveEnabled()) {
                        // This method can only be used when there are no replicated entries in the batch.
                        UpdateBatchResult updRes = updateWithBatch(node, hasNear, req, res, locked, ver, dhtFut, ctx.isDrEnabled(), taskName, expiry, sndPrevVal);
                        deleted = updRes.deleted();
                        dhtFut = updRes.dhtFuture();
                        if (req.operation() == TRANSFORM)
                            retVal = updRes.invokeResults();
                    } else {
                        UpdateSingleResult updRes = updateSingle(node, hasNear, req, res, locked, ver, dhtFut, ctx.isDrEnabled(), taskName, expiry, sndPrevVal);
                        retVal = updRes.returnValue();
                        deleted = updRes.deleted();
                        dhtFut = updRes.dhtFuture();
                    }
                    if (retVal == null)
                        retVal = new GridCacheReturn(ctx, node.isLocal(), true, null, true);
                    res.returnValue(retVal);
                    if (dhtFut != null) {
                        if (req.writeSynchronizationMode() == PRIMARY_SYNC && // To avoid deadlock disable back-pressure for sender data node.
                        !ctx.discovery().cacheAffinityNode(ctx.discovery().node(nodeId), ctx.name()) && !dhtFut.isDone()) {
                            final IgniteRunnable tracker = GridNioBackPressureControl.threadTracker();
                            if (tracker != null && tracker instanceof GridNioMessageTracker) {
                                ((GridNioMessageTracker) tracker).onMessageReceived();
                                dhtFut.listen(new IgniteInClosure<IgniteInternalFuture<Void>>() {

                                    @Override
                                    public void apply(IgniteInternalFuture<Void> fut) {
                                        ((GridNioMessageTracker) tracker).onMessageProcessed();
                                    }
                                });
                            }
                        }
                        ctx.mvcc().addAtomicFuture(dhtFut.id(), dhtFut);
                    }
                } else {
                    // Should remap all keys.
                    remap = true;
                    res.remapTopologyVersion(top.topologyVersion());
                }
            } finally {
                top.readUnlock();
            }
        } catch (GridCacheEntryRemovedException e) {
            assert false : "Entry should not become obsolete while holding lock.";
            e.printStackTrace();
        } finally {
            if (locked != null)
                unlockEntries(locked, req.topologyVersion());
            // Enqueue if necessary after locks release.
            if (deleted != null) {
                assert !deleted.isEmpty();
                assert ctx.deferredDelete() : this;
                for (IgniteBiTuple<GridDhtCacheEntry, GridCacheVersion> e : deleted) ctx.onDeferredDelete(e.get1(), e.get2());
            }
            // TODO fire events only after successful fsync
            if (ctx.shared().wal() != null)
                ctx.shared().wal().fsync(null);
        }
    } catch (GridDhtInvalidPartitionException ignore) {
        if (log.isDebugEnabled())
            log.debug("Caught invalid partition exception for cache entry (will remap update request): " + req);
        remap = true;
        res.remapTopologyVersion(ctx.topology().topologyVersion());
    } catch (Throwable e) {
        // At least RuntimeException can be thrown by the code above when GridCacheContext is cleaned and there is
        // an attempt to use cleaned resources.
        U.error(log, "Unexpected exception during cache update", e);
        res.addFailedKeys(req.keys(), e);
        completionCb.apply(req, res);
        if (e instanceof Error)
            throw (Error) e;
        return;
    }
    if (remap) {
        assert dhtFut == null;
        completionCb.apply(req, res);
    } else if (dhtFut != null)
        dhtFut.map(node, res.returnValue(), res, completionCb);
    if (req.writeSynchronizationMode() != FULL_ASYNC)
        req.cleanup(!node.isLocal());
    sendTtlUpdateRequest(expiry);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridNioMessageTracker(org.apache.ignite.internal.util.nio.GridNioMessageTracker) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)64 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 IgniteException (org.apache.ignite.IgniteException)17 ArrayList (java.util.ArrayList)14 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 HashMap (java.util.HashMap)10 UUID (java.util.UUID)10 IOException (java.io.IOException)9 List (java.util.List)9 Map (java.util.Map)8 CacheStorePartialUpdateException (org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException)8 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)8 LinkedHashMap (java.util.LinkedHashMap)7 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Event (org.apache.ignite.events.Event)6 IgfsPath (org.apache.ignite.igfs.IgfsPath)6