2012-01-03 15:28:47 -07:00
.. _io-scala:
2013-02-10 18:34:29 +01:00
I/O (Scala)
2013-02-14 08:29:54 +01:00
===========
2012-01-03 15:28:47 -07:00
Introduction
------------
2013-02-10 16:46:59 +01:00
The `` akka.io `` package has been developed in collaboration between the Akka
2013-02-17 13:38:06 +13:00
and `spray.io`_ teams. Its design combines experiences from the
`` spray-io `` module with improvements that were jointly developed for
2013-02-10 16:46:59 +01:00
more general consumption as an actor-based service.
2012-01-24 13:37:26 -07:00
This documentation is in progress and some sections may be incomplete. More will be coming.
2012-01-03 15:28:47 -07:00
2013-02-10 16:46:59 +01:00
.. note ::
2013-02-10 18:34:29 +01:00
The old I/O implementation has been deprecated and its documentation has been moved: :ref: `io-scala-old`
2013-02-10 16:46:59 +01:00
Terminology, Concepts
---------------------
2013-02-17 13:38:06 +13:00
The I/O API is completely actor based, meaning that all operations are implemented with message passing instead of
direct method calls. Every I/O driver (TCP, UDP) has a special actor, called a *manager* that serves
as an entry point for the API. I/O is broken into several drivers. The manager for a particular driver
is accessible through the `` IO `` entry point. For example the following code
2013-02-10 16:46:59 +01:00
looks up the TCP manager and returns its `` ActorRef `` :
.. code-block :: scala
val tcpManager = IO(Tcp)
2013-02-17 13:38:06 +13:00
The manager receives I/O command messages and instantiates worker actors in response. The worker actors present
themselves to the API user in the reply to the command that was sent. For example after a `` Connect `` command sent to
the TCP manager the manager creates an actor representing the TCP connection. All operations related to the given TCP
connections can be invoked by sending messages to the connection actor which announces itself by sending a `` Connected ``
message.
2013-02-10 16:46:59 +01:00
DeathWatch and Resource Management
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-02-17 13:38:06 +13:00
I/O worker actors receive commands and also send out events. They usually need a user-side counterpart actor listening
for these events (such events could be inbound connections, incoming bytes or acknowledgements for writes). These worker
actors *watch* their listener counterparts. If the listener stops then the worker will automatically release any
resources that it holds. This design makes the API more robust against resource leaks.
2013-02-10 16:46:59 +01:00
2013-02-10 18:34:29 +01:00
Thanks to the completely actor based approach of the I/O API the opposite direction works as well: a user actor
2013-02-17 13:38:06 +13:00
responsible for handling a connection can watch the connection actor to be notified if it unexpectedly terminates.
2013-02-10 18:34:29 +01:00
2013-02-10 16:46:59 +01:00
Write models (Ack, Nack)
^^^^^^^^^^^^^^^^^^^^^^^^
2013-02-17 13:38:06 +13:00
I/O devices have a maximum throughput which limits the frequency and size of writes. When an
application tries to push more data than a device can handle, the driver has to buffer bytes until the device
is able to write them. With buffering it is possible to handle short bursts of intensive writes --- but no buffer is infinite.
"Flow control" is needed to avoid overwhelming device buffers.
2013-02-10 16:46:59 +01:00
2013-02-17 13:38:06 +13:00
Akka supports two types of flow control:
2013-02-10 16:46:59 +01:00
2013-02-17 13:38:06 +13:00
* *Ack-based* , where the driver notifies the writer when writes have succeeded.
* *Nack-based* , where the driver notifies the writer when writes have failed.
Each of these models is available in both the TCP and the UDP implementations of Akka I/O.
Individual writes can be acknowledged by providing an ack object in the write message (`` Write `` in the case of TCP and
`` Send `` for UDP). When the write is complete the worker will send the ack object to the writing actor. This can be
used to implement *ack-based* flow control; sending new data only when old data has been acknowledged.
If a write (or any other command) fails, the driver notifies the actor that sent the command with a special message
(`` CommandFailed `` in the case of UDP and TCP). This message will also notify the writer of a failed write, serving as a
nack for that write. Please note, that in a nack-based flow-control setting the writer has to be prepared for the fact
that the failed write might not be the most recent write it sent. For example, the failure notification for a write
`` W1 `` might arrive after additional write commands `` W2 `` and `` W3 `` have been sent. If the writer wants to resend any
nacked messages it may need to keep a buffer of pending messages.
2013-02-10 16:46:59 +01:00
.. warning ::
2013-02-17 13:38:06 +13:00
An acknowledged write does not mean acknowledged delivery or storage; receiving an ack for a write simply signals that
the I/O driver has successfully processed the write. The Ack/Nack protocol described here is a means of flow control
not error handling. In other words, data may still be lost, even if every write is acknowledged.
2012-01-23 23:44:41 -07:00
ByteString
^^^^^^^^^^
2013-02-17 13:38:06 +13:00
To maintain isolation, actors should communicate with immutable objects only. `` ByteString `` is an
immutable container for bytes. It is used by Akka's I/O system as an efficient, immutable alternative
the traditional byte containers used for I/O on the JVM, such as `` Array[Byte] `` and `` ByteBuffer `` .
2012-01-23 23:44:41 -07:00
2013-02-17 13:38:06 +13:00
`` ByteString `` is a `rope-like <http://en.wikipedia.org/wiki/Rope_(computer_science)> `_ data structure that is immutable
and provides fast concatenation and slicing operations (perfect for I/O). When two `` ByteString ` ` \s are concatenated
together they are both stored within the resulting `` ByteString `` instead of copying both to a new `` Array `` . Operations
such as `` drop `` and `` take `` return `` ByteString ` ` \s that still reference the original ` ` Array `` , but just change the
offset and length that is visible. Great care has also been taken to make sure that the internal `` Array `` cannot be
modified. Whenever a potentially unsafe `` Array `` is used to create a new `` ByteString `` a defensive copy is created. If
you require a `` ByteString `` that only blocks a much memory as necessary for it's content, use the `` compact `` method to
get a `` CompactByteString `` instance. If the `` ByteString `` represented only a slice of the original array, this will
result in copying all bytes in that slice.
2012-01-23 23:44:41 -07:00
2012-05-11 10:30:43 +02:00
`` ByteString `` inherits all methods from `` IndexedSeq `` , and it also has some new ones. For more information, look up the `` akka.util.ByteString `` class and it's companion object in the ScalaDoc.
2012-01-23 23:44:41 -07:00
2013-02-17 13:38:06 +13:00
`` ByteString `` also comes with its own optimized builder and iterator classes `` ByteStringBuilder `` and
`` ByteIterator `` which provide extra features in addition to those of normal builders and iterators.
2012-06-17 19:28:08 +02:00
Compatibility with java.io
..........................
2013-02-17 13:38:06 +13:00
A `` ByteStringBuilder `` can be wrapped in a `` java.io.OutputStream `` via the `` asOutputStream `` method. Likewise, `` ByteIterator `` can we wrapped in a `` java.io.InputStream `` via `` asInputStream `` . Using these, `` akka.io `` applications can integrate legacy code based on `` java.io `` streams.
2012-06-17 19:28:08 +02:00
2013-02-17 13:38:06 +13:00
Encoding and decoding binary data
2012-06-17 19:28:08 +02:00
....................................
`` ByteStringBuilder `` and `` ByteIterator `` support encoding and decoding of binary data. As an example, consider a stream of binary data frames with the following format:
.. code-block :: text
frameLen: Int
n: Int
m: Int
n times {
a: Short
b: Long
}
data: m times Double
2013-02-17 13:38:06 +13:00
In this example, the data will be stored in arrays of `` a `` , `` b `` of length `` n `` and `` data `` of length `` m `` .
2012-06-17 19:28:08 +02:00
Decoding of such frames can be efficiently implemented in the following fashion:
2012-08-21 10:45:01 +02:00
.. includecode :: code/docs/io/BinaryCoding.scala
2012-06-19 17:16:30 +02:00
:include: decoding
2012-06-17 19:28:08 +02:00
This implementation naturally follows the example data format. In a true Scala application, one might, of course, want use specialized immutable Short/Long/Double containers instead of mutable Arrays.
2013-02-17 13:38:06 +13:00
After extracting data from a `` ByteIterator `` , the remaining content can also be turned back into a `` ByteString `` using
the `` toSeq `` method. No bytes are copied. Because of immutability the underlying bytes can be shared between both the
`` ByteIterator `` and the `` ByteString `` .
2012-06-17 19:28:08 +02:00
2012-08-21 10:45:01 +02:00
.. includecode :: code/docs/io/BinaryCoding.scala
2012-06-19 17:16:30 +02:00
:include: rest-to-seq
2013-02-10 16:46:59 +01:00
2013-02-17 13:38:06 +13:00
In general, conversions from ByteString to ByteIterator and vice versa are O(1) for non-chunked ByteStrings and (at worst) O(nChunks) for chunked ByteStrings.
2012-06-17 19:28:08 +02:00
Encoding of data also is very natural, using `` ByteStringBuilder ``
2012-08-21 10:45:01 +02:00
.. includecode :: code/docs/io/BinaryCoding.scala
2012-06-19 17:16:30 +02:00
:include: encoding
2012-06-17 19:28:08 +02:00
2013-02-10 16:46:59 +01:00
Using TCP
---------
2012-01-23 23:44:41 -07:00
2013-02-17 13:38:06 +13:00
All of the Akka I/O APIs are accessed through manager objects. When using an I/O API, the first step is to acquire a
reference to the appropriate manager. The code below shows how to acquire a reference to the `` Tcp `` manager.
2013-02-10 18:34:29 +01:00
.. code-block :: scala
import akka.io.IO
import akka.io.Tcp
val tcpManager = IO(Tcp)
2013-02-17 13:38:06 +13:00
The manager is an actor that handles the underlying low level I/O resources (selectors, channels) and instantiates
workers for specific tasks, such as listening to incoming connections.
.. _connecting-scala:
2012-01-23 23:44:41 -07:00
2013-02-10 16:46:59 +01:00
Connecting
^^^^^^^^^^
2012-01-24 13:37:26 -07:00
2013-02-10 18:34:29 +01:00
The first step of connecting to a remote address is sending a `` Connect `` message to the TCP manager:
.. code-block :: scala
import akka.io.Tcp._
IO(Tcp) ! Connect(remoteSocketAddress)
2013-02-17 13:38:06 +13:00
When connecting, it is also possible to set various socket options or specify a local address:
.. code-block :: scala
2013-02-10 18:34:29 +01:00
IO(Tcp) ! Connect(remoteSocketAddress, Some(localSocketAddress), List(SO.KeepAlive(true)))
2013-02-17 13:38:06 +13:00
After issuing the `` Connect `` command the TCP manager spawns a worker actor to handle commands related to the
2013-02-10 18:34:29 +01:00
connection. This worker actor will reveal itself by replying with a `` Connected `` message to the actor who sent the
`` Connect `` command.
.. code-block :: scala
case Connected(remoteAddress, localAddress) =>
connectionActor = sender
At this point, there is still no listener associated with the connection. To finish the connection setup a `` Register ``
has to be sent to the connection actor with the listener `` ActorRef `` as a parameter.
.. code-block :: scala
connectionActor ! Register(listener)
2013-02-17 13:38:06 +13:00
Upon registration, the connection actor will watch the listener actor provided in the `` listener `` parameter.
If the listener actor stops, the connection is closed, and all resources allocated for the connection released. During the
lifetime of the connection the listener may receive various event notifications:
2013-02-10 18:34:29 +01:00
.. code-block :: scala
case Received(dataByteString) => // handle incoming chunk of data
case CommandFailed(cmd) => // handle failure of command: cmd
case _: ConnectionClosed => // handle closed connections
2013-02-17 13:38:06 +13:00
`` ConnectionClosed `` is a trait, which the different connection close events all implement.
2013-02-10 18:34:29 +01:00
The last line handles all connection close events in the same way. It is possible to listen for more fine-grained
2013-02-17 13:38:06 +13:00
connection close events, see :ref: `closing-connections-scala` below.
2013-02-10 18:34:29 +01:00
2012-01-24 13:37:26 -07:00
2013-02-10 16:46:59 +01:00
Accepting connections
^^^^^^^^^^^^^^^^^^^^^
2012-01-03 15:28:47 -07:00
2013-02-17 13:38:06 +13:00
To create a TCP server and listen for inbound connection, a `` Bind `` command has to be sent to the TCP manager.
This will instruct the TCP manager to listen for TCP connections on a particular address.
2013-02-10 18:34:29 +01:00
.. code-block :: scala
import akka.io.IO
import akka.io.Tcp
IO(Tcp) ! Bind(handler, localAddress)
The actor sending the `` Bind `` message will receive a `` Bound `` message signalling that the server is ready to accept
2013-02-17 13:38:06 +13:00
incoming connections. The process for accepting connections is similar to the process for making :ref:`outgoing
connections <connecting-scala>`: when an incoming connection is established, the actor provided as ` `handler` ` will
receive a `` Connected `` message whose sender is the connection actor.
2013-02-10 18:34:29 +01:00
.. code-block :: scala
case Connected(remoteAddress, localAddress) =>
connectionActor = sender
At this point, there is still no listener associated with the connection. To finish the connection setup a `` Register ``
has to be sent to the connection actor with the listener `` ActorRef `` as a parameter.
.. code-block :: scala
connectionActor ! Register(listener)
2013-02-17 13:38:06 +13:00
Upon registration, the connection actor will watch the listener actor provided in the `` listener `` parameter.
2013-02-10 18:34:29 +01:00
If the listener stops, the connection is closed, and all resources allocated for the connection released. During the
2013-02-17 13:38:06 +13:00
connection lifetime the listener will receive various event notifications in the same way as in the outbound
2013-02-10 18:34:29 +01:00
connection case.
2013-02-17 13:38:06 +13:00
.. _closing-connections-scala:
2013-02-10 18:34:29 +01:00
Closing connections
^^^^^^^^^^^^^^^^^^^
A connection can be closed by sending one of the commands `` Close `` , `` ConfirmedClose `` or `` Abort `` to the connection
actor.
`` Close `` will close the connection by sending a `` FIN `` message, but without waiting for confirmation from
the remote endpoint. Pending writes will be flushed. If the close is successful, the listener will be notified with
2013-02-17 13:38:06 +13:00
`` Closed `` .
2013-02-10 18:34:29 +01:00
`` ConfirmedClose `` will close the sending direction of the connection by sending a `` FIN `` message, but receives
will continue until the remote endpoint closes the connection, too. Pending writes will be flushed. If the close is
2013-02-17 13:38:06 +13:00
successful, the listener will be notified with `` ConfirmedClosed `` .
2013-02-10 18:34:29 +01:00
`` Abort `` will immediately terminate the connection by sending a `` RST `` message to the remote endpoint. Pending
2013-02-17 13:38:06 +13:00
writes will be not flushed. If the close is successful, the listener will be notified with `` Aborted `` .
2013-02-10 18:34:29 +01:00
`` PeerClosed `` will be sent to the listener if the connection has been closed by the remote endpoint.
`` ErrorClosed `` will be sent to the listener whenever an error happened that forced the connection to be closed.
All close notifications are subclasses of `` ConnectionClosed `` so listeners who do not need fine-grained close events
may handle all close events in the same way.
Throttling Reads and Writes
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-02-12 13:08:41 +01:00
*This section is not yet ready. More coming soon*
2012-01-24 13:37:26 -07:00
2013-02-10 16:46:59 +01:00
Using UDP
---------
2012-01-03 15:28:47 -07:00
2013-02-17 13:38:06 +13:00
UDP support comes in two flavors: connectionless and connection-based. With connectionless UDP, workers can send datagrams
to any remote address. Connection-based UDP workers are linked to a single remote address.
The connectionless UDP manager is accessed through `` UdpFF `` . `` UdpFF `` refers to the "fire-and-forget" style of sending
UDP datagrams.
2013-02-10 18:34:29 +01:00
.. code-block :: scala
import akka.io.IO
import akka.io.UdpFF
val connectionLessUdp = IO(UdpFF)
2013-02-17 13:38:06 +13:00
The connection-based UDP manager is accessed through `` UdpConn `` .
.. code-block :: scala
2013-02-10 18:34:29 +01:00
import akka.io.UdpConn
val connectionBasedUdp = IO(UdpConn)
UDP servers can be only implemented by the connectionless API, but clients can use both.
2012-01-24 13:37:26 -07:00
2013-02-10 16:46:59 +01:00
Connectionless UDP
2013-02-10 18:34:29 +01:00
^^^^^^^^^^^^^^^^^^
Simple Send
............
To simply send a UDP datagram without listening to an answer one needs to send the `` SimpleSender `` command to the
2013-02-17 13:38:06 +13:00
`` UdpFF `` manager:
2013-02-10 18:34:29 +01:00
.. code-block :: scala
2013-02-12 13:08:41 +01:00
IO(UdpFF) ! SimpleSender
2013-02-10 18:34:29 +01:00
// or with socket options:
import akka.io.Udp._
IO(UdpFF) ! SimpleSender(List(SO.Broadcast(true)))
The manager will create a worker for sending, and the worker will reply with a `` SimpleSendReady `` message:
.. code-block :: scala
case SimpleSendReady =>
simpleSender = sender
After saving the sender of the `` SimpleSendReady `` message it is possible to send out UDP datagrams with a simple
message send:
.. code-block :: scala
simpleSender ! Send(data, serverAddress)
Bind (and Send)
...............
To listen for UDP datagrams arriving on a given port, the `` Bind `` command has to be sent to the connectionless UDP
manager
.. code-block :: scala
IO(UdpFF) ! Bind(handler, localAddress)
After the bind succeeds, the sender of the `` Bind `` command will be notified with a `` Bound `` message. The sender of
this message is the worker for the UDP channel bound to the local address.
.. code-block :: scala
case Bound =>
udpWorker = sender // Save the worker ref for later use
The actor passed in the `` handler `` parameter will receive inbound UDP datagrams sent to the bound address:
.. code-block :: scala
case Received(dataByteString, remoteAddress) => // Do something with the data
The `` Received `` message contains the payload of the datagram and the address of the sender.
It is also possible to send UDP datagrams using the `` ActorRef `` of the worker saved in `` udpWorker `` :
.. code-block :: scala
udpWorker ! Send(data, serverAddress)
.. note ::
The difference between using a bound UDP worker to send instead of a simple-send worker is that in the former case
the sender field of the UDP datagram will be the bound local address, while in the latter it will be an undetermined
ephemeral port.
2012-01-24 13:37:26 -07:00
2013-02-10 16:46:59 +01:00
Connection based UDP
^^^^^^^^^^^^^^^^^^^^
2012-01-24 13:37:26 -07:00
2013-02-17 13:38:06 +13:00
The service provided by the connection based UDP API is similar to the bind-and-send service we saw earlier, but
the main difference is that a connection is only able to send to the `` remoteAddress `` it was connected to, and will
2013-02-10 18:34:29 +01:00
receive datagrams only from that address.
Connecting is similar to what we have seen in the previous section:
.. code-block :: scala
IO(UdpConn) ! Connect(handler, remoteAddress)
2013-02-17 13:38:06 +13:00
Or, with more options:
.. code-block :: scala
2013-02-10 18:34:29 +01:00
IO(UdpConn) ! Connect(handler, Some(localAddress), remoteAddress, List(SO.Broadcast(true)))
After the connect succeeds, the sender of the `` Connect `` command will be notified with a `` Connected `` message. The sender of
this message is the worker for the UDP connection.
.. code-block :: scala
case Connected =>
udpConnectionActor = sender // Save the worker ref for later use
The actor passed in the `` handler `` parameter will receive inbound UDP datagrams sent to the bound address:
.. code-block :: scala
case Received(dataByteString) => // Do something with the data
The `` Received `` message contains the payload of the datagram but unlike in the connectionless case, no sender address
2013-02-17 13:38:06 +13:00
is provided, as a UDP connection only receives messages from the endpoint it has been connected to.
2013-02-10 18:34:29 +01:00
2013-02-17 13:38:06 +13:00
UDP datagrams can be sent by sending a `` Send `` message to the worker actor.
2013-02-10 18:34:29 +01:00
.. code-block :: scala
udpConnectionActor ! Send(data)
2013-02-17 13:38:06 +13:00
Again, like the `` Received `` message, the `` Send `` message does not contain a remote address. This is because the address
will always be the endpoint we originally connected to.
2013-02-10 18:34:29 +01:00
2013-02-10 16:46:59 +01:00
.. note ::
2013-02-10 18:34:29 +01:00
There is a small performance benefit in using connection based UDP API over the connectionless one.
2013-02-10 16:46:59 +01:00
If there is a SecurityManager enabled on the system, every connectionless message send has to go through a security
2013-02-10 18:34:29 +01:00
check, while in the case of connection-based UDP the security check is cached after connect, thus writes does
2013-02-10 16:46:59 +01:00
not suffer an additional performance penalty.
2012-01-24 13:37:26 -07:00
2013-02-10 18:34:29 +01:00
Throttling Reads and Writes
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-02-12 13:08:41 +01:00
*This section is not yet ready. More coming soon*
2013-02-10 18:34:29 +01:00
2013-02-10 16:46:59 +01:00
Architecture in-depth
---------------------
2012-07-12 17:16:19 +02:00
2013-02-10 16:46:59 +01:00
For further details on the design and internal architecture see :ref: `io-layer` .
2012-01-24 13:37:26 -07:00
2013-02-15 09:13:39 +01:00
.. _spray.io: http://spray.io