Spark Streaming Write Ahead Log重启后不重放数据

时间:2016-01-21 11:20:26

标签: apache-spark spark-streaming

要有一种简单的方法来测试Spark Streaming Write Ahead Log我创建了一个非常简单的自定义输入接收器,它将生成字符串并存储它们:

class InMemoryStringReceiver extends Receiver[String](StorageLevel.MEMORY_AND_DISK_SER) {

  val batchID = System.currentTimeMillis()

  def onStart() {
    new Thread("InMemoryStringReceiver") {
      override def run(): Unit = {
        var i = 0
        while(true) {
          //http://spark.apache.org/docs/latest/streaming-custom-receivers.html
          //To implement a reliable receiver, you have to use store(multiple-records) to store data.
          store(ArrayBuffer(s"$batchID-$i"))
          println(s"Stored => [$batchID-$i)]")
          Thread.sleep(1000L)
          i = i + 1
        }
      }
    }.start()
  }

  def onStop() {}
}

然后我创建了一个简单的应用程序,它将使用自定义接收器来流式处理数据并对其进行处理:

object DStreamResilienceTest extends App {

  val conf = new SparkConf().setMaster("local[*]").setAppName("DStreamResilienceTest").set("spark.streaming.receiver.writeAheadLog.enable", "true")
  val ssc = new StreamingContext(conf, Seconds(1))
  ssc.checkpoint("hdfs://myhdfsserver/user/spark/checkpoint/DStreamResilienceTest")
  val customReceiverStream: ReceiverInputDStream[String] = ssc.receiverStream(new InMemoryStringReceiver())
  customReceiverStream.foreachRDD { (rdd: RDD[String]) =>
    println(s"processed => [${rdd.collect().toList}]")
    Thread.sleep(2000L)
  }
  ssc.start()
  ssc.awaitTermination()

}

正如您所看到的,每个接收到的RDD的处理都有2秒的休眠,而字符串每秒存储一次。这会产生积压并且新的字符串堆积起来,并且应该存储在WAL中。实际上,我可以看到检查点dirs中的文件得到更新。运行应用程序我得到如下输出:

[info] Stored => [1453374654941-0)]
[info] processed => [List(1453374654941-0)]
[info] Stored => [1453374654941-1)]
[info] Stored => [1453374654941-2)]
[info] processed => [List(1453374654941-1)]
[info] Stored => [1453374654941-3)]
[info] Stored => [1453374654941-4)]
[info] processed => [List(1453374654941-2)]
[info] Stored => [1453374654941-5)]
[info] Stored => [1453374654941-6)]
[info] processed => [List(1453374654941-3)]
[info] Stored => [1453374654941-7)]
[info] Stored => [1453374654941-8)]
[info] processed => [List(1453374654941-4)]
[info] Stored => [1453374654941-9)]
[info] Stored => [1453374654941-10)]

正如您所料,存储不在处理中。所以我杀了应用程序并重新启动它。这次我在foreachRDD中注释了睡眠,以便处理可以清除任何积压:

[info] Stored => [1453374753946-0)]
[info] processed => [List(1453374753946-0)]
[info] Stored => [1453374753946-1)]
[info] processed => [List(1453374753946-1)]
[info] Stored => [1453374753946-2)]
[info] processed => [List(1453374753946-2)]
[info] Stored => [1453374753946-3)]
[info] processed => [List(1453374753946-3)]
[info] Stored => [1453374753946-4)]
[info] processed => [List(1453374753946-4)]

正如您所看到的那样,新事件已处理,但上一批中没有。旧的WAL日志被清除,我看到这样的日志消息,但旧的数据没有得到处理。

INFO WriteAheadLogManager : Recovered 1 write ahead log files from hdfs://myhdfsserver/user/spark/checkpoint/DStreamResilienceTest/receivedData/0

我做错了什么?我正在使用Spark 1.5.2。

1 个答案:

答案 0 :(得分:2)

Shixiong(Ryan)朱在Spark Users mailing list上回答了这个问题。

按照他的建议使用StreamingContext.getOrCreate