Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/7728#discussion_r35828557
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/impl/PeriodicCheckpointer.scala ---
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.mllib.impl
+
+import scala.collection.mutable
+
+import org.apache.hadoop.fs.{Path, FileSystem}
+
+import org.apache.spark.{SparkContext, Logging}
+import org.apache.spark.storage.StorageLevel
+
+
+/**
+ * This abstraction helps with persisting and checkpointing RDDs and types
derived from RDDs
+ * (such as Graphs and DataFrames). In documentation, we use the phrase
"Dataset" to refer to
+ * the distributed data type (RDD, Graph, etc.).
+ *
+ * Specifically, this abstraction automatically handles persisting and
(optionally) checkpointing,
+ * as well as unpersisting and removing checkpoint files.
+ *
+ * Users should call update() when a new Dataset has been created,
+ * before the Dataset has been materialized. After updating
[[PeriodicCheckpointer]], users are
+ * responsible for materializing the Dataset to ensure that persisting and
checkpointing actually
+ * occur.
+ *
+ * When update() is called, this does the following:
+ * - Persist new Dataset (if not yet persisted), and put in queue of
persisted Datasets.
+ * - Unpersist Datasets from queue until there are at most 3 persisted
Datasets.
+ * - If using checkpointing and the checkpoint interval has been reached,
+ * - Checkpoint the new Dataset, and put in a queue of checkpointed
Datasets.
+ * - Remove older checkpoints.
+ *
+ * WARNINGS:
+ * - This class should NOT be copied (since copies may conflict on which
Datasets should be
+ * checkpointed).
+ * - This class removes checkpoint files once later Datasets have been
checkpointed.
+ * However, references to the older Datasets will still return
isCheckpointed = true.
+ *
+ * Example usage:
+ * {{{
+ * val (data1, data2, data3, ...) = ...
+ * val cp = new PeriodicCheckpointer(data1, dir, 2)
+ * data1.count();
+ * // persisted: data1
+ * cp.update(data2)
+ * data2.count();
+ * // persisted: data1, data2
+ * // checkpointed: data2
+ * cp.update(data3)
+ * data3.count();
+ * // persisted: data1, data2, data3
+ * // checkpointed: data2
+ * cp.update(data4)
+ * data4.count();
+ * // persisted: data2, data3, data4
+ * // checkpointed: data4
+ * cp.update(data5)
+ * data5.count();
+ * // persisted: data3, data4, data5
+ * // checkpointed: data4
+ * }}}
+ *
+ * @param currentData Initial Dataset
+ * @param checkpointInterval Datasets will be checkpointed at this
interval
+ * @param sc SparkContext for the Datasets given to this checkpointer
+ * @tparam T Dataset type, such as RDD[Double]
+ */
+private[mllib] abstract class PeriodicCheckpointer[T](
+ var currentData: T,
+ val checkpointInterval: Int,
+ val sc: SparkContext) extends Logging {
+
+ /** FIFO queue of past checkpointed Datasets */
+ private val checkpointQueue = mutable.Queue[T]()
+
+ /** FIFO queue of past persisted Datasets */
+ private val persistedQueue = mutable.Queue[T]()
+
+ /** Number of times [[update()]] has been called */
+ private var updateCount = 0
+
+ update(currentData)
+
+ /**
+ * Update [[currentData]] with a new Dataset. Handle persistence and
checkpointing as needed.
+ * Since this handles persistence and checkpointing, this should be
called before the Dataset
+ * has been materialized.
+ *
+ * @param newData New Dataset created from previous Datasets in the
lineage.
+ */
+ def update(newData: T): Unit = {
+ persist(newData)
+ persistedQueue.enqueue(newData)
+ // We try to maintain 2 Datasets in persistedQueue to support the
semantics of this class:
+ // Users should call [[update()]] when a new Dataset has been created,
+ // before the Dataset has been materialized.
+ while (persistedQueue.size > 3) {
+ val dataToUnpersist = persistedQueue.dequeue()
+ unpersist(dataToUnpersist)
+ }
+ updateCount += 1
+
+ // Handle checkpointing (after persisting)
+ if ((updateCount % checkpointInterval) == 0 &&
sc.getCheckpointDir.nonEmpty) {
+ // Add new checkpoint before removing old checkpoints.
+ checkpoint(newData)
+ checkpointQueue.enqueue(newData)
+ // Remove checkpoints before the latest one.
+ var canDelete = true
+ while (checkpointQueue.size > 1 && canDelete) {
+ // Delete the oldest checkpoint only if the next checkpoint exists.
+ if (isCheckpointed(checkpointQueue.get(1).get)) {
--- End diff --
`.get(1).get` -> `(0)` or `head`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]