Skip to content

Commit 2ccccb6

Browse files
irenarindoshugoghx
authored andcommitted
refact(server): create new list and lookup worker queries
1 parent 7c9fabb commit 2ccccb6

File tree

12 files changed

+269
-571
lines changed

12 files changed

+269
-571
lines changed

internal/daemon/controller/handlers/workers/worker_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ func (s Service) toProto(ctx context.Context, in *server.Worker, opt ...handlers
900900
out.LastStatusTime = in.GetLastStatusTime().GetTimestamp()
901901
}
902902
if outputFields.Has(globals.ActiveConnectionCountField) {
903-
out.ActiveConnectionCount = &wrapperspb.UInt32Value{Value: in.ActiveConnectionCount()}
903+
out.ActiveConnectionCount = &wrapperspb.UInt32Value{Value: in.ActiveConnectionCount}
904904
}
905905
if outputFields.Has(globals.ControllerGeneratedActivationToken) && in.ControllerGeneratedActivationToken != "" {
906906
out.ControllerGeneratedActivationToken = &wrapperspb.StringValue{Value: in.ControllerGeneratedActivationToken}

internal/db/schema/migrations/oss/postgres/86/01_server_worker_local_storage_state.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ alter table server_worker
3636
on update cascade;
3737

3838
drop view server_worker_aggregate;
39-
-- Updated in 99/01_split_worker_tag_tables.up.sql
39+
-- Removed in 94/01_split_worker_tag_tables.up.sql
4040
-- Replaces view created in 52/01_worker_operational_state.up.sql to add the worker local storage state
4141
create view server_worker_aggregate as
4242
with worker_config_tags(worker_id, source, tags) as (
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- Copyright (c) HashiCorp, Inc.
2+
-- SPDX-License-Identifier: BUSL-1.1
3+
4+
begin;
5+
6+
-- Create new tables for worker config and worker api tags
7+
create table server_worker_config_tag(
8+
worker_id wt_public_id
9+
constraint server_worker_fkey
10+
references server_worker (public_id)
11+
on delete cascade
12+
on update cascade,
13+
key wt_tagpair,
14+
value wt_tagpair,
15+
primary key (worker_id, key, value)
16+
);
17+
comment on table server_worker_config_tag is
18+
'server_worker_config_tag is a table where each row represents a worker config tag.';
19+
20+
create table server_worker_api_tag(
21+
worker_id wt_public_id
22+
constraint server_worker_fkey
23+
references server_worker (public_id)
24+
on delete cascade
25+
on update cascade,
26+
key wt_tagpair,
27+
value wt_tagpair,
28+
primary key (worker_id, key, value)
29+
);
30+
comment on table server_worker_api_tag is
31+
'server_worker_api_tag is a table where each row represents a worker api tag.';
32+
33+
-- Migrate from server_worker_tag to the new tables
34+
insert into server_worker_config_tag
35+
(worker_id, key, value)
36+
select worker_id, key, value
37+
from server_worker_tag
38+
where source = 'configuration';
39+
40+
insert into server_worker_api_tag
41+
(worker_id, key, value)
42+
select worker_id, key, value
43+
from server_worker_tag
44+
where source = 'api';
45+
46+
-- Removes view created in 86/01_server_worker_local_storage_state.up.sql
47+
-- This view is removed in favor of custom sql in query.go
48+
drop view server_worker_aggregate;
49+
50+
-- Drop the old tables
51+
drop table server_worker_tag;
52+
drop table server_worker_tag_enm;
53+
54+
commit;

internal/db/schema/migrations/oss/postgres/99/01_split_worker_tag_table.up.sql

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

internal/db/schema/migrations/oss/postgres_99_01_test.go renamed to internal/db/schema/migrations/oss/postgres_94_01_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Test_WorkerTagTableSplit(t *testing.T) {
2929
t.Parallel()
3030
require := require.New(t)
3131
const priorMigration = 92001
32-
const serverEnumMigration = 99001
32+
const serverEnumMigration = 94001
3333
dialect := dbtest.Postgres
3434
ctx := context.Background()
3535

internal/server/query.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,82 @@
44
package server
55

66
const (
7+
listWorkersQuery = `
8+
with connection_count (worker_id, count) as (
9+
select worker_id,
10+
count(1) as count
11+
from session_connection
12+
where closed_reason is null
13+
group by worker_id
14+
)
15+
select w.public_id,
16+
w.scope_id,
17+
w.description,
18+
w.name,
19+
w.address,
20+
w.create_time,
21+
w.update_time,
22+
w.version,
23+
w.last_status_time,
24+
w.type,
25+
w.release_version,
26+
w.operational_state,
27+
w.local_storage_state,
28+
cc.count as active_connection_count,
29+
wt.tags as api_tags,
30+
ct.tags as config_tags
31+
from server_worker w
32+
left join ( select worker_id,
33+
json_agg(json_build_object('key', key, 'value', value)) as tags
34+
from server_worker_api_tag
35+
group by worker_id) wt
36+
on w.public_id = wt.worker_id
37+
left join ( select worker_id,
38+
json_agg(json_build_object('key', key, 'value', value)) as tags
39+
from server_worker_config_tag group by worker_id) ct
40+
on w.public_id = ct.worker_id
41+
left join connection_count as cc
42+
on w.public_id = cc.worker_id
43+
`
44+
45+
lookupWorkerQuery = `
46+
with connection_count (worker_id, count) as (
47+
select worker_id,
48+
count(1) as count
49+
from session_connection
50+
where closed_reason is null
51+
group by worker_id
52+
)
53+
select w.public_id,
54+
w.scope_id,
55+
w.description,
56+
w.name,
57+
w.address,
58+
w.create_time,
59+
w.update_time,
60+
w.version,
61+
w.last_status_time,
62+
w.type,
63+
w.release_version,
64+
w.operational_state,
65+
w.local_storage_state,
66+
cc.count as active_connection_count,
67+
wt.tags as api_tags,
68+
ct.tags as config_tags
69+
from server_worker w
70+
left join ( select worker_id,
71+
json_agg(json_build_object('key', key, 'value', value)) as tags
72+
from server_worker_api_tag
73+
group by worker_id) wt
74+
on w.public_id = wt.worker_id
75+
left join ( select worker_id,
76+
json_agg(json_build_object('key', key, 'value', value)) as tags
77+
from server_worker_config_tag group by worker_id) ct
78+
on w.public_id = ct.worker_id
79+
left join connection_count as cc
80+
on w.public_id = cc.worker_id
81+
where w.public_id = @worker_id
82+
`
783
getStorageBucketCredentialStatesByWorkerId = `
884
select spsb.public_id as storage_bucket_id,
985
wsbcs.permission_type, wsbcs.state,

0 commit comments

Comments
 (0)