如何跨分区平衡我的数据?

gsa*_*ras 11 python hadoop distributed-computing bigdata apache-spark

编辑:答案有帮助,但我在Spark中描述了我的解决方案:memoryOverhead问题.


我有一个带有202092分区的RDD,它读取其他人创建的数据集.我可以手动看到数据在分区之间没有平衡,例如它们中的一些有0个图像而其他有4k,而平均值是432.当处理数据时,我收到了这个错误:

Container killed by YARN for exceeding memory limits. 16.9 GB of 16 GB physical memory used. Consider boosting spark.yarn.executor.memoryOverhead.
Run Code Online (Sandbox Code Playgroud)

而memoryOverhead已经提升了.我觉得有些尖峰正在发生,这使得Yarn杀死我的容器,因为尖峰溢出了指定的边界.

那么我该怎么做才能确保我的数据在各个分区之间(大致)平衡?


我的想法是repartition()会工作,它会调用shuffling:

dataset = dataset.repartition(202092)
Run Code Online (Sandbox Code Playgroud)

但是我得到了同样的错误,尽管有编程指南的指示:

重新分区(numPartitions)

随机重新调整RDD中的数据以创建更多或更少的分区并在它们之间进行平衡.这总是随机播放网络上的所有数据.


检查我的玩具示例:

data = sc.parallelize([0,1,2], 3).mapPartitions(lambda x: range((x.next() + 1) * 1000))
d = data.glom().collect()
len(d[0])     # 1000
len(d[1])     # 2000
len(d[2])     # 3000
repartitioned_data = data.repartition(3)
re_d = repartitioned_data.glom().collect()
len(re_d[0])  # 1854
len(re_d[1])  # 1754
len(re_d[2])  # 2392
repartitioned_data = data.repartition(6)
re_d = repartitioned_data.glom().collect()
len(re_d[0])  # 422
len(re_d[1])  # 845
len(re_d[2])  # 1643
len(re_d[3])  # 1332
len(re_d[4])  # 1547
len(re_d[5])  # 211
repartitioned_data = data.repartition(12)
re_d = repartitioned_data.glom().collect()
len(re_d[0])  # 132
len(re_d[1])  # 265
len(re_d[2])  # 530
len(re_d[3])  # 1060
len(re_d[4])  # 1025
len(re_d[5])  # 145
len(re_d[6])  # 290
len(re_d[7])  # 580
len(re_d[8])  # 1113
len(re_d[9])  # 272
len(re_d[10]) # 522
len(re_d[11]) # 66
Run Code Online (Sandbox Code Playgroud)

Dan*_*bos 5

我认为内存开销限制超出了问题,这是由于在获取过程中使用了DirectMemory缓冲区。我认为它已在2.0.0中修复。(我们遇到了同样的问题,但是当发现升级到2.0.0可以解决问题时,我们就不再深入研究了。不幸的是,我没有Spark问题编号来支持我。)


之后的不均匀分隔repartition令人惊讶。与https://github.com/apache/spark/blob/v2.0.0/core/src/main/scala/org/apache/spark/rdd/RDD.scala#L443对比。Spark甚至会在中生成随机密钥repartition,因此不会使用可能有偏差的哈希来完成。

我尝试了您的示例,并使用Spark 1.6.2和Spark 2.0.0 获得了完全相同的结果。但不是来自Scala spark-shell:

scala> val data = sc.parallelize(1 to 3, 3).mapPartitions { it => (1 to it.next * 1000).iterator }
data: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[6] at mapPartitions at <console>:24

scala> data.mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res1: Seq[Int] = WrappedArray(1000, 2000, 3000)

scala> data.repartition(3).mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res2: Seq[Int] = WrappedArray(1999, 2001, 2000)

scala> data.repartition(6).mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res3: Seq[Int] = WrappedArray(999, 1000, 1000, 1000, 1001, 1000)

scala> data.repartition(12).mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res4: Seq[Int] = WrappedArray(500, 501, 501, 501, 501, 500, 499, 499, 499, 499, 500, 500)
Run Code Online (Sandbox Code Playgroud)

如此美丽的分区!


(对不起,这不是一个完整的答案。到目前为止,我只想分享我的发现。)

  • 直接内存缓冲区是由JVM分配但在堆外部的内存。通常,Spark执行程序受堆大小限制,因此不会被YARN杀死。分配直接缓冲区使它可以使用比堆大小更多的内存,并被YARN杀死。(https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html),在reducer任务从映射器中提取数据时,在重排期间会分配大型直接缓冲区。 (2认同)