summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2021-04-09 17:10:06 +0200
committerJon Marius Venstad <venstad@gmail.com>2021-04-09 17:10:06 +0200
commit971f6dcd33c0788a7ca146de814ad0db9f416abf (patch)
tree67b384cda5bccf2026443c02a57775bbd4f53845 /vespajlib
parentef1971a1deb1d79d120d2648d96188c77bc41003 (diff)
Remove unused nio code
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/Acceptor.java91
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/Blob.java86
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/Connection.java55
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/ConnectionFactory.java21
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/Listener.java564
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/ReadLine.java83
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/SelectLoopHook.java26
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/SlowInflate.java27
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/UpdateInterest.java64
-rw-r--r--vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java108
-rw-r--r--vespajlib/src/test/java/com/yahoo/io/ListenerTestCase.java108
-rw-r--r--vespajlib/src/test/java/com/yahoo/io/SlowInflateTestCase.java61
12 files changed, 0 insertions, 1294 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/io/Acceptor.java b/vespajlib/src/main/java/com/yahoo/io/Acceptor.java
deleted file mode 100644
index 4be68371e72..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/Acceptor.java
+++ /dev/null
@@ -1,91 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import java.io.IOException;
-import java.nio.channels.SocketChannel;
-import java.nio.channels.ServerSocketChannel;
-import java.util.logging.Logger;
-import java.util.logging.Level;
-import java.net.InetSocketAddress;
-
-
-/**
- * Class for accepting new connections in separate thread.
- *
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- *
- */
-public class Acceptor extends Thread {
- private static Logger log = Logger.getLogger(Acceptor.class.getName());
-
- private int port;
- ServerSocketChannel socket;
- private Listener listener;
- private boolean initialized = false;
- private ConnectionFactory factory;
- private FatalErrorHandler fatalErrorHandler;
-
- public Acceptor(Listener listener, ConnectionFactory factory, int port) {
- super("Acceptor-" + listener.getName() + "-" + port);
- this.listener = listener;
- this.factory = factory;
- this.port = port;
- }
-
- public Acceptor listen() throws IOException {
- socket = ServerSocketChannel.open();
- socket.configureBlocking(true);
- socket.socket().setReuseAddress(true);
- socket.socket().bind(new InetSocketAddress(port));
- initialized = true;
- return this;
- }
-
- /**
- * Register a handler for fatal errors.
- *
- * @param f The FatalErrorHandler instance to be registered
- */
- public synchronized void setFatalErrorHandler(FatalErrorHandler f) {
- fatalErrorHandler = f;
- }
-
- public void run() {
- try {
- log.fine("Acceptor thread started");
- if (!initialized) {
- log.severe("Acceptor was not initialized. aborting");
- return;
- }
-
- while (!isInterrupted()) {
- SocketChannel c = null; // hush jikes
-
- try {
- c = socket.accept();
- c.configureBlocking(false);
- listener.addNewConnection(factory.newConnection(c, listener));
- } catch (java.nio.channels.IllegalBlockingModeException e) {
- log.log(Level.SEVERE, "Unable to set nonblocking", e);
- try {
- if (c != null) {
- c.close();
- }
- } catch (IOException ee) {}
- } catch (IOException e) {
- log.log(Level.WARNING,
- "Error accepting connection on port=" + port, e);
- try {
- if (c != null) {
- c.close();
- }
- } catch (IOException ee) {}
- }
- }
- } catch (Throwable t) {
- if (fatalErrorHandler != null) {
- fatalErrorHandler.handle(t, null);
- }
- }
- }
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/Blob.java b/vespajlib/src/main/java/com/yahoo/io/Blob.java
deleted file mode 100644
index dc73581df59..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/Blob.java
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import java.nio.ByteBuffer;
-
-/**
- * A Blob contains opaque data in the form of a byte array.
- **/
-public class Blob {
-
- /**
- * Shared empty array.
- **/
- private static byte[] empty = new byte[0];
-
- /**
- * Internal data, will never be 'null'.
- **/
- private byte[] data;
-
- /**
- * Create a Blob containing an empty byte array.
- **/
- public Blob() {
- data = empty;
- }
-
- /**
- * Create a Blob containg a copy of a subset of the given byte
- * array.
- **/
- public Blob(byte[] src, int offset, int length) {
- data = new byte[length];
- System.arraycopy(src, offset, data, 0, length);
- }
-
- /**
- * Create a Blob containing a copy of the given byte array.
- **/
- public Blob(byte[] src) {
- this(src, 0, src.length);
- }
-
- /**
- * Create a Blob containing a copy of the data held by the given
- * blob.
- **/
- public Blob(Blob src) {
- this(src.data);
- }
-
- /**
- * Create a Blob containing a number of bytes read from a byte
- * buffer.
- **/
- public Blob(ByteBuffer src, int length) {
- data = new byte[length];
- src.get(data);
- }
-
- /**
- * Create a Blob containing all bytes that could be read from a
- * byte buffer.
- **/
- public Blob(ByteBuffer src) {
- this(src, src.remaining());
- }
-
- /**
- * Obtain the internal data held by this object.
- *
- * @return internal data
- **/
- public byte[] get() {
- return data;
- }
-
- /**
- * Write the data held by this object to the given byte buffer.
- *
- * @param dst where to write the contained data
- **/
- public void write(ByteBuffer dst) {
- dst.put(data);
- }
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/Connection.java b/vespajlib/src/main/java/com/yahoo/io/Connection.java
deleted file mode 100644
index 37b300b54b6..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/Connection.java
+++ /dev/null
@@ -1,55 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import java.nio.channels.SocketChannel;
-import java.io.IOException;
-
-
-/**
- * Connection interface is the abstraction for an operating
- * asynchronous NIO connection. One is created for each
- * "accept" on the channel.
- *
- * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- *
- */
-public interface Connection {
-
- /**
- * called when the channel can accept a write, and is
- * enabled for writing
- */
- public void write() throws IOException;
-
- /**
- * Called when the channel can accept a read, and is
- * enabled for reading
- */
- public void read() throws IOException;
-
- /**
- * Called when the channel should be closed.
- */
- public void close() throws IOException;
-
- /**
- * Called when a socket has completed connecting to its
- * destination. (Asynchronous connect)
- */
- public void connect() throws IOException;
-
- /**
- * called to get the correct initial SelectionKey operation
- * flags for the next Select cycle, for this channel
- */
- public int selectOps();
-
- /**
- * Called to get the SocketChannel for this Connection.
- *
- * @return Returns the SocketChannel representing this connection
- */
- public SocketChannel socketChannel();
-}
-
diff --git a/vespajlib/src/main/java/com/yahoo/io/ConnectionFactory.java b/vespajlib/src/main/java/com/yahoo/io/ConnectionFactory.java
deleted file mode 100644
index 9872bb63eaf..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/ConnectionFactory.java
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-
-/**
- * @author <a href="mailto:borud@yahoo-inc.com">Bj\u00F8rn Borud</a>
- */
-
-import java.nio.channels.SocketChannel;
-
-
-/**
- * A factory interface used for associating SocketChannel and Listener
- * information with the application's Connection object.
- *
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
- */
-public interface ConnectionFactory {
- public Connection newConnection(SocketChannel channel, Listener listener);
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/Listener.java b/vespajlib/src/main/java/com/yahoo/io/Listener.java
deleted file mode 100644
index c76ffeaabad..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/Listener.java
+++ /dev/null
@@ -1,564 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-
-import java.io.IOException;
-import java.nio.channels.ClosedChannelException;
-import java.net.InetSocketAddress;
-
-import java.util.Iterator;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.IdentityHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.ArrayList;
-
-import java.util.logging.Logger;
-import java.util.logging.Level;
-
-import java.nio.channels.SocketChannel;
-import java.nio.channels.ServerSocketChannel;
-import java.nio.channels.Selector;
-import java.nio.channels.SelectionKey;
-
-
-/**
- * A basic Reactor implementation using NIO.
- *
- * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- *
- */
-public class Listener extends Thread {
- private static Logger log = Logger.getLogger(Listener.class.getName());
- private Selector selector;
- Map<Integer, Acceptor> acceptors = new HashMap<>();
- Map<ServerSocketChannel, ConnectionFactory> factories = new IdentityHashMap<>();
-
- private FatalErrorHandler fatalErrorHandler;
-
- private List<SelectLoopHook> selectLoopPreHooks;
- private List<SelectLoopHook> selectLoopPostHooks;
-
- final private LinkedList<Connection> newConnections = new LinkedList<>();
-
- // queue of SelectionKeys that need to be updated
- final private LinkedList<UpdateInterest> modifyInterestOpsQueue = new LinkedList<>();
-
- public Listener(String name) {
- super("Listener-" + name);
-
- try {
- selector = Selector.open();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- log.fine(name + " listener created " + this);
- }
-
- /**
- * Register a handler for fatal errors.
- *
- * @param f The FatalErrorHandler instance to be registered
- */
- public synchronized void setFatalErrorHandler(FatalErrorHandler f) {
- fatalErrorHandler = f;
- }
-
- /**
- * Add pre-select loop hook. Not threadsafe so please do this
- * during initial setup before you start the listener.
- */
- public void addSelectLoopPreHook(SelectLoopHook hook) {
- if (selectLoopPreHooks == null) {
- selectLoopPreHooks = new ArrayList<>(5);
- }
- selectLoopPreHooks.add(hook);
- }
-
- /**
- * Add pre-select loop hook. Not threadsafe so please do this
- * during initial setup before you start the listener.
- */
- public void addSelectLoopPostHook(SelectLoopHook hook) {
- if (selectLoopPostHooks == null) {
- selectLoopPostHooks = new ArrayList<>(5);
- }
- selectLoopPostHooks.add(hook);
- }
-
- /**
- * Run all the select loop pre hooks
- */
- private void runSelectLoopPreHooks() {
- if (selectLoopPreHooks == null) {
- return;
- }
-
- for (SelectLoopHook hook : selectLoopPreHooks) {
- hook.selectLoopHook(true);
- }
- }
-
- /**
- * Run all the select loop post hooks
- */
- private void runSelectLoopPostHooks() {
- if (selectLoopPostHooks == null) {
- return;
- }
-
- for (SelectLoopHook hook : selectLoopPostHooks) {
- hook.selectLoopHook(false);
- }
- }
-
- /**
- * Add a listening port and create an Acceptor thread which accepts
- * new connections on this port.
- *
- * @param factory The connection factory for new connections
- * on this port
- * @param port The port we are going to listen to.
- */
- public synchronized void listen(ConnectionFactory factory, int port)
- throws IOException {
- // make sure we have only one acceptor per listen port
- if (acceptors.containsKey(port)) {
- log.warning("Already listening to port=" + port);
- return;
- }
-
- Acceptor a = new Acceptor(this, factory, port);
-
- // inherit the fatal error handling of listener
- if (fatalErrorHandler != null) {
- a.setFatalErrorHandler(fatalErrorHandler);
- }
-
- a.listen().start();
- acceptors.put(port, a);
- }
-
- /**
- * Add a listening port without creating a separate acceptor
- * thread.
- *
- * @param factory The connection factory for new connections
- * on this port
- * @param port The port we are going to listen to.
- */
- public synchronized void listenNoAcceptor(ConnectionFactory factory, int port)
- throws IOException {
- ServerSocketChannel s = ServerSocketChannel.open();
-
- s.configureBlocking(false);
- s.socket().setReuseAddress(true);
- s.socket().bind(new InetSocketAddress(port)); // use non-specific IP
- String host = s.socket().getInetAddress().getHostName();
-
- factories.put(s, factory);
- s.register(selector, SelectionKey.OP_ACCEPT);
- log.fine("listener " + host + ":" + port);
- }
-
- // ==================================================================
- // ==================================================================
- // ==================================================================
-
-
- /**
- * This is the preferred way of modifying interest ops, giving a
- * Connection rather than a SelectionKey as input. This way the
- * we can look it up and ensure the correct SelectionKey is always
- * used.
- *
- * @return Returns a <code>this</code> reference for chaining
- */
- public Listener modifyInterestOps(Connection connection,
- int op, boolean set) {
- return modifyInterestOps(connection.socketChannel().keyFor(selector), op,
- set);
- }
-
- /**
- * Batch version of modifyInterestOps().
- *
- * @return Returns a <code>this</code> reference for chaining
- */
- public Listener modifyInterestOpsBatch(Connection connection,
- int op, boolean set) {
- return modifyInterestOpsBatch(
- connection.socketChannel().keyFor(selector), op, set);
- }
-
- /**
- * Enqueue change to interest set of SelectionKey. This is a workaround
- * for an NIO design error that makes it impossible to update interest
- * sets for a SelectionKey while a select is in progress -- and sometimes
- * you actually want to do this from other threads, which will then
- * block. Hence, we make it possible to enqueue requests for
- * SelectionKey modification in the thread where select runs.
- *
- * @return Returns a <code>this</code> reference for chaining
- */
- public Listener modifyInterestOps(SelectionKey key, int op, boolean set) {
- synchronized (modifyInterestOpsQueue) {
- modifyInterestOpsQueue.addLast(new UpdateInterest(key, op, set));
- }
- selector.wakeup();
- return this;
- }
-
- /**
- * Does the same as modifyInterestOps(), but does not call
- * wakeup on the selector. Allows adding more modifications
- * before we wake up the selector.
- *
- * @return Returns a <code>this</code> reference for chaining
- */
- public Listener modifyInterestOpsBatch(SelectionKey key,
- int op,
- boolean set) {
- synchronized (modifyInterestOpsQueue) {
- modifyInterestOpsQueue.addLast(new UpdateInterest(key, op, set));
- }
- return this;
- }
-
- /**
- * Signal that a batch update of SelectionKey is done and the
- * selector should be awoken. Also see modifyInterestOps().
- *
- * @return Returns a <code>this</code> reference for chaining
- */
- public Listener modifyInterestOpsDone() {
- selector.wakeup();
- return this;
- }
-
- /**
- * Process enqueued changes to SelectionKeys. Also see
- * modifyInterestOps().
- */
- private void processModifyInterestOps() {
- synchronized (modifyInterestOpsQueue) {
- while (!modifyInterestOpsQueue.isEmpty()) {
- UpdateInterest u = modifyInterestOpsQueue.removeFirst();
-
- u.doUpdate();
- }
- }
- }
-
- // ==================================================================
- // ==================================================================
- // ==================================================================
-
-
- /**
- * Thread entry point
- */
- public void run() {
- log.fine("Started listener");
- try {
- selectLoop();
- } catch (Throwable t) {
- if (fatalErrorHandler != null) {
- fatalErrorHandler.handle(t, null);
- }
- }
- }
-
- /**
- * Check channels for readiness and deal with channels that have
- * pending operations.
- */
- private void selectLoop() {
- while (!Thread.currentThread().isInterrupted()) {
- processNewConnections();
- processModifyInterestOps();
-
- try {
- int n = selector.select();
-
- if (0 == n) {
- continue;
- }
- } catch (java.io.IOException e) {
- log.log(Level.WARNING, "error during select", e);
- return;
- }
-
- runSelectLoopPreHooks();
-
- Iterator<SelectionKey> i = selector.selectedKeys().iterator();
-
- while (i.hasNext()) {
- SelectionKey key = i.next();
-
- i.remove();
-
- if (!key.isValid()) {
- continue;
- }
-
- if (key.isReadable()) {
- performRead(key);
- if (!key.isValid()) {
- continue;
- }
- }
-
- if (key.isWritable()) {
- performWrite(key);
- if (!key.isValid()) {
- continue;
- }
- }
-
- if (key.isConnectable()) {
- performConnect(key);
- if (!key.isValid()) {
- continue;
- }
- }
-
- if (key.isAcceptable()) {
- performAccept(key);
- }
- }
-
- runSelectLoopPostHooks();
- }
- }
-
- /**
- * This method is used by the Acceptor to hand off newly accepted
- * connections to the Listener. Note that this is run in the
- * context of the Acceptor thread, so doing things here versus
- * doing them in the acceptNewConnections(), which runs in the context
- * of the Listener thread, is a tradeoff that may need to be
- * re-evaluated
- *
- */
- public Connection addNewConnection(Connection newConn) {
-
- // ensure nonblocking and handle possible errors
- // if setting nonblocking fails. this code is really redundant
- // but necessary because the older version of this method set
- // the connection nonblocking, and clients might still expect
- // this behavior.
- //
- SocketChannel channel = newConn.socketChannel();
-
- if (channel.isBlocking()) {
- try {
- channel.configureBlocking(false);
- } catch (java.nio.channels.IllegalBlockingModeException e) {
- log.log(Level.SEVERE, "Unable to set nonblocking", e);
- try {
- channel.close();
- } catch (java.io.IOException ee) {
- log.log(Level.WARNING, "channel close failed", ee);
- }
- return newConn;
- } catch (java.io.IOException e) {
- log.log(Level.SEVERE, "Unable to set nonblocking", e);
- return newConn;
- }
- }
-
- synchronized (newConnections) {
- newConnections.addLast(newConn);
- }
- selector.wakeup();
- return newConn;
- }
-
- /**
- * This method is called from the selectLoop() method in order to
- * process new incoming connections.
- */
- private synchronized void processNewConnections() {
- synchronized (newConnections) {
- while (!newConnections.isEmpty()) {
- Connection conn = newConnections.removeFirst();
-
- try {
- conn.socketChannel().register(selector, conn.selectOps(),
- conn);
- } catch (ClosedChannelException e) {
- log.log(Level.WARNING, "register channel failed", e);
- return;
- }
- }
- }
- }
-
- /**
- * Accept new connection. This will loop over accept() until
- * there are no more new connections to accept. If any error
- * occurs after a successful accept, the socket in question will
- * be discarded, but we will continue to try to accept new
- * connections if available.
- *
- */
- private void performAccept(SelectionKey key) {
- SocketChannel channel;
- ServerSocketChannel ssChannel;
-
- if (Thread.currentThread().isInterrupted()) {
- return;
- }
-
- while (true) {
- try {
- ssChannel = (ServerSocketChannel) key.channel();
- channel = ssChannel.accept();
-
- // if for some reason there was no connection we just
- // ignore it.
- if (null == channel) {
- return;
- }
- } catch (java.io.IOException e) {
- log.log(Level.WARNING, "accept failed", e);
- return;
- }
-
- // set nonblocking and handle possible errors
- try {
- channel.configureBlocking(false);
- } catch (java.nio.channels.IllegalBlockingModeException e) {
- log.log(Level.SEVERE, "Unable to set nonblocking", e);
- try {
- channel.close();
- } catch (java.io.IOException ee) {
- log.log(Level.WARNING, "channel close failed", ee);
- continue;
- }
- continue;
- } catch (java.io.IOException e) {
- log.log(Level.WARNING, "IO error occurred", e);
- try {
- channel.close();
- } catch (java.io.IOException ee) {
- log.log(Level.WARNING, "channel close failed", ee);
- continue;
- }
- continue;
- }
-
- ConnectionFactory factory = factories.get(ssChannel);
- Connection conn = factory.newConnection(channel, this);
-
- try {
- channel.register(selector, conn.selectOps(), conn);
- } catch (java.nio.channels.ClosedChannelException e) {
- log.log(Level.WARNING, "register channel failed", e);
- }
- }
- }
-
- /**
- * Complete asynchronous connect operation. <em>Note that
- * asynchronous connect does not work properly in 1.4,
- * so you should not use this if you run anything older
- * than 1.5/5.0</em>.
- *
- */
- private void performConnect(SelectionKey key) {
- if (Thread.currentThread().isInterrupted()) {
- return;
- }
-
- Connection c = (Connection) key.attachment();
-
- try {
- c.connect();
- } catch (IOException e) {
- log.log(Level.FINE, "connect failed", e);
- try {
- c.close();
- } catch (IOException e2) {
- log.log(Level.FINE, "close failed", e);
- }
- }
- }
-
- /**
- * Perform read operation on channel which is now ready for reading
- */
- private void performRead(SelectionKey key) {
- if (Thread.currentThread().isInterrupted()) {
- return;
- }
-
- Connection c = (Connection) key.attachment();
-
- try {
- c.read();
- } catch (IOException e) {
- log.log(Level.FINE, "read failed", e);
- try {
- c.close();
- } catch (IOException e2) {
- log.log(Level.FINE, "close failed", e);
- }
- }
- }
-
- /**
- * Perform write operation(s) on channel which is now ready for
- * writing
- */
- private void performWrite(SelectionKey key) {
- if (Thread.currentThread().isInterrupted()) {
- return;
- }
-
- Connection c = (Connection) key.attachment();
-
- try {
- c.write();
- } catch (IOException e) {
- log.log(Level.FINE, " write failed", e);
- try {
- c.close();
- } catch (IOException e2) {// ignore
- }
- }
- }
-
- // ============================================================
- // ==== connections made outside listener
- // ============================================================
-
- /**
- * Register a connection that was set up outside the listener.
- * Typically what we do when we actively reach out and connect
- * somewhere.
- */
- public void registerConnection(Connection connection) {
- synchronized (newConnections) {
- newConnections.addLast(connection);
- }
- selector.wakeup();
- }
-
- /**
- * Perform clean shutdown of Listener.
- *
- * TODO: implement
- */
- public void shutdown() {// make writing impossible
- // make listening on new ports impossible
- // close all listening connections (kill all listener threads)
- // flush outbound data if the connection wants it
- // close all connections
- // have some sort of grace-period before forcibly shutting down
- }
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/ReadLine.java b/vespajlib/src/main/java/com/yahoo/io/ReadLine.java
deleted file mode 100644
index 6e7f9f5e50e..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/ReadLine.java
+++ /dev/null
@@ -1,83 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import java.nio.Buffer;
-import java.nio.charset.Charset;
-import java.nio.ByteBuffer;
-
-
-/**
- * Conventient utility for reading lines from ByteBuffers. Please
- * read the method documentation for readLine() carefully. The NIO
- * ByteBuffer abstraction is somewhat clumsy and thus usage of this
- * code requires that you understand the semantics clearly.
- *
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- *
- */
-public class ReadLine {
- static private Charset charset = Charset.forName("latin1");
-
- /**
- * Extract next line from a byte buffer. Looks for EOL characters
- * between start and limit, and returns a string between start and
- * the EOL charachers. It skips ahead past any remaining EOL
- * characters and sets position to the first non-EOL character.
- *
- * If it doesn't find an EOL characher between start and limit
- */
- public static String readLine(ByteBuffer buffer) {
- int start = buffer.position();
-
- for (int i = start; i < buffer.limit(); i++) {
-
- if (isEolChar(buffer.get(i))) {
-
- // detect and skip EOL at beginning. Also, update
- // position so we compact the buffer if we exit the
- // for loop without having found a proper string
- if (i == start) {
- for (; (i < buffer.limit()) && isEolChar(buffer.get(i)); i++) {
- ;
- }
- start = i;
- buffer.position(i);
- continue;
- }
-
- // limit() returns a buffer (before Java 9) so we have to up-cast.
- // The downcast to Buffer is done to avoid "redundant cast" warning on Java 9.
- // TODO: when Java 8 is gone, remove the casts and above comments.
- // extract string between start and i.
- String line = charset.decode((ByteBuffer) ((Buffer)buffer.slice()).limit(i - start)).toString();
-
- // skip remaining
- for (; (i < buffer.limit()) && isEolChar(buffer.get(i)); i++) {
- ;
- }
-
- buffer.position(i);
- return line;
- }
- }
-
- // if we get here we didn't find any string. this may be
- // because the buffer has no more content, ie. limit == position.
- // if that is the case we clear the buffer.
- //
- // if we have content, but no more EOL characters we compact the
- // buffer.
- //
- if (buffer.hasRemaining()) {
- buffer.compact();
- } else {
- buffer.clear();
- }
-
- return null;
- }
-
- static boolean isEolChar(byte b) {
- return ((10 == b) || (13 == b));
- }
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/SelectLoopHook.java b/vespajlib/src/main/java/com/yahoo/io/SelectLoopHook.java
deleted file mode 100644
index 976aeacc510..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/SelectLoopHook.java
+++ /dev/null
@@ -1,26 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-
-/**
- * This interface defines a callback hook which applications can
- * use to get work done before or after the select loop finishes
- * its tasks.
- *
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- *
- */
-public interface SelectLoopHook {
-
- /**
- * Callback which can be called before or after
- * select loop has done its work, depending on
- * how you register the hook.
- *
- * @param before is <code>true</code> if the hook
- * was called before the channels in the ready
- * set have been processed, and <code>false</code>
- * if called after.
- */
- public void selectLoopHook(boolean before);
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/SlowInflate.java b/vespajlib/src/main/java/com/yahoo/io/SlowInflate.java
deleted file mode 100644
index df95504d3b9..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/SlowInflate.java
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-
-import java.util.zip.Inflater;
-
-
-/**
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- */
-public class SlowInflate {
- private Inflater inflater = new Inflater();
-
- public byte[] unpack(byte[] compressed, int inflatedLen) {
- byte[] decompressed = new byte[inflatedLen];
-
- inflater.reset();
- inflater.setInput(compressed);
- inflater.finished();
- try {
- inflater.inflate(decompressed);
- } catch (java.util.zip.DataFormatException e) {
- throw new RuntimeException("Decompression failure: " + e);
- }
- return decompressed;
- }
-}
diff --git a/vespajlib/src/main/java/com/yahoo/io/UpdateInterest.java b/vespajlib/src/main/java/com/yahoo/io/UpdateInterest.java
deleted file mode 100644
index 21c14336270..00000000000
--- a/vespajlib/src/main/java/com/yahoo/io/UpdateInterest.java
+++ /dev/null
@@ -1,64 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import java.nio.channels.SelectionKey;
-
-
-/**
- * Command object to perform interest set updates. Workaround for NIO
- * design flaw which makes it impossible to update the interest set of
- * a SelectionKey while select() is in progress. There should be a
- * more elegant way around this, but if it turns out to be performant
- * enough we leave it like this.
- *
- * <P>
- * Of course, the ideal would be to have NIO fixed.
- *
- * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
- * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
- */
-public class UpdateInterest {
- private SelectionKey key;
- private int operation;
- private boolean set;
-
- /**
- * Make sure this can't be run
- */
- @SuppressWarnings("unused")
- private UpdateInterest() {}
-
- /**
- * Create an object for encapsulating a interest set change
- * request.
- *
- * @param key The key we wish to update
- * @param operation The operation we wish to set or remove
- * @param set Whether we want to set (true) or clear (false) the
- * operation in the interest set
- */
- public UpdateInterest(SelectionKey key, int operation, boolean set) {
- this.key = key;
- this.operation = operation;
- this.set = set;
- }
-
- /**
- * This method is used for actually applying the updates to the
- * SelectionKey in question at a time when it is safe to do so.
- * If the SelectionKey has been invalidated in the meanwhile we
- * do nothing.
- */
- public void doUpdate() {
- // bail if this key isn't valid anymore
- if ((key == null) || (!key.isValid())) {
- return;
- }
-
- if (set) {
- key.interestOps(key.interestOps() | operation);
- } else {
- key.interestOps(key.interestOps() & (~operation));
- }
- }
-}
diff --git a/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java b/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java
deleted file mode 100644
index c9018224b1c..00000000000
--- a/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java
+++ /dev/null
@@ -1,108 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import org.junit.Test;
-
-import java.nio.ByteBuffer;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-public class BlobTestCase {
-
- @Test
- public void testEmpty() {
- Blob empty = new Blob();
- assertTrue(empty.get() != null);
- assertEquals(0, empty.get().length);
- }
-
- @Test
- public void testCopyArray() {
- byte[] d = { 1, 2, 3 };
- Blob b = new Blob(d);
- d[0] = 7;
- d[1] = 8;
- d[2] = 9;
- assertEquals(3, b.get().length);
- assertEquals(1, b.get()[0]);
- assertEquals(2, b.get()[1]);
- assertEquals(3, b.get()[2]);
- }
-
- @Test
- public void testCopyArraySubset() {
- byte[] d = { 1, 2, 3 };
- Blob b = new Blob(d, 1, 1);
- d[0] = 7;
- d[1] = 8;
- d[2] = 9;
- assertEquals(1, b.get().length);
- assertEquals(2, b.get()[0]);
- }
-
- @Test
- public void testCopyBlob() {
- byte[] d = { 1, 2, 3 };
- Blob b = new Blob(d);
- Blob x = new Blob(b);
- b.get()[1] = 4;
- assertEquals(3, x.get().length);
- assertEquals(1, x.get()[0]);
- assertEquals(4, b.get()[1]);
- assertEquals(2, x.get()[1]);
- assertEquals(3, x.get()[2]);
- }
-
- @Test
- public void testReadBuffer() {
- ByteBuffer buf = ByteBuffer.allocate(100);
- buf.put((byte)1);
- buf.put((byte)2);
- buf.put((byte)3);
- buf.flip();
- assertEquals(3, buf.remaining());
- Blob b = new Blob(buf);
- assertEquals(0, buf.remaining());
- assertEquals(3, b.get().length);
- assertEquals(1, b.get()[0]);
- assertEquals(2, b.get()[1]);
- assertEquals(3, b.get()[2]);
- }
-
- @Test
- public void testReadPartialBuffer() {
- ByteBuffer buf = ByteBuffer.allocate(100);
- buf.put((byte)1);
- buf.put((byte)2);
- buf.put((byte)3);
- buf.put((byte)4);
- buf.put((byte)5);
- buf.flip();
- assertEquals(5, buf.remaining());
- Blob b = new Blob(buf, 3);
- assertEquals(2, buf.remaining());
- assertEquals(3, b.get().length);
- assertEquals(1, b.get()[0]);
- assertEquals(2, b.get()[1]);
- assertEquals(3, b.get()[2]);
- assertEquals(4, buf.get());
- assertEquals(5, buf.get());
- assertEquals(0, buf.remaining());
- }
-
- @Test
- public void testWriteBuffer() {
- byte[] d = { 1, 2, 3 };
- Blob b = new Blob(d);
- ByteBuffer buf = ByteBuffer.allocate(100);
- b.write(buf);
- buf.flip();
- assertEquals(3, buf.remaining());
- assertEquals(1, buf.get());
- assertEquals(2, buf.get());
- assertEquals(3, buf.get());
- assertEquals(0, buf.remaining());
- }
-
-}
diff --git a/vespajlib/src/test/java/com/yahoo/io/ListenerTestCase.java b/vespajlib/src/test/java/com/yahoo/io/ListenerTestCase.java
deleted file mode 100644
index 173197ae27c..00000000000
--- a/vespajlib/src/test/java/com/yahoo/io/ListenerTestCase.java
+++ /dev/null
@@ -1,108 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import static org.junit.Assert.*;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.nio.ByteBuffer;
-import java.nio.channels.SelectionKey;
-import java.nio.channels.SocketChannel;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.yahoo.collections.Tuple2;
-import com.yahoo.concurrent.Receiver;
-import com.yahoo.concurrent.Receiver.MessageState;
-
-/**
- * Test a NIO based Reactor pattern implementation, com.yahoo.io.Listener.
- *
- * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
- */
-public class ListenerTestCase {
-
- @Before
- public void setUp() throws Exception {
- }
-
- @After
- public void tearDown() throws Exception {
- }
-
- Receiver<Byte> r = new Receiver<>();
-
- private final class MockConnection implements Connection {
-
- private SocketChannel channel;
-
- MockConnection(SocketChannel channel, Listener listener) {
- this.channel = channel;
- }
-
- @Override
- public void write() throws IOException {
- }
-
- @Override
- public void read() throws IOException {
- ByteBuffer b = ByteBuffer.allocate(1);
- channel.read(b);
- b.flip();
- r.put(b.get());
- }
-
- @Override
- public void close() throws IOException {
- channel.close();
-
- }
-
- @Override
- public void connect() throws IOException {
- }
-
- @Override
- public int selectOps() {
- return SelectionKey.OP_READ;
- }
-
- @Override
- public SocketChannel socketChannel() {
- return channel;
- }
- }
-
- private final class GetConnection implements ConnectionFactory {
-
- @Override
- public Connection newConnection(SocketChannel channel, Listener listener) {
- return new MockConnection(channel, listener);
- }
- }
-
- @Test
- public final void testRun() throws IOException, InterruptedException {
- Listener l = new Listener("ListenerTestCase");
- l.listen(new GetConnection(), 0);
- l.start();
- int port = ((InetSocketAddress) l.acceptors.get(0).socket.getLocalAddress()).getPort();
- Socket s = new Socket("127.0.0.1", port);
- final byte expected = 42;
- s.getOutputStream().write(expected);
- s.getOutputStream().flush();
- s.close();
- Tuple2<MessageState, Byte> received = r.get(60 * 1000);
- l.acceptors.get(0).interrupt();
- l.acceptors.get(0).socket.close();
- l.acceptors.get(0).join();
- l.interrupt();
- l.join();
- assertTrue("Test timed out.", received.first == MessageState.VALID);
- assertEquals(expected, received.second.byteValue());
- }
-
-}
diff --git a/vespajlib/src/test/java/com/yahoo/io/SlowInflateTestCase.java b/vespajlib/src/test/java/com/yahoo/io/SlowInflateTestCase.java
deleted file mode 100644
index 7726cd7ea68..00000000000
--- a/vespajlib/src/test/java/com/yahoo/io/SlowInflateTestCase.java
+++ /dev/null
@@ -1,61 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.io;
-
-import static org.junit.Assert.*;
-
-import java.util.Arrays;
-import java.util.zip.Deflater;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import com.yahoo.text.Utf8;
-
-/**
- * Check decompressor used among other things for packed summary fields.
- *
- * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
- */
-public class SlowInflateTestCase {
-
- private String value;
- private byte[] raw;
- private byte[] output;
- private byte[] compressed;
- private int compressedDataLength;
-
- @Before
- public void setUp() throws Exception {
- value = "000000000000000000000000000000000000000000000000000000000000000";
- raw = Utf8.toBytesStd(value);
- output = new byte[raw.length * 2];
- Deflater compresser = new Deflater();
- compresser.setInput(raw);
- compresser.finish();
- compressedDataLength = compresser.deflate(output);
- compresser.end();
- compressed = Arrays.copyOf(output, compressedDataLength);
- }
-
- @Test
- public final void test() {
- byte[] unpacked = new SlowInflate().unpack(compressed, raw.length);
- assertArrayEquals(raw, unpacked);
- }
-
- @Test
- public final void testCorruptData() {
- compressed[0] = (byte) (compressed[0] ^ compressed[1]);
- compressed[1] = (byte) (compressed[1] ^ compressed[2]);
- compressed[2] = (byte) (compressed[2] ^ compressed[3]);
- compressed[3] = (byte) (compressed[3] ^ compressed[4]);
- boolean caught = false;
- try {
- new SlowInflate().unpack(compressed, raw.length);
- } catch (RuntimeException e) {
- caught = true;
- }
- assertTrue(caught);
- }
-
-}