Apache Spark 黑名单(Blacklist)机制介绍

来源:https://www.iteblog.com/archives/1907.html

在使用 Apache Spark 的时候,作业会以分布式的方式在不同的节点上运行;特别是当集群的规模很大时,集群的节点出现各种问题是很常见的,比如某个磁盘出现问题等。我们都知道 Apache Spark 是一个高性能、容错的分布式计算框架,一旦它知道某个计算所在的机器出现问题(比如磁盘故障),它会依据之前生成的 lineage 重新调度这个 Task。

我们现在来考虑下下面的场景:

  • 有个节点上的磁盘由于某些原因出现间歇性故障,导致某些扇区不能被读取。假设我们的 Spark 作业需要的数据正好就在这些扇区上,这将会导致这个 Task 失败。
  • 这个作业的 Driver 获取到这个信息,知道 Task 失败了,所以它会重新提交这个 Task。
  • Scheduler 获取这个请求之后,它会考虑到数据的本地性问题,所以很可能还是把这个 Task 分发到上述的机器,因为它并不知道上述机器的磁盘出现了问题。
  • 因为这个机器的磁盘出现问题,所以这个 Task 可能一样失败。然后 Driver 重新这些操作,最终导致了 Spark 作业出现失败!

上面提到的场景其实对我们人来说可以通过某些措施来避免。但是对于 Apache Spark 2.2.0 版本之前是无法避免的,不过高兴的是,来自 Cloudera 的工程师解决了这个问题:引入了黑名单机制 Blacklist(详情可以参见SPARK-8425,具体的设计文档参见Design Doc for Blacklist Mechanism),并且随着 Apache Spark 2.2.0 版本发布,不过目前还处于实验性阶段。

黑名单机制其实是通过维护之前出现问题的执行器(Executors)和节点(Hosts)的记录。当某个任务(Task)出现失败,那么黑名单机制将会追踪这个任务关联的执行器以及主机,并记下这些信息;当在这个节点调度任务出现失败的次数超过一定的数目(默认为2),那么调度器将不会再将任务分发到那台节点。调度器甚至可以杀死那台机器对应的执行器,这些都可以通过相应的配置实现。

我们可以通过 Apache Spark WEB UI 界面看到执行器的状态(Status):如果执行器处于黑名单状态,你可以在页面上看到其状态为 Blacklisted ,否则为 Active。如下图所示:

172804_3EOp_3455048.png

目前黑名单机制可以通过一系列的参数来控制,主要如下:

参数默认值含义
spark.blacklist.enabledfalse如果这个参数这为 true,那么 Spark 将不再会往黑名单里面的执行器调度任务。黑名单算法可以由其他“spark.blacklist”配置选项进一步控制,详情参见下面的介绍。
spark.blacklist.timeout1h(实验性) How long a node or executor is blacklisted for the entire application, before it is unconditionally removed from the blacklist to attempt running new tasks.
spark.blacklist.task.maxTaskAttemptsPerExecutor1(实验性) For a given task, how many times it can be retried on one executor before the executor is blacklisted for that task.
spark.blacklist.task.maxTaskAttemptsPerNode2(实验性) For a given task, how many times it can be retried on one node, before the entire node is blacklisted for that task.
spark.blacklist.stage.maxFailedTasksPerExecutor2(实验性) How many different tasks must fail on one executor, within one stage, before the
executor is blacklisted for that stage.
spark.blacklist.stage.maxFailedExecutorsPerNode2(实验性) How many different executors are marked as blacklisted for a given stae, before the entire node is marked as failed for the stage.
spark.blacklist.application.maxFailedTasksPerExecutor2(实验性) How many different tasks must fail on one executor, in successful task sets, before the executor is blacklisted for the entire application. Blacklisted executors will be automatically added back to the pool of available resources after the timeout specified by spark.blacklist.timeout. Note that with dynamic allocation, though, the executors may get marked as idle and be reclaimed by the cluster manager.
spark.blacklist.application.maxFailedExecutorsPerNode2(实验性) How many different executors must be blacklisted for the entire application, before the node is blacklisted for the entire application. Blacklisted nodes will be automatically added back to the pool of available resources after the timeout specified by spark.blacklist.timeout. Note that with dynamic allocation, though, the executors on the node may get marked as idle and be reclaimed by the cluster manager.
spark.blacklist.killBlacklistedExecutorsfalse(实验性) If set to "true", allow Spark to automatically kill, and attempt to re-create, executors when they are blacklisted. Note that, when an entire node is added to the blacklist, all of the executors on that node will be killed.

因为黑名单机制目前还处于实验性状态,所以上面的一些参数可能会在后面的 Spark 中有所修改。

 

转载于:https://my.oschina.net/u/3455048/blog/1606255

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spark Streaming 可以通过使用 DStream.transform() 方法和广播变量来过滤黑名单。具体步骤如下: 1. 创建一个黑名单列表,可以从文件或数据库中读取。 2. 将黑名单列表广播到所有的 Spark Streaming Executor 上。 3. 在 DStream.transform() 方法中,使用广播变量来过滤黑名单。 4. 返回过滤后的 DStream。 示例代码如下: ```python # 创建黑名单列表 blacklist = ["user1", "user2", "user3"] # 广播黑名单列表到所有 Executor 上 broadcast_blacklist = sc.broadcast(blacklist) # 定义过滤函数 def filter_by_blacklist(rdd): # 获取广播变量中的黑名单列表 blacklist = broadcast_blacklist.value # 过滤黑名单中的用户 return rdd.filter(lambda x: x[0] not in blacklist) # 创建输入 DStream input_dstream = ssc.socketTextStream("localhost", 9999) # 过滤黑名单 filtered_dstream = input_dstream.transform(filter_by_blacklist) # 输出过滤后的 DStream filtered_dstream.pprint() # 启动 StreamingContext ssc.start() ssc.awaitTermination() ``` ### 回答2: Spark Streaming是一款流式计算引擎,与传统批处理相比,它具有低延迟、实时处理等优点。在实际应用中,我们经常需要对数据进行处理和过滤,为了应对恶意攻击、垃圾信息等问题,我们需要实时过滤黑名单中的数据。 在Spark Streaming中过滤黑名单,可以采用一些类似于Spark Core的操作。具体可以分为以下几步: 1. 首先定义一个黑名单RDD,包含被屏蔽的IP地址等信息,这个RDD可以使用外部存储系统如Redis、MySQL等获取。 2. 然后从数据源中获取数据,可以使用诸如Kafka、Flume、Socket等方式。 3. 对于获取的数据,需要进行筛选,根据黑名单中的IP地址等信息过滤掉不需要的数据。这里可以使用filter等操作,将需要保留的数据进行输出。 4. 最后,将过滤后的数据进行处理和保存。 代码实现可以如下: ``` from pyspark import SparkContext from pyspark.streaming import StreamingContext sc = SparkContext(appName="BlackList") ssc = StreamingContext(sc, 5) # 5秒为一个批次 # 黑名单RDD blackList = ['1.1.1.1', '2.2.2.2', '3.3.3.3'] blackListRDD = sc.parallelize(blackList).map(lambda x: (x, True)) # 接收数据流,过滤黑名单 dataStream = ssc.socketTextStream("localhost", 9999) dataStream.filter(lambda x: x not in blackList).pprint() ssc.start() ssc.awaitTermination() ``` 这里实现了一个简单的例子,黑名单包含了三个IP地址,数据从本地socket端口获取,通过filter过滤掉了黑名单中的IP地址。可以根据实际业务需求进行修改和扩展。 总之,在Spark Streaming中过滤黑名单可以采用类似于Spark Core的操作,在数据源操作、筛选过滤、处理与保存后等方面进行逐步处理和过滤。 ### 回答3: Spark StreamingApache Spark中的一个流处理框架,可以用来从实时流中持续接收和分析数据,然后对数据进行处理和转换。在实时流分析中,常常需要对来自特定用户或特定来源的数据进行过滤操作,这时就需要使用过滤黑名单的功能。 过滤黑名单是指在Spark Streaming中过滤掉已经被定义为黑名单的数据,这些数据是根据某些条件或规则来定义的。在Spark Streaming中,过滤黑名单通常使用DStream.filter()函数进行实现,具体实现方式如下: 1. 首先,需要定义一个黑名单列表,这个列表中包含所有需要被过滤掉的数据。可以使用RDD或DataFrame来定义列表。 2. 对于实时流中的每个批次数据,使用DStream.filter()函数来应用黑名单过滤操作。具体过程如下: a. 使用transform()函数来将RDD创建为DStream,并传递每个RDD的黑名单列表。 b. 在transform()函数中,使用RDD.filter()函数来过滤掉在黑名单中的数据。 c. 将过滤后的RDD返回到DStream中。 d. 最后,对过滤后的DStream进行处理,比如计算或存储数据。 通过这种方式,就可以有效地实现对黑名单数据的过滤操作,从而提高实时流分析的效率和准确性。需要注意的是,在处理实时流数据时,需要考虑到数据的实时性和时效性,尽量减少延迟和出错的机会,以保证数据处理的高效性和准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值