Search in sources :

Example 41 with Message

use of org.apache.ignite.plugin.extensions.communication.Message in project ignite by apache.

the class MessageCodeGenerator method processFile.

/**
 * Recursively process provided file or directory.
 *
 * @param file File.
 * @param ldr Class loader.
 * @param prefixLen Path prefix length.
 * @param col Classes.
 * @throws Exception In case of error.
 */
@SuppressWarnings("unchecked")
private void processFile(File file, ClassLoader ldr, int prefixLen, Collection<Class<? extends Message>> col) throws Exception {
    assert file != null;
    assert ldr != null;
    assert prefixLen > 0;
    assert col != null;
    if (!file.exists())
        throw new FileNotFoundException("File doesn't exist: " + file);
    if (file.isDirectory()) {
        for (File f : file.listFiles()) processFile(f, ldr, prefixLen, col);
    } else {
        assert file.isFile();
        String path = file.getPath();
        if (path.endsWith(".class")) {
            String clsName = path.substring(prefixLen, path.length() - 6).replace(File.separatorChar, '.');
            Class<?> cls = Class.forName(clsName, false, ldr);
            if (cls.getDeclaringClass() == null && cls.getEnclosingClass() == null && !BASE_CLS.equals(cls) && BASE_CLS.isAssignableFrom(cls))
                col.add((Class<? extends Message>) cls);
        }
    }
}
Also used : Message(org.apache.ignite.plugin.extensions.communication.Message) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 42 with Message

use of org.apache.ignite.plugin.extensions.communication.Message in project ignite by apache.

the class MessageCodeGenerator method classes.

/**
 * Gets all direct marshallable classes.
 * First classes will be classes from {@code classesOrder} with same order
 * as ordered values. Other classes will be at the end and ordered by name
 * (with package prefix).
 * That orders need for saving {@code directType} value.
 *
 * @return Classes.
 * @throws Exception In case of error.
 */
private Collection<Class<? extends Message>> classes() throws Exception {
    Collection<Class<? extends Message>> col = new TreeSet<>(new Comparator<Class<? extends Message>>() {

        @Override
        public int compare(Class<? extends Message> c1, Class<? extends Message> c2) {
            return c1.getName().compareTo(c2.getName());
        }
    });
    ClassLoader ldr = getClass().getClassLoader();
    for (URL url : IgniteUtils.classLoaderUrls(ldr)) {
        File file = new File(url.toURI());
        int prefixLen = file.getPath().length() + 1;
        processFile(file, ldr, prefixLen, col);
    }
    return col;
}
Also used : Message(org.apache.ignite.plugin.extensions.communication.Message) TreeSet(java.util.TreeSet) File(java.io.File) URL(java.net.URL)

Example 43 with Message

use of org.apache.ignite.plugin.extensions.communication.Message in project ignite by apache.

the class CacheClientsConcurrentStartTest method doTest.

/**
 * @throws Exception If failed.
 */
private void doTest() throws Exception {
    final AtomicBoolean failed = new AtomicBoolean();
    startGrids(SRV_CNT);
    for (int i = 0; i < SRV_CNT; i++) {
        ((TestRecordingCommunicationSpi) ignite(i).configuration().getCommunicationSpi()).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridDhtPartitionsFullMessage) {
                    try {
                        U.sleep(ThreadLocalRandom.current().nextLong(500) + 100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        });
    }
    List<IgniteInternalFuture<?>> futs = new ArrayList<>();
    for (int i = 0; i < CLIENTS_CNT; i++) {
        final int idx = i;
        IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

            @Override
            public void run() {
                Random rnd = new Random();
                try {
                    Ignite ignite = startGrid(SRV_CNT + idx);
                    assertTrue(ignite.configuration().isClientMode());
                    for (int i = 0; i < CACHES / 2; i++) {
                        String cacheName = "cache-" + rnd.nextInt(CACHES);
                        IgniteCache<Object, Object> cache = getCache(ignite, cacheName);
                        cache.put(ignite.cluster().localNode().id(), UUID.randomUUID());
                        IgniteAtomicSequence seq = ignite.atomicSequence("seq-" + rnd.nextInt(20), 0, true);
                        seq.getAndIncrement();
                    }
                    while (!stopped) {
                        IgniteCache<Object, Object> cache = getCache(ignite, "cache-" + rnd.nextInt(CACHES));
                        int val = Math.abs(rnd.nextInt(100));
                        if (val >= 0 && val < 40)
                            cache.containsKey(ignite.cluster().localNode().id());
                        else if (val >= 40 && val < 80)
                            cache.get(ignite.cluster().localNode().id());
                        else
                            cache.put(ignite.cluster().localNode().id(), UUID.randomUUID());
                        Thread.sleep(10);
                    }
                } catch (Exception e) {
                    log.error("Unexpected error: " + e, e);
                    failed.set(true);
                }
            }
        }, 1, "client-thread");
        futs.add(fut);
    }
    Thread.sleep(10_000);
    stopped = true;
    for (IgniteInternalFuture<?> fut : futs) fut.get();
    assertFalse(failed.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) TcpDiscoveryCustomEventMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryCustomEventMessage) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) GridIoMessage(org.apache.ignite.internal.managers.communication.GridIoMessage) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite)

Example 44 with Message

use of org.apache.ignite.plugin.extensions.communication.Message in project ignite by apache.

the class GridTcpCommunicationSpiConcurrentConnectSelfTest method concurrentConnect.

/**
 * @param threads Number of threads.
 * @param msgPerThread Messages per thread.
 * @param iters Number of iterations.
 * @param sleep If {@code true} sleeps random time before starts send messages.
 * @param load Run load threads flag.
 * @throws Exception If failed.
 */
private void concurrentConnect(final int threads, final int msgPerThread, final int iters, final boolean sleep, boolean load) throws Exception {
    log.info("Concurrent connect [threads=" + threads + ", msgPerThread=" + msgPerThread + ", iters=" + iters + ", load=" + load + ", sleep=" + sleep + ']');
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> loadFut = null;
    if (load) {
        loadFut = GridTestUtils.runMultiThreadedAsync(new Callable<Long>() {

            @Override
            public Long call() throws Exception {
                long dummyRes = 0;
                List<String> list = new ArrayList<>();
                while (!stop.get()) {
                    for (int i = 0; i < 100; i++) {
                        String str = new String(new byte[i]);
                        list.add(str);
                        dummyRes += str.hashCode();
                    }
                    if (list.size() > 1000_000) {
                        list = new ArrayList<>();
                        System.gc();
                    }
                }
                return dummyRes;
            }
        }, 2, "test-load");
    }
    try {
        for (int i = 0; i < iters; i++) {
            log.info("Iteration: " + i);
            final AtomicInteger msgId = new AtomicInteger();
            final int expMsgs = threads * msgPerThread;
            CountDownLatch latch = new CountDownLatch(expMsgs);
            MessageListener lsnr = new MessageListener(latch);
            createSpis(lsnr);
            final AtomicInteger idx = new AtomicInteger();
            try {
                final Callable<Void> c = new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        int idx0 = idx.getAndIncrement();
                        Thread.currentThread().setName("Test thread [idx=" + idx0 + ", grid=" + (idx0 % 2) + ']');
                        CommunicationSpi<Message> spi = spis.get(idx0 % 2);
                        ClusterNode srcNode = nodes.get(idx0 % 2);
                        ClusterNode dstNode = nodes.get((idx0 + 1) % 2);
                        if (sleep) {
                            ThreadLocalRandom rnd = ThreadLocalRandom.current();
                            long millis = rnd.nextLong(10);
                            if (millis > 0)
                                Thread.sleep(millis);
                        }
                        for (int i = 0; i < msgPerThread; i++) spi.sendMessage(dstNode, new GridTestMessage(srcNode.id(), msgId.incrementAndGet(), 0));
                        return null;
                    }
                };
                List<Thread> threadsList = new ArrayList<>();
                final AtomicBoolean fail = new AtomicBoolean();
                final AtomicLong tId = new AtomicLong();
                for (int t = 0; t < threads; t++) {
                    Thread t0 = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                c.call();
                            } catch (Throwable e) {
                                log.error("Unexpected error: " + e, e);
                                fail.set(true);
                            }
                        }
                    }) {

                        @Override
                        public long getId() {
                            // Override getId to use all connections.
                            return tId.getAndIncrement();
                        }
                    };
                    threadsList.add(t0);
                    t0.start();
                }
                for (Thread t0 : threadsList) t0.join();
                assertTrue(latch.await(10, TimeUnit.SECONDS));
                for (CommunicationSpi spi : spis) {
                    ConcurrentMap<UUID, GridCommunicationClient> clients = U.field(spi, "clients");
                    assertEquals(1, clients.size());
                    final GridNioServer srv = U.field(spi, "nioSrvr");
                    final int conns = pairedConnections ? 2 : 1;
                    GridTestUtils.waitForCondition(new GridAbsPredicate() {

                        @Override
                        public boolean apply() {
                            Collection sessions = U.field(srv, "sessions");
                            return sessions.size() == conns * connectionsPerNode;
                        }
                    }, 5000);
                    Collection sessions = U.field(srv, "sessions");
                    assertEquals(conns * connectionsPerNode, sessions.size());
                }
                assertEquals(expMsgs, lsnr.cntr.get());
            } finally {
                stopSpis();
            }
        }
    } finally {
        stop.set(true);
        if (loadFut != null)
            loadFut.get();
    }
}
Also used : GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) CommunicationSpi(org.apache.ignite.spi.communication.CommunicationSpi) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) GridCommunicationClient(org.apache.ignite.internal.util.nio.GridCommunicationClient) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Collection(java.util.Collection)

Example 45 with Message

use of org.apache.ignite.plugin.extensions.communication.Message in project ignite by apache.

the class GridTcpCommunicationSpiConcurrentConnectSelfTest method stopSpis.

/**
 * @throws Exception If failed.
 */
private void stopSpis() throws Exception {
    if (timeoutProcessor != null) {
        timeoutProcessor.onKernalStop(true);
        timeoutProcessor.stop(true);
        timeoutProcessor = null;
    }
    for (CommunicationSpi<Message> spi : spis) {
        spi.onContextDestroyed();
        spi.setListener(null);
        spi.spiStop();
    }
    for (IgniteTestResources rsrcs : spiRsrcs) rsrcs.stopThreads();
}
Also used : GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) IgniteTestResources(org.apache.ignite.testframework.junits.IgniteTestResources)

Aggregations

Message (org.apache.ignite.plugin.extensions.communication.Message)56 ClusterNode (org.apache.ignite.cluster.ClusterNode)30 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 Ignite (org.apache.ignite.Ignite)14 IgniteException (org.apache.ignite.IgniteException)11 UUID (java.util.UUID)9 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)9 GridTestMessage (org.apache.ignite.spi.communication.GridTestMessage)9 GridDhtPartitionsFullMessage (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage)8 IgniteTestResources (org.apache.ignite.testframework.junits.IgniteTestResources)8 ArrayList (java.util.ArrayList)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 GridIoMessage (org.apache.ignite.internal.managers.communication.GridIoMessage)7 GridDhtPartitionSupplyMessage (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage)7 HashMap (java.util.HashMap)6 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)6 Collection (java.util.Collection)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5