【发布时间】:2018-01-24 08:30:08
【问题描述】:
我正在使用 spark 从 Kafka Stream 接收数据,以接收有关发送定期健康更新的 IOT 设备的状态以及设备中存在的各种传感器的状态。我的 Spark 应用程序使用 Spark 直接流侦听单个主题以接收来自 Kafka 流的更新消息。我需要根据每个设备的传感器状态触发不同的警报。但是,当我添加更多使用 Kakfa 向 Spark 发送数据的 IOT 设备时,尽管添加了更多机器并且执行器数量增加,但 Spark 无法扩展。下面我给出了我的 Spark 应用程序的精简版,其中通知触发部分被删除,但存在相同的性能问题。
// Method for update the Device state , it just a in memory object which tracks the device state .
private static Optional<DeviceState> trackDeviceState(Time time, String key, Optional<ProtoBufEventUpdate> updateOpt,
State<DeviceState> state) {
int batchTime = toSeconds(time);
ProtoBufEventUpdate eventUpdate = (updateOpt == null)?null:updateOpt.orNull();
if(eventUpdate!=null)
eventUpdate.setBatchTime(ProximityUtil.toSeconds(time));
if (state!=null && state.exists()) {
DeviceState deviceState = state.get();
if (state.isTimingOut()) {
deviceState.markEnd(batchTime);
}
if (updateOpt.isPresent()) {
deviceState = DeviceState.updatedDeviceState(deviceState, eventUpdate);
state.update(deviceState);
}
} else if (updateOpt.isPresent()) {
DeviceState deviceState = DeviceState.newDeviceState(eventUpdate);
state.update(deviceState);
return Optional.of(deviceState);
}
return Optional.absent();
}
SparkConf conf = new SparkConf()
.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
.set("spark.streaming.receiver.writeAheadLog.enable", "true")
.set("spark.rpc.netty.dispatcher.numThreads", String.valueOf(Runtime.getRuntime().availableProcessors()))
JavaStreamingContext context= new JavaStreamingContext(conf, Durations.seconds(10));
Map<String, String> kafkaParams = new HashMap<String, String>();
kafkaParams.put( “zookeeper.connect”, “192.168.60.20:2181,192.168.60.21:2181,192.168.60.22:2181”);
kafkaParams.put("metadata.broker.list", “192.168.60.20:9092,192.168.60.21:9092,192.168.60.22:9092”);
kafkaParams.put(“group.id”, “spark_iot”);
HashSet<String> topics=new HashSet<>();
topics.add(“iottopic”);
JavaPairInputDStream<String, ProtoBufEventUpdate> inputStream = KafkaUtils.
createDirectStream(context, String.class, ProtoBufEventUpdate.class, KafkaKryoCodec.class, ProtoBufEventUpdateCodec.class, kafkaParams, topics);
JavaPairDStream<String, ProtoBufEventUpdate> updatesStream = inputStream.mapPartitionsToPair(t -> {
List<Tuple2<String, ProtoBufEventUpdate>> eventupdateList=new ArrayList<>();
t.forEachRemaining(tuple->{
String key=tuple._1;
ProtoBufEventUpdate eventUpdate =tuple._2;
Util.mergeStateFromStats(eventUpdate);
eventupdateList.add(new Tuple2<String, ProtoBufEventUpdate>(key,eventUpdate));
});
return eventupdateList.iterator();
});
JavaMapWithStateDStream<String, ProtoBufEventUpdate, DeviceState, DeviceState> devceMapStream = null;
devceMapStream=updatesStream.mapWithState(StateSpec.function(Engine::trackDeviceState)
.numPartitions(20)
.timeout(Durations.seconds(1800)));
devceMapStream.checkpoint(new Duration(batchDuration*1000));
JavaPairDStream<String, DeviceState> deviceStateStream = devceMapStream
.stateSnapshots()
.cache();
deviceStateStream.foreachRDD(rdd->{
if(rdd != null && !rdd.isEmpty()){
rdd.foreachPartition(tuple->{
tuple.forEachRemaining(t->{
SparkExecutorLog.error("Engine::getUpdates Tuple data "+ t._2);
});
});
}
});
即使负载增加,我也没有看到 Executor 实例的 CPU 使用率增加。大多数时候 Executor 实例 CPU 处于空闲状态。我尝试增加 kakfa 分区(目前 Kafka 有 72 个分区。我也尝试将其降低到 36 个)。我也尝试增加 devceMapStream partitions 。但我看不到任何性能改进。代码没有在 IO 上花费任何时间。
我在 Amazon EMR(Yarn) 上使用 6 个执行程序实例运行我们的 Spark 应用程序,每台机器有 4 个内核和 32 GB 内存。它试图将执行程序实例的数量增加到 9 个,然后增加到 15 个,但没有看到任何性能改进。还通过将 spark.default.parallelism 值设置为 20, 36, 72, 100 玩了一下,但我可以看到 20 是给我更好性能的那个(也许每个执行器的核心数对此有一些影响)。
spark-submit --deploy-mode cluster --class com.ajay.Engine --supervise --driver-memory 5G --driver-cores 8 --executor-memory 4G --executor-cores 4 --conf spark.default.parallelism=20 --num-executors 36 --conf spark.dynamicAllocation.enabled=false --conf spark.streaming.unpersist=false --conf spark.eventLog.enabled=false --conf spark.driver.extraJavaOptions=-Dlog4j.configuration=log4j.properties --conf spark.executor.extraJavaOptions=-XX:+HeapDumpOnOutOfMemoryError --conf spark.executor.extraJavaOptions=-XX:HeapDumpPath=/tmp --conf spark.executor.extraJavaOptions=-XX:+UseG1GC --conf spark.driver.extraJavaOptions=-XX:+UseG1GC --conf spark.executor.extraJavaOptions=-Dlog4j.configuration=log4j.properties s3://test/engine.jar
目前 Spark 正在努力在 10 秒内完成处理(我什至尝试过不同的批处理持续时间,例如 5、10、15 等)。完成一个批次需要 15-23 秒,输入速率为每秒 1600 条记录,每批次有 17000 条记录。我需要使用 statesteam 定期检查设备的状态,以查看设备是否发出任何警报或任何传感器已停止响应。我不确定如何提高 spark 应用程序的性能?
【问题讨论】:
-
在 Spark UI 中查看时,哪个任务花费的时间最多?
-
deviceStateStream.foreachRDD ,这个任务大约需要 6-9 秒。
-
你为什么使用
rdd.collect();?它会将所有数据洗牌到运行驱动程序的节点,这是不健康的,你不应该在生产中这样做。另外,为什么要缓存状态快照? -
我只是为了说明应用程序逻辑而使用 rdd.collect()。在实际代码中,我不是在做收集,而是在做 rdd.foreachPartition(destinationTuples -> { // 一些基于设备状态发送通知的逻辑 });我会更新问题。我只是缓存 stateSnapshots,因为它已将计算结果保存到 DeviceState 中的不同集合中,我想单独迭代以发送警报。在缓存的状态快照上发生了多个操作。由于我在第一个问题上苦苦挣扎,所以我评论了其他人。
标签: java apache-spark apache-kafka spark-streaming