Skip to content

Commit

Permalink
address patrick's and ali's comments from the previous PR
Browse files Browse the repository at this point in the history
  • Loading branch information
haoyuan committed Mar 22, 2014
1 parent 8859371 commit 776a56c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ import org.apache.spark.util.Utils


/**
* Creates and maintains the logical mapping between logical blocks and tachyon fs
* locations. By default, one block is mapped to one file with a name given by its BlockId.
* However, it is also possible to have a block map to only a segment of a file, by calling
* mapBlockToFileSegment().
* Creates and maintains the logical mapping between logical blocks and tachyon fs locations. By
* default, one block is mapped to one file with a name given by its BlockId.
*
* @param rootDirs The directories to use for storing block files. Data will be hashed among these.
*/
Expand All @@ -44,11 +42,12 @@ private[spark] class TachyonBlockManager(
extends TachyonFilePathResolver with Logging {

val client = if (master != null && master != "") TachyonFS.get(master) else null

if (client == null) {
logError("Failed to connect to the Tachyon as the master address is not configured")
System.exit(ExecutorExitCode.DISK_STORE_FAILED_TO_CREATE_DIR)
System.exit(ExecutorExitCode.TACHYON_STORE_FAILED_TO_INITIALIZE)
}

private val MAX_DIR_CREATION_ATTEMPTS = 10
private val subDirsPerTachyonDir =
shuffleManager.conf.get("spark.tachyonStore.subDirectories", "64").toInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private class TachyonStore(
val segment = tachyonManager.getBlockLocation(blockId)
val file = tachyonManager.getFile(blockId)
val is = file.getInStream(ReadType.CACHE)
var buffer : ByteBuffer = null
var buffer: ByteBuffer = null
if (is != null){
val size = segment.length - segment.offset
val bs = new Array[Byte](size.asInstanceOf[Int])
Expand Down
75 changes: 59 additions & 16 deletions docs/scala-programming-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,24 @@ A complete list of actions is available in the [RDD API doc](api/core/index.html

## RDD Persistence

One of the most important capabilities in Spark is *persisting* (or *caching*) a dataset in memory across operations. When you persist an RDD, each node stores any slices of it that it computes in memory and reuses them in other actions on that dataset (or datasets derived from it). This allows future actions to be much faster (often by more than 10x). Caching is a key tool for building iterative algorithms with Spark and for interactive use from the interpreter.

You can mark an RDD to be persisted using the `persist()` or `cache()` methods on it. The first time it is computed in an action, it will be kept in memory on the nodes. The cache is fault-tolerant -- if any partition of an RDD is lost, it will automatically be recomputed using the transformations that originally created it.

In addition, each RDD can be stored using a different *storage level*, allowing you, for example, to persist the dataset on disk, or persist it in memory but as serialized Java objects (to save space), or even replicate it across nodes. These levels are chosen by passing a [`org.apache.spark.storage.StorageLevel`](api/core/index.html#org.apache.spark.storage.StorageLevel) object to `persist()`. The `cache()` method is a shorthand for using the default storage level, which is `StorageLevel.MEMORY_ONLY` (store deserialized objects in memory). The complete set of available storage levels is:
One of the most important capabilities in Spark is *persisting* (or *caching*) a dataset in memory
across operations. When you persist an RDD, each node stores any slices of it that it computes in
memory and reuses them in other actions on that dataset (or datasets derived from it). This allows
future actions to be much faster (often by more than 10x). Caching is a key tool for building
iterative algorithms with Spark and for interactive use from the interpreter.

You can mark an RDD to be persisted using the `persist()` or `cache()` methods on it. The first time
it is computed in an action, it will be kept in memory on the nodes. The cache is fault-tolerant --
if any partition of an RDD is lost, it will automatically be recomputed using the transformations
that originally created it.

In addition, each RDD can be stored using a different *storage level*, allowing you, for example, to
persist the dataset on disk, or persist it in memory but as serialized Java objects (to save space),
or even replicate it across nodes. These levels are chosen by passing a
[`org.apache.spark.storage.StorageLevel`](api/core/index.html#org.apache.spark.storage.StorageLevel)
object to `persist()`. The `cache()` method is a shorthand for using the default storage level,
which is `StorageLevel.MEMORY_ONLY` (store deserialized objects in memory). The complete set of
available storage levels is:

<table class="table">
<tr><th style="width:23%">Storage Level</th><th>Meaning</th></tr>
Expand All @@ -292,8 +305,8 @@ In addition, each RDD can be stored using a different *storage level*, allowing
</tr>
<tr>
<td> MEMORY_AND_DISK_SER </td>
<td> Similar to MEMORY_ONLY_SER, but spill partitions that don't fit in memory to disk instead of recomputing them
on the fly each time they're needed. </td>
<td> Similar to MEMORY_ONLY_SER, but spill partitions that don't fit in memory to disk instead of
recomputing them on the fly each time they're needed. </td>
</tr>
<tr>
<td> DISK_ONLY </td>
Expand All @@ -307,8 +320,9 @@ In addition, each RDD can be stored using a different *storage level*, allowing

### Which Storage Level to Choose?

Spark's storage levels are meant to provide different tradeoffs between memory usage and CPU efficiency.
We recommend going through the following process to select one:
Spark's storage levels are meant to provide different trade-offs between memory usage and CPU
efficiency. It allows uses to choose memory, disk, or Tachyon for storing data. We recommend going
through the following process to select one:

* If your RDDs fit comfortably with the default storage level (`MEMORY_ONLY`), leave them that way. This is the most
CPU-efficient option, allowing operations on the RDDs to run as fast as possible.
Expand All @@ -322,17 +336,38 @@ We recommend going through the following process to select one:
application). *All* the storage levels provide full fault tolerance by recomputing lost data, but the replicated ones
let you continue running tasks on the RDD without waiting to recompute a lost partition.

If you want to define your own storage level (say, with replication factor of 3 instead of 2), then use the function factor method `apply()` of the [`StorageLevel`](api/core/index.html#org.apache.spark.storage.StorageLevel$) singleton object.
If you want to define your own storage level (say, with replication factor of 3 instead of 2), then
use the function factor method `apply()` of the
[`StorageLevel`](api/core/index.html#org.apache.spark.storage.StorageLevel$) singleton object.

Spark has a block manager inside the Executors that let you chose memory, disk, or Tachyon. The
latter is for storing RDDs off-heap outside the Executor JVM on top of the memory management system
[Tachyon](http://tachyon-project.org/). This mode has the following advantages:

* Executor crash won't lose the data cached.
* Executors can have smaller memory footprint, allowing you to run more executors on the same
machine as the bulk of the memory will be inside Tachyon.
* There won't be GC overheads with data stored in Tachyon.

# Shared Variables

Normally, when a function passed to a Spark operation (such as `map` or `reduce`) is executed on a remote cluster node, it works on separate copies of all the variables used in the function. These variables are copied to each machine, and no updates to the variables on the remote machine are propagated back to the driver program. Supporting general, read-write shared variables across tasks would be inefficient. However, Spark does provide two limited types of *shared variables* for two common usage patterns: broadcast variables and accumulators.
Normally, when a function passed to a Spark operation (such as `map` or `reduce`) is executed on a
remote cluster node, it works on separate copies of all the variables used in the function. These
variables are copied to each machine, and no updates to the variables on the remote machine are
propagated back to the driver program. Supporting general, read-write shared variables across tasks
would be inefficient. However, Spark does provide two limited types of *shared variables* for two
common usage patterns: broadcast variables and accumulators.

## Broadcast Variables

Broadcast variables allow the programmer to keep a read-only variable cached on each machine rather than shipping a copy of it with tasks. They can be used, for example, to give every node a copy of a large input dataset in an efficient manner. Spark also attempts to distribute broadcast variables using efficient broadcast algorithms to reduce communication cost.
Broadcast variables allow the programmer to keep a read-only variable cached on each machine rather
than shipping a copy of it with tasks. They can be used, for example, to give every node a copy of a
large input dataset in an efficient manner. Spark also attempts to distribute broadcast variables
using efficient broadcast algorithms to reduce communication cost.

Broadcast variables are created from a variable `v` by calling `SparkContext.broadcast(v)`. The broadcast variable is a wrapper around `v`, and its value can be accessed by calling the `value` method. The interpreter session below shows this:
Broadcast variables are created from a variable `v` by calling `SparkContext.broadcast(v)`. The
broadcast variable is a wrapper around `v`, and its value can be accessed by calling the `value`
method. The interpreter session below shows this:

{% highlight scala %}
scala> val broadcastVar = sc.broadcast(Array(1, 2, 3))
Expand All @@ -342,13 +377,21 @@ scala> broadcastVar.value
res0: Array[Int] = Array(1, 2, 3)
{% endhighlight %}

After the broadcast variable is created, it should be used instead of the value `v` in any functions run on the cluster so that `v` is not shipped to the nodes more than once. In addition, the object `v` should not be modified after it is broadcast in order to ensure that all nodes get the same value of the broadcast variable (e.g. if the variable is shipped to a new node later).
After the broadcast variable is created, it should be used instead of the value `v` in any functions
run on the cluster so that `v` is not shipped to the nodes more than once. In addition, the object
`v` should not be modified after it is broadcast in order to ensure that all nodes get the same
value of the broadcast variable (e.g. if the variable is shipped to a new node later).

## Accumulators

Accumulators are variables that are only "added" to through an associative operation and can therefore be efficiently supported in parallel. They can be used to implement counters (as in MapReduce) or sums. Spark natively supports accumulators of numeric value types and standard mutable collections, and programmers can add support for new types.
Accumulators are variables that are only "added" to through an associative operation and can
therefore be efficiently supported in parallel. They can be used to implement counters (as in
MapReduce) or sums. Spark natively supports accumulators of numeric value types and standard mutable
collections, and programmers can add support for new types.

An accumulator is created from an initial value `v` by calling `SparkContext.accumulator(v)`. Tasks running on the cluster can then add to it using the `+=` operator. However, they cannot read its value. Only the driver program can read the accumulator's value, using its `value` method.
An accumulator is created from an initial value `v` by calling `SparkContext.accumulator(v)`. Tasks
running on the cluster can then add to it using the `+=` operator. However, they cannot read its
value. Only the driver program can read the accumulator's value, using its `value` method.

The interpreter session below shows an accumulator being used to add up the elements of an array:

Expand Down

0 comments on commit 776a56c

Please sign in to comment.