Skip to content

Commit d29fcb2

Browse files
committed
add context as parameter to functions in DB interface
Signed-off-by: Jacob Lisi <[email protected]>
1 parent d0f8944 commit d29fcb2

File tree

13 files changed

+164
-137
lines changed

13 files changed

+164
-137
lines changed

pkg/alertmanager/multitenant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (am *MultitenantAlertmanager) updateConfigs(now time.Time) error {
351351
// poll the configuration server. Not re-entrant.
352352
func (am *MultitenantAlertmanager) poll() (map[string]configs.View, error) {
353353
configID := am.latestConfig
354-
cfgs, err := am.configsAPI.GetAlerts(configID)
354+
cfgs, err := am.configsAPI.GetAlerts(context.Background(), configID)
355355
if err != nil {
356356
level.Warn(util.Logger).Log("msg", "MultitenantAlertmanager: configs server poll failed", "err", err)
357357
return nil, err

pkg/configs/api/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (a *API) getConfig(w http.ResponseWriter, r *http.Request) {
8282
}
8383
logger := util.WithContext(r.Context(), util.Logger)
8484

85-
cfg, err := a.db.GetConfig(userID)
85+
cfg, err := a.db.GetConfig(r.Context(), userID)
8686
if err == sql.ErrNoRows {
8787
http.Error(w, "No configuration", http.StatusNotFound)
8888
return
@@ -132,7 +132,7 @@ func (a *API) setConfig(w http.ResponseWriter, r *http.Request) {
132132
http.Error(w, fmt.Sprintf("Invalid templates: %v", err), http.StatusBadRequest)
133133
return
134134
}
135-
if err := a.db.SetConfig(userID, cfg); err != nil {
135+
if err := a.db.SetConfig(r.Context(), userID, cfg); err != nil {
136136
// XXX: Untested
137137
level.Error(logger).Log("msg", "error storing config", "err", err)
138138
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -206,15 +206,15 @@ func (a *API) getConfigs(w http.ResponseWriter, r *http.Request) {
206206
logger := util.WithContext(r.Context(), util.Logger)
207207
rawSince := r.FormValue("since")
208208
if rawSince == "" {
209-
cfgs, cfgErr = a.db.GetAllConfigs()
209+
cfgs, cfgErr = a.db.GetAllConfigs(r.Context())
210210
} else {
211211
since, err := strconv.ParseUint(rawSince, 10, 0)
212212
if err != nil {
213213
level.Info(logger).Log("msg", "invalid config ID", "err", err)
214214
http.Error(w, err.Error(), http.StatusBadRequest)
215215
return
216216
}
217-
cfgs, cfgErr = a.db.GetConfigs(configs.ID(since))
217+
cfgs, cfgErr = a.db.GetConfigs(r.Context(), configs.ID(since))
218218
}
219219

220220
if cfgErr != nil {
@@ -241,7 +241,7 @@ func (a *API) deactivateConfig(w http.ResponseWriter, r *http.Request) {
241241
}
242242
logger := util.WithContext(r.Context(), util.Logger)
243243

244-
if err := a.db.DeactivateConfig(userID); err != nil {
244+
if err := a.db.DeactivateConfig(r.Context(), userID); err != nil {
245245
if err == sql.ErrNoRows {
246246
level.Info(logger).Log("msg", "deactivate config - no configuration", "userID", userID)
247247
http.Error(w, "No configuration", http.StatusNotFound)
@@ -263,7 +263,7 @@ func (a *API) restoreConfig(w http.ResponseWriter, r *http.Request) {
263263
}
264264
logger := util.WithContext(r.Context(), util.Logger)
265265

266-
if err := a.db.RestoreConfig(userID); err != nil {
266+
if err := a.db.RestoreConfig(r.Context(), userID); err != nil {
267267
if err == sql.ErrNoRows {
268268
level.Info(logger).Log("msg", "restore config - no configuration", "userID", userID)
269269
http.Error(w, "No configuration", http.StatusNotFound)

pkg/configs/client/client.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -17,10 +18,10 @@ import (
1718
type Client interface {
1819
// GetRules returns all Cortex configurations from a configs API server
1920
// that have been updated after the given configs.ID was last updated.
20-
GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error)
21+
GetRules(ctx context.Context, since configs.ID) (map[string]configs.VersionedRulesConfig, error)
2122

2223
// GetAlerts fetches all the alerts that have changes since since.
23-
GetAlerts(since configs.ID) (*ConfigsResponse, error)
24+
GetAlerts(ctx context.Context, since configs.ID) (*ConfigsResponse, error)
2425
}
2526

2627
// New creates a new ConfigClient.
@@ -55,7 +56,7 @@ type configsClient struct {
5556
}
5657

5758
// GetRules implements ConfigClient.
58-
func (c configsClient) GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
59+
func (c configsClient) GetRules(ctx context.Context, since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
5960
suffix := ""
6061
if since != 0 {
6162
suffix = fmt.Sprintf("?since=%d", since)
@@ -76,7 +77,7 @@ func (c configsClient) GetRules(since configs.ID) (map[string]configs.VersionedR
7677
}
7778

7879
// GetAlerts implements ConfigClient.
79-
func (c configsClient) GetAlerts(since configs.ID) (*ConfigsResponse, error) {
80+
func (c configsClient) GetAlerts(ctx context.Context, since configs.ID) (*ConfigsResponse, error) {
8081
suffix := ""
8182
if since != 0 {
8283
suffix = fmt.Sprintf("?since=%d", since)
@@ -117,22 +118,22 @@ type dbStore struct {
117118
}
118119

119120
// GetRules implements ConfigClient.
120-
func (d dbStore) GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
121+
func (d dbStore) GetRules(ctx context.Context, since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
121122
if since == 0 {
122-
return d.db.GetAllRulesConfigs()
123+
return d.db.GetAllRulesConfigs(ctx)
123124
}
124-
return d.db.GetRulesConfigs(since)
125+
return d.db.GetRulesConfigs(ctx, since)
125126
}
126127

127128
// GetAlerts implements ConfigClient.
128-
func (d dbStore) GetAlerts(since configs.ID) (*ConfigsResponse, error) {
129+
func (d dbStore) GetAlerts(ctx context.Context, since configs.ID) (*ConfigsResponse, error) {
129130
var resp map[string]configs.View
130131
var err error
131132
if since == 0 {
132-
resp, err = d.db.GetAllConfigs()
133+
resp, err = d.db.GetAllConfigs(ctx)
133134

134135
}
135-
resp, err = d.db.GetConfigs(since)
136+
resp, err = d.db.GetConfigs(ctx, since)
136137
if err != nil {
137138
return nil, err
138139
}

pkg/configs/client/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ type instrumented struct {
4949
next Client
5050
}
5151

52-
func (i instrumented) GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
52+
func (i instrumented) GetRules(ctx context.Context, since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
5353
var cfgs map[string]configs.VersionedRulesConfig
5454
err := instrument.CollectedRequest(context.Background(), "Configs.GetConfigs", configsRequestDuration, instrument.ErrorCode, func(_ context.Context) error {
5555
var err error
56-
cfgs, err = i.next.GetRules(since) // Warning: this will produce an incorrect result if the configID ever overflows
56+
cfgs, err = i.next.GetRules(ctx, since) // Warning: this will produce an incorrect result if the configID ever overflows
5757
return err
5858
})
5959
return cfgs, err
6060
}
6161

62-
func (i instrumented) GetAlerts(since configs.ID) (*ConfigsResponse, error) {
62+
func (i instrumented) GetAlerts(ctx context.Context, since configs.ID) (*ConfigsResponse, error) {
6363
var cfgs *ConfigsResponse
6464
err := instrument.CollectedRequest(context.Background(), "Configs.GetConfigs", configsRequestDuration, instrument.ErrorCode, func(_ context.Context) error {
6565
var err error
66-
cfgs, err = i.next.GetAlerts(since) // Warning: this will produce an incorrect result if the configID ever overflows
66+
cfgs, err = i.next.GetAlerts(ctx, since) // Warning: this will produce an incorrect result if the configID ever overflows
6767
return err
6868
})
6969
return cfgs, err

pkg/configs/db/db.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package db
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"io/ioutil"
@@ -31,28 +32,28 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
3132
// DB is the interface for the database.
3233
type DB interface {
3334
// GetRulesConfig gets the user's ruler config
34-
GetRulesConfig(userID string) (configs.VersionedRulesConfig, error)
35+
GetRulesConfig(ctx context.Context, userID string) (configs.VersionedRulesConfig, error)
3536

3637
// SetRulesConfig does a compare-and-swap (CAS) on the user's rules config.
3738
// `oldConfig` must precisely match the current config in order to change the config to `newConfig`.
3839
// Will return `true` if the config was updated, `false` otherwise.
39-
SetRulesConfig(userID string, oldConfig, newConfig configs.RulesConfig) (bool, error)
40+
SetRulesConfig(ctx context.Context, userID string, oldConfig, newConfig configs.RulesConfig) (bool, error)
4041

4142
// GetAllRulesConfigs gets all of the ruler configs
42-
GetAllRulesConfigs() (map[string]configs.VersionedRulesConfig, error)
43+
GetAllRulesConfigs(ctx context.Context) (map[string]configs.VersionedRulesConfig, error)
4344

4445
// GetRulesConfigs gets all of the configs that have been added or have
4546
// changed since the provided config.
46-
GetRulesConfigs(since configs.ID) (map[string]configs.VersionedRulesConfig, error)
47+
GetRulesConfigs(ctx context.Context, since configs.ID) (map[string]configs.VersionedRulesConfig, error)
4748

48-
GetConfig(userID string) (configs.View, error)
49-
SetConfig(userID string, cfg configs.Config) error
49+
GetConfig(ctx context.Context, userID string) (configs.View, error)
50+
SetConfig(ctx context.Context, userID string, cfg configs.Config) error
5051

51-
GetAllConfigs() (map[string]configs.View, error)
52-
GetConfigs(since configs.ID) (map[string]configs.View, error)
52+
GetAllConfigs(ctx context.Context) (map[string]configs.View, error)
53+
GetConfigs(ctx context.Context, since configs.ID) (map[string]configs.View, error)
5354

54-
DeactivateConfig(userID string) error
55-
RestoreConfig(userID string) error
55+
DeactivateConfig(ctx context.Context, userID string) error
56+
RestoreConfig(ctx context.Context, userID string) error
5657

5758
Close() error
5859
}

pkg/configs/db/memory/memory.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memory
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"time"
@@ -23,7 +24,7 @@ func New(_, _ string) (*DB, error) {
2324
}
2425

2526
// GetConfig gets the user's configuration.
26-
func (d *DB) GetConfig(userID string) (configs.View, error) {
27+
func (d *DB) GetConfig(ctx context.Context, userID string) (configs.View, error) {
2728
c, ok := d.cfgs[userID]
2829
if !ok {
2930
return configs.View{}, sql.ErrNoRows
@@ -32,7 +33,7 @@ func (d *DB) GetConfig(userID string) (configs.View, error) {
3233
}
3334

3435
// SetConfig sets configuration for a user.
35-
func (d *DB) SetConfig(userID string, cfg configs.Config) error {
36+
func (d *DB) SetConfig(ctx context.Context, userID string, cfg configs.Config) error {
3637
if !cfg.RulesConfig.FormatVersion.IsValid() {
3738
return fmt.Errorf("invalid rule format version %v", cfg.RulesConfig.FormatVersion)
3839
}
@@ -42,12 +43,12 @@ func (d *DB) SetConfig(userID string, cfg configs.Config) error {
4243
}
4344

4445
// GetAllConfigs gets all of the configs.
45-
func (d *DB) GetAllConfigs() (map[string]configs.View, error) {
46+
func (d *DB) GetAllConfigs(ctx context.Context) (map[string]configs.View, error) {
4647
return d.cfgs, nil
4748
}
4849

4950
// GetConfigs gets all of the configs that have changed recently.
50-
func (d *DB) GetConfigs(since configs.ID) (map[string]configs.View, error) {
51+
func (d *DB) GetConfigs(ctx context.Context, since configs.ID) (map[string]configs.View, error) {
5152
cfgs := map[string]configs.View{}
5253
for user, c := range d.cfgs {
5354
if c.ID > since {
@@ -60,8 +61,8 @@ func (d *DB) GetConfigs(since configs.ID) (map[string]configs.View, error) {
6061
// SetDeletedAtConfig sets a deletedAt for configuration
6162
// by adding a single new row with deleted_at set
6263
// the same as SetConfig is actually insert
63-
func (d *DB) SetDeletedAtConfig(userID string, deletedAt time.Time) error {
64-
cv, err := d.GetConfig(userID)
64+
func (d *DB) SetDeletedAtConfig(ctx context.Context, userID string, deletedAt time.Time) error {
65+
cv, err := d.GetConfig(ctx, userID)
6566
if err != nil {
6667
return err
6768
}
@@ -73,13 +74,13 @@ func (d *DB) SetDeletedAtConfig(userID string, deletedAt time.Time) error {
7374
}
7475

7576
// DeactivateConfig deactivates configuration for a user by creating new configuration with DeletedAt set to now
76-
func (d *DB) DeactivateConfig(userID string) error {
77-
return d.SetDeletedAtConfig(userID, time.Now())
77+
func (d *DB) DeactivateConfig(ctx context.Context, userID string) error {
78+
return d.SetDeletedAtConfig(ctx, userID, time.Now())
7879
}
7980

8081
// RestoreConfig restores deactivated configuration for a user by creating new configuration with empty DeletedAt
81-
func (d *DB) RestoreConfig(userID string) error {
82-
return d.SetDeletedAtConfig(userID, time.Time{})
82+
func (d *DB) RestoreConfig(ctx context.Context, userID string) error {
83+
return d.SetDeletedAtConfig(ctx, userID, time.Time{})
8384
}
8485

8586
// Close finishes using the db. Noop.
@@ -88,7 +89,7 @@ func (d *DB) Close() error {
8889
}
8990

9091
// GetRulesConfig gets the rules config for a user.
91-
func (d *DB) GetRulesConfig(userID string) (configs.VersionedRulesConfig, error) {
92+
func (d *DB) GetRulesConfig(ctx context.Context, userID string) (configs.VersionedRulesConfig, error) {
9293
c, ok := d.cfgs[userID]
9394
if !ok {
9495
return configs.VersionedRulesConfig{}, sql.ErrNoRows
@@ -101,22 +102,22 @@ func (d *DB) GetRulesConfig(userID string) (configs.VersionedRulesConfig, error)
101102
}
102103

103104
// SetRulesConfig sets the rules config for a user.
104-
func (d *DB) SetRulesConfig(userID string, oldConfig, newConfig configs.RulesConfig) (bool, error) {
105+
func (d *DB) SetRulesConfig(ctx context.Context, userID string, oldConfig, newConfig configs.RulesConfig) (bool, error) {
105106
c, ok := d.cfgs[userID]
106107
if !ok {
107-
return true, d.SetConfig(userID, configs.Config{RulesConfig: newConfig})
108+
return true, d.SetConfig(ctx, userID, configs.Config{RulesConfig: newConfig})
108109
}
109110
if !oldConfig.Equal(c.Config.RulesConfig) {
110111
return false, nil
111112
}
112-
return true, d.SetConfig(userID, configs.Config{
113+
return true, d.SetConfig(ctx, userID, configs.Config{
113114
AlertmanagerConfig: c.Config.AlertmanagerConfig,
114115
RulesConfig: newConfig,
115116
})
116117
}
117118

118119
// GetAllRulesConfigs gets the rules configs for all users that have them.
119-
func (d *DB) GetAllRulesConfigs() (map[string]configs.VersionedRulesConfig, error) {
120+
func (d *DB) GetAllRulesConfigs(ctx context.Context) (map[string]configs.VersionedRulesConfig, error) {
120121
cfgs := map[string]configs.VersionedRulesConfig{}
121122
for user, c := range d.cfgs {
122123
cfg := c.GetVersionedRulesConfig()
@@ -129,7 +130,7 @@ func (d *DB) GetAllRulesConfigs() (map[string]configs.VersionedRulesConfig, erro
129130

130131
// GetRulesConfigs gets the rules configs that have changed
131132
// since the given config version.
132-
func (d *DB) GetRulesConfigs(since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
133+
func (d *DB) GetRulesConfigs(ctx context.Context, since configs.ID) (map[string]configs.VersionedRulesConfig, error) {
133134
cfgs := map[string]configs.VersionedRulesConfig{}
134135
for user, c := range d.cfgs {
135136
if c.ID <= since {

0 commit comments

Comments
 (0)