-
Notifications
You must be signed in to change notification settings - Fork 657
[Poc] Use Background pool to get JobInfo from Ray Dashboard #4043
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
4069033
45635be
72142c0
fe90e70
727d2c2
d9a1a88
0f1f766
da0e45e
afcb9e1
82b3acf
69be5fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "net/http" | ||
| "strings" | ||
|
|
||
| cmap "github.com/orcaman/concurrent-map/v2" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/util/json" | ||
| "k8s.io/apimachinery/pkg/util/yaml" | ||
|
|
@@ -25,12 +26,13 @@ var ( | |
| ) | ||
|
|
||
| type RayDashboardClientInterface interface { | ||
| InitClient(client *http.Client, dashboardURL string) | ||
| InitClient(client *http.Client, dashboardURL string, workerPool *WorkerPool, jobInfoMap *cmap.ConcurrentMap[string, *utiltypes.RayJobInfo]) | ||
| UpdateDeployments(ctx context.Context, configJson []byte) error | ||
| // V2/multi-app Rest API | ||
| GetServeDetails(ctx context.Context) (*utiltypes.ServeDetails, error) | ||
| GetMultiApplicationStatus(context.Context) (map[string]*utiltypes.ServeApplicationStatus, error) | ||
| GetJobInfo(ctx context.Context, jobId string) (*utiltypes.RayJobInfo, error) | ||
| AsyncGetJobInfo(ctx context.Context, jobId string) | ||
| ListJobs(ctx context.Context) (*[]utiltypes.RayJobInfo, error) | ||
| SubmitJob(ctx context.Context, rayJob *rayv1.RayJob) (string, error) | ||
| SubmitJobReq(ctx context.Context, request *utiltypes.RayJobRequest) (string, error) | ||
|
|
@@ -41,12 +43,16 @@ type RayDashboardClientInterface interface { | |
|
|
||
| type RayDashboardClient struct { | ||
| client *http.Client | ||
| workerPool *WorkerPool | ||
| jobInfoMap *cmap.ConcurrentMap[string, *utiltypes.RayJobInfo] | ||
| dashboardURL string | ||
| } | ||
|
|
||
| func (r *RayDashboardClient) InitClient(client *http.Client, dashboardURL string) { | ||
| func (r *RayDashboardClient) InitClient(client *http.Client, dashboardURL string, workerPool *WorkerPool, jobInfoMap *cmap.ConcurrentMap[string, *utiltypes.RayJobInfo]) { | ||
| r.client = client | ||
| r.dashboardURL = dashboardURL | ||
| r.workerPool = workerPool | ||
| r.jobInfoMap = jobInfoMap | ||
| } | ||
|
|
||
| // UpdateDeployments update the deployments in the Ray cluster. | ||
|
|
@@ -161,6 +167,24 @@ func (r *RayDashboardClient) GetJobInfo(ctx context.Context, jobId string) (*uti | |
| return &jobInfo, nil | ||
| } | ||
|
|
||
| func (r *RayDashboardClient) AsyncGetJobInfo(ctx context.Context, jobId string) { | ||
| if _, ok := r.workerPool.channelContent.Get(jobId); ok { | ||
| return | ||
| } | ||
| r.workerPool.channelContent.Set(jobId, struct{}{}) | ||
| r.workerPool.taskQueue <- func() { | ||
| jobInfo, err := r.GetJobInfo(ctx, jobId) | ||
| r.workerPool.channelContent.Remove(jobId) | ||
|
||
| if err != nil { | ||
| fmt.Printf("AsyncGetJobInfo: error: %v\n", err) | ||
| return | ||
| } | ||
| if jobInfo != nil { | ||
| r.jobInfoMap.Set(jobId, jobInfo) | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
183
to
200
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's actually an edge case.
In this case, we shouldn't store the result. I think the solution to handle this edge case is using another backgroud go routine to list all rayjob CR, and check is there any additional key in need your two's advice
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that is not hard to avoid. We just need to put a placeholder into the map and only update the map if the placeholder exists.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we also need to clear the jobInfoMap before each job retry and deletion. |
||
|
|
||
| func (r *RayDashboardClient) ListJobs(ctx context.Context) (*[]utiltypes.RayJobInfo, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.dashboardURL+JobPath, nil) | ||
| if err != nil { | ||
|
|
@@ -211,6 +235,7 @@ func (r *RayDashboardClient) SubmitJobReq(ctx context.Context, request *utiltype | |
| } | ||
|
|
||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| resp, err := r.client.Do(req) | ||
| if err != nil { | ||
| return | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||
| package dashboardclient | ||||||
|
|
||||||
| import ( | ||||||
| "sync" | ||||||
|
|
||||||
| cmap "github.com/orcaman/concurrent-map/v2" | ||||||
| ) | ||||||
|
|
||||||
| type WorkerPool struct { | ||||||
| channelContent cmap.ConcurrentMap[string, struct{}] | ||||||
|
||||||
| taskQueue chan func() | ||||||
| stop chan struct{} | ||||||
| wg sync.WaitGroup | ||||||
| workers int | ||||||
| } | ||||||
|
|
||||||
| func NewWorkerPool(taskQueue chan func()) *WorkerPool { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing a task queue channel is weird. Specifying a worker count is more understandable. You can also make a buffered channel based on the worker count internally. |
||||||
| wp := &WorkerPool{ | ||||||
| taskQueue: taskQueue, | ||||||
| workers: 10, | ||||||
| stop: make(chan struct{}), | ||||||
| channelContent: cmap.New[struct{}](), | ||||||
| } | ||||||
|
|
||||||
| // Start workers immediately | ||||||
| wp.Start() | ||||||
| return wp | ||||||
| } | ||||||
|
|
||||||
| // Start launches worker goroutines to consume from queue | ||||||
| func (wp *WorkerPool) Start() { | ||||||
|
||||||
| for i := 0; i < wp.workers; i++ { | ||||||
| wp.wg.Add(1) | ||||||
| go wp.worker() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // worker consumes and executes tasks from the queue | ||||||
| func (wp *WorkerPool) worker() { | ||||||
| defer wp.wg.Done() | ||||||
|
|
||||||
| for { | ||||||
| select { | ||||||
| case <-wp.stop: | ||||||
| return | ||||||
| case task := <-wp.taskQueue: | ||||||
| if task != nil { | ||||||
| task() // Execute the job | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Stop shuts down all workers | ||||||
| func (wp *WorkerPool) Stop() { | ||||||
| close(wp.stop) | ||||||
| wp.wg.Wait() | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we try not injecting this into the RayJobReconciler? I think it should be an implementation detail of the dashboard client and should be better hidden by the it.