Skip to content

Commit 3bebc0b

Browse files
committed
refactor(runtime): store procedure state
1 parent 2588955 commit 3bebc0b

37 files changed

Lines changed: 803 additions & 809 deletions

CONTEXT.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ Legacy REST endpoints were removed from OpenAPI and code:
9999
```text
100100
/api/v1/command
101101
/api/v1/procedure
102-
/api/v1/procedures
103102
/api/v1/logs
104103
/api/v1/metrics
105104
/api/v1/pstree

api/openapi.yaml

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,28 @@ components:
251251
additionalProperties:
252252
$ref: '#/components/schemas/RuntimeUIPackage'
253253

254-
CommandStatusMap:
254+
LockStatus:
255255
type: object
256-
additionalProperties: true
256+
required:
257+
- status
258+
- last_status_change
259+
properties:
260+
status:
261+
type: string
262+
enum: [running, done, error, waiting]
263+
exit_code:
264+
type: integer
265+
nullable: true
266+
last_status_change:
267+
type: integer
268+
format: int64
269+
270+
ProcedureStatusMap:
271+
type: object
272+
additionalProperties:
273+
type: object
274+
additionalProperties:
275+
$ref: '#/components/schemas/LockStatus'
257276

258277
ScrollLogMap:
259278
type: object
@@ -299,9 +318,8 @@ components:
299318
updated_at:
300319
type: string
301320
format: date-time
302-
commands:
303-
type: object
304-
additionalProperties: true
321+
procedures:
322+
$ref: '#/components/schemas/ProcedureStatusMap'
305323

306324
DeletedScroll:
307325
type: object
@@ -583,26 +601,7 @@ paths:
583601
content:
584602
application/json:
585603
schema:
586-
$ref: '#/components/schemas/CommandStatusMap'
587-
588-
/api/v1/scrolls/{id}/procedures:
589-
get:
590-
operationId: getScrollProcedures
591-
summary: Get procedure state
592-
tags: [runtime, daemon]
593-
parameters:
594-
- name: id
595-
in: path
596-
required: true
597-
schema:
598-
type: string
599-
responses:
600-
'200':
601-
description: Procedure state
602-
content:
603-
application/json:
604-
schema:
605-
$ref: '#/components/schemas/CommandStatusMap'
604+
$ref: '#/components/schemas/ProcedureStatusMap'
606605

607606
/api/v1/scrolls/{id}/consoles:
608607
get:

apps/druid/adapters/cli/client/command.go

Lines changed: 0 additions & 104 deletions
This file was deleted.

apps/druid/adapters/cli/client/procedure.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var ProcedureListCommand = &cobra.Command{
2828
if err != nil {
2929
return err
3030
}
31-
statuses, err := daemon.GetScrollProcedures(cmd.Context(), args[0])
31+
statuses, err := daemon.GetScrollQueue(cmd.Context(), args[0])
3232
if err != nil {
3333
return err
3434
}
@@ -70,7 +70,7 @@ type procedureRow struct {
7070
console string
7171
}
7272

73-
func procedureRows(file *domain.File, statuses map[string]domain.ScrollLockStatus, consoles map[string]domain.Console) []procedureRow {
73+
func procedureRows(file *domain.File, statuses domain.ProcedureStatusMap, consoles map[string]domain.Console) []procedureRow {
7474
if file == nil {
7575
return nil
7676
}
@@ -88,11 +88,7 @@ func procedureRows(file *domain.File, statuses map[string]domain.ScrollLockStatu
8888
}
8989
for idx, procedure := range definition.Procedures {
9090
name := domain.ProcedureName(command, idx, procedure)
91-
procedureStatusValue := ""
92-
if name != command {
93-
procedureStatusValue = string(statuses[name])
94-
}
95-
status := procedureStatus(name, procedureStatusValue, string(statuses[command]), consoles)
91+
status := procedureStatus(name, statuses[command][name], consoles)
9692
console := "no"
9793
if _, ok := consoles[name]; ok {
9894
console = "yes"
@@ -103,9 +99,9 @@ func procedureRows(file *domain.File, statuses map[string]domain.ScrollLockStatu
10399
return rows
104100
}
105101

106-
func procedureStatus(name string, status string, commandStatus string, consoles map[string]domain.Console) string {
107-
if status != "" {
108-
return status
102+
func procedureStatus(name string, status domain.LockStatus, consoles map[string]domain.Console) string {
103+
if status.Status != "" {
104+
return string(status.Status)
109105
}
110106
if console, ok := consoles[name]; ok {
111107
if console.Exit == nil {
@@ -116,12 +112,6 @@ func procedureStatus(name string, status string, commandStatus string, consoles
116112
}
117113
return string(domain.ScrollLockStatusError)
118114
}
119-
if commandStatus == string(domain.ScrollLockStatusRunning) {
120-
return string(domain.ScrollLockStatusWaiting)
121-
}
122-
if commandStatus != "" {
123-
return commandStatus
124-
}
125115
return "-"
126116
}
127117

apps/druid/adapters/cli/client/procedure_test.go

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ func TestProcedureRowsCombineConfigStatusAndConsoles(t *testing.T) {
1919
},
2020
},
2121
}}
22-
rows := procedureRows(file, map[string]domain.ScrollLockStatus{
23-
"start": domain.ScrollLockStatusWaiting,
24-
"coldstart": domain.ScrollLockStatusRunning,
22+
rows := procedureRows(file, domain.ProcedureStatusMap{
23+
"start": {
24+
"coldstart": {Status: domain.ScrollLockStatusRunning},
25+
"start.1": {Status: domain.ScrollLockStatusWaiting},
26+
},
2527
}, map[string]domain.Console{
2628
"coldstart": {},
2729
})
@@ -48,8 +50,10 @@ func TestProcedureRowsDoNotMarkEveryProcedureRunningFromCommandStatus(t *testing
4850
},
4951
},
5052
}}
51-
rows := procedureRows(file, map[string]domain.ScrollLockStatus{
52-
"start": domain.ScrollLockStatusRunning,
53+
rows := procedureRows(file, domain.ProcedureStatusMap{
54+
"start": {
55+
"start": {Status: domain.ScrollLockStatusWaiting},
56+
},
5357
}, map[string]domain.Console{
5458
"coldstart": {},
5559
})
@@ -62,38 +66,6 @@ func TestProcedureRowsDoNotMarkEveryProcedureRunningFromCommandStatus(t *testing
6266
}
6367
}
6468

65-
func TestCommandRunCallsDaemon(t *testing.T) {
66-
daemon := &fakeProcedureDaemon{}
67-
withClientConfig(t, Config{Daemon: func() (RuntimeDaemon, error) { return daemon, nil }})
68-
69-
if err := CommandRunCommand.RunE(&cobra.Command{}, []string{"scroll-a", "start"}); err != nil {
70-
t.Fatal(err)
71-
}
72-
if daemon.runScroll != "scroll-a" || daemon.runCommand != "start" {
73-
t.Fatalf("run scroll=%q command=%q", daemon.runScroll, daemon.runCommand)
74-
}
75-
}
76-
77-
func TestCommandRowsCombineConfigAndQueue(t *testing.T) {
78-
file := &domain.File{Commands: map[string]*domain.CommandInstructionSet{
79-
"install": {Run: domain.RunModeOnce, Procedures: []*domain.Procedure{{}}},
80-
"start": {Run: domain.RunModeRestart, Procedures: []*domain.Procedure{{}, {}}},
81-
}}
82-
rows := commandRows(file, map[string]domain.ScrollLockStatus{
83-
"start": domain.ScrollLockStatusWaiting,
84-
})
85-
86-
if len(rows) != 2 {
87-
t.Fatalf("rows = %#v", rows)
88-
}
89-
if rows[0] != (commandRow{command: "install", status: "-", runMode: "once", procedures: 1}) {
90-
t.Fatalf("row 0 = %#v", rows[0])
91-
}
92-
if rows[1] != (commandRow{command: "start", status: "waiting", runMode: "restart", procedures: 2}) {
93-
t.Fatalf("row 1 = %#v", rows[1])
94-
}
95-
}
96-
9769
func TestProcedureAttachRequiresActiveConsole(t *testing.T) {
9870
daemon := &fakeProcedureDaemon{consoles: map[string]domain.Console{"start": {}}}
9971
var attachedScroll, attachedConsole string
@@ -125,9 +97,7 @@ func withClientConfig(t *testing.T, cfg Config) {
12597
}
12698

12799
type fakeProcedureDaemon struct {
128-
runScroll string
129-
runCommand string
130-
consoles map[string]domain.Console
100+
consoles map[string]domain.Console
131101
}
132102

133103
func (f *fakeProcedureDaemon) CreateScroll(ctx context.Context, name string, artifact string, registryCredentials []api.RegistryCredential) (*api.RuntimeScroll, error) {
@@ -150,21 +120,11 @@ func (f *fakeProcedureDaemon) DeleteScroll(ctx context.Context, id string) (*api
150120
return nil, nil
151121
}
152122

153-
func (f *fakeProcedureDaemon) RunScrollCommand(ctx context.Context, id string, command string) (*api.RuntimeScroll, error) {
154-
f.runScroll = id
155-
f.runCommand = command
156-
return &api.RuntimeScroll{Id: id}, nil
157-
}
158-
159123
func (f *fakeProcedureDaemon) GetScrollConfig(ctx context.Context, id string) (*domain.File, error) {
160124
return &domain.File{}, nil
161125
}
162126

163-
func (f *fakeProcedureDaemon) GetScrollProcedures(ctx context.Context, id string) (map[string]domain.ScrollLockStatus, error) {
164-
return nil, nil
165-
}
166-
167-
func (f *fakeProcedureDaemon) GetScrollQueue(ctx context.Context, id string) (map[string]domain.ScrollLockStatus, error) {
127+
func (f *fakeProcedureDaemon) GetScrollQueue(ctx context.Context, id string) (domain.ProcedureStatusMap, error) {
168128
return nil, nil
169129
}
170130

apps/druid/adapters/cli/client/register.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ type RuntimeDaemon interface {
1515
ListScrolls(ctx context.Context) ([]api.RuntimeScroll, error)
1616
GetScroll(ctx context.Context, id string) (*api.RuntimeScroll, error)
1717
DeleteScroll(ctx context.Context, id string) (*api.DeletedScroll, error)
18-
RunScrollCommand(ctx context.Context, id string, command string) (*api.RuntimeScroll, error)
1918
GetScrollConfig(ctx context.Context, id string) (*domain.File, error)
20-
GetScrollQueue(ctx context.Context, id string) (map[string]domain.ScrollLockStatus, error)
21-
GetScrollProcedures(ctx context.Context, id string) (map[string]domain.ScrollLockStatus, error)
19+
GetScrollQueue(ctx context.Context, id string) (domain.ProcedureStatusMap, error)
2220
GetScrollConsoles(ctx context.Context, id string) (map[string]domain.Console, error)
2321
GetScrollPorts(ctx context.Context, id string) ([]api.RuntimePortStatus, error)
2422
StartScroll(ctx context.Context, id string) (*api.RuntimeScroll, error)
@@ -43,10 +41,8 @@ var config Config
4341
func Register(root *cobra.Command, cfg Config) {
4442
config = cfg
4543
RoutingCommand.AddCommand(RoutingTargetsCommand, RoutingApplyCommand)
46-
CommandCommand.AddCommand(CommandRunCommand, CommandListCommand)
4744
ProcedureCommand.AddCommand(ProcedureListCommand, ProcedureAttachCommand)
4845
root.AddCommand(
49-
CommandCommand,
5046
CreateCommand,
5147
DeleteCommand,
5248
DescribeCommand,

0 commit comments

Comments
 (0)