forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core Worker] implement ObjectInterface and add test framework (ray-p…
- Loading branch information
Showing
14 changed files
with
611 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
#include "context.h" | ||
|
||
namespace ray { | ||
|
||
/// per-thread context for core worker. | ||
struct WorkerThreadContext { | ||
WorkerThreadContext() | ||
: current_task_id(TaskID::FromRandom()), task_index(0), put_index(0) {} | ||
|
||
int GetNextTaskIndex() { return ++task_index; } | ||
|
||
int GetNextPutIndex() { return ++put_index; } | ||
|
||
const TaskID &GetCurrentTaskID() const { return current_task_id; } | ||
|
||
void SetCurrentTask(const TaskID &task_id) { | ||
current_task_id = task_id; | ||
task_index = 0; | ||
put_index = 0; | ||
} | ||
|
||
void SetCurrentTask(const raylet::TaskSpecification &spec) { | ||
SetCurrentTask(spec.TaskId()); | ||
} | ||
|
||
private: | ||
/// The task ID for current task. | ||
TaskID current_task_id; | ||
|
||
/// Number of tasks that have been submitted from current task. | ||
int task_index; | ||
|
||
/// Number of objects that have been put from current task. | ||
int put_index; | ||
}; | ||
|
||
thread_local std::unique_ptr<WorkerThreadContext> WorkerContext::thread_context_ = | ||
nullptr; | ||
|
||
WorkerContext::WorkerContext(WorkerType worker_type, const DriverID &driver_id) | ||
: worker_type(worker_type), | ||
worker_id(worker_type == WorkerType::DRIVER | ||
? ClientID::FromBinary(driver_id.Binary()) | ||
: ClientID::FromRandom()), | ||
current_driver_id(worker_type == WorkerType::DRIVER ? driver_id : DriverID::Nil()) { | ||
// For worker main thread which initializes the WorkerContext, | ||
// set task_id according to whether current worker is a driver. | ||
// (For other threads it's set to randmom ID via GetThreadContext). | ||
GetThreadContext().SetCurrentTask( | ||
(worker_type == WorkerType::DRIVER) ? TaskID::FromRandom() : TaskID::Nil()); | ||
} | ||
|
||
const WorkerType WorkerContext::GetWorkerType() const { return worker_type; } | ||
|
||
const ClientID &WorkerContext::GetWorkerID() const { return worker_id; } | ||
|
||
int WorkerContext::GetNextTaskIndex() { return GetThreadContext().GetNextTaskIndex(); } | ||
|
||
int WorkerContext::GetNextPutIndex() { return GetThreadContext().GetNextPutIndex(); } | ||
|
||
const DriverID &WorkerContext::GetCurrentDriverID() const { return current_driver_id; } | ||
|
||
const TaskID &WorkerContext::GetCurrentTaskID() const { | ||
return GetThreadContext().GetCurrentTaskID(); | ||
} | ||
|
||
void WorkerContext::SetCurrentTask(const raylet::TaskSpecification &spec) { | ||
current_driver_id = spec.DriverId(); | ||
GetThreadContext().SetCurrentTask(spec); | ||
} | ||
|
||
WorkerThreadContext &WorkerContext::GetThreadContext() { | ||
if (thread_context_ == nullptr) { | ||
thread_context_ = std::unique_ptr<WorkerThreadContext>(new WorkerThreadContext()); | ||
} | ||
|
||
return *thread_context_; | ||
} | ||
|
||
} // namespace ray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef RAY_CORE_WORKER_CONTEXT_H | ||
#define RAY_CORE_WORKER_CONTEXT_H | ||
|
||
#include "common.h" | ||
#include "ray/raylet/task_spec.h" | ||
|
||
namespace ray { | ||
|
||
struct WorkerThreadContext; | ||
|
||
class WorkerContext { | ||
public: | ||
WorkerContext(WorkerType worker_type, const DriverID &driver_id); | ||
|
||
const WorkerType GetWorkerType() const; | ||
|
||
const ClientID &GetWorkerID() const; | ||
|
||
const DriverID &GetCurrentDriverID() const; | ||
|
||
const TaskID &GetCurrentTaskID() const; | ||
|
||
void SetCurrentTask(const raylet::TaskSpecification &spec); | ||
|
||
int GetNextTaskIndex(); | ||
|
||
int GetNextPutIndex(); | ||
|
||
private: | ||
/// Type of the worker. | ||
const WorkerType worker_type; | ||
|
||
/// ID for this worker. | ||
const ClientID worker_id; | ||
|
||
/// Driver ID for this worker. | ||
DriverID current_driver_id; | ||
|
||
private: | ||
static WorkerThreadContext &GetThreadContext(); | ||
|
||
/// Per-thread worker context. | ||
static thread_local std::unique_ptr<WorkerThreadContext> thread_context_; | ||
}; | ||
|
||
} // namespace ray | ||
|
||
#endif // RAY_CORE_WORKER_CONTEXT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "core_worker.h" | ||
#include "context.h" | ||
|
||
namespace ray { | ||
|
||
CoreWorker::CoreWorker(const enum WorkerType worker_type, const enum Language language, | ||
const std::string &store_socket, const std::string &raylet_socket, | ||
DriverID driver_id) | ||
: worker_type_(worker_type), | ||
language_(language), | ||
worker_context_(worker_type, driver_id), | ||
store_socket_(store_socket), | ||
raylet_socket_(raylet_socket), | ||
task_interface_(*this), | ||
object_interface_(*this), | ||
task_execution_interface_(*this) {} | ||
|
||
Status CoreWorker::Connect() { | ||
// connect to plasma. | ||
RAY_ARROW_RETURN_NOT_OK(store_client_.Connect(store_socket_)); | ||
|
||
// connect to raylet. | ||
::Language lang = ::Language::PYTHON; | ||
if (language_ == ray::Language::JAVA) { | ||
lang = ::Language::JAVA; | ||
} | ||
|
||
// TODO: currently RayletClient would crash in its constructor if it cannot | ||
// connect to Raylet after a number of retries, this needs to be changed | ||
// so that the worker (java/python .etc) can retrieve and handle the error | ||
// instead of crashing. | ||
raylet_client_ = std::unique_ptr<RayletClient>( | ||
new RayletClient(raylet_socket_, worker_context_.GetWorkerID(), | ||
(worker_type_ == ray::WorkerType::WORKER), | ||
worker_context_.GetCurrentDriverID(), lang)); | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace ray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.