Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-19267][SS]Fix a race condition when stopping StateStore #16627

Closed
wants to merge 4 commits into from
Closed

[SPARK-19267][SS]Fix a race condition when stopping StateStore #16627

wants to merge 4 commits into from

Conversation

zsxwing
Copy link
Member

@zsxwing zsxwing commented Jan 18, 2017

What changes were proposed in this pull request?

There is a race condition when stopping StateStore which makes StateStoreSuite.maintenance flaky. StateStore.stop doesn't wait for the running task to finish, and an out-of-date task may fail doMaintenance and cancel the new task. Here is a reproducer: zsxwing@dde1b5b

This PR adds MaintenanceTask to eliminate the race condition.

How was this patch tested?

Jenkins

@@ -198,7 +214,7 @@ object StateStore extends Logging {
private def doMaintenance(): Unit = {
logDebug("Doing maintenance")
if (SparkEnv.get == null) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another potential issue here: if a SparkContext is created after checking SparkEnv.get == null, the following stop may cancel a new valid task. However, I think that won't happen in practice, so don't fix it.

@SparkQA
Copy link

SparkQA commented Jan 18, 2017

Test build #71545 has finished for PR 16627 at commit 5fa096c.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jan 19, 2017

Test build #71624 has finished for PR 16627 at commit 4e6b89b.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

Copy link
Contributor

@tdas tdas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a more surgical fix in mind when I said that put multiple volatile objects into a class so that we can replace the class completely. Here is what I had in mind.

https://github.com/apache/spark/compare/master...tdas:state-store-fix?expand=1

What do you think?

def unload(storeId: StateStoreId): Unit = LOCK.synchronized { loadedProviders.remove(storeId) }

/** Whether a state store provider is loaded or not */
def isLoaded(storeId: StateStoreId): Boolean = LOCK.synchronized {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do these need to be synchronized by external Lock?

if (SparkEnv.get == null) {
stop()
} else {
LOCK.synchronized { loadedProviders.toSeq }.foreach { case (id, provider) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is locking the state store while maintenance is going on. since it using the same lock as the external lock this, the task using the store will block on the maintenance task.

@zsxwing
Copy link
Member Author

zsxwing commented Jan 20, 2017

@tdas could you take another look? I fixed some minor issues in your patch.

Copy link
Contributor

@tdas tdas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM.


@volatile private var maintenanceTask: ScheduledFuture[_] = null
@volatile private var _coordRef: StateStoreCoordinatorRef = null
class MaintenanceTask(periodMs: Long, task: => Unit, onError: => Unit) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should mention the properties of this class. that it automatically cancels the periodic task if there is an exception. and what is onError for.

@@ -179,14 +210,14 @@ object StateStore extends Logging {
/** Start the periodic maintenance task if not already started and if Spark active */
private def startMaintenanceIfNeeded(): Unit = loadedProviders.synchronized {
val env = SparkEnv.get
if (maintenanceTask == null && env != null) {
if (env != null && (maintenanceTask == null || !maintenanceTask.isRunning)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace this with the method isMaintenanceRunning?

@SparkQA
Copy link

SparkQA commented Jan 20, 2017

Test build #71739 has finished for PR 16627 at commit cbcd6e8.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@tdas
Copy link
Contributor

tdas commented Jan 21, 2017

LGTM, pending tests.

@SparkQA
Copy link

SparkQA commented Jan 21, 2017

Test build #71744 has finished for PR 16627 at commit d66f8d7.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

asfgit pushed a commit that referenced this pull request Jan 21, 2017
## What changes were proposed in this pull request?

There is a race condition when stopping StateStore which makes `StateStoreSuite.maintenance` flaky. `StateStore.stop` doesn't wait for the running task to finish, and an out-of-date task may fail `doMaintenance` and cancel the new task. Here is a reproducer: zsxwing@dde1b5b

This PR adds MaintenanceTask to eliminate the race condition.

## How was this patch tested?

Jenkins

Author: Shixiong Zhu <[email protected]>
Author: Tathagata Das <[email protected]>

Closes #16627 from zsxwing/SPARK-19267.

(cherry picked from commit ea31f92)
Signed-off-by: Tathagata Das <[email protected]>
@asfgit asfgit closed this in ea31f92 Jan 21, 2017
@zsxwing zsxwing deleted the SPARK-19267 branch January 23, 2017 21:47
uzadude pushed a commit to uzadude/spark that referenced this pull request Jan 27, 2017
## What changes were proposed in this pull request?

There is a race condition when stopping StateStore which makes `StateStoreSuite.maintenance` flaky. `StateStore.stop` doesn't wait for the running task to finish, and an out-of-date task may fail `doMaintenance` and cancel the new task. Here is a reproducer: zsxwing@dde1b5b

This PR adds MaintenanceTask to eliminate the race condition.

## How was this patch tested?

Jenkins

Author: Shixiong Zhu <[email protected]>
Author: Tathagata Das <[email protected]>

Closes apache#16627 from zsxwing/SPARK-19267.
cmonkey pushed a commit to cmonkey/spark that referenced this pull request Feb 15, 2017
## What changes were proposed in this pull request?

There is a race condition when stopping StateStore which makes `StateStoreSuite.maintenance` flaky. `StateStore.stop` doesn't wait for the running task to finish, and an out-of-date task may fail `doMaintenance` and cancel the new task. Here is a reproducer: zsxwing@dde1b5b

This PR adds MaintenanceTask to eliminate the race condition.

## How was this patch tested?

Jenkins

Author: Shixiong Zhu <[email protected]>
Author: Tathagata Das <[email protected]>

Closes apache#16627 from zsxwing/SPARK-19267.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants