-
Notifications
You must be signed in to change notification settings - Fork 311
/
backend-config.go
418 lines (367 loc) · 12.7 KB
/
backend-config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package backendconfig
// go:generate mockgen -destination=../mocks/backend-config/mock_backendconfig.go -package=mock_backendconfig github.com/rudderlabs/rudder-server/backend-config BackendConfig
// go:generate mockgen -destination=./mock_workspaceconfig.go -package=backendconfig -source=./backend-config.go workspaceConfig
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net/url"
"reflect"
"sort"
"sync"
"time"
"github.com/rudderlabs/rudder-go-kit/config"
"github.com/rudderlabs/rudder-go-kit/logger"
"github.com/rudderlabs/rudder-go-kit/stats"
"github.com/rudderlabs/rudder-server/backend-config/internal/cache"
"github.com/rudderlabs/rudder-server/rruntime"
"github.com/rudderlabs/rudder-server/services/controlplane/identity"
"github.com/rudderlabs/rudder-server/services/diagnostics"
"github.com/rudderlabs/rudder-server/utils/pubsub"
"github.com/rudderlabs/rudder-server/utils/sysUtils"
"github.com/rudderlabs/rudder-server/utils/types"
"github.com/rudderlabs/rudder-server/utils/types/deployment"
)
var (
// environment variables
configBackendURL string
cpRouterURL string
pollInterval, regulationsPollInterval time.Duration
configJSONPath string
configFromFile bool
maxRegulationsPerRequest int
configEnvReplacementEnabled bool
dbCacheEnabled bool
LastSync string
LastRegulationSync string
// DefaultBackendConfig will be initialized be Setup to either a WorkspaceConfig or MultiWorkspaceConfig.
DefaultBackendConfig BackendConfig
pkgLogger = logger.NewLogger().Child("backend-config")
IoUtil = sysUtils.NewIoUtil()
Diagnostics diagnostics.DiagnosticsI
cacheOverride cache.Cache
incrementalConfigUpdates bool
)
func disableCache() {
cacheOverride = new(noCache)
}
type noCache struct{}
func (*noCache) Get(context.Context) ([]byte, error) {
return nil, fmt.Errorf(`noCache: cache disabled`)
}
type workspaceConfig interface {
SetUp() error
// Deprecated: use Identity() instead.
AccessToken() string
Get(context.Context) (map[string]ConfigT, error)
Identity() identity.Identifier
}
type BackendConfig interface {
workspaceConfig
WaitForConfig(ctx context.Context)
Subscribe(ctx context.Context, topic Topic) pubsub.DataChannel
Stop()
StartWithIDs(ctx context.Context, workspaces string)
}
type backendConfigImpl struct {
workspaceConfig
eb *pubsub.PublishSubscriber
ctx context.Context
cancel context.CancelFunc
blockChan chan struct{}
initializedLock sync.RWMutex
initialized bool
curSourceJSON map[string]ConfigT
curSourceJSONLock sync.RWMutex
usingCache bool
cache cache.Cache
}
func loadConfig() {
configBackendURL = config.GetString("CONFIG_BACKEND_URL", "https://api.rudderstack.com")
cpRouterURL = config.GetString("CP_ROUTER_URL", "https://cp-router.rudderlabs.com")
config.RegisterDurationConfigVariable(5, &pollInterval, true, time.Second, []string{"BackendConfig.pollInterval", "BackendConfig.pollIntervalInS"}...)
config.RegisterDurationConfigVariable(300, ®ulationsPollInterval, true, time.Second, []string{"BackendConfig.regulationsPollInterval", "BackendConfig.regulationsPollIntervalInS"}...)
config.RegisterStringConfigVariable("/etc/rudderstack/workspaceConfig.json", &configJSONPath, false, "BackendConfig.configJSONPath")
config.RegisterBoolConfigVariable(false, &configFromFile, false, "BackendConfig.configFromFile")
config.RegisterIntConfigVariable(1000, &maxRegulationsPerRequest, true, 1, "BackendConfig.maxRegulationsPerRequest")
config.RegisterBoolConfigVariable(true, &configEnvReplacementEnabled, false, "BackendConfig.envReplacementEnabled")
config.RegisterBoolConfigVariable(false, &incrementalConfigUpdates, false, "BackendConfig.incrementalConfigUpdates")
config.RegisterBoolConfigVariable(true, &dbCacheEnabled, false, "BackendConfig.dbCacheEnabled")
}
func Init() {
Diagnostics = diagnostics.Diagnostics
loadConfig()
}
func trackConfig(preConfig, curConfig ConfigT) {
Diagnostics.DisableMetrics(curConfig.EnableMetrics)
if diagnostics.EnableConfigIdentifyMetric {
if len(preConfig.Sources) == 0 && len(curConfig.Sources) > 0 {
Diagnostics.Identify(map[string]interface{}{
diagnostics.ConfigIdentify: curConfig.Sources[0].WorkspaceID,
})
}
}
if diagnostics.EnableConfigProcessedMetric {
noOfSources := len(curConfig.Sources)
noOfDestinations := 0
for _, source := range curConfig.Sources {
noOfDestinations += len(source.Destinations)
}
Diagnostics.Track(diagnostics.ConfigProcessed, map[string]interface{}{
diagnostics.SourcesCount: noOfSources,
diagnostics.DesitanationCount: noOfDestinations,
})
}
}
func filterProcessorEnabledWorkspaceConfig(config map[string]ConfigT) map[string]ConfigT {
filterConfig := make(map[string]ConfigT, len(config))
for workspaceID, wConfig := range config {
filterConfig[workspaceID] = filterProcessorEnabledDestinations(wConfig)
}
return filterConfig
}
func filterProcessorEnabledDestinations(config ConfigT) ConfigT {
var modifiedConfig ConfigT
modifiedConfig.Libraries = config.Libraries
modifiedConfig.Sources = make([]SourceT, 0)
for _, source := range config.Sources {
var destinations []DestinationT
for _, destination := range source.Destinations { // TODO skipcq: CRT-P0006
pkgLogger.Debug(destination.Name, " IsProcessorEnabled: ", destination.IsProcessorEnabled)
if destination.IsProcessorEnabled {
destinations = append(destinations, destination)
}
}
source.Destinations = destinations
modifiedConfig.Sources = append(modifiedConfig.Sources, source)
}
return modifiedConfig
}
func (bc *backendConfigImpl) configUpdate(ctx context.Context, workspaces string) {
statConfigBackendError := stats.Default.NewStat("config_backend.errors", stats.CountType)
var (
sourceJSON map[string]ConfigT
err error
)
defer func() {
cacheConfigGauge := stats.Default.NewStat("config_from_cache", stats.GaugeType)
if bc.usingCache {
cacheConfigGauge.Gauge(1)
} else {
cacheConfigGauge.Gauge(0)
}
}()
sourceJSON, err = bc.workspaceConfig.Get(ctx)
if err != nil {
statConfigBackendError.Increment()
pkgLogger.Warnf("Error fetching config from backend: %v", err)
bc.initializedLock.RLock()
if bc.initialized {
bc.initializedLock.RUnlock()
return
}
bc.initializedLock.RUnlock()
// try to get config from cache
sourceJSONBytes, cacheErr := bc.cache.Get(ctx)
if cacheErr != nil {
pkgLogger.Warnf("Error fetching config from cache: %v", cacheErr)
return
}
err = json.Unmarshal(sourceJSONBytes, &sourceJSON)
if err != nil {
pkgLogger.Warnf("Error unmarshalling cached config: %v", cacheErr)
return
}
bc.usingCache = true
} else {
bc.usingCache = false
}
// sorting the sourceJSON.
// json unmarshal does not guarantee order. For DeepEqual to work as expected, sorting is necessary
for workspace := range sourceJSON {
sort.Slice(sourceJSON[workspace].Sources, func(i, j int) bool {
return sourceJSON[workspace].Sources[i].ID < sourceJSON[workspace].Sources[j].ID
})
}
bc.curSourceJSONLock.Lock()
if !reflect.DeepEqual(bc.curSourceJSON, sourceJSON) {
if len(workspaces) > 0 {
pkgLogger.Infof("Workspace Config changed: %d", len(workspaces))
} else {
pkgLogger.Infof("Workspace Config changed")
}
if len(sourceJSON) == 1 { // only use diagnostics if there is one workspace
for _, wConfig := range sourceJSON {
trackConfig(bc.curSourceJSON[wConfig.WorkspaceID], wConfig)
}
}
filteredSourcesJSON := filterProcessorEnabledWorkspaceConfig(sourceJSON)
bc.curSourceJSON = sourceJSON
bc.curSourceJSONLock.Unlock()
LastSync = time.Now().Format(time.RFC3339) // TODO fix concurrent access
bc.eb.Publish(string(TopicBackendConfig), sourceJSON)
bc.eb.Publish(string(TopicProcessConfig), filteredSourcesJSON)
} else {
bc.curSourceJSONLock.Unlock()
}
bc.initializedLock.Lock()
bc.initialized = true
bc.initializedLock.Unlock()
}
func (bc *backendConfigImpl) pollConfigUpdate(ctx context.Context, workspaces string) {
for {
bc.configUpdate(ctx, workspaces)
select {
case <-ctx.Done():
return
case <-time.After(pollInterval):
}
}
}
/*
Subscribe subscribes a channel to a specific topic of backend config updates.
Channel will receive a new pubsub.DataEvent each time the backend configuration is updated.
Data of the DataEvent should be a backendconfig.ConfigT struct.
Available topics are:
- TopicBackendConfig: Will receive complete backend configuration
- TopicProcessConfig: Will receive only backend configuration of processor enabled destinations
- TopicRegulations: Will receive all regulations
*/
func (bc *backendConfigImpl) Subscribe(ctx context.Context, topic Topic) pubsub.DataChannel {
return bc.eb.Subscribe(ctx, string(topic))
}
func newForDeployment(deploymentType deployment.Type, region string, configEnvHandler types.ConfigEnvI) (BackendConfig, error) {
backendConfig := &backendConfigImpl{
eb: pubsub.New(),
}
parsedConfigBackendURL, err := url.Parse(configBackendURL)
if err != nil {
return nil, fmt.Errorf("invalid config backend URL: %v", err)
}
switch deploymentType {
case deployment.DedicatedType:
backendConfig.workspaceConfig = &singleWorkspaceConfig{
configJSONPath: configJSONPath,
configBackendURL: parsedConfigBackendURL,
configEnvHandler: configEnvHandler,
region: region,
}
case deployment.MultiTenantType:
backendConfig.workspaceConfig = &namespaceConfig{
configBackendURL: parsedConfigBackendURL,
configEnvHandler: configEnvHandler,
cpRouterURL: cpRouterURL,
region: region,
incrementalConfigUpdates: incrementalConfigUpdates,
}
default:
return nil, fmt.Errorf("deployment type %q not supported", deploymentType)
}
return backendConfig, backendConfig.SetUp()
}
// Setup backend config
func Setup(configEnvHandler types.ConfigEnvI) (err error) {
deploymentType, err := deployment.GetFromEnv()
region := config.GetString("region", "")
if err != nil {
return fmt.Errorf("deployment type from env: %w", err)
}
backendConfig, err := newForDeployment(deploymentType, region, configEnvHandler)
if err != nil {
return err
}
DefaultBackendConfig = backendConfig
return nil
}
func (bc *backendConfigImpl) StartWithIDs(ctx context.Context, workspaces string) {
var err error
ctx, cancel := context.WithCancel(ctx)
bc.ctx = ctx
bc.cancel = cancel
bc.blockChan = make(chan struct{})
bc.cache = cacheOverride
if !dbCacheEnabled {
bc.cache = &noCache{}
}
if bc.cache == nil {
identifier := bc.Identity()
u, _ := identifier.BasicAuth()
secret := sha256.Sum256([]byte(u))
cacheKey := identifier.ID()
bc.cache, err = cache.Start(
ctx,
secret,
cacheKey,
func() pubsub.DataChannel { return bc.Subscribe(ctx, TopicBackendConfig) },
)
if err != nil {
// the only reason why we should resume by using no cache,
// would be if no database configuration has been set
if config.IsSet("DB.host") {
panic(fmt.Errorf("error starting backend config cache: %w", err))
} else {
pkgLogger.Warnf("Failed to start backend config cache, no cache will be used: %w", err)
bc.cache = &noCache{}
}
}
}
rruntime.Go(func() {
bc.pollConfigUpdate(ctx, workspaces)
close(bc.blockChan)
})
}
func (bc *backendConfigImpl) Stop() {
if bc.cancel != nil {
bc.cancel()
<-bc.blockChan
}
bc.initializedLock.Lock()
bc.initialized = false
bc.initializedLock.Unlock()
}
// WaitForConfig waits until backend config has been initialized
func (bc *backendConfigImpl) WaitForConfig(ctx context.Context) {
for {
bc.initializedLock.RLock()
if bc.initialized {
bc.initializedLock.RUnlock()
return
}
bc.initializedLock.RUnlock()
pkgLogger.Info("Waiting for backend config")
select {
case <-ctx.Done():
return
case <-time.After(pollInterval):
}
}
}
func GetConfigBackendURL() string {
return configBackendURL
}
func getNotOKError(respBody []byte, statusCode int) error {
errMsg := ""
if len(respBody) > 0 {
errMsg = fmt.Sprintf(": %s", respBody)
}
return fmt.Errorf("backend config request failed with %d%s", statusCode, errMsg)
}
func (bc *backendConfigImpl) Identity() identity.Identifier {
result := bc.workspaceConfig.Identity()
if result.ID() == "" && bc.usingCache { // in case of a cached config the ID is not set when operating in single workspace mode
bc.curSourceJSONLock.RLock()
curConfig := bc.curSourceJSON
bc.curSourceJSONLock.RUnlock()
if len(curConfig) == 1 {
for workspaceID := range curConfig {
return &identity.IdentifierDecorator{
Identifier: result,
Id: workspaceID,
}
}
}
return bc.workspaceConfig.Identity()
}
return result
}