forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
87 lines (76 loc) · 3.57 KB
/
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package filestorage // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/filestorage"
import (
"errors"
"fmt"
"io/fs"
"os"
"time"
)
// Config defines configuration for file storage extension.
type Config struct {
Directory string `mapstructure:"directory,omitempty"`
Timeout time.Duration `mapstructure:"timeout,omitempty"`
Compaction *CompactionConfig `mapstructure:"compaction,omitempty"`
// FSync specifies that fsync should be called after each database write
FSync bool `mapstructure:"fsync,omitempty"`
}
// CompactionConfig defines configuration for optional file storage compaction.
type CompactionConfig struct {
// OnStart specifies that compaction is attempted each time on start
OnStart bool `mapstructure:"on_start,omitempty"`
// OnRebound specifies that compaction is attempted online, when rebound conditions are met.
// This typically happens when storage usage has increased, which caused increase in space allocation
// and afterwards it had most items removed. We want to run the compaction online only when there are
// not too many elements still being stored (which is an indication that "heavy usage" period is over)
// so compaction should be relatively fast and at the same time there is relatively large volume of space
// that might be reclaimed.
OnRebound bool `mapstructure:"on_rebound,omitempty"`
// Directory specifies where the temporary files for compaction will be stored
Directory string `mapstructure:"directory,omitempty"`
// ReboundNeededThresholdMiB specifies the minimum total allocated size (both used and empty)
// to mark the need for online compaction
ReboundNeededThresholdMiB int64 `mapstructure:"rebound_needed_threshold_mib"`
// ReboundTriggerThresholdMiB is used when compaction is marked as needed. When allocated data size drops
// below the specified value, the compactions starts and the flag marking need for compaction is cleared
ReboundTriggerThresholdMiB int64 `mapstructure:"rebound_trigger_threshold_mib"`
// MaxTransactionSize specifies the maximum number of items that might be present in single compaction iteration
MaxTransactionSize int64 `mapstructure:"max_transaction_size,omitempty"`
// CheckInterval specifies frequency of compaction check
CheckInterval time.Duration `mapstructure:"check_interval,omitempty"`
// CleanupOnStart specifies removal of temporary files is performed on start.
// It will remove all the files in the compaction directory starting with tempdb,
// temp files will be left if a previous run of the process is killed while compacting.
CleanupOnStart bool `mapstructure:"cleanup_on_start,omitempty"`
}
func (cfg *Config) Validate() error {
var dirs []string
if cfg.Compaction.OnStart {
dirs = []string{cfg.Directory, cfg.Compaction.Directory}
} else {
dirs = []string{cfg.Directory}
}
for _, dir := range dirs {
info, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("directory must exist: %w", err)
}
fsErr := &fs.PathError{}
if errors.As(err, &fsErr) {
return fmt.Errorf("problem accessing configured directory: %s, err: %w", dir, fsErr)
}
}
if !info.IsDir() {
return fmt.Errorf("%s is not a directory", dir)
}
}
if cfg.Compaction.MaxTransactionSize < 0 {
return errors.New("max transaction size for compaction cannot be less than 0")
}
if cfg.Compaction.OnRebound && cfg.Compaction.CheckInterval <= 0 {
return errors.New("compaction check interval must be positive when rebound compaction is set")
}
return nil
}