Skip to content

Commit 9de1882

Browse files
committed
feat: optimize GetTaskJob and DeleteTaskJob
Signed-off-by: Gaius <gaius.qi@gmail.com>
1 parent 5547307 commit 9de1882

File tree

21 files changed

+275
-289
lines changed

21 files changed

+275
-289
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module d7y.io/dragonfly/v2
33
go 1.21
44

55
require (
6-
d7y.io/api/v2 v2.0.142
6+
d7y.io/api/v2 v2.0.148
77
github.com/MysteriousPotato/go-lockable v1.0.0
88
github.com/RichardKnop/machinery v1.10.8
99
github.com/Showmax/go-fqdn v1.0.0
@@ -129,7 +129,7 @@ require (
129129
github.com/distribution/reference v0.5.0 // indirect
130130
github.com/docker/go-metrics v0.0.1 // indirect
131131
github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1 // indirect
132-
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
132+
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
133133
github.com/felixge/httpsnoop v1.0.4 // indirect
134134
github.com/fsnotify/fsnotify v1.7.0 // indirect
135135
github.com/gabriel-vasile/mimetype v1.4.4 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
5353
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
5454
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
5555
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
56-
d7y.io/api/v2 v2.0.142 h1:u1gQZwCKJODdJB883J9e5SRK9VmOTmApX13FX/bR0Vk=
57-
d7y.io/api/v2 v2.0.142/go.mod h1:IakrltEphFvcLIQs3NVeb9PAe66MGDnd2/HMs9DKYu8=
56+
d7y.io/api/v2 v2.0.148 h1:11waj+EuaHdx95Fkr3hXJJckNGw9Hu5U0ohtCbpIirw=
57+
d7y.io/api/v2 v2.0.148/go.mod h1:hyEaaIglThVWRHODk2yHN/tpa1L+/nPgdQFwPsL6fNc=
5858
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
5959
github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
6060
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.0.0/go.mod h1:uGG2W01BaETf0Ozp+QxxKJdMBNRWPdstHG0Fmdwn1/U=
@@ -434,8 +434,8 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
434434
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
435435
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
436436
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
437-
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
438-
github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew=
437+
github.com/envoyproxy/protoc-gen-validate v1.1.0 h1:tntQDh69XqOCOZsDz0lVJQez/2L6Uu2PdjCQwWCJ3bM=
438+
github.com/envoyproxy/protoc-gen-validate v1.1.0/go.mod h1:sXRDRVmzEbkM7CVcM06s9shE/m23dg3wzjl0UWqJ2q4=
439439
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
440440
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
441441
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=

internal/job/constants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const (
3030
// SyncPeersJob is the name of syncing peers job.
3131
SyncPeersJob = "sync_peers"
3232

33-
// ListTasksJob is the name of listing tasks job.
34-
ListTasksJob = "list_tasks"
33+
// GetTaskJob is the name of getting task job.
34+
GetTaskJob = "get_task"
3535

36-
// DeleteTasksJob is the name of deleting tasks job.
36+
// DeleteTaskJob is the name of deleting task job.
3737
DeleteTaskJob = "delete_task"
3838
)
3939

internal/job/types.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package job
1818

1919
import "d7y.io/dragonfly/v2/scheduler/resource"
2020

21+
// PreheatRequest defines the request parameters for preheating.
2122
type PreheatRequest struct {
2223
URL string `json:"url" validate:"required,url"`
2324
Tag string `json:"tag" validate:"omitempty"`
@@ -29,17 +30,18 @@ type PreheatRequest struct {
2930
PieceLength uint32 `json:"pieceLength" validate:"omitempty"`
3031
}
3132

33+
// PreheatResponse defines the response parameters for preheating.
3234
type PreheatResponse struct {
33-
TaskID string `json:"taskID"`
35+
TaskID string `json:"task_id"`
3436
}
3537

36-
// ListTasksRequest defines the request parameters for listing tasks.
37-
type ListTasksRequest struct {
38+
// GetTaskRequest defines the request parameters for getting task.
39+
type GetTaskRequest struct {
3840
TaskID string `json:"task_id" validate:"required"`
3941
}
4042

41-
// ListTasksResponse defines the response parameters for listing tasks.
42-
type ListTasksResponse struct {
43+
// GetTaskResponse defines the response parameters for getting task.
44+
type GetTaskResponse struct {
4345
Peers []*resource.Peer `json:"peers"`
4446
}
4547

@@ -48,16 +50,14 @@ type DeleteTaskRequest struct {
4850
TaskID string `json:"task_id" validate:"required"`
4951
}
5052

51-
// Task includes information about a task along with peer details and a description.
52-
type Task struct {
53-
Task *resource.Task `json:"task"`
54-
Peer *resource.Peer `json:"peer"`
55-
Description string `json:"description"`
53+
// DeleteTaskResponse defines the response parameters for deleting task.
54+
type DeleteTaskResponse struct {
55+
SuccessPeers []*DeletePeerResponse `json:"success_peers"`
56+
FailurePeers []*DeletePeerResponse `json:"failure_peers"`
5657
}
5758

58-
// DeleteTaskResponse represents the response after attempting to delete tasks,
59-
// categorizing them into successfully and unsuccessfully deleted.
60-
type DeleteTaskResponse struct {
61-
SuccessTasks []*Task `json:"success_tasks"`
62-
FailureTasks []*Task `json:"failure_tasks"`
59+
// DeletePeerResponse represents the response after attempting to delete a peer.
60+
type DeletePeerResponse struct {
61+
Peer *resource.Peer `json:"peer"`
62+
Description string `json:"description"`
6363
}

manager/config/config.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ type JobConfig struct {
296296

297297
// Sync peers configuration.
298298
SyncPeers SyncPeersConfig `yaml:"syncPeers" mapstructure:"syncPeers"`
299-
300-
// Manager tasks configuration.
301-
ManagerTasks ManagerTasksConfig `yaml:"managerTasks" mapstructure:"managerTasks"`
302299
}
303300

304301
type PreheatConfig struct {
@@ -318,11 +315,6 @@ type SyncPeersConfig struct {
318315
Timeout time.Duration `yaml:"timeout" mapstructure:"timeout"`
319316
}
320317

321-
type ManagerTasksConfig struct {
322-
// Timeout is the timeout for manager tasks information for the single scheduler.
323-
Timeout time.Duration `yaml:"timeout" mapstructure:"timeout"`
324-
}
325-
326318
type PreheatTLSClientConfig struct {
327319
// CACert is the CA certificate for preheat tls handshake, it can be path or PEM format string.
328320
CACert types.PEMContent `yaml:"caCert" mapstructure:"caCert"`
@@ -463,9 +455,6 @@ func New() *Config {
463455
Interval: DefaultJobSyncPeersInterval,
464456
Timeout: DefaultJobSyncPeersTimeout,
465457
},
466-
ManagerTasks: ManagerTasksConfig{
467-
Timeout: DefaultJobManagerTasksTimeout,
468-
},
469458
},
470459
ObjectStorage: ObjectStorageConfig{
471460
Enable: false,

manager/config/constant_otel.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ package config
1919
import "go.opentelemetry.io/otel/attribute"
2020

2121
const (
22-
AttributeID = attribute.Key("d7y.manager.id")
23-
AttributePreheatType = attribute.Key("d7y.manager.preheat.type")
24-
AttributePreheatURL = attribute.Key("d7y.manager.preheat.url")
25-
AttributeDeleteTaskID = attribute.Key("d7y.manager.delete_task.id")
26-
AttributeListTasksID = attribute.Key("d7y.manager.list_tasks.id")
27-
AttributeListTasksPage = attribute.Key("d7y.manager.list_tasks.page")
28-
AttributeListTasksPerPage = attribute.Key("d7y.manager.list_tasks.per_page")
22+
AttributeID = attribute.Key("d7y.manager.id")
23+
AttributePreheatType = attribute.Key("d7y.manager.preheat.type")
24+
AttributePreheatURL = attribute.Key("d7y.manager.preheat.url")
25+
AttributeDeleteTaskID = attribute.Key("d7y.manager.delete_task.id")
26+
AttributeGetTaskID = attribute.Key("d7y.manager.get_task.id")
2927
)
3028

3129
const (
@@ -34,5 +32,5 @@ const (
3432
SpanGetLayers = "get-layers"
3533
SpanAuthWithRegistry = "auth-with-registry"
3634
SpanDeleteTask = "delete-task"
37-
SpanListTasks = "list-tasks"
35+
SpanGetTask = "get-task"
3836
)

manager/config/constants.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ const (
9898

9999
// DefaultJobSyncPeersTimeout is the default timeout for syncing all peers information from the scheduler.
100100
DefaultJobSyncPeersTimeout = 10 * time.Minute
101-
102-
// DefaultJobManagerTasksTimeout is the default timeout for manager tasks, for delete task and list tasks.
103-
DefaultJobManagerTasksTimeout = 10 * time.Minute
104101
)
105102

106103
const (

manager/handlers/job.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,28 @@ func (h *Handlers) CreateJob(ctx *gin.Context) {
6060
}
6161

6262
ctx.JSON(http.StatusOK, job)
63-
case job.DeleteTaskJob:
64-
var json types.CreateDeleteTaskJobRequest
63+
case job.GetTaskJob:
64+
var json types.CreateGetTaskJobRequest
6565
if err := ctx.ShouldBindBodyWith(&json, binding.JSON); err != nil {
6666
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
6767
return
6868
}
6969

70-
job, err := h.service.CreateDeleteTaskJob(ctx.Request.Context(), json)
70+
job, err := h.service.CreateGetTaskJob(ctx.Request.Context(), json)
7171
if err != nil {
7272
ctx.Error(err) // nolint: errcheck
7373
return
7474
}
7575

7676
ctx.JSON(http.StatusOK, job)
77-
case job.ListTasksJob:
78-
var json types.CreateListTasksJobRequest
77+
case job.DeleteTaskJob:
78+
var json types.CreateDeleteTaskJobRequest
7979
if err := ctx.ShouldBindBodyWith(&json, binding.JSON); err != nil {
8080
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
8181
return
8282
}
8383

84-
job, err := h.service.CreateListTasksJob(ctx.Request.Context(), json)
84+
job, err := h.service.CreateDeleteTaskJob(ctx.Request.Context(), json)
8585
if err != nil {
8686
ctx.Error(err) // nolint: errcheck
8787
return

manager/handlers/job_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var (
3939
"user_id": 4,
4040
"bio": "bio"
4141
}`
42-
mockListTasksJobReqBody = `
42+
mockGetTaskJobReqBody = `
4343
{
44-
"type": "list_tasks",
44+
"type": "get_task",
4545
"user_id": 4,
4646
"bio": "bio"
4747
}`
@@ -62,12 +62,12 @@ var (
6262
Type: "preheat",
6363
BIO: "bio",
6464
}
65-
mockListTasksCreateJobRequest = types.CreateListTasksJobRequest{
65+
mockCreateGetTaskJobRequest = types.CreateGetTaskJobRequest{
6666
UserID: 4,
67-
Type: "list_tasks",
67+
Type: "get_task",
6868
BIO: "bio",
6969
}
70-
mockDeleteTaskCreateJobRequest = types.CreateDeleteTaskJobRequest{
70+
mockCreateDeleteTaskJobRequest = types.CreateDeleteTaskJobRequest{
7171
UserID: 4,
7272
Type: "delete_task",
7373
BIO: "bio",
@@ -83,10 +83,10 @@ var (
8383
BIO: "bio",
8484
TaskID: "2",
8585
}
86-
mockListTasksJobModel = &models.Job{
86+
mockGetTaskJobModel = &models.Job{
8787
BaseModel: mockBaseModel,
8888
UserID: 4,
89-
Type: "list_tasks",
89+
Type: "get_task",
9090
BIO: "bio",
9191
TaskID: "2",
9292
}
@@ -153,24 +153,24 @@ func TestHandlers_CreateJob(t *testing.T) {
153153
},
154154
{
155155
name: "success",
156-
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockListTasksJobReqBody)),
156+
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockGetTaskJobReqBody)),
157157
mock: func(ms *mocks.MockServiceMockRecorder) {
158-
ms.CreateListTasksJob(gomock.Any(), gomock.Eq(mockListTasksCreateJobRequest)).Return(mockListTasksJobModel, nil).Times(1)
158+
ms.CreateGetTaskJob(gomock.Any(), gomock.Eq(mockCreateGetTaskJobRequest)).Return(mockGetTaskJobModel, nil).Times(1)
159159
},
160160
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
161161
assert := assert.New(t)
162162
assert.Equal(http.StatusOK, w.Code)
163163
job := models.Job{}
164164
err := json.Unmarshal(w.Body.Bytes(), &job)
165165
assert.NoError(err)
166-
assert.Equal(mockListTasksJobModel, &job)
166+
assert.Equal(mockGetTaskJobModel, &job)
167167
},
168168
},
169169
{
170170
name: "success",
171171
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockDeleteTaskJobReqBody)),
172172
mock: func(ms *mocks.MockServiceMockRecorder) {
173-
ms.CreateDeleteTaskJob(gomock.Any(), gomock.Eq(mockDeleteTaskCreateJobRequest)).Return(mockDeleteTaskJobModel, nil).Times(1)
173+
ms.CreateDeleteTaskJob(gomock.Any(), gomock.Eq(mockCreateDeleteTaskJobRequest)).Return(mockDeleteTaskJobModel, nil).Times(1)
174174
},
175175
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
176176
assert := assert.New(t)

0 commit comments

Comments
 (0)