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;
}
}
}
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();
}
}
}
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));
}
}
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", "