-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
575 lines (516 loc) · 22 KB
/
config.go
File metadata and controls
575 lines (516 loc) · 22 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package forma
import (
"time"
)
// Config consolidates settings from both modules
type Config struct {
Database DatabaseConfig `json:"database"`
Query QueryConfig `json:"query"`
Entity EntityConfig `json:"entity"`
Transaction TransactionConfig `json:"transaction"`
Performance PerformanceConfig `json:"performance"`
Logging LoggingConfig `json:"logging"`
Metrics MetricsConfig `json:"metrics"`
Reference ReferenceConfig `json:"reference"`
DuckDB DuckDBConfig `json:"duckdb"`
SchemaRegistry SchemaRegistry `json:"-"` // Custom schema registry implementation (optional)
}
// DatabaseConfig contains database connection settings
type DatabaseConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"password"`
SSLMode string `json:"sslMode"`
Schema string `json:"schema"`
MaxConnections int `json:"maxConnections"`
MaxIdleConns int `json:"maxIdleConns"`
ConnMaxLifetime time.Duration `json:"connMaxLifetime"`
ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"`
Timeout time.Duration `json:"timeout"`
TableNames TableNames `json:"tableNames"`
}
// QueryConfig contains query execution settings
type QueryConfig struct {
DefaultTimeout time.Duration `json:"defaultTimeout"`
MaxRows int `json:"maxRows"`
DefaultPageSize int `json:"defaultPageSize"`
MaxPageSize int `json:"maxPageSize"`
EnableQueryPlan bool `json:"enableQueryPlan"`
EnableOptimization bool `json:"enableOptimization"`
CacheQueryPlans bool `json:"cacheQueryPlans"`
QueryPlanCacheTTL time.Duration `json:"queryPlanCacheTTL"`
}
// EntityConfig contains entity management settings
type EntityConfig struct {
EnableReferenceValidation bool `json:"enableReferenceValidation"`
EnableCascadeDelete bool `json:"enableCascadeDelete"`
BatchSize int `json:"batchSize"`
CacheEnabled bool `json:"cacheEnabled"`
CacheTTL time.Duration `json:"cacheTTL"`
MaxEntitySize int `json:"maxEntitySize"`
EnableVersioning bool `json:"enableVersioning"`
SchemaDirectory string `json:"schemaDirectory"`
}
// TransactionConfig contains transaction settings
type TransactionConfig struct {
DefaultTimeout time.Duration `json:"defaultTimeout"`
MaxTimeout time.Duration `json:"maxTimeout"`
MaxRetryAttempts int `json:"maxRetryAttempts"`
RetryAttempts int `json:"retryAttempts"`
RetryDelay time.Duration `json:"retryDelay"`
IsolationLevel string `json:"isolationLevel"`
EnableDeadlockDetection bool `json:"enableDeadlockDetection"`
DeadlockCheckInterval time.Duration `json:"deadlockCheckInterval"`
DeadlockMaxWaitTime time.Duration `json:"deadlockMaxWaitTime"`
SlowTransactionThreshold time.Duration `json:"slowTransactionThreshold"`
MinSuccessRate float64 `json:"minSuccessRate"`
MaxAverageDuration time.Duration `json:"maxAverageDuration"`
MaxConnectionPoolUsage float64 `json:"maxConnectionPoolUsage"`
}
// PerformanceConfig contains performance monitoring settings
type PerformanceConfig struct {
EnableMonitoring bool `json:"enableMonitoring"`
SlowQueryThreshold time.Duration `json:"slowQueryThreshold"`
SlowOperationThreshold time.Duration `json:"slowOperationThreshold"`
MetricsCollectionInterval time.Duration `json:"metricsCollectionInterval"`
BatchSize int `json:"batchSize"`
MaxBatchSize int `json:"maxBatchSize"`
Batch BatchConfig `json:"batch"`
// Unified monitoring settings
MaxMetricsHistory int `json:"maxMetricsHistory"`
MaxAlertsHistory int `json:"maxAlertsHistory"`
MaxRecommendations int `json:"maxRecommendations"`
EnableAlerting bool `json:"enableAlerting"`
EnableRecommendations bool `json:"enableRecommendations"`
AlertingInterval time.Duration `json:"alertingInterval"`
RecommendationInterval time.Duration `json:"recommendationInterval"`
// Memory monitoring
EnableMemoryMonitoring bool `json:"enableMemoryMonitoring"`
MemoryThreshold int64 `json:"memoryThreshold"`
// Correlation tracking
EnableCorrelationTracking bool `json:"enableCorrelationTracking"`
CorrelationTTL time.Duration `json:"correlationTTL"`
}
// BatchConfig contains batch processing settings
type BatchConfig struct {
EnableDynamicSizing bool `json:"enableDynamicSizing"`
EnableParallelProcessing bool `json:"enableParallelProcessing"`
EnableBatchStreaming bool `json:"enableBatchStreaming"`
ParallelThreshold int `json:"parallelThreshold"`
StreamingThreshold int `json:"streamingThreshold"`
MaxParallelWorkers int `json:"maxParallelWorkers"`
StreamingChunkSize int `json:"streamingChunkSize"`
StreamingDelay int `json:"streamingDelay"` // milliseconds
MaxComplexityPerBatch int `json:"maxComplexityPerBatch"`
AttributeComplexityScore int `json:"attributeComplexityScore"`
OptimalChunkSize int `json:"optimalChunkSize"`
}
// LoggingConfig contains logging settings
type LoggingConfig struct {
Level string `json:"level"`
Format string `json:"format"`
EnableStructured bool `json:"enableStructured"`
EnablePerformance bool `json:"enablePerformance"`
EnableQueryLogging bool `json:"enableQueryLogging"`
LogSlowQueries bool `json:"logSlowQueries"`
SlowQueryThreshold time.Duration `json:"slowQueryThreshold"`
MaxLogSize int `json:"maxLogSize"`
LogRotation bool `json:"logRotation"`
SanitizeParameters bool `json:"sanitizeParameters"`
LogQueries bool `json:"logQueries"`
LogErrors bool `json:"logErrors"`
LogSecurityEvents bool `json:"logSecurityEvents"`
LogPerformanceWarnings bool `json:"logPerformanceWarnings"`
LogAllOperations bool `json:"logAllOperations"`
EnableDetailedLogging bool `json:"enableDetailedLogging"`
}
// MetricsConfig contains metrics collection settings
type MetricsConfig struct {
Enabled bool `json:"enabled"`
Provider string `json:"provider"` // prometheus, statsd, etc.
Endpoint string `json:"endpoint"`
CollectionInterval time.Duration `json:"collectionInterval"`
EnableHistograms bool `json:"enableHistograms"`
EnableCounters bool `json:"enableCounters"`
EnableGauges bool `json:"enableGauges"`
Namespace string `json:"namespace"`
Labels map[string]string `json:"labels"`
MaxSamples int `json:"maxSamples"`
EnableOperationMetrics bool `json:"enableOperationMetrics"`
EnableTransactionMetrics bool `json:"enableTransactionMetrics"`
EnablePatternMetrics bool `json:"enablePatternMetrics"`
}
// DuckDBConfig contains DuckDB connection and S3 settings for federated queries
type DuckDBConfig struct {
Enabled bool `json:"enabled"`
DBPath string `json:"dbPath"` // path to local DuckDB file (or ":memory:")
MemoryLimitMB int `json:"memoryLimitMB"` // memory limit for DuckDB in MB
EnableS3 bool `json:"enableS3"` // enable S3/http file system
S3Endpoint string `json:"s3Endpoint"` // custom S3 endpoint (for MinIO)
S3AccessKey string `json:"s3AccessKey"`
S3SecretKey string `json:"s3SecretKey"`
S3Region string `json:"s3Region"`
EnableParquet bool `json:"enableParquet"` // enable parquet extension
Extensions []string `json:"extensions"` // additional extensions to load
MaxConnections int `json:"maxConnections"`
QueryTimeout time.Duration `json:"queryTimeout"` // per-query timeout for DuckDB access
MaxParallelism int `json:"maxParallelism"` // max threads/pragmas for DuckDB
CircuitBreakerThreshold float64 `json:"circuitBreakerThreshold"` // failure rate to trip circuit breaker (0..1)
Routing RoutingPolicy `json:"routing"` // routing policy for federated queries
}
// RoutingStrategy specifies the federated query routing algorithm.
type RoutingStrategy string
const (
// RoutingStrategyFreshnessFirst prefers the hot (PostgreSQL) tier for
// queries that explicitly request fresh data (PreferHot flag).
RoutingStrategyFreshnessFirst RoutingStrategy = "freshness-first"
// RoutingStrategyCostFirst routes large scans to DuckDB to reduce
// PostgreSQL load; small scans stay on the hot tier.
RoutingStrategyCostFirst RoutingStrategy = "cost-first"
// RoutingStrategyHybrid uses DuckDB by default but short-circuits to
// PostgreSQL when the result set is expected to be small or hot data is
// explicitly preferred.
RoutingStrategyHybrid RoutingStrategy = "hybrid"
)
// RoutingPolicy defines federated query routing behavior
type RoutingPolicy struct {
Strategy RoutingStrategy `json:"strategy"` // "freshness-first", "cost-first", "hybrid"
HotTTL time.Duration `json:"hotTTL"` // TTL to consider data "hot"
MaxDuckDBScanRows int `json:"maxDuckDBScanRows"` // threshold for preferring cold scans
AllowS3Fallback bool `json:"allowS3Fallback"` // allow falling back to S3/DuckDB when PG not used
}
// ReferenceConfig contains reference management settings
type ReferenceConfig struct {
ValidateOnCreate bool `json:"validateOnCreate"`
ValidateOnUpdate bool `json:"validateOnUpdate"`
CheckIntegrity bool `json:"checkIntegrity"`
CascadeDelete bool `json:"cascadeDelete"`
CascadeUpdate bool `json:"cascadeUpdate"`
MaxCascadeDepth int `json:"maxCascadeDepth"`
CascadeRules map[string]CascadeRule `json:"cascadeRules,omitempty"`
EnableCaching bool `json:"enableCaching"`
CacheTTL time.Duration `json:"cacheTTL"`
MaxCacheSize int `json:"maxCacheSize"`
BatchSize int `json:"batchSize"`
}
// CascadeRule defines cascade behavior for specific schema relationships
type CascadeRule struct {
SourceSchema string `json:"sourceSchema"`
TargetSchema string `json:"targetSchema"`
Action CascadeAction `json:"action"`
MaxDepth int `json:"maxDepth,omitempty"`
}
// CascadeAction defines the type of cascade action
type CascadeAction string
const (
CascadeActionDelete CascadeAction = "delete"
CascadeActionUpdate CascadeAction = "update"
CascadeActionNullify CascadeAction = "nullify"
CascadeActionRestrict CascadeAction = "restrict"
)
// DefaultConfig returns a default configuration
func DefaultConfig(schemaRegistry SchemaRegistry) *Config {
return &Config{
SchemaRegistry: schemaRegistry,
Database: defaultDatabaseConfig(),
Query: defaultQueryConfig(),
Entity: defaultEntityConfig(),
Transaction: defaultTransactionConfig(),
Performance: defaultPerformanceConfig(),
Logging: defaultLoggingConfig(),
Metrics: defaultMetricsConfig(),
Reference: defaultReferenceConfig(),
DuckDB: defaultDuckDBConfig(),
}
}
// -----------------------------------------------------------------------
// Functional Options pattern
// -----------------------------------------------------------------------
// Option is a functional option that mutates a Config.
// Use the With* constructors below to build option values, then pass them to
// NewConfig to obtain a fully-configured Config without touching the struct
// fields directly.
type Option func(*Config)
// NewConfig creates a Config starting from the defaults produced by
// DefaultConfig(nil) and then applies each provided Option in order.
// The schema registry can be set via WithSchemaRegistry.
//
// Example:
//
// cfg := forma.NewConfig(
// forma.WithDatabase(forma.DatabaseConfig{Host: "db.example.com", Port: 5432, MaxConnections: 50}),
// forma.WithDuckDB(forma.DuckDBConfig{Enabled: true, DBPath: ":memory:"}),
// )
func NewConfig(opts ...Option) *Config {
cfg := DefaultConfig(nil)
for _, opt := range opts {
opt(cfg)
}
return cfg
}
// WithSchemaRegistry sets the schema registry on the config.
func WithSchemaRegistry(sr SchemaRegistry) Option {
return func(c *Config) { c.SchemaRegistry = sr }
}
// WithDatabase replaces the DatabaseConfig section.
func WithDatabase(db DatabaseConfig) Option {
return func(c *Config) { c.Database = db }
}
// WithQuery replaces the QueryConfig section.
func WithQuery(q QueryConfig) Option {
return func(c *Config) { c.Query = q }
}
// WithEntity replaces the EntityConfig section.
func WithEntity(e EntityConfig) Option {
return func(c *Config) { c.Entity = e }
}
// WithTransaction replaces the TransactionConfig section.
func WithTransaction(t TransactionConfig) Option {
return func(c *Config) { c.Transaction = t }
}
// WithPerformance replaces the PerformanceConfig section.
func WithPerformance(p PerformanceConfig) Option {
return func(c *Config) { c.Performance = p }
}
// WithLogging replaces the LoggingConfig section.
func WithLogging(l LoggingConfig) Option {
return func(c *Config) { c.Logging = l }
}
// WithMetrics replaces the MetricsConfig section.
func WithMetrics(m MetricsConfig) Option {
return func(c *Config) { c.Metrics = m }
}
// WithReference replaces the ReferenceConfig section.
func WithReference(r ReferenceConfig) Option {
return func(c *Config) { c.Reference = r }
}
// WithDuckDB replaces the DuckDBConfig section.
func WithDuckDB(d DuckDBConfig) Option {
return func(c *Config) { c.DuckDB = d }
}
// defaultDatabaseConfig returns default database configuration.
func defaultDatabaseConfig() DatabaseConfig {
return DatabaseConfig{
Host: "localhost",
Port: 5432,
Schema: "public",
MaxConnections: 25,
MaxIdleConns: 5,
ConnMaxLifetime: 5 * time.Minute,
ConnMaxIdleTime: 5 * time.Minute,
Timeout: 30 * time.Second,
}
}
// defaultQueryConfig returns default query configuration.
func defaultQueryConfig() QueryConfig {
return QueryConfig{
DefaultTimeout: 30 * time.Second,
MaxRows: 10000,
DefaultPageSize: 50,
MaxPageSize: 100,
EnableQueryPlan: true,
EnableOptimization: true,
CacheQueryPlans: true,
QueryPlanCacheTTL: 1 * time.Hour,
}
}
// defaultEntityConfig returns default entity configuration.
func defaultEntityConfig() EntityConfig {
return EntityConfig{
EnableReferenceValidation: true,
EnableCascadeDelete: false,
BatchSize: 100,
CacheEnabled: true,
CacheTTL: 5 * time.Minute,
MaxEntitySize: 1024 * 1024, // 1MB
EnableVersioning: true,
}
}
// defaultTransactionConfig returns default transaction configuration.
func defaultTransactionConfig() TransactionConfig {
return TransactionConfig{
DefaultTimeout: 30 * time.Second,
MaxTimeout: 5 * time.Minute,
MaxRetryAttempts: 3,
RetryAttempts: 3,
RetryDelay: 100 * time.Millisecond,
IsolationLevel: "READ_COMMITTED",
EnableDeadlockDetection: true,
DeadlockCheckInterval: 5 * time.Second,
DeadlockMaxWaitTime: 30 * time.Second,
SlowTransactionThreshold: 2 * time.Second,
MinSuccessRate: 95.0,
MaxAverageDuration: 1 * time.Second,
MaxConnectionPoolUsage: 80.0,
}
}
// defaultPerformanceConfig returns default performance configuration.
func defaultPerformanceConfig() PerformanceConfig {
return PerformanceConfig{
EnableMonitoring: true,
SlowQueryThreshold: 1 * time.Second,
SlowOperationThreshold: 2 * time.Second,
MetricsCollectionInterval: 30 * time.Second,
BatchSize: 100,
MaxBatchSize: 1000,
Batch: defaultBatchConfig(),
// Unified monitoring defaults
MaxMetricsHistory: 10000,
MaxAlertsHistory: 1000,
MaxRecommendations: 100,
EnableAlerting: true,
EnableRecommendations: true,
AlertingInterval: 1 * time.Minute,
RecommendationInterval: 5 * time.Minute,
// Memory monitoring defaults
EnableMemoryMonitoring: true,
MemoryThreshold: 100 * 1024 * 1024, // 100MB
// Correlation tracking defaults
EnableCorrelationTracking: true,
CorrelationTTL: 1 * time.Hour,
}
}
// defaultBatchConfig returns default batch configuration.
func defaultBatchConfig() BatchConfig {
return BatchConfig{
EnableDynamicSizing: true,
EnableParallelProcessing: true,
EnableBatchStreaming: true,
ParallelThreshold: 50,
StreamingThreshold: 500,
MaxParallelWorkers: 4,
StreamingChunkSize: 100,
StreamingDelay: 10,
MaxComplexityPerBatch: 500,
AttributeComplexityScore: 1,
OptimalChunkSize: 10,
}
}
// defaultLoggingConfig returns default logging configuration.
func defaultLoggingConfig() LoggingConfig {
return LoggingConfig{
Level: "info",
Format: "json",
EnableStructured: true,
EnablePerformance: true,
EnableQueryLogging: false,
LogSlowQueries: true,
SlowQueryThreshold: 1 * time.Second,
MaxLogSize: 100 * 1024 * 1024, // 100MB
LogRotation: true,
SanitizeParameters: true,
LogQueries: false,
LogErrors: true,
LogSecurityEvents: true,
LogPerformanceWarnings: true,
LogAllOperations: false,
EnableDetailedLogging: true,
}
}
// defaultMetricsConfig returns default metrics configuration.
func defaultMetricsConfig() MetricsConfig {
return MetricsConfig{
Enabled: true,
Provider: "prometheus",
CollectionInterval: 30 * time.Second,
EnableHistograms: true,
EnableCounters: true,
EnableGauges: true,
Namespace: "dataplane",
MaxSamples: 10000,
EnableOperationMetrics: true,
EnableTransactionMetrics: true,
EnablePatternMetrics: true,
}
}
// defaultReferenceConfig returns default reference configuration.
func defaultReferenceConfig() ReferenceConfig {
return ReferenceConfig{
ValidateOnCreate: true,
ValidateOnUpdate: true,
CheckIntegrity: true,
CascadeDelete: false,
CascadeUpdate: false,
MaxCascadeDepth: 5,
EnableCaching: true,
CacheTTL: 5 * time.Minute,
MaxCacheSize: 1000,
BatchSize: 100,
}
}
// defaultDuckDBConfig returns default DuckDB configuration.
func defaultDuckDBConfig() DuckDBConfig {
return DuckDBConfig{
Enabled: false,
DBPath: ":memory:",
MemoryLimitMB: 0,
EnableS3: false,
EnableParquet: false,
Extensions: []string{},
MaxConnections: 1,
QueryTimeout: 30 * time.Second,
MaxParallelism: 1,
CircuitBreakerThreshold: 0.5,
Routing: RoutingPolicy{
Strategy: RoutingStrategyHybrid,
HotTTL: 5 * time.Minute,
MaxDuckDBScanRows: 100000,
AllowS3Fallback: true,
},
}
}
// Validate validates the configuration
func (c *Config) Validate() error {
// Add validation logic here
if c.Database.MaxConnections <= 0 {
return &ConfigError{Field: "database.maxConnections", Message: "must be greater than 0"}
}
if c.Query.DefaultPageSize <= 0 {
return &ConfigError{Field: "query.defaultPageSize", Message: "must be greater than 0"}
}
if c.Query.MaxPageSize < c.Query.DefaultPageSize {
return &ConfigError{Field: "query.maxPageSize", Message: "must be greater than or equal to defaultPageSize"}
}
if c.Performance.BatchSize <= 0 {
return &ConfigError{Field: "performance.batchSize", Message: "must be greater than 0"}
}
if c.Performance.MaxBatchSize < c.Performance.BatchSize {
return &ConfigError{Field: "performance.maxBatchSize", Message: "must be greater than or equal to batchSize"}
}
// DuckDB specific validation
if c.DuckDB.MemoryLimitMB < 0 {
return &ConfigError{Field: "duckdb.memoryLimitMB", Message: "must be greater than or equal to 0"}
}
if c.DuckDB.Enabled && c.DuckDB.MaxConnections <= 0 {
return &ConfigError{Field: "duckdb.maxConnections", Message: "must be greater than 0 when duckdb enabled"}
}
if c.DuckDB.QueryTimeout < 0 {
return &ConfigError{Field: "duckdb.queryTimeout", Message: "must be greater than or equal to 0"}
}
allowed := map[RoutingStrategy]bool{
RoutingStrategyFreshnessFirst: true,
RoutingStrategyCostFirst: true,
RoutingStrategyHybrid: true,
"": true,
}
if !allowed[c.DuckDB.Routing.Strategy] {
return &ConfigError{Field: "duckdb.routing.strategy", Message: "invalid routing strategy"}
}
if c.DuckDB.Routing.HotTTL < 0 {
return &ConfigError{Field: "duckdb.routing.hotTTL", Message: "must be greater than or equal to 0"}
}
if c.DuckDB.Routing.MaxDuckDBScanRows < 0 {
return &ConfigError{Field: "duckdb.routing.maxDuckDBScanRows", Message: "must be greater than or equal to 0"}
}
return nil
}
// ConfigError represents a configuration validation error
type ConfigError struct {
Field string `json:"field"`
Message string `json:"message"`
}
func (e *ConfigError) Error() string {
return "config validation error for field '" + e.Field + "': " + e.Message
}