Spark Streaming 编程指南
- Overview
- A Quick Example
- Basic Concepts
- Performance Tuning
- Fault-tolerance Semantics
- Migration Guide from 0.9.1 or below to 1.x
- Where to Go from Here
概述
Spark Streaming是Spark核心API的扩展,提供对实时数据的可扩展、高吞吐、容错的流式计算。数据可以从很多数据源摄入,比如Kafka,Flume,Twitter,ZeroMQ,Kinesis或则TCP套接字,数据可以被一些高层的带有复杂算法的方法处理,比如map、reduce、join和window.最后,被处理后的数据被输出到文件系统中、数据库中或实时仪表盘上。事实上,你可以在Spark流中使用Spark自带的graph processing和machine learning。
在Spark内部,运作方式如下图。Spark Steaming接收实时的输入数据并把它们划分成batches,这些batches稍后会被Spark Engine处理生成最终的结果流。
Spark Streaming提供一个叫做离散流或DStream的高层抽象,它代表不间断的数据流。DStream既可以在来自数据源的数据流中被创建(比如Kafaka,Flume等),也可以在其它DStream中应用高层的操作。在内部,一个DStream就代表一个RDDs序列。
这篇编程指南展示了怎样开始写一个包含DStream的Spark Streaming程序。你可以使用Scala、java或Python(spark1.2引入),都会在这里展示。整篇文章中会有标签标识不同的代码片段。
注意:这里对于Python语言有几个既不相同又不适用的APIs。以下会高亮出来。
A Quick Example
在编写自己的Spark Streaming之前,我们浏览一下一个简单的Spark Streaming程序是怎样的。我们看下统计一个监听的TCP连接中text的单词个数的例子,你所要做的全部工作如下:
First, we import the names of the Spark Streaming classes and some implicit conversions from StreamingContext into our environment in order to add useful methods to other classes we need (like DStream). StreamingContext is the main entry point for all streaming functionality. We create a local StreamingContext with two execution threads, and a batch interval of 1 second.
首先,我们把Spark Streaming 的类和StreamingContext中的一些相关类import进来。StreamingContext是最主要的方法切入点,我们通过2个执行线程创建本地的StreamingContext和一个1秒为时间片的batch。
import org.apache.spark.*;
import org.apache.spark.api.java.function.*;
import org.apache.spark.streaming.*;
import org.apache.spark.streaming.api.java.*;
import scala.Tuple2; // Create a local StreamingContext with two working thread and batch interval of 1 second
SparkConf conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(1))
通过这些上下文,我们就创建了一个从TCP获取资源的DStream,并且已经指定了hostname(如localhost等)和端口号。
// Create a DStream that will connect to hostname:port, like localhost:9999
JavaReceiverInputDStream<String> lines = jssc.socketTextStream("localhost", 9999);
This lines
DStream represents the stream of data that will be received from the data server. Each record in this DStream is a line of text. Next, we want to split the lines by space characters into words.
下面的lines表示将要从数据源接收到的数据流。这个DStream中的每一条记录就是一行text。接下来,我们想要将lines中的单词根据空格分开。
// Split each line into words
JavaDStream<String> words = lines.flatMap(
new FlatMapFunction<String, String>() {
@Override public Iterable<String> call(String x) {
return Arrays.asList(x.split(" "));
}
});
flatMap是一变多的DStream操作,它会将一条记录分成多个新的单词。在这个例子中,每一行将被分割成多个word,这些word组成的流就表示为words DStream。接下来,我们要对这些word进行累加。
// Count each word in each batch
JavaPairDStream<String, Integer> pairs = words.mapToPair(
new PairFunction<String, String, Integer>() {
@Override public Tuple2<String, Integer> call(String s) {
return new Tuple2<String, Integer>(s, 1);
}
});
JavaPairDStream<String, Integer> wordCounts = pairs.reduceByKey(
new Function2<Integer, Integer, Integer>() {
@Override public Integer call(Integer i1, Integer i2) {
return i1 + i2;
}
}); // Print the first ten elements of each RDD generated in this DStream to the console
wordCounts.print();
The words
DStream is further mapped (one-to-one transformation) to a DStream of (word, 1)
pairs, which is then reduced to get the frequency of words in each batch of data. Finally, wordCounts.print()
will print a few of the counts generated every second.
Note that when these lines are executed, Spark Streaming only sets up the computation it will perform when it is started, and no real processing has started yet. To start the processing after all the transformations have been setup, we finally call
ssc.start() // Start the computation
ssc.awaitTermination() // Wait for the computation to terminate
The complete code can be found in the Spark Streaming example NetworkWordCount.
If you have already downloaded and built Spark, you can run this example as follows. You will first need to run Netcat (a small utility found in most Unix-like systems) as a data server by using
$ nc -lk 9999
Then, in a different terminal, you can start the example by using
$ ./bin/run-example streaming.NetworkWordCount localhost 9999
Then, any lines typed in the terminal running the netcat server will be counted and printed on screen every second. It will look something like the following.
|
|
Basic Concepts
Next, we move beyond the simple example and elaborate on the basics of Spark Streaming.
Linking
Similar to Spark, Spark Streaming is available through Maven Central. To write your own Spark Streaming program, you will have to add the following dependency to your SBT or Maven project.
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.6.0</version>
</dependency>
For ingesting data from sources like Kafka, Flume, and Kinesis that are not present in the Spark Streaming core API, you will have to add the corresponding artifact spark-streaming-xyz_2.10
to the dependencies. For example, some of the common ones are as follows.
Source | Artifact |
---|---|
Kafka | spark-streaming-kafka_2.10 |
Flume | spark-streaming-flume_2.10 |
Kinesis | spark-streaming-kinesis-asl_2.10 [Amazon Software License] |
spark-streaming-twitter_2.10 | |
ZeroMQ | spark-streaming-zeromq_2.10 |
MQTT | spark-streaming-mqtt_2.10 |
For an up-to-date list, please refer to the Maven repository for the full list of supported sources and artifacts.
Initializing StreamingContext
To initialize a Spark Streaming program, a StreamingContext object has to be created which is the main entry point of all Spark Streaming functionality.
A StreamingContext object can be created from a SparkConf object.
import org.apache.spark._
import org.apache.spark.streaming._
val conf = new SparkConf().setAppName(appName).setMaster(master)
val ssc = new StreamingContext(conf, Seconds(1))
The appName
parameter is a name for your application to show on the cluster UI. master
is a Spark, Mesos or YARN cluster URL, or a special“local[*]” string to run in local mode. In practice, when running on a cluster, you will not want to hardcode master
in the program, but ratherlaunch the application with spark-submit
and receive it there. However, for local testing and unit tests, you can pass “local[*]” to run Spark Streaming in-process (detects the number of cores in the local system). Note that this internally creates a SparkContext (starting point of all Spark functionality) which can be accessed as ssc.sparkContext
.
The batch interval must be set based on the latency requirements of your application and available cluster resources. See the Performance Tuning section for more details.
A StreamingContext
object can also be created from an existing SparkContext
object.
import org.apache.spark.streaming._
val sc = ... // existing SparkContext
val ssc = new StreamingContext(sc, Seconds(1))
After a context is defined, you have to do the following.
- Define the input sources by creating input DStreams.
- Define the streaming computations by applying transformation and output operations to DStreams.
- Start receiving data and processing it using
streamingContext.start()
. - Wait for the processing to be stopped (manually or due to any error) using
streamingContext.awaitTermination()
. - The processing can be manually stopped using
streamingContext.stop()
.
Points to remember:
- Once a context has been started, no new streaming computations can be set up or added to it.
- Once a context has been stopped, it cannot be restarted.
- Only one StreamingContext can be active in a JVM at the same time.
- stop() on StreamingContext also stops the SparkContext. To stop only the StreamingContext, set the optional parameter of
stop()
calledstopSparkContext
to false. - A SparkContext can be re-used to create multiple StreamingContexts, as long as the previous StreamingContext is stopped (without stopping the SparkContext) before the next StreamingContext is created.
离散流(DStreams)
离散流(也叫DStream)是Spark Streaming提供的最基本的抽象。它代表一个持续的数据流,无论是刚从数据源接收到的,或者是输入流被处理过后生成的新数据流。在内部,DStream代表着一连串的持续的RDD,如下图所示,每一个DStream 里的RDD都包含着一定时间间隔的数据。
任何使用在DStream上的操作在底层都被转换为对RDD的操作。比如,在最早的word count例子中,将流中的句子转换为单词,flatmap方法就是使用在lines DStream上去生成words DStream 的RDD。如下图所示
这些底层的RDD操作由Spark engine来执行。对DStream的操作隐藏了许多细节,提供给开发者一个方便的高层API。这些操作都将在后面详细讨论
Input DStreams 和 Receivers
Input DStream 代表刚从数据源获得的输入数据流。在quik example中,lines就是Input DStream,它代表着从netcat服务器接收到的数据流。每一个输入流(除了file Stream,将在后面讨论)都关联着一个Receiver,它从数据源接收数据并存储到Spark 内存*消费。
Spark Streaming 提供两种内置的streaming源
- 基本源:直接在sreamingContext API中可以使用的。例如:文件系统、socke连接、Akka
- Advanced sources:源头如Kafka,Flume,Kinesis,Twitter等等,这些需要依赖其他包,linking
接下来会详细讨论
注意,如果你要在你的应用中并行地接收多个数据流,你可以创建多个Input DStream,这也将会创建多个receivers来接收多个流数据。但是注意Spark worker/executor是一个长时间运行的task,因此它会占用分配给Spark 应用的核数中的一个。所以,要确保Spark Streaming应用有足够的核数(或者线程,如果本地运行)来处理已经接收到的数据,同时也要保证有足够的核数来运行receiver(s)。
需要记住的点
- 本地运行时,不要使用"local"或"local[1]"作为master URL。它们意味着只使用一个线程来跑task。如果使用了带有recevier的Input DStream(如sockets,Kafka,Flume等),那么仅有的这个线程就会去运行recevier,就没有线程来处理已经接收的数据了。因此,如果要本地运行,通常使用"local[n]",n > recevier的数量。(怎样设置master URL,参见Spark Properties)
- 同理,如果在集群上运行,分配的cores应该大于recevier。
Basic Sources
在之前的例子中,我们已经知道了ssc.socketTextStream(...)通过TCP socket连接接收text数据来创建DStream,除此之外,StreamingContext API提供从文件系统、Akka actors输入源来创建DStream的方法
- 文件流:从任何与HDFS API兼容的文件系统(如HDFS,S3,NFS等)的文件中读取数据,创建DStream:
streamingContext.fileStream<KeyClass, ValueClass, InputFormatClass>(dataDirectory);
Spark Streaming 会监控给出的目录dataDirectory并处理在此目录中创建的任何文件(不支持嵌套目录)。注意:
- 文件必须有相同的数据格式
- 文件必须通过原子操作移动到此目录或重命名
- 一旦完成移动,文件不能被改动。所以如果文件中的数据是连续增加的,新增的数据不会被读取。
对于简单的文件,有一个更简单的方法可以被使用:streamingContext.textFileStream(dataDirectory)。并且文件流不需要recevier,所以不需要分配多余的cores。
fileStream 在Python API中不适用,只有textFileStream可用
-
Streams based on Custom Actors:来从Akka actors接收数据创建DStream,详情参见Custom Receiver Guide
Python API:目前为止
actorStream
只适用于java 和 scala API,还不适用于Python API。 Queue of RDDs as a Stream: 为了使用测试数据测试Spark Streaming应用,也可以通过一个RDDs队列来创建DStream:streamingContext.queueStream(queueOfRDDs)。每一个队列里的RDD将会被当做一个数据batch来对待,像处理 stream一样。
更多的关于socket,files,和actors的详情,参看API文档里的有关方法:scala StreamingContext,Java JavaStreamingContext,Python StreamingContext.
Advanced Sources
Spark1.6.0中,Kafka, Kinesis, Flume and MQTT 在Python API中都有用。
This category of sources require interfacing with external non-Spark libraries, some of them with complex dependencies (e.g., Kafka and Flume). Hence, to minimize issues related to version conflicts of dependencies, the functionality to create DStreams from these sources has been moved to separate libraries that can be linked to explicitly when necessary. For example, if you want to create a DStream using data from Twitter’s stream of tweets, you have to do the following:
这一类的source中需要扩展的非Spark类库,它们其中一些有着很复杂的依赖(像Kafka和Flume).因此,为了减小依赖间的版本冲突带来的问题,这些source被设计成分离的API,需要时再引入。举个例子,如果你想创建一个Twitter流,你将要以下步骤:
- Linking:添加 spark-streaming-twitter_2.10到SBT/Maven依赖中
- Programming:导入TwtterUtils类并通过TwitterUtils.createStream创建DStream。
- Deploying:生成一个包含有所有依赖的JAR,并部署这个JAR.详情见Deploying section.
import org.apache.spark.streaming.twitter.*; TwitterUtils.createStream(jssc);
注意这些advanced sources 在Spark Shell,因此不能再Shell里测试.如果确实想在Shell 里面使用,你需要下载相应的 Maven依赖,并添加入classpath.
一些advanced sources如下:
Kafka: Spark Streaming 1.6.0 is compatible with Kafka 0.8.2.1. See the Kafka Integration Guide for more details.
Flume: Spark Streaming 1.6.0 is compatible with Flume 1.6.0. See the Flume Integration Guide for more details.
Kinesis: Spark Streaming 1.6.0 is compatible with Kinesis Client Library 1.2.1. See the Kinesis Integration Guide for more details.
Twitter: Spark Streaming’s TwitterUtils uses Twitter4j to get the public stream of tweets using Twitter’s Streaming API. Authentication information can be provided by any of the methods supported by Twitter4J library. You can either get the public stream, or get the filtered stream based on a keywords. See the API documentation (Scala, Java) and examples (TwitterPopularTags and TwitterAlgebirdCMS).
Custom Sources
Python API目前为止暂时还不支持
Input DStreams 也能被第三方数据源创建。你所要做的就是实现一个用户自定义的recevier来从custorm source接收数据,然后将数据推送给Spark。详情请参见Custom Receiver Guide
Receiver的可靠性
根据可靠性可将数据源分为两类。支持数据ack的数据源(比如Kafka和Flume)如果系统从这些可靠的数据源收到数据并正常ack,这就能保证没有数据回应为任何错误而丢失。以下是两类数据源:
- 可靠Receiver :可靠的recevier正确接到数据并存储到Spark里后,向数据源发送ack信息
- 不可靠Receiver :一个不可靠的recevier不发送ack信息给数据源。这可以用在不支持ack的数据源中,或者是使用可靠数据源但不想使用ack时。
关于如何写可靠的recevier,见Custom Receiver Guide.
Transformations on DStreams
和RDDs一样,transformation 允许输入数据中的数据被改动。DStreams 在普通的Spark RDD上支持不少transformation。它们的其中一些如下:
Transformation | Meaning |
---|---|
map(func) |
源DStream中的每一个元素在通过方法func的处理后返回一个新的DStream |
flatMap(func) |
和map类似,但是每一个输入目标可能对应着0个或多个输出目标 |
filter(func) |
返回使func返回true的DStream,过滤器 |
repartition(numPartitions) |
通过创建更多或更少的分区来改变DStream的并行度 |
union(otherStream) |
返回一个包含源DStream和其它DStream的元素的集合的新的DStream |
count() |
返回一个每个RDD只包含一个元素的单元素RDD DStream,这个RDD中的元素就是源DStream中每个RDD中元素个数的统计值 |
reduce(func) |
返回一个单元素RDD DStream,每一个RDD的元素就是源DStream中的每一个RDD的元素通过func方法聚合得到的。这个方法应该被设计成associative 的,以便利于并行计算。 |
countByValue() |
当在元素类型为K的DStream上使用时,就会返回一个包含(K,Long)对的DStream,值就是源DStream中每个RDD中每个键出现的频率 |
reduceByKey(func, [numTasks]) |
在含有(K,V)对的DStream上调用,返回每个K对应的V通过给定函数聚合之后的键值对DStream。注意:默认情况下,使用Spark 默认的并行task数(本地情况默认为2,集群情况下通过配置文件属性spark.default.parallelism来配置)来进行分组。你可以通过numTask参数来设置不同的tasks数 |
join(otherStream, [numTasks]) |
在包含(K,V)和(K,W)对的两条DStream上使用此方法,将返回包含(K,(V,W))对的DStream |
cogroup(otherStream, [numTasks]) |
在包含(K,V)和(K,W)对的DStream上调用,将会返回一个包含(K,Seq[V],Seq[W])的tuples |
transform(func) |
使用func方法处理源DStream的每一个RDD,每个RDD生成一个新的RDD。这可以被用来处理DStream的任意RDD |
updateStateByKey(func) |
通过给定的方法func作用于先前的状态和每个键对应的新值来改变DStream中每个键的状态,返回一个带有新状态的DSream。这能被用做维持每个键任意状态数据。 |
A few of these transformations are worth discussing in more detail.
UpdateStateByKey Operation
The updateStateByKey
operation allows you to maintain arbitrary state while continuously updating it with new information. To use this, you will have to do two steps.
- Define the state - The state can be an arbitrary data type.
- Define the state update function - Specify with a function how to update the state using the previous state and the new values from an input stream.
In every batch, Spark will apply the state update function for all existing keys, regardless of whether they have new data in a batch or not. If the update function returns None
then the key-value pair will be eliminated.
Let’s illustrate this with an example. Say you want to maintain a running count of each word seen in a text data stream. Here, the running count is the state and it is an integer. We define the update function as:
def updateFunction(newValues: Seq[Int], runningCount: Option[Int]): Option[Int] = {
val newCount = ... // add the new values with the previous running count to get the new count
Some(newCount)
}
This is applied on a DStream containing words (say, the pairs
DStream containing (word, 1)
pairs in the earlier example).
val runningCounts = pairs.updateStateByKey[Int](updateFunction _)
The update function will be called for each word, with newValues
having a sequence of 1’s (from the (word, 1)
pairs) and the runningCount
having the previous count. For the complete Scala code, take a look at the example StatefulNetworkWordCount.scala.
Note that using updateStateByKey
requires the checkpoint directory to be configured, which is discussed in detail in the checkpointing section.
Transform Operation
The transform
operation (along with its variations like transformWith
) allows arbitrary RDD-to-RDD functions to be applied on a DStream. It can be used to apply any RDD operation that is not exposed in the DStream API. For example, the functionality of joining every batch in a data stream with another dataset is not directly exposed in the DStream API. However, you can easily use transform
to do this. This enables very powerful possibilities. For example, one can do real-time data cleaning by joining the input data stream with precomputed spam information (maybe generated with Spark as well) and then filtering based on it.
val spamInfoRDD = ssc.sparkContext.newAPIHadoopRDD(...) // RDD containing spam information
val cleanedDStream = wordCounts.transform(rdd => {
rdd.join(spamInfoRDD).filter(...) // join data stream with spam information to do data cleaning
...
})
Note that the supplied function gets called in every batch interval. This allows you to do time-varying RDD operations, that is, RDD operations, number of partitions, broadcast variables, etc. can be changed between batches.
Window Operations
Spark Streaming also provides windowed computations, which allow you to apply transformations over a sliding window of data. The following figure illustrates this sliding window.
As shown in the figure, every time the window slides over a source DStream, the source RDDs that fall within the window are combined and operated upon to produce the RDDs of the windowed DStream. In this specific case, the operation is applied over the last 3 time units of data, and slides by 2 time units. This shows that any window operation needs to specify two parameters.
- window length - The duration of the window (3 in the figure).
- sliding interval - The interval at which the window operation is performed (2 in the figure).
These two parameters must be multiples of the batch interval of the source DStream (1 in the figure).
Let’s illustrate the window operations with an example. Say, you want to extend the earlier example by generating word counts over the last 30 seconds of data, every 10 seconds. To do this, we have to apply the reduceByKey
operation on the pairs
DStream of (word, 1)
pairs over the last 30 seconds of data. This is done using the operation reduceByKeyAndWindow
.
// Reduce last 30 seconds of data, every 10 seconds
val windowedWordCounts = pairs.reduceByKeyAndWindow((a:Int,b:Int) => (a + b), Seconds(30), Seconds(10))
Some of the common window operations are as follows. All of these operations take the said two parameters - windowLength and slideInterval.
Transformation | Meaning |
---|---|
window(windowLength, slideInterval) | Return a new DStream which is computed based on windowed batches of the source DStream. |
countByWindow(windowLength,slideInterval) | Return a sliding window count of elements in the stream. |
reduceByWindow(func, windowLength,slideInterval) | Return a new single-element stream, created by aggregating elements in the stream over a sliding interval using func. The function should be associative so that it can be computed correctly in parallel. |
reduceByKeyAndWindow(func,windowLength, slideInterval, [numTasks]) | When called on a DStream of (K, V) pairs, returns a new DStream of (K, V) pairs where the values for each key are aggregated using the given reduce function func over batches in a sliding window. Note: By default, this uses Spark's default number of parallel tasks (2 for local mode, and in cluster mode the number is determined by the config propertyspark.default.parallelism ) to do the grouping. You can pass an optional numTasks argument to set a different number of tasks. |
reduceByKeyAndWindow(func, invFunc,windowLength, slideInterval, [numTasks]) |
A more efficient version of the above |
countByValueAndWindow(windowLength,slideInterval, [numTasks]) | When called on a DStream of (K, V) pairs, returns a new DStream of (K, Long) pairs where the value of each key is its frequency within a sliding window. Like in reduceByKeyAndWindow , the number of reduce tasks is configurable through an optional argument. |
Join Operations
Finally, its worth highlighting how easily you can perform different kinds of joins in Spark Streaming.
Stream-stream joins
Streams can be very easily joined with other streams.
val stream1: DStream[String, String] = ...
val stream2: DStream[String, String] = ...
val joinedStream = stream1.join(stream2)
Here, in each batch interval, the RDD generated by stream1
will be joined with the RDD generated by stream2
. You can also do leftOuterJoin
,rightOuterJoin
, fullOuterJoin
. Furthermore, it is often very useful to do joins over windows of the streams. That is pretty easy as well.
val windowedStream1 = stream1.window(Seconds(20))
val windowedStream2 = stream2.window(Minutes(1))
val joinedStream = windowedStream1.join(windowedStream2)
Stream-dataset joins
This has already been shown earlier while explain DStream.transform
operation. Here is yet another example of joining a windowed stream with a dataset.
val dataset: RDD[String, String] = ...
val windowedStream = stream.window(Seconds(20))...
val joinedStream = windowedStream.transform { rdd => rdd.join(dataset) }
In fact, you can also dynamically change the dataset you want to join against. The function provided to transform
is evaluated every batch interval and therefore will use the current dataset that dataset
reference points to.
The complete list of DStream transformations is available in the API documentation. For the Scala API, see DStream and PairDStreamFunctions. For the Java API, see JavaDStream and JavaPairDStream. For the Python API, see DStream.
Output Operations on DStreams
Output operations allow DStream’s data to be pushed out to external systems like a database or a file systems. Since the output operations actually allow the transformed data to be consumed by external systems, they trigger the actual execution of all the DStream transformations (similar to actions for RDDs). Currently, the following output operations are defined:
Output Operation | Meaning |
---|---|
print() | Prints the first ten elements of every batch of data in a DStream on the driver node running the streaming application. This is useful for development and debugging. Python API This is called pprint() in the Python API. |
saveAsTextFiles(prefix, [suffix]) | Save this DStream's contents as text files. The file name at each batch interval is generated based on prefix and suffix: "prefix-TIME_IN_MS[.suffix]". |
saveAsObjectFiles(prefix, [suffix]) | Save this DStream's contents as SequenceFiles of serialized Java objects. The file name at each batch interval is generated based on prefix and suffix: "prefix-TIME_IN_MS[.suffix]". Python API This is not available in the Python API. |
saveAsHadoopFiles(prefix, [suffix]) | Save this DStream's contents as Hadoop files. The file name at each batch interval is generated based on prefix and suffix: "prefix-TIME_IN_MS[.suffix]". Python API This is not available in the Python API. |
foreachRDD(func) | The most generic output operator that applies a function, func, to each RDD generated from the stream. This function should push the data in each RDD to an external system, such as saving the RDD to files, or writing it over the network to a database. Note that the function func is executed in the driver process running the streaming application, and will usually have RDD actions in it that will force the computation of the streaming RDDs. |
Design Patterns for using foreachRDD
dstream.foreachRDD
is a powerful primitive that allows data to be sent out to external systems. However, it is important to understand how to use this primitive correctly and efficiently. Some of the common mistakes to avoid are as follows.
Often writing data to external system requires creating a connection object (e.g. TCP connection to a remote server) and using it to send data to a remote system. For this purpose, a developer may inadvertently try creating a connection object at the Spark driver, and then try to use it in a Spark worker to save records in the RDDs. For example (in Scala),
dstream.foreachRDD { rdd =>
val connection = createNewConnection() // executed at the driver
rdd.foreach { record =>
connection.send(record) // executed at the worker
}
}
This is incorrect as this requires the connection object to be serialized and sent from the driver to the worker. Such connection objects are rarely transferrable across machines. This error may manifest as serialization errors (connection object not serializable), initialization errors (connection object needs to be initialized at the workers), etc. The correct solution is to create the connection object at the worker.
However, this can lead to another common mistake - creating a new connection for every record. For example,
dstream.foreachRDD { rdd =>
rdd.foreach { record =>
val connection = createNewConnection()
connection.send(record)
connection.close()
}
}
Typically, creating a connection object has time and resource overheads. Therefore, creating and destroying a connection object for each record can incur unnecessarily high overheads and can significantly reduce the overall throughput of the system. A better solution is to userdd.foreachPartition
- create a single connection object and send all the records in a RDD partition using that connection.
dstream.foreachRDD { rdd =>
rdd.foreachPartition { partitionOfRecords =>
val connection = createNewConnection()
partitionOfRecords.foreach(record => connection.send(record))
connection.close()
}
}
This amortizes the connection creation overheads over many records.
Finally, this can be further optimized by reusing connection objects across multiple RDDs/batches. One can maintain a static pool of connection objects than can be reused as RDDs of multiple batches are pushed to the external system, thus further reducing the overheads.
dstream.foreachRDD { rdd =>
rdd.foreachPartition { partitionOfRecords =>
// ConnectionPool is a static, lazily initialized pool of connections
val connection = ConnectionPool.getConnection()
partitionOfRecords.foreach(record => connection.send(record))
ConnectionPool.returnConnection(connection) // return to the pool for future reuse
}
}
Note that the connections in the pool should be lazily created on demand and timed out if not used for a while. This achieves the most efficient sending of data to external systems.
Other points to remember:
DStreams are executed lazily by the output operations, just like RDDs are lazily executed by RDD actions. Specifically, RDD actions inside the DStream output operations force the processing of the received data. Hence, if your application does not have any output operation, or has output operations like
dstream.foreachRDD()
without any RDD action inside them, then nothing will get executed. The system will simply receive the data and discard it.By default, output operations are executed one-at-a-time. And they are executed in the order they are defined in the application.
DataFrame and SQL Operations
You can easily use DataFrames and SQL operations on streaming data. You have to create a SQLContext using the SparkContext that the StreamingContext is using. Furthermore this has to done such that it can be restarted on driver failures. This is done by creating a lazily instantiated singleton instance of SQLContext. This is shown in the following example. It modifies the earlier word count example to generate word counts using DataFrames and SQL. Each RDD is converted to a DataFrame, registered as a temporary table and then queried using SQL.
/** DataFrame operations inside your streaming program */
val words: DStream[String] = ...
words.foreachRDD { rdd =>
// Get the singleton instance of SQLContext
val sqlContext = SQLContext.getOrCreate(rdd.sparkContext)
import sqlContext.implicits._
// Convert RDD[String] to DataFrame
val wordsDataFrame = rdd.toDF("word")
// Register as table
wordsDataFrame.registerTempTable("words")
// Do word count on DataFrame using SQL and print it
val wordCountsDataFrame =
sqlContext.sql("select word, count(*) as total from words group by word")
wordCountsDataFrame.show()
}
See the full source code.
You can also run SQL queries on tables defined on streaming data from a different thread (that is, asynchronous to the running StreamingContext). Just make sure that you set the StreamingContext to remember a sufficient amount of streaming data such that the query can run. Otherwise the StreamingContext, which is unaware of the any asynchronous SQL queries, will delete off old streaming data before the query can complete. For example, if you want to query the last batch, but your query can take 5 minutes to run, then callstreamingContext.remember(Minutes(5))
(in Scala, or equivalent in other languages).
See the DataFrames and SQL guide to learn more about DataFrames.
MLlib Operations
You can also easily use machine learning algorithms provided by MLlib. First of all, there are streaming machine learning algorithms (e.g.Streaming Linear Regression, Streaming KMeans, etc.) which can simultaneously learn from the streaming data as well as apply the model on the streaming data. Beyond these, for a much larger class of machine learning algorithms, you can learn a learning model offline (i.e. using historical data) and then apply the model online on streaming data. See the MLlib guide for more details.
Caching / Persistence
Similar to RDDs, DStreams also allow developers to persist the stream’s data in memory. That is, using the persist()
method on a DStream will automatically persist every RDD of that DStream in memory. This is useful if the data in the DStream will be computed multiple times (e.g., multiple operations on the same data). For window-based operations like reduceByWindow
and reduceByKeyAndWindow
and state-based operations like updateStateByKey
, this is implicitly true. Hence, DStreams generated by window-based operations are automatically persisted in memory, without the developer calling persist()
.
For input streams that receive data over the network (such as, Kafka, Flume, sockets, etc.), the default persistence level is set to replicate the data to two nodes for fault-tolerance.
Note that, unlike RDDs, the default persistence level of DStreams keeps the data serialized in memory. This is further discussed in thePerformance Tuning section. More information on different persistence levels can be found in the Spark Programming Guide.
Checkpointing
A streaming application must operate 24/7 and hence must be resilient to failures unrelated to the application logic (e.g., system failures, JVM crashes, etc.). For this to be possible, Spark Streaming needs to checkpoint enough information to a fault- tolerant storage system such that it can recover from failures. There are two types of data that are checkpointed.
-
Metadata checkpointing - Saving of the information defining the streaming computation to fault-tolerant storage like HDFS. This is used to recover from failure of the node running the driver of the streaming application (discussed in detail later). Metadata includes:
- Configuration - The configuration that was used to create the streaming application.
- DStream operations - The set of DStream operations that define the streaming application.
- Incomplete batches - Batches whose jobs are queued but have not completed yet.
- Data checkpointing - Saving of the generated RDDs to reliable storage. This is necessary in some stateful transformations that combine data across multiple batches. In such transformations, the generated RDDs depend on RDDs of previous batches, which causes the length of the dependency chain to keep increasing with time. To avoid such unbounded increases in recovery time (proportional to dependency chain), intermediate RDDs of stateful transformations are periodically checkpointed to reliable storage (e.g. HDFS) to cut off the dependency chains.
To summarize, metadata checkpointing is primarily needed for recovery from driver failures, whereas data or RDD checkpointing is necessary even for basic functioning if stateful transformations are used.
When to enable Checkpointing
Checkpointing must be enabled for applications with any of the following requirements:
-
Usage of stateful transformations - If either
updateStateByKey
orreduceByKeyAndWindow
(with inverse function) is used in the application, then the checkpoint directory must be provided to allow for periodic RDD checkpointing. - Recovering from failures of the driver running the application - Metadata checkpoints are used to recover with progress information.
Note that simple streaming applications without the aforementioned stateful transformations can be run without enabling checkpointing. The recovery from driver failures will also be partial in that case (some received but unprocessed data may be lost). This is often acceptable and many run Spark Streaming applications in this way. Support for non-Hadoop environments is expected to improve in the future.
How to configure Checkpointing
Checkpointing can be enabled by setting a directory in a fault-tolerant, reliable file system (e.g., HDFS, S3, etc.) to which the checkpoint information will be saved. This is done by using streamingContext.checkpoint(checkpointDirectory)
. This will allow you to use the aforementioned stateful transformations. Additionally, if you want to make the application recover from driver failures, you should rewrite your streaming application to have the following behavior.
- When the program is being started for the first time, it will create a new StreamingContext, set up all the streams and then call start().
- When the program is being restarted after failure, it will re-create a StreamingContext from the checkpoint data in the checkpoint directory.
This behavior is made simple by using StreamingContext.getOrCreate
. This is used as follows.
// Function to create and setup a new StreamingContext
def functionToCreateContext(): StreamingContext = {
val ssc = new StreamingContext(...) // new context
val lines = ssc.socketTextStream(...) // create DStreams
...
ssc.checkpoint(checkpointDirectory) // set checkpoint directory
ssc
}
// Get StreamingContext from checkpoint data or create a new one
val context = StreamingContext.getOrCreate(checkpointDirectory, functionToCreateContext _)
// Do additional setup on context that needs to be done,
// irrespective of whether it is being started or restarted
context. ...
// Start the context
context.start()
context.awaitTermination()
If the checkpointDirectory
exists, then the context will be recreated from the checkpoint data. If the directory does not exist (i.e., running for the first time), then the function functionToCreateContext
will be called to create a new context and set up the DStreams. See the Scala exampleRecoverableNetworkWordCount. This example appends the word counts of network data into a file.
In addition to using getOrCreate
one also needs to ensure that the driver process gets restarted automatically on failure. This can only be done by the deployment infrastructure that is used to run the application. This is further discussed in the Deployment section.
Note that checkpointing of RDDs incurs the cost of saving to reliable storage. This may cause an increase in the processing time of those batches where RDDs get checkpointed. Hence, the interval of checkpointing needs to be set carefully. At small batch sizes (say 1 second), checkpointing every batch may significantly reduce operation throughput. Conversely, checkpointing too infrequently causes the lineage and task sizes to grow, which may have detrimental effects. For stateful transformations that require RDD checkpointing, the default interval is a multiple of the batch interval that is at least 10 seconds. It can be set by using dstream.checkpoint(checkpointInterval)
. Typically, a checkpoint interval of 5 - 10 sliding intervals of a DStream is a good setting to try.
Deploying Applications
This section discusses the steps to deploy a Spark Streaming application.
Requirements
To run a Spark Streaming applications, you need to have the following.
Cluster with a cluster manager - This is the general requirement of any Spark application, and discussed in detail in the deployment guide.
Package the application JAR - You have to compile your streaming application into a JAR. If you are using
spark-submit
to start the application, then you will not need to provide Spark and Spark Streaming in the JAR. However, if your application uses advanced sources(e.g. Kafka, Flume, Twitter), then you will have to package the extra artifact they link to, along with their dependencies, in the JAR that is used to deploy the application. For example, an application usingTwitterUtils
will have to includespark-streaming-twitter_2.10
and all its transitive dependencies in the application JAR.Configuring sufficient memory for the executors - Since the received data must be stored in memory, the executors must be configured with sufficient memory to hold the received data. Note that if you are doing 10 minute window operations, the system has to keep at least last 10 minutes of data in memory. So the memory requirements for the application depends on the operations used in it.
Configuring checkpointing - If the stream application requires it, then a directory in the Hadoop API compatible fault-tolerant storage (e.g. HDFS, S3, etc.) must be configured as the checkpoint directory and the streaming application written in a way that checkpoint information can be used for failure recovery. See the checkpointing section for more details.
-
Configuring automatic restart of the application driver - To automatically recover from a driver failure, the deployment infrastructure that is used to run the streaming application must monitor the driver process and relaunch the driver if it fails. Different cluster managers have different tools to achieve this.
- Spark Standalone - A Spark application driver can be submitted to run within the Spark Standalone cluster (see cluster deploy mode), that is, the application driver itself runs on one of the worker nodes. Furthermore, the Standalone cluster manager can be instructed tosupervise the driver, and relaunch it if the driver fails either due to non-zero exit code, or due to failure of the node running the driver. See cluster mode and supervise in the Spark Standalone guide for more details.
- YARN - Yarn supports a similar mechanism for automatically restarting an application. Please refer to YARN documentation for more details.
- Mesos - Marathon has been used to achieve this with Mesos.
Configuring write ahead logs - Since Spark 1.2, we have introduced write ahead logs for achieving strong fault-tolerance guarantees. If enabled, all the data received from a receiver gets written into a write ahead log in the configuration checkpoint directory. This prevents data loss on driver recovery, thus ensuring zero data loss (discussed in detail in the Fault-tolerance Semantics section). This can be enabled by setting the configuration parameter
spark.streaming.receiver.writeAheadLog.enable
totrue
. However, these stronger semantics may come at the cost of the receiving throughput of individual receivers. This can be corrected by running more receivers in parallel to increase aggregate throughput. Additionally, it is recommended that the replication of the received data within Spark be disabled when the write ahead log is enabled as the log is already stored in a replicated storage system. This can be done by setting the storage level for the input stream toStorageLevel.MEMORY_AND_DISK_SER
.-
Setting the max receiving rate - If the cluster resources is not large enough for the streaming application to process data as fast as it is being received, the receivers can be rate limited by setting a maximum rate limit in terms of records / sec. See the configuration parameters
spark.streaming.receiver.maxRate
for receivers andspark.streaming.kafka.maxRatePerPartition
for Direct Kafka approach. In Spark 1.5, we have introduced a feature called backpressure that eliminate the need to set this rate limit, as Spark Streaming automatically figures out the rate limits and dynamically adjusts them if the processing conditions change. This backpressure can be enabled by setting theconfiguration parameterspark.streaming.backpressure.enabled
totrue
.
Upgrading Application Code
If a running Spark Streaming application needs to be upgraded with new application code, then there are two possible mechanisms.
The upgraded Spark Streaming application is started and run in parallel to the existing application. Once the new one (receiving the same data as the old one) has been warmed up and is ready for prime time, the old one be can be brought down. Note that this can be done for data sources that support sending the data to two destinations (i.e., the earlier and upgraded applications).
The existing application is shutdown gracefully (see
StreamingContext.stop(...)
orJavaStreamingContext.stop(...)
for graceful shutdown options) which ensure data that has been received is completely processed before shutdown. Then the upgraded application can be started, which will start processing from the same point where the earlier application left off. Note that this can be done only with input sources that support source-side buffering (like Kafka, and Flume) as data needs to be buffered while the previous application was down and the upgraded application is not yet up. And restarting from earlier checkpoint information of pre-upgrade code cannot be done. The checkpoint information essentially contains serialized Scala/Java/Python objects and trying to deserialize objects with new, modified classes may lead to errors. In this case, either start the upgraded app with a different checkpoint directory, or delete the previous checkpoint directory.
Other Considerations
If the data is being received by the receivers faster than what can be processed, you can limit the rate by setting the configuration parameterspark.streaming.receiver.maxRate
.
Monitoring Applications
Beyond Spark’s monitoring capabilities, there are additional capabilities specific to Spark Streaming. When a StreamingContext is used, theSpark web UI shows an additional Streaming
tab which shows statistics about running receivers (whether receivers are active, number of records received, receiver error, etc.) and completed batches (batch processing times, queueing delays, etc.). This can be used to monitor the progress of the streaming application.
The following two metrics in web UI are particularly important:
- Processing Time - The time to process each batch of data.
- Scheduling Delay - the time a batch waits in a queue for the processing of previous batches to finish.
If the batch processing time is consistently more than the batch interval and/or the queueing delay keeps increasing, then it indicates that the system is not able to process the batches as fast they are being generated and is falling behind. In that case, consider reducing the batch processing time.
The progress of a Spark Streaming program can also be monitored using the StreamingListener interface, which allows you to get receiver status and processing times. Note that this is a developer API and it is likely to be improved upon (i.e., more information reported) in the future.
Performance Tuning
Getting the best performance out of a Spark Streaming application on a cluster requires a bit of tuning. This section explains a number of the parameters and configurations that can be tuned to improve the performance of you application. At a high level, you need to consider two things:
Reducing the processing time of each batch of data by efficiently using cluster resources.
Setting the right batch size such that the batches of data can be processed as fast as they are received (that is, data processing keeps up with the data ingestion).
Reducing the Batch Processing Times
There are a number of optimizations that can be done in Spark to minimize the processing time of each batch. These have been discussed in detail in the Tuning Guide. This section highlights some of the most important ones.
Level of Parallelism in Data Receiving
Receiving data over the network (like Kafka, Flume, socket, etc.) requires the data to be deserialized and stored in Spark. If the data receiving becomes a bottleneck in the system, then consider parallelizing the data receiving. Note that each input DStream creates a single receiver (running on a worker machine) that receives a single stream of data. Receiving multiple data streams can therefore be achieved by creating multiple input DStreams and configuring them to receive different partitions of the data stream from the source(s). For example, a single Kafka input DStream receiving two topics of data can be split into two Kafka input streams, each receiving only one topic. This would run two receivers, allowing data to be received in parallel, thus increasing overall throughput. These multiple DStreams can be unioned together to create a single DStream. Then the transformations that were being applied on a single input DStream can be applied on the unified stream. This is done as follows.
val numStreams = 5
val kafkaStreams = (1 to numStreams).map { i => KafkaUtils.createStream(...) }
val unifiedStream = streamingContext.union(kafkaStreams)
unifiedStream.print()
Another parameter that should be considered is the receiver’s blocking interval, which is determined by the configuration parameterspark.streaming.blockInterval
. For most receivers, the received data is coalesced together into blocks of data before storing inside Spark’s memory. The number of blocks in each batch determines the number of tasks that will be used to process the received data in a map-like transformation. The number of tasks per receiver per batch will be approximately (batch interval / block interval). For example, block interval of 200 ms will create 10 tasks per 2 second batches. If the number of tasks is too low (that is, less than the number of cores per machine), then it will be inefficient as all available cores will not be used to process the data. To increase the number of tasks for a given batch interval, reduce the block interval. However, the recommended minimum value of block interval is about 50 ms, below which the task launching overheads may be a problem.
An alternative to receiving data with multiple input streams / receivers is to explicitly repartition the input data stream (usinginputStream.repartition(<number of partitions>)
). This distributes the received batches of data across the specified number of machines in the cluster before further processing.
Level of Parallelism in Data Processing
Cluster resources can be under-utilized if the number of parallel tasks used in any stage of the computation is not high enough. For example, for distributed reduce operations like reduceByKey
and reduceByKeyAndWindow
, the default number of parallel tasks is controlled by thespark.default.parallelism
configuration property. You can pass the level of parallelism as an argument (see PairDStreamFunctions
documentation), or set the spark.default.parallelism
configuration property to change the default.
Data Serialization
The overheads of data serialization can be reduced by tuning the serialization formats. In the case of streaming, there are two types of data that are being serialized.
Input data: By default, the input data received through Receivers is stored in the executors’ memory withStorageLevel.MEMORY_AND_DISK_SER_2. That is, the data is serialized into bytes to reduce GC overheads, and replicated for tolerating executor failures. Also, the data is kept first in memory, and spilled over to disk only if the memory is insufficient to hold all of the input data necessary for the streaming computation. This serialization obviously has overheads – the receiver must deserialize the received data and re-serialize it using Spark’s serialization format.
Persisted RDDs generated by Streaming Operations: RDDs generated by streaming computations may be persisted in memory. For example, window operations persist data in memory as they would be processed multiple times. However, unlike the Spark Core default ofStorageLevel.MEMORY_ONLY, persisted RDDs generated by streaming computations are persisted withStorageLevel.MEMORY_ONLY_SER (i.e. serialized) by default to minimize GC overheads.
In both cases, using Kryo serialization can reduce both CPU and memory overheads. See the Spark Tuning Guide for more details. For Kryo, consider registering custom classes, and disabling object reference tracking (see Kryo-related configurations in the Configuration Guide).
In specific cases where the amount of data that needs to be retained for the streaming application is not large, it may be feasible to persist data (both types) as deserialized objects without incurring excessive GC overheads. For example, if you are using batch intervals of a few seconds and no window operations, then you can try disabling serialization in persisted data by explicitly setting the storage level accordingly. This would reduce the CPU overheads due to serialization, potentially improving performance without too much GC overheads.
Task Launching Overheads
If the number of tasks launched per second is high (say, 50 or more per second), then the overhead of sending out tasks to the slaves may be significant and will make it hard to achieve sub-second latencies. The overhead can be reduced by the following changes:
Task Serialization: Using Kryo serialization for serializing tasks can reduce the task sizes, and therefore reduce the time taken to send them to the slaves. This is controlled by the
spark.closure.serializer
property. However, at this time, Kryo serialization cannot be enabled for closure serialization. This may be resolved in a future release.Execution mode: Running Spark in Standalone mode or coarse-grained Mesos mode leads to better task launch times than the fine-grained Mesos mode. Please refer to the Running on Mesos guide for more details.
These changes may reduce batch processing time by 100s of milliseconds, thus allowing sub-second batch size to be viable.
Setting the Right Batch Interval
For a Spark Streaming application running on a cluster to be stable, the system should be able to process data as fast as it is being received. In other words, batches of data should be processed as fast as they are being generated. Whether this is true for an application can be found bymonitoring the processing times in the streaming web UI, where the batch processing time should be less than the batch interval.
Depending on the nature of the streaming computation, the batch interval used may have significant impact on the data rates that can be sustained by the application on a fixed set of cluster resources. For example, let us consider the earlier WordCountNetwork example. For a particular data rate, the system may be able to keep up with reporting word counts every 2 seconds (i.e., batch interval of 2 seconds), but not every 500 milliseconds. So the batch interval needs to be set such that the expected data rate in production can be sustained.
A good approach to figure out the right batch size for your application is to test it with a conservative batch interval (say, 5-10 seconds) and a low data rate. To verify whether the system is able to keep up with the data rate, you can check the value of the end-to-end delay experienced by each processed batch (either look for “Total delay” in Spark driver log4j logs, or use the StreamingListener interface). If the delay is maintained to be comparable to the batch size, then system is stable. Otherwise, if the delay is continuously increasing, it means that the system is unable to keep up and it therefore unstable. Once you have an idea of a stable configuration, you can try increasing the data rate and/or reducing the batch size. Note that a momentary increase in the delay due to temporary data rate increases may be fine as long as the delay reduces back to a low value (i.e., less than batch size).
Memory Tuning
Tuning the memory usage and GC behavior of Spark applications has been discussed in great detail in the Tuning Guide. It is strongly recommended that you read that. In this section, we discuss a few tuning parameters specifically in the context of Spark Streaming applications.
The amount of cluster memory required by a Spark Streaming application depends heavily on the type of transformations used. For example, if you want to use a window operation on the last 10 minutes of data, then your cluster should have sufficient memory to hold 10 minutes worth of data in memory. Or if you want to use updateStateByKey
with a large number of keys, then the necessary memory will be high. On the contrary, if you want to do a simple map-filter-store operation, then the necessary memory will be low.
In general, since the data received through receivers is stored with StorageLevel.MEMORY_AND_DISK_SER_2, the data that does not fit in memory will spill over to the disk. This may reduce the performance of the streaming application, and hence it is advised to provide sufficient memory as required by your streaming application. Its best to try and see the memory usage on a small scale and estimate accordingly.
Another aspect of memory tuning is garbage collection. For a streaming application that requires low latency, it is undesirable to have large pauses caused by JVM Garbage Collection.
There are a few parameters that can help you tune the memory usage and GC overheads:
Persistence Level of DStreams: As mentioned earlier in the Data Serialization section, the input data and RDDs are by default persisted as serialized bytes. This reduces both the memory usage and GC overheads, compared to deserialized persistence. Enabling Kryo serialization further reduces serialized sizes and memory usage. Further reduction in memory usage can be achieved with compression (see the Spark configuration
spark.rdd.compress
), at the cost of CPU time.Clearing old data: By default, all input data and persisted RDDs generated by DStream transformations are automatically cleared. Spark Streaming decides when to clear the data based on the transformations that are used. For example, if you are using a window operation of 10 minutes, then Spark Streaming will keep around the last 10 minutes of data, and actively throw away older data. Data can be retained for a longer duration (e.g. interactively querying older data) by setting
streamingContext.remember
.CMS Garbage Collector: Use of the concurrent mark-and-sweep GC is strongly recommended for keeping GC-related pauses consistently low. Even though concurrent GC is known to reduce the overall processing throughput of the system, its use is still recommended to achieve more consistent batch processing times. Make sure you set the CMS GC on both the driver (using
--driver-java-options
inspark-submit
) and the executors (using Spark configurationspark.executor.extraJavaOptions
).-
Other tips: To further reduce GC overheads, here are some more tips to try.
- Use Tachyon for off-heap storage of persisted RDDs. See more detail in the Spark Programming Guide.
- Use more executors with smaller heap sizes. This will reduce the GC pressure within each JVM heap.
Fault-tolerance Semantics
In this section, we will discuss the behavior of Spark Streaming applications in the event of failures.
Background
To understand the semantics provided by Spark Streaming, let us remember the basic fault-tolerance semantics of Spark’s RDDs.
- An RDD is an immutable, deterministically re-computable, distributed dataset. Each RDD remembers the lineage of deterministic operations that were used on a fault-tolerant input dataset to create it.
- If any partition of an RDD is lost due to a worker node failure, then that partition can be re-computed from the original fault-tolerant dataset using the lineage of operations.
- Assuming that all of the RDD transformations are deterministic, the data in the final transformed RDD will always be the same irrespective of failures in the Spark cluster.
Spark operates on data in fault-tolerant file systems like HDFS or S3. Hence, all of the RDDs generated from the fault-tolerant data are also fault-tolerant. However, this is not the case for Spark Streaming as the data in most cases is received over the network (except when fileStream
is used). To achieve the same fault-tolerance properties for all of the generated RDDs, the received data is replicated among multiple Spark executors in worker nodes in the cluster (default replication factor is 2). This leads to two kinds of data in the system that need to recovered in the event of failures:
- Data received and replicated - This data survives failure of a single worker node as a copy of it exists on one of the other nodes.
- Data received but buffered for replication - Since this is not replicated, the only way to recover this data is to get it again from the source.
Furthermore, there are two kinds of failures that we should be concerned about:
- Failure of a Worker Node - Any of the worker nodes running executors can fail, and all in-memory data on those nodes will be lost. If any receivers were running on failed nodes, then their buffered data will be lost.
- Failure of the Driver Node - If the driver node running the Spark Streaming application fails, then obviously the SparkContext is lost, and all executors with their in-memory data are lost.
With this basic knowledge, let us understand the fault-tolerance semantics of Spark Streaming.
Definitions
The semantics of streaming systems are often captured in terms of how many times each record can be processed by the system. There are three types of guarantees that a system can provide under all possible operating conditions (despite failures, etc.)
- At most once: Each record will be either processed once or not processed at all.
- At least once: Each record will be processed one or more times. This is stronger than at-most once as it ensure that no data will be lost. But there may be duplicates.
- Exactly once: Each record will be processed exactly once - no data will be lost and no data will be processed multiple times. This is obviously the strongest guarantee of the three.
Basic Semantics
In any stream processing system, broadly speaking, there are three steps in processing the data.
Receiving the data: The data is received from sources using Receivers or otherwise.
Transforming the data: The received data is transformed using DStream and RDD transformations.
Pushing out the data: The final transformed data is pushed out to external systems like file systems, databases, dashboards, etc.
If a streaming application has to achieve end-to-end exactly-once guarantees, then each step has to provide an exactly-once guarantee. That is, each record must be received exactly once, transformed exactly once, and pushed to downstream systems exactly once. Let’s understand the semantics of these steps in the context of Spark Streaming.
Receiving the data: Different input sources provide different guarantees. This is discussed in detail in the next subsection.
Transforming the data: All data that has been received will be processed exactly once, thanks to the guarantees that RDDs provide. Even if there are failures, as long as the received input data is accessible, the final transformed RDDs will always have the same contents.
Pushing out the data: Output operations by default ensure at-least once semantics because it depends on the type of output operation (idempotent, or not) and the semantics of the downstream system (supports transactions or not). But users can implement their own transaction mechanisms to achieve exactly-once semantics. This is discussed in more details later in the section.
Semantics of Received Data
Different input sources provide different guarantees, ranging from at-least once to exactly once. Read for more details.
With Files
If all of the input data is already present in a fault-tolerant file system like HDFS, Spark Streaming can always recover from any failure and process all of the data. This gives exactly-once semantics, meaning all of the data will be processed exactly once no matter what fails.
With Receiver-based Sources
For input sources based on receivers, the fault-tolerance semantics depend on both the failure scenario and the type of receiver. As we discussed earlier, there are two types of receivers:
- Reliable Receiver - These receivers acknowledge reliable sources only after ensuring that the received data has been replicated. If such a receiver fails, the source will not receive acknowledgment for the buffered (unreplicated) data. Therefore, if the receiver is restarted, the source will resend the data, and no data will be lost due to the failure.
- Unreliable Receiver - Such receivers do not send acknowledgment and therefore can lose data when they fail due to worker or driver failures.
Depending on what type of receivers are used we achieve the following semantics. If a worker node fails, then there is no data loss with reliable receivers. With unreliable receivers, data received but not replicated can get lost. If the driver node fails, then besides these losses, all of the past data that was received and replicated in memory will be lost. This will affect the results of the stateful transformations.
To avoid this loss of past received data, Spark 1.2 introduced write ahead logs which save the received data to fault-tolerant storage. With thewrite ahead logs enabled and reliable receivers, there is zero data loss. In terms of semantics, it provides an at-least once guarantee.
The following table summarizes the semantics under failures:
Deployment Scenario | Worker Failure | Driver Failure |
---|---|---|
Spark 1.1 or earlier, OR Spark 1.2 or later without write ahead logs |
Buffered data lost with unreliable receivers Zero data loss with reliable receivers At-least once semantics |
Buffered data lost with unreliable receivers Past data lost with all receivers Undefined semantics |
Spark 1.2 or later with write ahead logs | Zero data loss with reliable receivers At-least once semantics |
Zero data loss with reliable receivers and files At-least once semantics |
With Kafka Direct API
In Spark 1.3, we have introduced a new Kafka Direct API, which can ensure that all the Kafka data is received by Spark Streaming exactly once. Along with this, if you implement exactly-once output operation, you can achieve end-to-end exactly-once guarantees. This approach (experimental as of Spark 1.6.0) is further discussed in the Kafka Integration Guide.
Semantics of output operations
Output operations (like foreachRDD
) have at-least once semantics, that is, the transformed data may get written to an external entity more than once in the event of a worker failure. While this is acceptable for saving to file systems using the saveAs***Files
operations (as the file will simply get overwritten with the same data), additional effort may be necessary to achieve exactly-once semantics. There are two approaches.
Idempotent updates: Multiple attempts always write the same data. For example,
saveAs***Files
always writes the same data to the generated files.-
Transactional updates: All updates are made transactionally so that updates are made exactly once atomically. One way to do this would be the following.
- Use the batch time (available in
foreachRDD
) and the partition index of the RDD to create an identifier. This identifier uniquely identifies a blob data in the streaming application. -
Update external system with this blob transactionally (that is, exactly once, atomically) using the identifier. That is, if the identifier is not already committed, commit the partition data and the identifier atomically. Else, if this was already committed, skip the update.
dstream.foreachRDD { (rdd, time) =>
rdd.foreachPartition { partitionIterator =>
val partitionId = TaskContext.get.partitionId()
val uniqueId = generateUniqueId(time.milliseconds, partitionId)
// use this uniqueId to transactionally commit the data in partitionIterator
}
}
- Use the batch time (available in
Migration Guide from 0.9.1 or below to 1.x
Between Spark 0.9.1 and Spark 1.0, there were a few API changes made to ensure future API stability. This section elaborates the steps required to migrate your existing code to 1.0.
Input DStreams: All operations that create an input stream (e.g., StreamingContext.socketStream
, FlumeUtils.createStream
, etc.) now returnsInputDStream / ReceiverInputDStream (instead of DStream) for Scala, and JavaInputDStream / JavaPairInputDStream /JavaReceiverInputDStream / JavaPairReceiverInputDStream (instead of JavaDStream) for Java. This ensures that functionality specific to input streams can be added to these classes in the future without breaking binary compatibility. Note that your existing Spark Streaming applications should not require any change (as these new classes are subclasses of DStream/JavaDStream) but may require recompilation with Spark 1.0.
Custom Network Receivers: Since the release to Spark Streaming, custom network receivers could be defined in Scala using the class NetworkReceiver. However, the API was limited in terms of error handling and reporting, and could not be used from Java. Starting Spark 1.0, this class has been replaced by Receiver which has the following advantages.
- Methods like
stop
andrestart
have been added to for better control of the lifecycle of a receiver. See the custom receiver guide for more details. - Custom receivers can be implemented using both Scala and Java.
To migrate your existing custom receivers from the earlier NetworkReceiver to the new Receiver, you have to do the following.
- Make your custom receiver class extend
org.apache.spark.streaming.receiver.Receiver
instead oforg.apache.spark.streaming.dstream.NetworkReceiver
. - Earlier, a BlockGenerator object had to be created by the custom receiver, to which received data was added for being stored in Spark. It had to be explicitly started and stopped from
onStart()
andonStop()
methods. The new Receiver class makes this unnecessary as it adds a set of methods namedstore(<data>)
that can be called to store the data in Spark. So, to migrate your custom network receiver, remove any BlockGenerator object (does not exist any more in Spark 1.0 anyway), and usestore(...)
methods on received data.
Actor-based Receivers: Data could have been received using any Akka Actors by extending the actor class withorg.apache.spark.streaming.receivers.Receiver
trait. This has been renamed to org.apache.spark.streaming.receiver.ActorHelper
and thepushBlock(...)
methods to store received data has been renamed to store(...)
. Other helper classes in theorg.apache.spark.streaming.receivers
package were also moved to org.apache.spark.streaming.receiver
package and renamed for better clarity.