Search in sources :

Example 51 with IgnitePredicate

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

the class TcpCommunicationSpi method resetNioServer.

/**
 * Recreates tpcSrvr socket instance.
 *
 * @return Server instance.
 * @throws IgniteCheckedException Thrown if it's not possible to create server.
 */
private GridNioServer<Message> resetNioServer() throws IgniteCheckedException {
    if (boundTcpPort >= 0)
        throw new IgniteCheckedException("Tcp NIO server was already created on port " + boundTcpPort);
    IgniteCheckedException lastEx = null;
    // If configured TCP port is busy, find first available in range.
    int lastPort = locPortRange == 0 ? locPort : locPort + locPortRange - 1;
    for (int port = locPort; port <= lastPort; port++) {
        try {
            MessageFactory msgFactory = new MessageFactory() {

                private MessageFactory impl;

                @Nullable
                @Override
                public Message create(short type) {
                    if (impl == null)
                        impl = getSpiContext().messageFactory();
                    assert impl != null;
                    return impl.create(type);
                }
            };
            GridNioMessageReaderFactory readerFactory = new GridNioMessageReaderFactory() {

                private MessageFormatter formatter;

                @Override
                public MessageReader reader(GridNioSession ses, MessageFactory msgFactory) throws IgniteCheckedException {
                    if (formatter == null)
                        formatter = getSpiContext().messageFormatter();
                    assert formatter != null;
                    ConnectionKey key = ses.meta(CONN_IDX_META);
                    return key != null ? formatter.reader(key.nodeId(), msgFactory) : null;
                }
            };
            GridNioMessageWriterFactory writerFactory = new GridNioMessageWriterFactory() {

                private MessageFormatter formatter;

                @Override
                public MessageWriter writer(GridNioSession ses) throws IgniteCheckedException {
                    if (formatter == null)
                        formatter = getSpiContext().messageFormatter();
                    assert formatter != null;
                    ConnectionKey key = ses.meta(CONN_IDX_META);
                    return key != null ? formatter.writer(key.nodeId()) : null;
                }
            };
            GridDirectParser parser = new GridDirectParser(log.getLogger(GridDirectParser.class), msgFactory, readerFactory);
            IgnitePredicate<Message> skipRecoveryPred = new IgnitePredicate<Message>() {

                @Override
                public boolean apply(Message msg) {
                    return msg instanceof RecoveryLastReceivedMessage;
                }
            };
            boolean clientMode = Boolean.TRUE.equals(ignite.configuration().isClientMode());
            IgniteBiInClosure<GridNioSession, Integer> queueSizeMonitor = !clientMode && slowClientQueueLimit > 0 ? new CI2<GridNioSession, Integer>() {

                @Override
                public void apply(GridNioSession ses, Integer qSize) {
                    checkClientQueueSize(ses, qSize);
                }
            } : null;
            GridNioFilter[] filters;
            if (isSslEnabled()) {
                GridNioSslFilter sslFilter = new GridNioSslFilter(ignite.configuration().getSslContextFactory().create(), true, ByteOrder.nativeOrder(), log);
                sslFilter.directMode(true);
                sslFilter.wantClientAuth(true);
                sslFilter.needClientAuth(true);
                filters = new GridNioFilter[] { new GridNioCodecFilter(parser, log, true), new GridConnectionBytesVerifyFilter(log), sslFilter };
            } else
                filters = new GridNioFilter[] { new GridNioCodecFilter(parser, log, true), new GridConnectionBytesVerifyFilter(log) };
            GridNioServer<Message> srvr = GridNioServer.<Message>builder().address(locHost).port(port).listener(srvLsnr).logger(log).selectorCount(selectorsCnt).igniteInstanceName(igniteInstanceName).serverName("tcp-comm").tcpNoDelay(tcpNoDelay).directBuffer(directBuf).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(sockSndBuf).socketReceiveBufferSize(sockRcvBuf).sendQueueLimit(msgQueueLimit).directMode(true).metricsListener(metricsLsnr).writeTimeout(sockWriteTimeout).selectorSpins(selectorSpins).filters(filters).writerFactory(writerFactory).skipRecoveryPredicate(skipRecoveryPred).messageQueueSizeListener(queueSizeMonitor).readWriteSelectorsAssign(usePairedConnections).build();
            boundTcpPort = port;
            // Ack Port the TCP server was bound to.
            if (log.isInfoEnabled()) {
                log.info("Successfully bound communication NIO server to TCP port " + "[port=" + boundTcpPort + ", locHost=" + locHost + ", selectorsCnt=" + selectorsCnt + ", selectorSpins=" + srvr.selectorSpins() + ", pairedConn=" + usePairedConnections + ']');
            }
            srvr.idleTimeout(idleConnTimeout);
            return srvr;
        } catch (IgniteCheckedException e) {
            if (X.hasCause(e, SSLException.class))
                throw new IgniteSpiException("Failed to create SSL context. SSL factory: " + ignite.configuration().getSslContextFactory() + '.', e);
            lastEx = e;
            if (log.isDebugEnabled())
                log.debug("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']');
            onException("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']', e);
        }
    }
    // If free port wasn't found.
    throw new IgniteCheckedException("Failed to bind to any port within range [startPort=" + locPort + ", portRange=" + locPortRange + ", locHost=" + locHost + ']', lastEx);
}
Also used : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) RecoveryLastReceivedMessage(org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) HandshakeMessage(org.apache.ignite.spi.communication.tcp.messages.HandshakeMessage) NodeIdMessage(org.apache.ignite.spi.communication.tcp.messages.NodeIdMessage) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) MessageFormatter(org.apache.ignite.plugin.extensions.communication.MessageFormatter) GridNioSslFilter(org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter) SSLException(javax.net.ssl.SSLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDirectParser(org.apache.ignite.internal.util.nio.GridDirectParser) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) GridNioMessageWriterFactory(org.apache.ignite.internal.util.nio.GridNioMessageWriterFactory) RecoveryLastReceivedMessage(org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) GridConnectionBytesVerifyFilter(org.apache.ignite.internal.util.nio.GridConnectionBytesVerifyFilter) IpcEndpoint(org.apache.ignite.internal.util.ipc.IpcEndpoint) IpcSharedMemoryServerEndpoint(org.apache.ignite.internal.util.ipc.shmem.IpcSharedMemoryServerEndpoint) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioMessageReaderFactory(org.apache.ignite.internal.util.nio.GridNioMessageReaderFactory)

Example 52 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testFileDataEvents.

/**
 * Checks events on file data transfer operations.
 *
 * @throws Exception If failed.
 */
public void testFileDataEvents() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 5;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    final IgfsPath file = new IgfsPath("/file1");
    final int dataSize = 1024;
    byte[] buf = new byte[dataSize];
    // Will generate IGFS_FILE_CREATED, IGFS_FILE_OPENED_WRITE, IGFS_FILE_CLOSED_WRITE.
    try (IgfsOutputStream os = igfs.create(file, false)) {
        // Will generate no events.
        os.write(buf);
    }
    // Will generate EVT_IGFS_FILE_OPENED_READ, IGFS_FILE_CLOSED_READ.
    try (IgfsInputStream is = igfs.open(file, 256)) {
        // Will generate no events.
        is.readFully(0, buf);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    assertEquals((long) dataSize, evt.dataSize());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_OPENED_READ, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_CLOSED_READ, evt.type());
    assertEquals(new IgfsPath("/file1"), evt.path());
    assertEquals((long) dataSize, evt.dataSize());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 53 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testTwoFiles.

/**
 * Checks events on CRUD operations on 2 files.
 *
 * @throws Exception If failed.
 */
public void testTwoFiles() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 4 + 3 + 2 + 2;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1");
    IgfsPath file1 = new IgfsPath(dir, "file1");
    // Will generate EVT_IGFS_FILE_CREATED event + EVT_IGFS_DIR_CREATED event + OPEN + CLOSE.
    igfs.create(file1, true).close();
    IgfsPath file2 = new IgfsPath(dir, "file2");
    // Will generate 1 EVT_IGFS_FILE_CREATED event + OPEN + CLOSE.
    igfs.create(file2, true).close();
    assertTrue(igfs.exists(dir));
    assertTrue(igfs.exists(file1));
    assertTrue(igfs.exists(file2));
    // Will generate 1 EVT_IGFS_FILE_DELETED and 1 EVT_IGFS_FILE_PURGED.
    assertTrue(igfs.delete(file1, false));
    // Same.
    assertTrue(igfs.delete(file2, false));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file1"), evt.path());
    assertEquals(0, evt.dataSize());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    evt = (IgfsEvent) evtList.get(5);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    evt = (IgfsEvent) evtList.get(6);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/file2"), evt.path());
    assertEquals(0, evt.dataSize());
    assertOneToOne(evtList.subList(7, 11), new EventPredicate(EVT_IGFS_FILE_DELETED, new IgfsPath("/dir1/file1")), new EventPredicate(EVT_IGFS_FILE_PURGED, new IgfsPath("/dir1/file1")), new EventPredicate(EVT_IGFS_FILE_DELETED, new IgfsPath("/dir1/file2")), new EventPredicate(EVT_IGFS_FILE_PURGED, new IgfsPath("/dir1/file2")));
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 54 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testSingleFileNestedDirs.

/**
 * Checks events on CRUD operations on a single file in nested directories.
 *
 * @throws Exception If failed.
 */
public void testSingleFileNestedDirs() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 6 + 1 + 1;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1/dir2/dir3");
    IgfsPath file = new IgfsPath(dir, "file1");
    // Will generate 3 EVT_IGFS_DIR_CREATED + EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
    // EVT_IGFS_FILE_CLOSED and a number of EVT_IGFS_META_UPDATED.
    igfs.create(file, true).close();
    IgfsPath mvFile = new IgfsPath(dir, "mvFile1");
    // Will generate EVT_IGFS_FILE_RENAMED.
    igfs.rename(file, mvFile);
    // Will generate EVT_IGFS_DIR_DELETED event.
    assertTrue(igfs.delete(dir.parent(), true));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3"), evt.path());
    evt = (IgfsEvent) evtList.get(3);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    assertFalse(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(4);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    evt = (IgfsEvent) evtList.get(5);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    assertEquals(0, evt.dataSize());
    evt = (IgfsEvent) evtList.get(6);
    assertEquals(EVT_IGFS_FILE_RENAMED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
    assertEquals(new IgfsPath("/dir1/dir2/dir3/mvFile1"), evt.newPath());
    evt = (IgfsEvent) evtList.get(7);
    assertEquals(EVT_IGFS_DIR_DELETED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 55 with IgnitePredicate

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

the class ClusterGroupAbstractTest method testExecution.

/**
 * @throws Exception If test failed.
 */
public void testExecution() throws Exception {
    String name = "oneMoreGrid";
    Collection<IgniteBiTuple<Ignite, IgnitePredicate<Event>>> lsnrs = new LinkedList<>();
    try {
        final AtomicInteger cnt = new AtomicInteger();
        Ignite g = startGrid(name);
        IgnitePredicate<Event> lsnr;
        if (!IgniteCluster.class.isAssignableFrom(projection().getClass())) {
            g.events().localListen(lsnr = new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    assert evt.type() == EVT_JOB_STARTED;
                    assert false;
                    return true;
                }
            }, EVT_JOB_STARTED);
            lsnrs.add(F.t(g, lsnr));
        }
        for (ClusterNode node : prj.nodes()) {
            g = G.ignite(node.id());
            g.events().localListen(lsnr = new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    assert evt.type() == EVT_JOB_STARTED;
                    synchronized (mux) {
                        cnt.incrementAndGet();
                        mux.notifyAll();
                    }
                    return true;
                }
            }, EVT_JOB_STARTED);
            lsnrs.add(F.t(g, lsnr));
        }
        run1(cnt);
        run2(cnt);
        call1(cnt);
        call2(cnt);
        call3(cnt);
        call4(cnt);
        call5(cnt);
        forkjoin1(cnt);
        forkjoin2(cnt);
        exec1(cnt);
        exec2(cnt);
        executorService(cnt);
        checkActiveFutures();
    } finally {
        for (IgniteBiTuple<Ignite, IgnitePredicate<Event>> t : lsnrs) t.get1().events().stopLocalListen(t.get2(), EVT_JOB_STARTED);
        stopGrid(name);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCluster(org.apache.ignite.IgniteCluster) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) LinkedList(java.util.LinkedList)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)81 Event (org.apache.ignite.events.Event)44 CountDownLatch (java.util.concurrent.CountDownLatch)35 Ignite (org.apache.ignite.Ignite)32 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)18 ArrayList (java.util.ArrayList)15 Ignition (org.apache.ignite.Ignition)10 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)10 Collection (java.util.Collection)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)8 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)7 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7