Search in sources :

Example 6 with ByteChannel

use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.

the class TCPReceiverThread method run.

// The Run Method.
// Started by main() on a single thread, this code manages reading TCP requests
@SuppressWarnings("resource")
public void run() {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    ServerSocketChannel errsock = null;
    boolean saw_error = false;
    while (true) {
        try {
            // Cleanup from any prior socket failures.  Rare unless we're really sick.
            if (errsock != null) {
                // One time attempt a socket close
                final ServerSocketChannel tmp2 = errsock;
                errsock = null;
                // Could throw, but errsock cleared for next pass
                tmp2.close();
            }
            // prevent deny-of-service endless socket-creates
            if (saw_error)
                Thread.sleep(100);
            saw_error = false;
            // More common-case setup of a ServerSocket
            if (SOCK == null) {
                SOCK = ServerSocketChannel.open();
                SOCK.socket().setReceiveBufferSize(AutoBuffer.BBP_BIG._size);
                SOCK.socket().bind(H2O.SELF._key);
            }
            // Block for TCP connection and setup to read from it.
            SocketChannel sock = SOCK.accept();
            ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
            ByteChannel wrappedSocket = socketChannelFactory.serverChannel(sock);
            bb.limit(bb.capacity());
            bb.position(0);
            while (bb.hasRemaining()) {
                // read first 8 bytes
                wrappedSocket.read(bb);
            }
            bb.flip();
            // 1 - small , 2 - big
            int chanType = bb.get();
            int port = bb.getChar();
            int sentinel = (0xFF) & bb.get();
            if (sentinel != 0xef) {
                if (H2O.SELF.getSecurityManager().securityEnabled) {
                    throw new IOException("Missing EOM sentinel when opening new SSL tcp channel.");
                } else {
                    throw H2O.fail("missing eom sentinel when opening new tcp channel");
                }
            }
            // todo compare against current cloud, refuse the con if no match
            // Do H2O.Intern in corresponding case branch, we can't do H2O.intern here since it wouldn't work
            // with ExternalFrameHandling ( we don't send the same information there as with the other communication)
            InetAddress inetAddress = sock.socket().getInetAddress();
            // Pass off the TCP connection to a separate reader thread
            switch(chanType) {
                case TCP_SMALL:
                    H2ONode h2o = H2ONode.intern(inetAddress, port);
                    new UDP_TCP_ReaderThread(h2o, wrappedSocket).start();
                    break;
                case TCP_BIG:
                    new TCPReaderThread(wrappedSocket, new AutoBuffer(wrappedSocket, inetAddress), inetAddress).start();
                    break;
                case TCP_EXTERNAL:
                    new ExternalFrameHandlerThread(wrappedSocket, new AutoBuffer(wrappedSocket, null)).start();
                    break;
                default:
                    throw H2O.fail("unexpected channel type " + chanType + ", only know 1 - Small, 2 - Big and 3 - ExternalFrameHandling");
            }
        } catch (java.nio.channels.AsynchronousCloseException ex) {
            // Socket closed for shutdown
            break;
        } catch (Exception e) {
            e.printStackTrace();
            // On any error from anybody, close all sockets & re-open
            Log.err("IO error on TCP port " + H2O.H2O_PORT + ": ", e);
            saw_error = true;
            // Signal error recovery on the next loop
            errsock = SOCK;
            // Signal error recovery on the next loop
            SOCK = null;
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ByteChannel(java.nio.channels.ByteChannel) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 7 with ByteChannel

use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.

the class ExternalFrameWriterClientTest method testWriting.

@Test
public void testWriting() throws IOException {
    final String[] nodes = new String[H2O.CLOUD._memary.length];
    // get ip and ports of h2o nodes
    for (int i = 0; i < nodes.length; i++) {
        nodes[i] = H2O.CLOUD._memary[i].getIpPortString();
    }
    // we will open 2 connection per h2o node
    final String[] connStrings = ArrayUtils.join(nodes, nodes);
    // The api expects that empty frame has to be in the DKV before we start working with it
    final String frameName = "fr";
    String[] colNames = { "NUM", "BOOL", "STR", "TIMESTAMP" };
    // vector types are inferred from expected types
    final byte[] expectedTypes = ExternalFrameUtils.prepareExpectedTypes(new Class[] { Integer.class, Boolean.class, String.class, Timestamp.class });
    ChunkUtils.initFrame(frameName, colNames);
    // number of chunks will be number of h2o nodes
    final long[] rowsPerChunk = new long[connStrings.length];
    Thread[] threads = new Thread[connStrings.length];
    // open all connections in connStrings array
    for (int idx = 0; idx < connStrings.length; idx++) {
        final int currentIndex = idx;
        threads[idx] = new Thread() {

            @Override
            public void run() {
                try {
                    ByteChannel sock = ExternalFrameUtils.getConnection(connStrings[currentIndex]);
                    ExternalFrameWriterClient writer = new ExternalFrameWriterClient(sock);
                    writer.createChunks(frameName, expectedTypes, currentIndex, 1000);
                    Timestamp time = new Timestamp(Calendar.getInstance().getTime().getTime());
                    for (int i = 0; i < 997; i++) {
                        writer.sendInt(i);
                        writer.sendBoolean(true);
                        writer.sendString("str_" + i);
                        writer.sendTimestamp(time);
                    }
                    writer.sendInt(0);
                    writer.sendBoolean(true);
                    writer.sendString(null);
                    writer.sendTimestamp(time);
                    writer.sendInt(1);
                    writer.sendBoolean(true);
                    writer.sendString("€");
                    writer.sendTimestamp(time);
                    // send NA for all columns
                    writer.sendNA();
                    writer.sendNA();
                    writer.sendNA();
                    writer.sendNA();
                    writer.waitUntilAllWritten();
                    sock.close();
                    rowsPerChunk[currentIndex] = 1000;
                } catch (IOException ignore) {
                }
            }
        };
        threads[idx].start();
    }
    // wait for all writer thread to finish
    for (Thread t : threads) {
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    ChunkUtils.finalizeFrame(frameName, rowsPerChunk, ExternalFrameUtils.vecTypesFromExpectedTypes(expectedTypes), null);
    Frame frame = null;
    try {
        frame = DKV.getGet(frameName);
        assertEquals(frame.anyVec().nChunks(), connStrings.length);
        assertEquals(frame._names.length, 4);
        assertEquals(frame.numCols(), 4);
        assertEquals(frame._names[0], "NUM");
        assertEquals(frame._names[1], "BOOL");
        assertEquals(frame._names[2], "STR");
        assertEquals(frame._names[3], "TIMESTAMP");
        assertEquals(frame.vec(0).get_type(), Vec.T_NUM);
        assertEquals(frame.vec(1).get_type(), Vec.T_NUM);
        assertEquals(frame.vec(2).get_type(), Vec.T_STR);
        assertEquals(frame.vec(3).get_type(), Vec.T_TIME);
        assertEquals(frame.numRows(), 1000 * connStrings.length);
        // last row should be NA
        assertEquals(frame.vec(0).at8(0), 0);
        BufferedString buff = new BufferedString();
        assertEquals(frame.vec(2).atStr(buff, 996).toString(), "str_996");
        assertEquals(frame.vec(2).atStr(buff, 997), null);
        assertEquals(frame.vec(2).atStr(buff, 998).toString(), "€");
        assertTrue(frame.vec(0).isNA(999));
        assertTrue(frame.vec(1).isNA(999));
        assertTrue(frame.vec(2).isNA(999));
        assertTrue(frame.vec(3).isNA(999));
    } finally {
        if (frame != null) {
            frame.remove();
        }
    }
}
Also used : Frame(water.fvec.Frame) BufferedString(water.parser.BufferedString) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) ByteChannel(java.nio.channels.ByteChannel) BufferedString(water.parser.BufferedString) Test(org.junit.Test)

Example 8 with ByteChannel

use of java.nio.channels.ByteChannel in project elasticsearch by elastic.

the class FileSystemUtilsTests method copySourceFilesToTarget.

@Before
public void copySourceFilesToTarget() throws IOException, URISyntaxException {
    src = createTempDir();
    dst = createTempDir();
    Files.createDirectories(src);
    Files.createDirectories(dst);
    txtFile = src.resolve("text-file.txt");
    try (ByteChannel byteChannel = Files.newByteChannel(txtFile, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
        expectedBytes = new byte[3];
        expectedBytes[0] = randomByte();
        expectedBytes[1] = randomByte();
        expectedBytes[2] = randomByte();
        byteChannel.write(ByteBuffer.wrap(expectedBytes));
    }
}
Also used : ByteChannel(java.nio.channels.ByteChannel) Before(org.junit.Before)

Example 9 with ByteChannel

use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.

the class ExternalFrameReaderClientTest method testReading.

@Test
public void testReading() throws IOException, InterruptedException {
    final String frameName = "testFrame";
    final long[] chunkLayout = { 2, 2, 2, 1 };
    final Frame testFrame = new TestFrameBuilder().withName(frameName).withColNames("ColA", "ColB").withVecTypes(Vec.T_NUM, Vec.T_STR).withDataForCol(0, ard(Double.NaN, 1, 2, 3, 4, 5.6, 7, -1, 3.14)).withDataForCol(1, ar("A", "B", "C", "E", "F", "I", "J", "", null)).withChunkLayout(chunkLayout).build();
    // create frame
    final String[] nodes = new String[H2O.CLOUD._memary.length];
    // get ip and ports of h2o nodes
    for (int i = 0; i < nodes.length; i++) {
        nodes[i] = H2O.CLOUD._memary[i].getIpPortString();
    }
    final int[] selectedColumnIndices = { 0, 1 };
    // specify expected types for selected columns
    final byte[] expectedTypes = { ExternalFrameUtils.EXPECTED_DOUBLE, ExternalFrameUtils.EXPECTED_STRING };
    final int nChunks = testFrame.anyVec().nChunks();
    // we will read from all chunks at the same time
    Thread[] threads = new Thread[nChunks];
    try {
        // open all connections in connStrings array
        for (int idx = 0; idx < nChunks; idx++) {
            final int currentChunkIdx = idx;
            threads[idx] = new Thread() {

                @Override
                public void run() {
                    try {
                        ByteChannel sock = ExternalFrameUtils.getConnection(nodes[currentChunkIdx % nodes.length]);
                        ExternalFrameReaderClient reader = new ExternalFrameReaderClient(sock, frameName, currentChunkIdx, selectedColumnIndices, expectedTypes);
                        int rowsRead = 0;
                        assertEquals(reader.getNumRows(), chunkLayout[currentChunkIdx]);
                        while (rowsRead < reader.getNumRows()) {
                            if (rowsRead == 0 & currentChunkIdx == 0) {
                                reader.readDouble();
                                assertTrue("[0,0] in chunk 0 should be NA", reader.isLastNA());
                            } else {
                                reader.readDouble();
                                assertFalse("Should not be NA", reader.isLastNA());
                            }
                            reader.readString();
                            assertFalse("Should not be NA", reader.isLastNA());
                            rowsRead++;
                        }
                        assertEquals("Num or rows read was " + rowsRead + ", expecting " + reader.getNumRows(), rowsRead, reader.getNumRows());
                        reader.waitUntilAllReceived();
                        sock.close();
                    } catch (AssertionError e) {
                        exc = e;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            threads[idx].start();
        }
        // wait for all writer thread to finish
        for (Thread t : threads) {
            t.join();
            if (exc != null) {
                throw exc;
            }
        }
    } finally {
        testFrame.remove();
    }
}
Also used : Frame(water.fvec.Frame) TestFrameBuilder(water.fvec.TestFrameBuilder) IOException(java.io.IOException) ByteChannel(java.nio.channels.ByteChannel) Test(org.junit.Test)

Example 10 with ByteChannel

use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.

the class HttpTunnelServerTests method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    this.server = new HttpTunnelServer(this.serverConnection);
    given(this.serverConnection.open(anyInt())).willAnswer(new Answer<ByteChannel>() {

        @Override
        public ByteChannel answer(InvocationOnMock invocation) throws Throwable {
            MockServerChannel channel = HttpTunnelServerTests.this.serverChannel;
            channel.setTimeout((Integer) invocation.getArguments()[0]);
            return channel;
        }
    });
    this.servletRequest = new MockHttpServletRequest();
    this.servletRequest.setAsyncSupported(true);
    this.servletResponse = new MockHttpServletResponse();
    this.request = new ServletServerHttpRequest(this.servletRequest);
    this.response = new ServletServerHttpResponse(this.servletResponse);
    this.serverChannel = new MockServerChannel();
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ByteChannel(java.nio.channels.ByteChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

ByteChannel (java.nio.channels.ByteChannel)11 ByteBuffer (java.nio.ByteBuffer)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 SocketChannel (java.nio.channels.SocketChannel)3 Before (org.junit.Before)2 Frame (water.fvec.Frame)2 InetAddress (java.net.InetAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 Timestamp (java.sql.Timestamp)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 TestFrameBuilder (water.fvec.TestFrameBuilder)1 BufferedString (water.parser.BufferedString)1