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

[Java] Fix setCurrentTask() in multi threading #3821

Merged
merged 7 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion java/runtime/src/main/java/org/ray/runtime/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public void execute(TaskSpec spec) {
currentActorId = returnId;
}
} finally {
runtime.getWorkerContext().setCurrentTask(null, null);
Thread.currentThread().setContextClassLoader(oldLoader);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I'm not clear on what classLoader is, but it seems like you could get rid of this line and pass in oldLoader in the setCurrentTask lines above?

Copy link
Contributor

Choose a reason for hiding this comment

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

@stephanie-wang classLoader is used to tell JVM where and how to load a class into memory. We use different class loaders for different drivers. So different drivers can have classes with the same name without conflicts.
@jovany-wang classLoader should be set and reset in the same way as the driver id.

}
}
Expand Down
16 changes: 7 additions & 9 deletions java/runtime/src/main/java/org/ray/runtime/WorkerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ public WorkerContext(WorkerMode workerMode, UniqueId driverId) {
taskIndex = ThreadLocal.withInitial(() -> 0);
putIndex = ThreadLocal.withInitial(() -> 0);
currentTaskId = ThreadLocal.withInitial(UniqueId::randomId);
currentClassLoader = null;
if (workerMode == WorkerMode.DRIVER) {
workerId = driverId;
currentTaskId.set(UniqueId.randomId());
currentDriverId = driverId;
currentClassLoader = null;
} else {
workerId = UniqueId.randomId();
setCurrentTask(null, null);
this.currentTaskId.set(UniqueId.NIL);
this.currentDriverId = UniqueId.NIL;
}
}

Expand All @@ -68,13 +69,10 @@ public void setCurrentTask(TaskSpec task, ClassLoader classLoader) {
Thread.currentThread().getId() == mainThreadId,
"This method should only be called from the main thread."
);
if (task != null) {
currentTaskId.set(task.taskId);
currentDriverId = task.driverId;
} else {
currentTaskId.set(UniqueId.NIL);
currentDriverId = UniqueId.NIL;
}

Preconditions.checkNotNull(task);
this.currentTaskId.set(task.taskId);
this.currentDriverId = task.driverId;
taskIndex.set(0);
putIndex.set(0);
currentClassLoader = classLoader;
Expand Down
3 changes: 3 additions & 0 deletions python/ray/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ def submit_task(self,
self.task_context.task_index += 1
# The parent task must be set for the submitted task.
assert not self.current_task_id.is_nil()
# Current driver id must not be nil when submitting a task.
# Because every task must belong to a driver.
assert not self.task_driver_id.is_nil()
# Submit the task to local scheduler.
function_descriptor_list = (
function_descriptor.get_function_descriptor_list())
Expand Down