Prepare 论文 Resilient Distributed Datasets: A Fault-Tolerant Abstraction for In-Memory Cluster Computing 弹性分布式数据集: 内存集群计算的容错抽象

Abstract

We present Resilient Distributed Datasets (RDDs), a distributed memory abstraction that lets programmers perform in-memory computations on large clusters in a fault-tolerant manner. RDDs are motivated by two types of applications that current computing frameworks handle inefficiently: iterative algorithms and interactive data mining tools. 我们提出了弹性分布式数据集(rdd),这是一种分布式内存抽象,允许程序员以容错的方式在大型集群上执行内存计算。rdd是由当前计算框架处理效率低下的两种类型的应用程序驱动的:迭代算法和交互式数据挖掘工具。

1 Introduction

Although current frameworks provide numerous abstractions for accessing a cluster’s computational resources, they lack abstractions for leveraging distributed memory. This makes them inefficient for an important class of emerging applications: those that reuse intermediate results across multiple computations. 尽管当前的框架为访问集群的计算资源提供了大量的抽象,但它们缺乏利用分布式内存的抽象。这使得它们对于一类重要的新兴应用程序效率低下:那些跨多个计算重用中间结果的应用程序。

Unfortunately, in most current frameworks, the only way to reuse data between computations (e.g., between two MapReduce jobs) is to write it to an external stable storage system, e.g., a distributed file system. 不幸的是,在大多数当前框架中,在计算之间(例如,在两个MapReduce作业之间)重用数据的唯一方法是将其写入外部稳定的存储系统,例如,分布式文件系统。 This incurs substantial overheads due to data replication, disk I/O, and serialization, which can dominate application execution times. 由于数据复制、磁盘I/O和序列化,这会导致大量的开销,这些开销会支配应用程序的执行时间。

In this paper, we propose a new abstraction called resilient distributed datasets (RDDs) that enables efficient data reuse in a broad range of applications. RDDs are fault-tolerant, parallel data structures that let users explicitly persist intermediate results in memory, control their partitioning to optimize data placement, and manipulate them using a rich set of operators. 在本文中,我们提出了一种新的抽象,称为弹性分布式数据集(rdd),它可以在广泛的应用中实现有效的数据重用。rdd是一种容错的并行数据结构,它允许用户显式地将中间结果保存在内存中,控制它们的分区以优化数据放置,并使用一组丰富的操作符对它们进行操作。

… With this interface, the only ways to provide fault tolerance are to replicate the data across machines or to log updates across machines. Both approaches are expensive for data-intensive workloads, as they require copying large amounts of data over the cluster network, whose bandwidth is far lower than that of RAM, and they incur substantial storage overhead. 使用此接口,提供容错的唯一方法是跨机器复制数据或跨机器log更新。对于数据密集型工作负载来说,这两种方法都很昂贵,因为它们需要在集群网络上复制大量数据,而集群网络的带宽远低于RAM的带宽,并且它们会产生大量的存储开销。

In contrast to these systems, RDDs provide an interface based on coarse-grained transformations (e.g., map, filter and join) that apply the same operation to many data items. This allows them to efficiently provide fault tolerance by logging the transformations used to build a dataset (its lineage) rather than the actual data. 与这些系统相反,rdd提供了基于粗粒度转换(例如,映射、过滤和连接)的接口,这些转换将相同的操作应用于许多数据项。这允许他们通过记录用于构建数据集(其沿袭)而不是实际数据的转换来有效地提供容错。

Although an interface based on coarse-grained transformations may at first seem limited, RDDs are a good fit for many parallel applications, because these applications naturally apply the same operation to multiple data items.尽管基于粗粒度转换的接口起初可能看起来很有限,但rdd非常适合许多并行应用程序,因为这些应用程序自然会对多个数据项应用相同的操作。

2 Resilient Distributed Datasets (RDDs)

2.1 RDD Abstraction

Formally, an RDD is a read-only, partitioned collection of records. RDDs can only be created through deterministic operations on either (1) data in stable storage or (2) other RDDs. We call these operations transformations to differentiate them from other operations on RDDs. Examples of transformations include map, filter, and join. 正式地说,RDD是一个只读的、分区的记录集合。rdd只能通过对(1)稳定存储中的数据或(2)其他rdd的确定性操作来创建。我们称这些操作为转换,以区别于rdd上的其他操作。转换的示例包括映射、筛选和连接。

RDDs do not need to be materialized at all times. Instead, an RDD has enough information about how it was derived from other datasets (its lineage) to compute its partitions from data in stable storage. This is a powerful property: in essence, a program cannot reference an RDD that it cannot reconstruct after a failure. 不需要在任何时候都具体化rdd。相反,RDD有足够的信息来了解它是如何从其他数据集(它的血统)中派生出来的,以便从稳定存储中的数据计算它的分区。这是一个强大的属性:本质上,程序不能引用在失败后无法重建的RDD。

Finally, users can control two other aspects of RDDs: persistence and partitioning. Users can indicate which RDDs they will reuse and choose a storage strategy for them (e.g., in-memory storage). They can also ask that an RDD’s elements be partitioned across machines based on a key in each record. This is useful for placement optimizations, such as ensuring that two datasets that will be joined together are hash-partitioned in the same way. 最后,用户可以控制rdd的另外两个方面:持久性和分区。用户可以指示他们将重用哪些rdd,并为它们选择存储策略(例如,内存存储)。他们还可以要求RDD的元素基于每条记录中的一个键跨机器进行分区。这对于放置优化非常有用,例如确保将连接在一起的两个数据集以相同的方式进行散列分区。

2.2 Spark Programming Interface Spark编程接口

略

2.2.1 Example: Console Log Mining 示例:控制台日志挖掘

略

2.3 Advantages of the RDD Model RDD模型的优点

Table 1: Comparison of RDDs with distributed shared memory

To understand the benefits of RDDs as a distributed memory abstraction, we compare them against distributed shared memory (DSM) in Table 1. In DSM systems, applications read and write to arbitrary locations in a global address space. Note that under this definition, we include not only traditional shared memory systems [24], but also other systems where applications make finegrained writes to shared state, including Piccolo [27], which provides a shared DHT, and distributed databases. DSM is a very general abstraction, but this generality makes it harder to implement in an efficient and faulttolerant manner on commodity clusters. 为了理解rdd作为分布式内存抽象的好处,我们将它们与表1中的分布式共享内存(DSM)进行比较。在DSM系统中,应用程序对全局地址空间中的任意位置进行读写。注意,在这个定义下,我们不仅包括传统的共享内存系统[24],还包括应用程序对共享状态进行细粒度写入的其他系统,包括提供共享DHT的Piccolo[27]和分布式数据库。DSM是一个非常一般化的抽象,但是这种一般化使得在商品集群上以高效和容错的方式实现它变得更加困难。

The main difference between RDDs and DSM is that RDDs can only be created (“written”) through coarsegrained transformations, while DSM allows reads and writes to each memory location.3 This restricts RDDs to applications that perform bulk writes, but allows for more efficient fault tolerance. In particular, RDDs do not need to incur the overhead of checkpointing, as they can be recovered using lineage.4 Furthermore, only the lost partitions of an RDD need to be recomputed upon failure, and they can be recomputed in parallel on different nodes, without having to roll back the whole program. rdd和DSM之间的主要区别在于rdd只能通过粗粒度转换创建(“写入”),而DSM允许对每个内存位置进行读写这将rdd限制为执行批量写入的应用程序,但允许更有效的容错。特别是,rdd不需要引起检查点的开销,因为它们可以使用继承来恢复此外,只有RDD的丢失分区需要在故障时重新计算,并且它们可以在不同节点上并行地重新计算,而不必回滚整个程序。

A second benefit of RDDs is that their immutable nature lets a system mitigate slow nodes (stragglers) by running backup copies of slow tasks as in MapReduce [10]. Backup tasks would be hard to implement with DSM, as the two copies of a task would access the same memory locations and interfere with each other’s updates. rdd的第二个好处是,它们的不可变特性允许系统通过运行MapReduce[10]中慢任务的备份副本来缓解慢节点(离散者)。使用DSM很难实现备份任务,因为任务的两个副本将访问相同的内存位置,并且会干扰彼此的更新。

Finally, RDDs provide two other benefits over DSM. First, in bulk operations on RDDs, a runtime can schedule tasks based on data locality to improve performance. Second, RDDs degrade gracefully when there is not enough memory to store them, as long as they are only being used in scan-based operations. Partitions that do not fit in RAM can be stored on disk and will provide similar performance to current data-parallel systems. 最后,相对于DSM, rdd提供了另外两个好处。首先,在rdd上的批量操作中,运行时可以根据数据位置安排任务,以提高性能。其次,当没有足够的内存来存储rdd时,只要它们只用于基于扫描的操作,rdd就会优雅地降级。不适合RAM的分区可以存储在磁盘上,并将提供与当前数据并行系统相似的性能。

2.4 Applications Not Suitable for RDDs

As discussed in the Introduction, RDDs are best suited for batch applications that apply the same operation to all elements of a dataset. In these cases, RDDs can efficiently remember each transformation as one step in a lineage graph and can recover lost partitions without having to log large amounts of data. RDDs would be less suitable for applications that make asynchronous finegrained updates to shared state, such as a storage system for a web application or an incremental web crawler. For these applications, it is more efficient to use systems that perform traditional update logging and data checkpointing, such as databases, RAMCloud [25], Percolator [26] and Piccolo [27]. Our goal is to provide an efficient programming model for batch analytics and leave these asynchronous applications to specialized systems. 正如在引言中所讨论的,rdd最适合对数据集的所有元素应用相同操作的批处理应用程序。在这些情况下,rdd可以有效地记住每个转换作为沿袭图中的一个步骤,并且可以恢复丢失的分区,而无需记录大量数据。rdd不太适合对共享状态进行异步细粒度更新的应用程序,例如web应用程序的存储系统或增量web爬虫。对于这些应用,使用执行传统更新日志记录和数据检查点的系统更有效,例如数据库、RAMCloud[25]、Percolator[26]和Piccolo[27]。我们的目标是为批处理分析提供高效的编程模型,并将这些异步应用程序留给专门的系统。

3 Spark Programming Interface

略

讲义&课程

6.824 2020 Lecture 15: Spark

Resilient Distributed Datasets: A Fault-Tolerant Abstraction for In-Memory Cluster Computing Zaharia et al., NSDI 2012

why are we looking at Spark?

  • widely-used for datacenter computations 可以将Spark看作MapReduce的后续产品,已经广泛应用于数据中心计算。
  • generalizes MapReduce into dataflow 将MapReduce的两个阶段概括为完整的多步数据流图
  • supports iterative applications better than MapReduce 对于程序员而言,其能够比MapReduce更好地支持迭代应用
  • successful research: ACM doctoral thesis award

three main topics:

  • programming model
  • execution strategy
  • fault tolerance

15.1 页面排名算法程序示例

let’s look at page-rank 页面排名算法,计算不同网页搜索结果对PageRank的重要性

  • here’s SparkPageRank.scala from the Spark source repository 部分Spark源码
  • like the code in Section 3.2.2, with more detail

PageRank在MapReduce上的效果不好,其涉及一堆截然不同的步骤,涉及迭代,其中存在循环,MapReduce对于迭代没有什么特殊设计,

val lines = spark.read.textFile("in").rdd
val links1 = lines.map{ s =>
    val parts = s.split("\\s+")
    (parts(0), parts(1))
}
val links2 = links1.distinct()
val links3 = links2.groupByKey()
val links4 = links3.cache()
var ranks = links4.mapValues(v => 1.0)

for (i <- 1 to 10) {
    val jj = links4.join(ranks)
    val contribs = jj.values.flatMap{
    case (urls, rank) =>
        urls.map(url => (url, rank / urls.size))
    }
    ranks = contribs.reduceByKey(_ + _).mapValues(0.15 + 0.85 * _)
}

val output = ranks.collect()
output.foreach(tup => println(s"${tup._1} has rank:  ${tup._2} ."))

page-rank input has one line per link, extracted from a big web crawl 页面排名的输入是一个巨大的行的集合,每行是一个网页链接,有两个URL,页面的url和页面指向的url。可以通过爬虫收集

  • from-url to-url
  • the input is vast!

my example input – file “in”: 输入示例
u1 u3
u1 u1
u2 u3
u2 u2
u3 u1

page-rank output is the “importance” of each page 输出每个页面的重要性

  • based on whether other important pages point to it 根据其他重要页面的指向判断
  • really models estimated probability that someone will visit each page 同时根据一些概率模型
  • user model:
    • 85% chance of following a link from current page 用户有85%可能在当前页面继续链接
    • 15% chance of visiting a random page 有15%的可能浏览其他页面

page-rank algorithm 页面排名算法

  • iterative, essentially simulates multiple rounds of users clicking links 会不断地、循环模拟用户点击链接的过程,算法会跟踪每个页面的rank
  • ranks (probabilities) gradually converge 排名(概率)将最终收敛并体现页面的重要性
  • page-rank would be awkward and slow in MapReduce 页面排名不能只是一个MapReduce程序,其多次调用MapReduce,每次调用模拟迭代中的一个步骤。可以在MapReduce中操作,但MapReduce只考虑了一个map、一个reduce,MapReduce从磁盘的GFS读取输入,更新每个页面的rank,并重新写入GFS系统。所以如果你将页面排名当作一系列MapReduce应用运行,这里会有许多文件io

下面会运行代码,然后分析其是如何执行的。

val lines = spark.read.textFile("in").rdd
val links1 = lines.map{ s =>
    val parts = s.split("\\s+")
    (parts(0), parts(1))
}
val links2 = links1.distinct()
val links3 = links2.groupByKey()
val links4 = links3.cache()
var ranks = links4.mapValues(v => 1.0)

for (i <- 1 to 10) {
    val jj = links4.join(ranks)
    val contribs = jj.values.flatMap{
    case (urls, rank) =>
        urls.map(url => (url, rank / urls.size))
    }
    ranks = contribs.reduceByKey(_ + _).mapValues(0.15 + 0.85 * _)
}

val output = ranks.collect()
output.foreach(tup => println(s"${tup._1} has rank:  ${tup._2} ."))

I’ll run page-rank in Spark (local machine, not a cluster): 程序的输出结果

./bin/run-example SparkPageRank in 10
u2 has rank:  0.2610116705534049 .
u3 has rank:  0.9999999999999998 .
u1 has rank:  1.7389883294465944 .

apparently u1 is the most important page. u1的排名最高(但pageRank的算法原理不是本节课的讨论重点)

let’s run some of the page-rank code in the Scala interpreter 在Scala解释器中运行

这里要注意的是,Spark实际上是从类似GFS的分布式系统中读取文件,可能碰巧是HDFS(HaDoop文件系统)。HDFS与GFS非常相似,会将这种许多url的文件分片到许多服务器上。所以读取文件实际上是Spark会在许多机器上运行计算,每个机器会读取输入文件的一个chunk或一个partition。直到系统或HDFS将文件分割到许多chunk/partition后才会终止。多个worker机器会查看输入文件的多个partition,类似MapReduce的工作过程。

val lines = spark.read.textFile("in").rdd
  -- what is lines? does it contain the content of file "in"?
  -- lines持有的是血统图
lines.collect()
  -- lines yields a list of strings, one per line of input
  -- if we run lines.collect() again, it re-reads file "in"
val links1 = lines.map{ s => val parts = s.split("\\s+"); (parts(0), parts(1)) }
links1.collect()
  -- map, split, tuple -- acts on each line in turn
  -- parses each string "x y" into tuple ( "x", "y" )
val links2 = links1.distinct()
  -- distinct() sorts or hashes to bring duplicates together
val links3 = links2.groupByKey()
  -- groupByKey() sorts or hashes to bring instances of each key together
val links4 = links3.cache()
  -- cache() == persist in memory
var ranks = links4.mapValues(v => 1.0)

-- now for first loop iteration
val jj = links4.join(ranks)
  -- the join brings each page's link list and current rank together
val contribs = jj.values.flatMap{ case (urls, rank) => urls.map(url => (url, rank / urls.size)) }
  -- for each link, the "from" page's rank divided by number of its links
ranks = contribs.reduceByKey(_ + _).mapValues(0.15 + 0.85 * _)
  -- sum up the links that lead to each page

-- second loop iteration
val jj2 = links4.join(ranks)
  -- join() brings together equal keys; must sort or hash
val contribs2 = jj2.values.flatMap{ case (urls, rank) => urls.map(url => (url, rank / urls.size)) }
ranks = contribs2.reduceByKey(_ + _).mapValues(0.15 + 0.85 * _)
  -- reduceByKey() brings together equal keys

-- the loop &c just creates a lineage graph.
-- it does not do any real work.

val output = ranks.collect()
  -- collect() is an action.
  -- it causes the whole computation to execute!
output.foreach(tup => println(s"${tup._1} has rank:  ${tup._2} ."))

until the final collect(), this code just creates a lineage graph 创建了一个血统图

  • it does not process the data 但还没有进行计算,计算只会在论文中所说的"action"后开始,action类似一个函数调用,之后会按照血统图进行执行

Figure 3: Lineage graph for datasets in PageRank.

what does the lineage graph look like? 血统图是什么?

  • Figure 3 类似论文中的图3
  • it’s a graph of transform stages – a data-flow graph
  • it’s a complete recipe for the computation 这是一个完整的计算公式
  • note that the loop added to the graph – there is not actually a cycle
    • there’s a new ranks/contribs for each loop iteration

lines.collect()读取了输入,读取了一个String集合,一次一行

.collect()告诉Spark使用血统图创建描述了所有转换步骤的Java字节码,当然只是读取文件。Spark会通过查看HDFS,查看你需要的数据在哪,只需要一些worker读取一些输入数据的partition,其将血统图中的每个转换编译为Java字节码,然后将字节码发送给所有worker,然后worker执行字节码,然后从所有worker中获取数据。

for multi-step computation, this programming model is more convenient than MapReduce

Figure 2: Spark runtime. The user’s driver program launches multiple workers, which read data blocks from a distributed file system and can persist computed RDD partitions in memory.

the Scala code runs in the “driver” machine of Figure 2

  • the driver constructs a lineage graph
  • the driver compiles Java bytecodes and sends them to worker machines
  • the driver then manages execution and data movement
val links1 = lines.map{ s => val parts = s.split("\\s+"); (parts(0), parts(1)) }
links1.collect()
  -- map, split, tuple -- acts on each line in turn
  -- parses each string "x y" into tuple ( "x", "y" )

这里调用.map,会对输入的每一项(这里即每一行)运行一个函数,即s =>指向的split函数,每次拆分一个String,最终组合成String数组。所以这里只是处理数据。

现在.collect获取到了每对url数据。当.map执行时,其可以独立地在每个worker上执行对应输入地partition,因为这里只是独立处理每一行数据,不同行间没有交互。所以所有worker可以并行地在所有partition上执行。

val links2 = links1.distinct()
  -- distinct() sorts or hashes to bring duplicates together
val links3 = links2.groupByKey()
  -- groupByKey() sorts or hashes to bring instances of each key together
val links4 = links3.cache()
  -- cache() == persist in memory
var ranks = links4.mapValues(v => 1.0)

下面是distinct函数,如果有多个连接指向其他页面,我们只希望对每个链接计数一次。

对于TB级别数据的去重压力很大,涉及到随机读,distinct要将每个重复的替换为唯一的输入,可能涉及到worker间的通信,shuffle,排序,对排序结果进行split。需要大量的计算,当然在本例中没有重复。

.groupByKey进行了分组。下面每次迭代都会reuse这里links3中的数据,来进行pageRank计算,这里我们不会想每次用时都重新进行map、distinct、groupByKey的计算。

这里的.cache就会进行持久化操作。

然后在pageRank的第一步,我们将每个页面的概率都初始化为1.0。之后每次迭代循环都会更新这里的排名。

学生提问:所以spark允许用户请求比cache更fine-granted schedulig的primitives,可以控制存储位置和计算方式?

教授:cache是一个通用persist调用的特例,可以告诉spark我想保存内存上的数据,或存到HDFS中,所以后续可以进行故障恢复。一开始数据是按照输入数据进行partitioning的,但是当我们开始转换/shuffle时,即distinct和groupByKey时不得不改变partitioning。如果没有特殊设置spark可能会使用hash来进行partition,但我们可以设计特殊机制。

-- now for first loop iteration
val jj = links4.join(ranks)
  -- the join brings each page's link list and current rank together
val contribs = jj.values.flatMap{ case (urls, rank) => urls.map(url => (url, rank / urls.size)) }
  -- for each link, the "from" page's rank divided by number of its links
ranks = contribs.reduceByKey(_ + _).mapValues(0.15 + 0.85 * _)
  -- sum up the links that lead to each page

下面进入第一个loop,join会将每个页面的链接和当前页面排名连接起来。而这个join就是论文中说的"wide transformation",它可能会通过url key来shuffle数据,来将链接项和rank组合在一起,我认为spark已经注意到partition中有同样key的link和rank已经在同一个worker中,spark注意到这一点并不会轻易移动数据,否则需要join两个rdd中对应的link和rank。

现在我们要将page rank推到其链接的每个page上。对每个page,我们在其指向的url上运行map,来表示不同的贡献,我们要总结每个page的贡献。这里需要进行shuffle,因为我们要进行wide transformer,因为我们要将所有页面的贡献汇总到同一个worker、同一个partition进行累加。

reduceByKey的作用是根据key累加第二个value,进行reduce操作,得到的结果用于pageRank。这里有两个操作,根据key进行reduce以及对value进行map,这里反映了15%随机访问和85%继续链接的概率。

现在rank进行了更新,已经开始接近前面展示过的最终结果了。

循环loop所作的事情实际上就是创建血统图,并不是更新loop中声明的变量,而是将新的transformation 节点添加到其构建的血统图中。

论文将运行该程序的机器称为"driver"。

该程序正在做许多许多MapReduce才能实现的工作,

循环不断将节点添加到血统图中,而不是创建循环血统图。并且这里的数据links一次又一次地被使用,Spark将其保存在内存中进行多次查询。

15.2 页面排名算法执行分析

what does the execution look like?

  • [diagram: driver, partitioned input file, workers]
  • input in HDFS (like GFS) 输入是在HDFS中预先分区,输入文件可能已经被分为多个64MB的子文件,Spark了解这一事实,并用对应的方式分割worker。
  • input data files are already “partitioned” over many storage servers first 1,000,000 lines in one partition, next lines in another, &c.
  • more partitions than machines, for load balance Spark可能会妥善分配worker,可能每个worker有不止一个partition。
  • each worker machine takes a partition, applies lineage graph in order 现在有read操作,将输入数据处理为from-to的url String
  • when computation on different partitions is independent (“narrow”): 而这完全是本地操作,在同样的worker上进行初始化map,逐条处理数据即可。到目前为止操作都只发生在一个一个的worker内,这里是narrow dependency,即只考虑独立的数据,不必担心与其他记录的关系。
    • no inter-machine communication required after first read
    • a worker applies series of transformations to input stream

this is already more efficient than MapReduce 现在已经比MapReduce高效很多了。

  • data is forwarded directly from one transformation to the next Spark中的数据直接按照血统图进行一个有一个转换
  • MR would need multiple Map+Reduces 而MapReduce每次都要进行退化的map操作,
    • with expensive store to GFS, then re-read, between each MapReduce的每个阶段都从GFS中对reduce输入,计算后写回GFS,下一个阶段再次重复,这里Spark消除了输入和写回,提升了效率

但并不是所有transformation都是narrow,一个一个读取数据,每条数据独立于其他数据。

what about distinct()? groupByKey()? join()? reduceByKey()?

  • these need to look at data from all partitions, not just one 他们需要查看所有的分区,而不是单独的分区
  • because all records with a given key must be considered together 以distinct为例,都需要查看所有的同样key的worker,也许我们可以一开始按key进行partition,但是并没有。也许distinct可以按key分区,但是也需要读取之前的所有分区
  • these are the paper’s “wide” dependencies (as opposed to “narrow”) 这些transformation称为wide

how are wide dependencies implemented?

  • [diagram]
  • a lot like Map intermediate output in MapReduce
  • the driver knows where the wide dependencies are
    • e.g. between the map() and the distinct() in page-rank
    • upstream transformation, downstream transformation
  • the data must be “shuffled” into new partitions
    • e.g. bring all of a given key together
  • after the upstream transformation:
    • split output up by shuffle criterion (typically some key)
    • arrange into buckets in memory, one per downstream partition
  • before the downstream transformation:
    • (wait until upstream transformation completes – driver manages this)
    • each worker fetches its bucket from each upstream worker 所以narrow 阶段最后要做的事情是输出会被切分到对应的桶中,对应下一transformation阶段不同的worker,即distinct会直接从对应桶中获取。
    • now the data is partitioned in a different way
  • wide is expensive!
    • all data is moved across the network
    • it’s a barrier – all workers must wait until all are done 当然每一wide transformation阶段都需要等待上一阶段的所有箭头执行完毕。

what if data is re-used?

  • e.g. links4 in our page-rank
  • by default, must be re-computed, e.g. re-read from input file
  • persist() and cache() cause links to be saved in memory for re-use

re-using persisted data is another big advantage over MapReduce

Spark can optimized based on its view of the whole lineage graph Spark可以看到全局的血统图,并且寻找可以优化的点

  • stream records, one at a time, though sequence of narrow transformations 对于narrow transformation只在一个机器上运行
    • increases locality, good for CPU data caches
    • avoids having to store entire partition of records in memory
  • notice when shuffles aren’t needed b/c inputs already partitioned in the same way 查看下一阶段的输入是否已经按照相同形式划分
    • e.g. links4.join(ranks) 在进行下一次循环的distinct前,会识别到数据已经被groupByKey处理过了,所有不需要进行shuffle。

能做到这一点还是因为程序一开始就能看到整个血统图。

15.3 容错性

这里追求的容错性不是类似数据库的,无法承受失去任何数据的绝对容错性,只是由于要重新计算的话花费很高,但也不是完全不可接受。例如Spark不会备份driver机器,崩溃后只要重新运行就好,一个机器可能顶多几个月才崩溃一次。并且Spark假设输入已经在如HDFS这种容错的文件系统上备份了,输入已经具备容错性。

what about fault tolerance?

  • what if one machine crashes? 一个worker崩溃了会将计算任务分割其他worker重新计算
  • its memory and computation state are lost
  • driver re-runs transformations on crashed machine’s partitions on other machines
    • usually each machine is responsible for many partitions
    • so load can be spread
    • thus re-computation is pretty fast
  • for narrow dependencies, only lost partitions have to be re-executed 对于narrow依赖,只有丢失的partition需要重新计算

what about failures when there are wide dependencies? 如果wide依赖崩溃了呢?一般来说在Spark的每个循环中,数据不会保存下来,除非你进行特殊要求。

  • re-computing one failed partition requires information from all partitions 如果前面有前置的narrow操作可以只在一个worker上重新计算,但是wide操作要获取所有partition的数据
  • so all partitions may need to re-execute from the start! 但是有的worker可能已经跨过了这个阶段,并舍弃/覆盖了之前的输出。可能需要重新准备该partition的所有内容以及其他partition的前置narrow内容


- even though they didn’t fail 这是无法接受的。

  • Spark supports checkpoints to HDFS (like GFS) to cope with this 所以Spark支持对特定操作建立检查点,保存到HDFS
    • driver only has to recompute along lineage from latest checkpoint
  • for page-rank, perhaps checkpoint ranks every 10th iteration 以pageRank为例,由于持久化非常费时,并且TB级别数据量太大,我们可以每10次迭代保存一次

但是事实是随着worker数量变多,故障的概率也在变大,在现在的系统中容错性越来越重要。Spark宣称transformation是确定性的,解释了为什么rdd是不可变的,使得可以只重新计算一个partition就可以从错误中恢复,而不用重新计算,过去有很多支持非确定性计算、可变数据的系统,往往没有好的容错策略,但是当时的机器数量很少,错误率很低,模型似乎是合理的。

limitations?

  • geared up for batch processing of bulk data Spark非常适合批量处理大量数据,但是和银行或网购转账场景无关。Spark可能有助于离线分析客户的购物习惯,而不是用于在线处理。
  • all records treated the same way
  • transformations are “functional” – turn input into output
    • no notion of modifying data in place

另一个问题是stream处理,许多情况下,人们的输入是输入流,并不是固定大小的,但是论文没有提到这一点,有个一变体是Spark Streaming更适合处理流式数据,将其分割为较小的批次一次次运行Spark。

15.4 总结

summary

  • Spark improves expressivity and performance vs MapReduce Spark在MapReduce上提升了表现力和性能,Spark明确了计算图(血统图)并可以进行优化
  • giving the framework a view of the complete dataflow is helpful
    • performance optimizations
    • failure recovery
  • what were the keys to performance?
    • leave data in memory between transformations, vs write to GFS then read
    • re-use of data in memory (e.g. links in page-rank) 使得RDD留在内存中以进行重用
  • Spark very successful, widely used

更多推荐