生而为人

程序员的自我修养

0%

Sorting is not supported on streaming DataFrames/Datasets, unless it is on aggregated DataFrame/Dataset in Complete output mode

Structured Streaming Programming Guide

这个状态执行几分钟

1
21/09/08 07:09:55 INFO StateStore [state-store-maintenance-task]: Retrieved reference to StateStoreCoordinator: org.apache.spark.sql.execution.streaming.state.StateStoreCoordinatorRef@2f640a0b

Streaming job gets stuck writing to checkpoint

Spark stateful streaming processing is stuck in StateStoreSave stage!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
User class threw exception: com.esotericsoftware.kryo.KryoException: java.lang.RuntimeException: Could not serialize lambda
Serialization trace:
metricValueProvider (org.apache.kafka.common.metrics.KafkaMetric)
metrics (org.apache.kafka.common.metrics.Metrics)
metrics (org.apache.kafka.clients.producer.internals.BufferPool)
free (org.apache.kafka.clients.producer.internals.RecordAccumulator)
accumulator (org.apache.kafka.clients.producer.KafkaProducer)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:101)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:508)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:651)
at com.esotericsoftware.kryo.serializers.MapSerializer.write(MapSerializer.java:113)
at com.esotericsoftware.kryo.serializers.MapSerializer.write(MapSerializer.java:39)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:575)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:79)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:508)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:575)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:79)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:508)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:575)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:79)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:508)
at com.esotericsoftware.kryo.Kryo.writeObjectOrNull(Kryo.java:629)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:86)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:508)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:651)
at org.apache.spark.serializer.KryoSerializationStream.writeObject(KryoSerializer.scala:245)
at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$blockifyObject$2.apply(TorrentBroadcast.scala:291)
at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$blockifyObject$2.apply(TorrentBroadcast.scala:291)
at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
at org.apache.spark.broadcast.TorrentBroadcast$.blockifyObject(TorrentBroadcast.scala:292)
at org.apache.spark.broadcast.TorrentBroadcast.writeBlocks(TorrentBroadcast.scala:127)
at org.apache.spark.broadcast.TorrentBroadcast.<init>(TorrentBroadcast.scala:88)
at org.apache.spark.broadcast.TorrentBroadcastFactory.newBroadcast(TorrentBroadcastFactory.scala:34)
at org.apache.spark.broadcast.BroadcastManager.newBroadcast(BroadcastManager.scala:62)
at org.apache.spark.SparkContext.broadcast(SparkContext.scala:1493)
at com.microsoft.sam.HistoryDataUploader.run(HistoryDataUploader.scala:121)
at com.microsoft.sam.HistoryDataUploader$.main(HistoryDataUploader.scala:207)
at com.microsoft.sam.HistoryDataUploader.main(HistoryDataUploader.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$2.run(ApplicationMaster.scala:684)
Caused by: java.lang.RuntimeException: Could not serialize lambda
at com.esotericsoftware.kryo.serializers.ClosureSerializer.write(ClosureSerializer.java:69)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:575)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:79)
... 35 more
Caused by: java.lang.NoSuchMethodException: org.apache.kafka.common.network.Selector$SelectorMetrics$$Lambda$38/747274186.writeReplace()
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at com.esotericsoftware.kryo.serializers.ClosureSerializer.write(ClosureSerializer.java:60)

spark3.0运行,延迟很长时间才调用 onQueryProgress,2.4上没有遇到该问题

Spark Structured Streaming StreamingQueryListener.onQueryProgress not called per microbatch?

状态保存的作用

将每次流计算的group结果,保存下来,用于下一次继续聚合。

具体执行内容是如何的?下次聚合会使用状态中的什么数据?

groupState.setTimeoutTimestamp()

  1. With EventTimeTimeout, the user also has to specify the the the event time watermark in the query using Dataset.withWatermark().
  2. With this setting, data that is older than the watermark are filtered out.
  3. The timeout can be set for a group by setting a timeout timestamp usingGroupState.setTimeoutTimestamp(), and the timeout would occur when the watermark advances beyond the set timestamp.
  4. You can control the timeout delay by two parameters - watermark delay and an additional duration beyond the timestamp in the event (which is guaranteed to be newer than watermark due to the filtering).
  5. Guarantees provided by this timeout are as follows:
  6. Timeout will never be occur before watermark has exceeded the set timeout.
  7. Similar to processing time timeouts, there is a no strict upper bound on the delay when the timeout actually occurs. The watermark can advance only when there is data in the stream, and the event time of the data has actually advanced.

timeout晚于watermark不生效,早于watermark会报错?到底应该如何设置?

问题

  1. flatmapgroupswithstate 什么时候会timeout
  2. state-store-maintenance-task

参数调整

1
2
3
4
5
6
val spark = SparkSession
.builder()
.appName(appName)
.config(sparkConf)
.getOrCreate()
spark.sessionState.conf.setConfString("spark.sql.streaming.minBatchesToRetain", "5")

这个参数放到sparkConf中,无效。

参考资料

  1. spark 的 structured streaming 状态保存
  2. GroupState — Group State in Arbitrary Stateful Streaming Aggregation
  3. Spark基于事件时间的“状态”流的深层分析 - withWatermark与mapGroupsWithState的关系
  4. State lifecycle management in Structured Streaming
  5. spark api doc
  6. state相关配置
  7. Spark Structured Streaming: 自维护(任意)状态流的“超时”(Timeout)问题
  8. StateStore in Apache Spark Structured Streaming
  9. spark 的 structured streaming 状态保存
  10. GroupStateImpl
  11. Spark 流计算状态管理进化史
  12. StateStoreRDD — RDD for Updating State (in StateStores Across Spark Cluster)
  13. Spark State Tools
  14. structured streaming的checkpoint文件无限增长
  15. State Storage in Spark Structured Streaming
  16. Extending data reprocessing period for arbitrary stateful processing applications
  17. StructuredStream StateStore机制

连续处理与微批处理

连续处理:

​ 优势:

​ 1. 时延低

​ 不足:

1. 吞吐量小
2. 连续处理系统通常有固定的计算拓扑,如果不停止整个系统,在运行状态下是无法改变的,这也可能会导致负载均衡的问题。计算拓扑是什么,为什么会有负载均衡的问题?

微批处理:

​ 优势:

  1. 可以在每个节点上实现高吞吐量,因为他们可以利用与批处理系统相同的优化操作(例如,向量化处理,Vectorized Processing)。什么是向量化处理?

查看jar包中是否包含某个文件

1
jar vtf analysis-tool-1.0.0-SNAPSHOT.jar application.yml