Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6dd51f9

Browse files
chore: test metricscache on postgres (#16711)
metricscache_test has been running tests against dbmem only, instead of against postgres. Unfortunately the implementations of GetTemplateAverageBuildTime have diverged between dbmem and postgres. This change gets the tests working on Postgres and test for the behaviour postgres provides.
1 parent 95363c9 commit 6dd51f9

File tree

6 files changed

+126
-96
lines changed

6 files changed

+126
-96
lines changed

coderd/coderd.go

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ func New(options *Options) *API {
422422
metricsCache := metricscache.New(
423423
options.Database,
424424
options.Logger.Named("metrics_cache"),
425+
options.Clock,
425426
metricscache.Intervals{
426427
TemplateBuildTimes: options.MetricsCacheRefreshInterval,
427428
DeploymentStats: options.AgentStatsRefreshInterval,

coderd/database/dbmem/dbmem.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ type data struct {
269269
presetParameters []database.TemplateVersionPresetParameter
270270
}
271271

272-
func tryPercentile(fs []float64, p float64) float64 {
272+
func tryPercentileCont(fs []float64, p float64) float64 {
273273
if len(fs) == 0 {
274274
return -1
275275
}
@@ -282,6 +282,14 @@ func tryPercentile(fs []float64, p float64) float64 {
282282
return fs[lower] + (fs[upper]-fs[lower])*(pos-float64(lower))
283283
}
284284

285+
func tryPercentileDisc(fs []float64, p float64) float64 {
286+
if len(fs) == 0 {
287+
return -1
288+
}
289+
sort.Float64s(fs)
290+
return fs[max(int(math.Ceil(float64(len(fs))*p/100-1)), 0)]
291+
}
292+
285293
func validateDatabaseTypeWithValid(v reflect.Value) (handled bool, err error) {
286294
if v.Kind() == reflect.Struct {
287295
return false, nil
@@ -2790,8 +2798,8 @@ func (q *FakeQuerier) GetDeploymentWorkspaceAgentStats(_ context.Context, create
27902798
latencies = append(latencies, agentStat.ConnectionMedianLatencyMS)
27912799
}
27922800

2793-
stat.WorkspaceConnectionLatency50 = tryPercentile(latencies, 50)
2794-
stat.WorkspaceConnectionLatency95 = tryPercentile(latencies, 95)
2801+
stat.WorkspaceConnectionLatency50 = tryPercentileCont(latencies, 50)
2802+
stat.WorkspaceConnectionLatency95 = tryPercentileCont(latencies, 95)
27952803

27962804
return stat, nil
27972805
}
@@ -2839,8 +2847,8 @@ func (q *FakeQuerier) GetDeploymentWorkspaceAgentUsageStats(_ context.Context, c
28392847
stat.WorkspaceTxBytes += agentStat.TxBytes
28402848
latencies = append(latencies, agentStat.ConnectionMedianLatencyMS)
28412849
}
2842-
stat.WorkspaceConnectionLatency50 = tryPercentile(latencies, 50)
2843-
stat.WorkspaceConnectionLatency95 = tryPercentile(latencies, 95)
2850+
stat.WorkspaceConnectionLatency50 = tryPercentileCont(latencies, 50)
2851+
stat.WorkspaceConnectionLatency95 = tryPercentileCont(latencies, 95)
28442852

28452853
for _, agentStat := range sessions {
28462854
stat.SessionCountVSCode += agentStat.SessionCountVSCode
@@ -4987,9 +4995,9 @@ func (q *FakeQuerier) GetTemplateAverageBuildTime(ctx context.Context, arg datab
49874995
}
49884996

49894997
var row database.GetTemplateAverageBuildTimeRow
4990-
row.Delete50, row.Delete95 = tryPercentile(deleteTimes, 50), tryPercentile(deleteTimes, 95)
4991-
row.Stop50, row.Stop95 = tryPercentile(stopTimes, 50), tryPercentile(stopTimes, 95)
4992-
row.Start50, row.Start95 = tryPercentile(startTimes, 50), tryPercentile(startTimes, 95)
4998+
row.Delete50, row.Delete95 = tryPercentileDisc(deleteTimes, 50), tryPercentileDisc(deleteTimes, 95)
4999+
row.Stop50, row.Stop95 = tryPercentileDisc(stopTimes, 50), tryPercentileDisc(stopTimes, 95)
5000+
row.Start50, row.Start95 = tryPercentileDisc(startTimes, 50), tryPercentileDisc(startTimes, 95)
49935001
return row, nil
49945002
}
49955003

@@ -6024,8 +6032,8 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
60246032
Username: user.Username,
60256033
AvatarURL: user.AvatarURL,
60266034
TemplateIDs: seenTemplatesByUserID[userID],
6027-
WorkspaceConnectionLatency50: tryPercentile(latencies, 50),
6028-
WorkspaceConnectionLatency95: tryPercentile(latencies, 95),
6035+
WorkspaceConnectionLatency50: tryPercentileCont(latencies, 50),
6036+
WorkspaceConnectionLatency95: tryPercentileCont(latencies, 95),
60296037
}
60306038
rows = append(rows, row)
60316039
}
@@ -6669,8 +6677,8 @@ func (q *FakeQuerier) GetWorkspaceAgentStats(_ context.Context, createdAfter tim
66696677
if !ok {
66706678
continue
66716679
}
6672-
stat.WorkspaceConnectionLatency50 = tryPercentile(latencies, 50)
6673-
stat.WorkspaceConnectionLatency95 = tryPercentile(latencies, 95)
6680+
stat.WorkspaceConnectionLatency50 = tryPercentileCont(latencies, 50)
6681+
stat.WorkspaceConnectionLatency95 = tryPercentileCont(latencies, 95)
66746682
statByAgent[stat.AgentID] = stat
66756683
}
66766684

@@ -6807,8 +6815,8 @@ func (q *FakeQuerier) GetWorkspaceAgentUsageStats(_ context.Context, createdAt t
68076815
for key, latencies := range latestAgentLatencies {
68086816
val, ok := latestAgentStats[key]
68096817
if ok {
6810-
val.WorkspaceConnectionLatency50 = tryPercentile(latencies, 50)
6811-
val.WorkspaceConnectionLatency95 = tryPercentile(latencies, 95)
6818+
val.WorkspaceConnectionLatency50 = tryPercentileCont(latencies, 50)
6819+
val.WorkspaceConnectionLatency95 = tryPercentileCont(latencies, 95)
68126820
}
68136821
latestAgentStats[key] = val
68146822
}

coderd/database/queries.sql.go

+5-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

+5-7
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,11 @@ WHERE
415415
ORDER BY created_at DESC;
416416

417417
-- name: GetWorkspaceUniqueOwnerCountByTemplateIDs :many
418-
SELECT
419-
template_id, COUNT(DISTINCT owner_id) AS unique_owners_sum
420-
FROM
421-
workspaces
422-
WHERE
423-
template_id = ANY(@template_ids :: uuid[]) AND deleted = false
424-
GROUP BY template_id;
418+
SELECT templates.id AS template_id, COUNT(DISTINCT workspaces.owner_id) AS unique_owners_sum
419+
FROM templates
420+
LEFT JOIN workspaces ON workspaces.template_id = templates.id AND workspaces.deleted = false
421+
WHERE templates.id = ANY(@template_ids :: uuid[])
422+
GROUP BY templates.id;
425423

426424
-- name: InsertWorkspace :one
427425
INSERT INTO

coderd/metricscache/metricscache.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/coder/coder/v2/coderd/database/dbauthz"
1616
"github.com/coder/coder/v2/coderd/database/dbtime"
1717
"github.com/coder/coder/v2/codersdk"
18+
"github.com/coder/quartz"
1819
"github.com/coder/retry"
1920
)
2021

@@ -26,6 +27,7 @@ import (
2627
type Cache struct {
2728
database database.Store
2829
log slog.Logger
30+
clock quartz.Clock
2931
intervals Intervals
3032

3133
templateWorkspaceOwners atomic.Pointer[map[uuid.UUID]int]
@@ -45,7 +47,7 @@ type Intervals struct {
4547
DeploymentStats time.Duration
4648
}
4749

48-
func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *Cache {
50+
func New(db database.Store, log slog.Logger, clock quartz.Clock, intervals Intervals, usage bool) *Cache {
4951
if intervals.TemplateBuildTimes <= 0 {
5052
intervals.TemplateBuildTimes = time.Hour
5153
}
@@ -55,6 +57,7 @@ func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *C
5557
ctx, cancel := context.WithCancel(context.Background())
5658

5759
c := &Cache{
60+
clock: clock,
5861
database: db,
5962
intervals: intervals,
6063
log: log,
@@ -104,7 +107,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error {
104107
Valid: true,
105108
},
106109
StartTime: sql.NullTime{
107-
Time: dbtime.Time(time.Now().AddDate(0, 0, -30)),
110+
Time: dbtime.Time(c.clock.Now().AddDate(0, 0, -30)),
108111
Valid: true,
109112
},
110113
})
@@ -131,7 +134,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error {
131134

132135
func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
133136
var (
134-
from = dbtime.Now().Add(-15 * time.Minute)
137+
from = c.clock.Now().Add(-15 * time.Minute)
135138
agentStats database.GetDeploymentWorkspaceAgentStatsRow
136139
err error
137140
)
@@ -155,8 +158,8 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
155158
}
156159
c.deploymentStatsResponse.Store(&codersdk.DeploymentStats{
157160
AggregatedFrom: from,
158-
CollectedAt: dbtime.Now(),
159-
NextUpdateAt: dbtime.Now().Add(c.intervals.DeploymentStats),
161+
CollectedAt: dbtime.Time(c.clock.Now()),
162+
NextUpdateAt: dbtime.Time(c.clock.Now().Add(c.intervals.DeploymentStats)),
160163
Workspaces: codersdk.WorkspaceDeploymentStats{
161164
Pending: workspaceStats.PendingWorkspaces,
162165
Building: workspaceStats.BuildingWorkspaces,

0 commit comments

Comments
 (0)