Search in sources :

Example 61 with IgniteBiTuple

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

the class IgfsAbstractSelfTest method checkDeadlocks.

/**
     * Check deadlocks by creating complex directories structure and then executing chaotic operations on it. A lot of
     * exception are expected here. We are not interested in them. Instead, we want to ensure that no deadlocks occur
     * during execution.
     *
     * @param lvlCnt Total levels in folder hierarchy.
     * @param childrenDirPerLvl How many children directories to create per level.
     * @param childrenFilePerLvl How many children file to create per level.
     * @param primaryLvlCnt How many levels will exist in the primary file system before check start.
     * @param renCnt How many renames to perform.
     * @param delCnt How many deletes to perform.
     * @param updateCnt How many updates to perform.
     * @param mkdirsCnt How many directory creations to perform.
     * @param createCnt How many file creations to perform.
     * @throws Exception If failed.
     */
@SuppressWarnings("ConstantConditions")
private void checkDeadlocks(final int lvlCnt, final int childrenDirPerLvl, final int childrenFilePerLvl, int primaryLvlCnt, int renCnt, int delCnt, int updateCnt, int mkdirsCnt, int createCnt) throws Exception {
    assert childrenDirPerLvl > 0;
    // First define file system structure.
    final Map<Integer, List<IgfsPath>> dirPaths = new HashMap<>();
    final Map<Integer, List<IgfsPath>> filePaths = new HashMap<>();
    Queue<IgniteBiTuple<Integer, IgfsPath>> queue = new ArrayDeque<>();
    // Add root directory.
    queue.add(F.t(0, IgfsPath.ROOT));
    while (!queue.isEmpty()) {
        IgniteBiTuple<Integer, IgfsPath> entry = queue.poll();
        int lvl = entry.getKey();
        if (lvl < lvlCnt) {
            int newLvl = lvl + 1;
            for (int i = 0; i < childrenDirPerLvl; i++) {
                IgfsPath path = new IgfsPath(entry.getValue(), "dir-" + newLvl + "-" + i);
                queue.add(F.t(newLvl, path));
                if (!dirPaths.containsKey(newLvl))
                    dirPaths.put(newLvl, new ArrayList<IgfsPath>());
                dirPaths.get(newLvl).add(path);
            }
            for (int i = 0; i < childrenFilePerLvl; i++) {
                IgfsPath path = new IgfsPath(entry.getValue(), "file-" + newLvl + "-" + i);
                if (!filePaths.containsKey(newLvl))
                    filePaths.put(newLvl, new ArrayList<IgfsPath>());
                filePaths.get(newLvl).add(path);
            }
        }
    }
    // Now as we have all paths defined, plan operations on them.
    final Random rand = new Random(SEED);
    final int totalOpCnt = renCnt + delCnt + updateCnt + mkdirsCnt + createCnt;
    if (totalOpCnt == 0)
        throw new RuntimeException("Operations count is zero.");
    final CyclicBarrier barrier = new CyclicBarrier(totalOpCnt);
    Collection<Thread> threads = new ArrayList<>(totalOpCnt);
    // Renames.
    for (int i = 0; i < renCnt; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    int fromLvl = rand.nextInt(lvlCnt) + 1;
                    int toLvl = rand.nextInt(lvlCnt) + 1;
                    List<IgfsPath> fromPaths;
                    List<IgfsPath> toPaths;
                    if (rand.nextInt(childrenDirPerLvl + childrenFilePerLvl) < childrenDirPerLvl) {
                        // Rename directories.
                        fromPaths = dirPaths.get(fromLvl);
                        toPaths = dirPaths.get(toLvl);
                    } else {
                        // Rename files.
                        fromPaths = filePaths.get(fromLvl);
                        toPaths = filePaths.get(toLvl);
                    }
                    IgfsPath fromPath = fromPaths.get(rand.nextInt(fromPaths.size()));
                    IgfsPath toPath = toPaths.get(rand.nextInt(toPaths.size()));
                    U.awaitQuiet(barrier);
                    igfs.rename(fromPath, toPath);
                } catch (IgniteException ignore) {
                // No-op.
                }
            }
        };
        threads.add(new Thread(r));
    }
    // Deletes.
    for (int i = 0; i < delCnt; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    int lvl = rand.nextInt(lvlCnt) + 1;
                    IgfsPath path = rand.nextInt(childrenDirPerLvl + childrenFilePerLvl) < childrenDirPerLvl ? dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size())) : filePaths.get(lvl).get(rand.nextInt(filePaths.get(lvl).size()));
                    U.awaitQuiet(barrier);
                    igfs.delete(path, true);
                } catch (IgniteException ignore) {
                // No-op.
                }
            }
        };
        threads.add(new Thread(r));
    }
    // Updates.
    for (int i = 0; i < updateCnt; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    int lvl = rand.nextInt(lvlCnt) + 1;
                    IgfsPath path = rand.nextInt(childrenDirPerLvl + childrenFilePerLvl) < childrenDirPerLvl ? dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size())) : filePaths.get(lvl).get(rand.nextInt(filePaths.get(lvl).size()));
                    U.awaitQuiet(barrier);
                    igfs.update(path, properties("owner", "group", null));
                } catch (IgniteException ignore) {
                // No-op.
                }
            }
        };
        threads.add(new Thread(r));
    }
    // Directory creations.
    final AtomicInteger dirCtr = new AtomicInteger();
    for (int i = 0; i < mkdirsCnt; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    int lvl = rand.nextInt(lvlCnt) + 1;
                    IgfsPath parentPath = dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size()));
                    IgfsPath path = new IgfsPath(parentPath, "newDir-" + dirCtr.incrementAndGet());
                    U.awaitQuiet(barrier);
                    igfs.mkdirs(path);
                } catch (IgniteException ignore) {
                // No-op.
                }
            }
        };
        threads.add(new Thread(r));
    }
    // File creations.
    final AtomicInteger fileCtr = new AtomicInteger();
    for (int i = 0; i < createCnt; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    int lvl = rand.nextInt(lvlCnt) + 1;
                    IgfsPath parentPath = dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size()));
                    IgfsPath path = new IgfsPath(parentPath, "newFile-" + fileCtr.incrementAndGet());
                    U.awaitQuiet(barrier);
                    IgfsOutputStream os = null;
                    try {
                        os = igfs.create(path, true);
                        os.write(chunk);
                    } finally {
                        U.closeQuiet(os);
                    }
                } catch (IOException | IgniteException ignore) {
                // No-op.
                }
            }
        };
        threads.add(new Thread(r));
    }
    // Create file/directory structure.
    for (int i = 0; i < lvlCnt; i++) {
        int lvl = i + 1;
        boolean targetToPrimary = !dual || lvl <= primaryLvlCnt;
        IgfsPath[] dirs = dirPaths.get(lvl).toArray(new IgfsPath[dirPaths.get(lvl).size()]);
        IgfsPath[] files = filePaths.get(lvl).toArray(new IgfsPath[filePaths.get(lvl).size()]);
        if (targetToPrimary)
            create(igfs, dirs, files);
        else
            create(igfsSecondary, dirs, files);
    }
    // Start all threads and wait for them to finish.
    for (Thread thread : threads) thread.start();
    U.joinThreads(threads, null);
}
Also used : HashMap(java.util.HashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) Random(java.util.Random) IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream) ArrayDeque(java.util.ArrayDeque) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgfsPath(org.apache.ignite.igfs.IgfsPath) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 62 with IgniteBiTuple

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestAtomicStamped.

/**
     * Tests atomic stamped value.
     *
     * @param topWorker Topology change worker.
     * @throws Exception If failed.
     */
private void doTestAtomicStamped(ConstantTopologyChangeWorker topWorker) throws Exception {
    try (IgniteAtomicStamped<Integer, Integer> s = grid(0).atomicStamped(STRUCTURE_NAME, 1, 1, true)) {
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {

            @Override
            public Object apply(Ignite ignite) {
                IgniteBiTuple<Integer, Integer> t = ignite.atomicStamped(STRUCTURE_NAME, 1, 1, false).get();
                assert t.get1() > 0;
                assert t.get2() > 0;
                return null;
            }
        });
        int val = s.value();
        while (!fut.isDone()) {
            IgniteBiTuple<Integer, Integer> t = s.get();
            assertEquals(val, (int) t.get1());
            assertEquals(val, (int) t.get2());
            ++val;
            s.set(val, val);
        }
        fut.get();
        for (Ignite g : G.allGrids()) {
            IgniteBiTuple<Integer, Integer> t = g.atomicStamped(STRUCTURE_NAME, 1, 1, false).get();
            assertEquals(val, (int) t.get1());
            assertEquals(val, (int) t.get2());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignite(org.apache.ignite.Ignite)

Example 63 with IgniteBiTuple

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

the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.

/**
     * @throws Exception If failed.
     */
public void testMultithreadedWithNodeRestart() throws Exception {
    final AtomicBoolean stop = new AtomicBoolean();
    final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
    final Collection<UUID> started = new GridConcurrentHashSet<>();
    final Collection<UUID> stopped = new GridConcurrentHashSet<>();
    final Random rnd = new Random();
    IgniteInternalFuture<?> starterFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < CONSUME_CNT; i++) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    IgniteEvents evts = grid(idx).events();
                    UUID consumeId = evts.remoteListenAsync(new P2<UUID, Event>() {

                        @Override
                        public boolean apply(UUID uuid, Event evt) {
                            return true;
                        }
                    }, null, EVT_JOB_STARTED).get(3000);
                    started.add(consumeId);
                    queue.add(F.t(idx, consumeId));
                } catch (ClusterTopologyException ignored) {
                // No-op.
                }
                U.sleep(10);
            }
            stop.set(true);
            return null;
        }
    }, 8, "consume-starter");
    IgniteInternalFuture<?> stopperFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
                if (t == null)
                    continue;
                int idx = t.get1();
                UUID consumeId = t.get2();
                try {
                    IgniteEvents evts = grid(idx).events();
                    evts.stopRemoteListenAsync(consumeId).get(3000);
                    stopped.add(consumeId);
                } catch (ClusterTopologyException ignored) {
                // No-op.
                }
            }
            return null;
        }
    }, 4, "consume-stopper");
    IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                startGrid("anotherGrid");
                stopGrid("anotherGrid");
            }
            return null;
        }
    }, 1, "node-restarter");
    IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    grid(idx).compute().runAsync(F.noop()).get(3000);
                } catch (IgniteException ignored) {
                // Ignore all job execution related errors.
                }
            }
            return null;
        }
    }, 1, "job-runner");
    starterFut.get();
    stopperFut.get();
    nodeRestarterFut.get();
    jobRunnerFut.get();
    IgniteBiTuple<Integer, UUID> t;
    while ((t = queue.poll()) != null) {
        int idx = t.get1();
        UUID consumeId = t.get2();
        grid(idx).events().stopRemoteListenAsync(consumeId).get(3000);
        stopped.add(consumeId);
    }
    Collection<UUID> notStopped = F.lose(started, true, stopped);
    assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) IgniteEvents(org.apache.ignite.IgniteEvents) IgniteException(org.apache.ignite.IgniteException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) UUID(java.util.UUID)

Example 64 with IgniteBiTuple

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

the class GridRestProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    if (isRestEnabled()) {
        if (notStartOnClient()) {
            U.quietAndInfo(log, "REST protocols do not start on client node. " + "To start the protocols on client node set '-DIGNITE_REST_START_ON_CLIENT=true' system property.");
            return;
        }
        // Register handlers.
        addHandler(new GridCacheCommandHandler(ctx));
        addHandler(new GridTaskCommandHandler(ctx));
        addHandler(new GridTopologyCommandHandler(ctx));
        addHandler(new GridVersionCommandHandler(ctx));
        addHandler(new DataStructuresCommandHandler(ctx));
        addHandler(new QueryCommandHandler(ctx));
        addHandler(new GridLogCommandHandler(ctx));
        addHandler(new GridChangeStateCommandHandler(ctx));
        // Start protocols.
        startTcpProtocol();
        startHttpProtocol();
        for (GridRestProtocol proto : protos) {
            Collection<IgniteBiTuple<String, Object>> props = proto.getProperties();
            if (props != null) {
                for (IgniteBiTuple<String, Object> p : props) {
                    String key = p.getKey();
                    if (key == null)
                        continue;
                    if (ctx.hasNodeAttribute(key))
                        throw new IgniteCheckedException("Node attribute collision for attribute [processor=GridRestProcessor, attr=" + key + ']');
                    ctx.addNodeAttribute(key, p.getValue());
                }
            }
        }
    }
}
Also used : GridCacheCommandHandler(org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheCommandHandler) GridTaskCommandHandler(org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridVersionCommandHandler(org.apache.ignite.internal.processors.rest.handlers.version.GridVersionCommandHandler) DataStructuresCommandHandler(org.apache.ignite.internal.processors.rest.handlers.datastructures.DataStructuresCommandHandler) GridTopologyCommandHandler(org.apache.ignite.internal.processors.rest.handlers.top.GridTopologyCommandHandler) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridLogCommandHandler(org.apache.ignite.internal.processors.rest.handlers.log.GridLogCommandHandler) GridChangeStateCommandHandler(org.apache.ignite.internal.processors.rest.handlers.cluster.GridChangeStateCommandHandler) QueryCommandHandler(org.apache.ignite.internal.processors.rest.handlers.query.QueryCommandHandler)

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