/
init.go
126 lines (100 loc) · 4.5 KB
/
init.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
// Package storage implements the storage backend.
package storage
import (
"fmt"
"path/filepath"
"strings"
"time"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/identity"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
"github.com/oasisprotocol/oasis-core/go/storage/api"
"github.com/oasisprotocol/oasis-core/go/storage/database"
)
const (
// CfgWorkerEnabled enables the storage worker.
CfgWorkerEnabled = "worker.storage.enabled"
cfgWorkerFetcherCount = "worker.storage.fetcher_count"
// CfgWorkerPublicRPCEnabled enables storage state access for all nodes instead of just
// storage committee members.
CfgWorkerPublicRPCEnabled = "worker.storage.public_rpc.enabled"
// CfgWorkerCheckpointerDisabled disables the storage checkpointer.
CfgWorkerCheckpointerDisabled = "worker.storage.checkpointer.disabled"
// CfgWorkerCheckpointCheckInterval configures the checkpointer check interval.
CfgWorkerCheckpointCheckInterval = "worker.storage.checkpointer.check_interval"
// CfgCheckpointSyncDisabled disables syncing from checkpoints on worker startup.
CfgWorkerCheckpointSyncDisabled = "worker.storage.checkpoint_sync.disabled"
// CfgWorkerDebugIgnoreApply is a debug option that makes the worker ignore
// all apply operations.
CfgWorkerDebugIgnoreApply = "worker.debug.storage.ignore_apply"
// CfgBackend configures the storage backend flag.
CfgBackend = "worker.storage.backend"
// CfgLRUSlots configures the LRU apply lock slots.
CfgLRUSlots = "worker.storage.root_cache.apply_lock_lru_slots"
// CfgMaxCacheSize configures the maximum in-memory cache size.
CfgMaxCacheSize = "worker.storage.max_cache_size"
cfgCrashEnabled = "worker.storage.crash.enabled"
// CfgInsecureSkipChecks disables known root checks.
CfgInsecureSkipChecks = "worker.storage.debug.insecure_skip_checks"
)
// Flags has the configuration flags.
var Flags = flag.NewFlagSet("", flag.ContinueOnError)
// GetLocalBackendDBDir returns the database name for local backends.
func GetLocalBackendDBDir(dataDir, backend string) string {
return filepath.Join(dataDir, database.DefaultFileName(backend))
}
// NewLocalBackend constructs a new Backend based on the configuration flags.
func NewLocalBackend(
dataDir string,
namespace common.Namespace,
identity *identity.Identity,
) (api.LocalBackend, error) {
cfg := &api.Config{
Backend: strings.ToLower(viper.GetString(CfgBackend)),
DB: dataDir,
Signer: identity.NodeSigner,
ApplyLockLRUSlots: uint64(viper.GetInt(CfgLRUSlots)),
InsecureSkipChecks: viper.GetBool(CfgInsecureSkipChecks) && cmdFlags.DebugDontBlameOasis(),
Namespace: namespace,
MaxCacheSize: int64(viper.GetSizeInBytes(CfgMaxCacheSize)),
}
var (
err error
impl api.Backend
)
switch cfg.Backend {
case database.BackendNameBadgerDB:
cfg.DB = GetLocalBackendDBDir(dataDir, cfg.Backend)
impl, err = database.New(cfg)
default:
err = fmt.Errorf("storage: unsupported backend: '%v'", cfg.Backend)
}
if err != nil {
return nil, err
}
crashEnabled := viper.GetBool(cfgCrashEnabled) && cmdFlags.DebugDontBlameOasis()
if crashEnabled {
impl = newCrashingWrapper(impl)
}
return api.NewMetricsWrapper(impl).(api.LocalBackend), nil
}
func init() {
Flags.Bool(CfgWorkerEnabled, false, "Enable storage worker")
Flags.Uint(cfgWorkerFetcherCount, 4, "Number of concurrent storage diff fetchers")
Flags.Bool(CfgWorkerPublicRPCEnabled, false, "Enable storage RPC access for all nodes")
Flags.Bool(CfgWorkerCheckpointerDisabled, false, "Disable the storage checkpointer")
Flags.Duration(CfgWorkerCheckpointCheckInterval, 1*time.Minute, "Storage checkpointer check interval")
Flags.Bool(CfgWorkerCheckpointSyncDisabled, false, "Disable initial storage sync from checkpoints")
Flags.Bool(CfgWorkerDebugIgnoreApply, false, "Ignore Apply operations (for debugging purposes)")
_ = Flags.MarkHidden(CfgWorkerDebugIgnoreApply)
Flags.String(CfgBackend, database.BackendNameBadgerDB, "Storage backend")
Flags.Bool(cfgCrashEnabled, false, "Enable the crashing storage wrapper")
Flags.Int(CfgLRUSlots, 1000, "How many LRU slots to use for Apply call locks in the MKVS tree root cache")
Flags.String(CfgMaxCacheSize, "64mb", "Maximum in-memory cache size")
Flags.Bool(CfgInsecureSkipChecks, false, "INSECURE: Skip known root checks")
_ = Flags.MarkHidden(CfgInsecureSkipChecks)
_ = Flags.MarkHidden(cfgCrashEnabled)
_ = viper.BindPFlags(Flags)
}