Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MAP_POPULATE for our bbolt mmaps #13573

Merged
merged 5 commits into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/13573.txt
@@ -0,0 +1,3 @@
```release-note:bug
storage/raft: On linux, use map_populate for bolt files to improve startup time.
```
2 changes: 1 addition & 1 deletion physical/raft/bolt_32bit_test.go
Expand Up @@ -32,7 +32,7 @@ func Test_BoltOptions(t *testing.T) {
os.Setenv(key, tc.env)
}

o := boltOptions()
o := boltOptions("")

if o.InitialMmapSize != tc.expectedSize {
t.Errorf("expected InitialMmapSize to be %d but it was %d", tc.expectedSize, o.InitialMmapSize)
Expand Down
2 changes: 1 addition & 1 deletion physical/raft/bolt_64bit_test.go
Expand Up @@ -32,7 +32,7 @@ func Test_BoltOptions(t *testing.T) {
os.Setenv(key, tc.env)
}

o := boltOptions()
o := boltOptions("")

if o.InitialMmapSize != tc.expectedSize {
t.Errorf("expected InitialMmapSize to be %d but it was %d", tc.expectedSize, o.InitialMmapSize)
Expand Down
36 changes: 36 additions & 0 deletions physical/raft/bolt_linux.go
@@ -0,0 +1,36 @@
package raft

import (
"context"
"os"

"github.com/shirou/gopsutil/mem"
"golang.org/x/sys/unix"
)

func init() {
getMmapFlags = getMmapFlagsLinux
}

func getMmapFlagsLinux(dbPath string) int {
if os.Getenv("VAULT_RAFT_DISABLE_MAP_POPULATE") != "" {
return 0
}
stat, err := os.Stat(dbPath)
if err != nil {
return 0
}
size := stat.Size()

v, err := mem.VirtualMemoryWithContext(context.Background())
if err != nil {
return 0
}

// We won't worry about swap, since we already tell people not to use it.
if v.Total > uint64(size) {
return unix.MAP_POPULATE
}

return 0
}
2 changes: 1 addition & 1 deletion physical/raft/fsm.go
Expand Up @@ -168,7 +168,7 @@ func (f *FSM) openDBFile(dbPath string) error {
}
}

opts := boltOptions()
opts := boltOptions(dbPath)
start := time.Now()
boltDB, err := bolt.Open(dbPath, 0o600, opts)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions physical/raft/raft.go
Expand Up @@ -40,6 +40,8 @@ const EnvVaultRaftNodeID = "VAULT_RAFT_NODE_ID"
// EnvVaultRaftPath is used to fetch the path where Raft data is stored from the environment.
const EnvVaultRaftPath = "VAULT_RAFT_PATH"

var getMmapFlags = func(string) int { return 0 }

// Verify RaftBackend satisfies the correct interfaces
var (
_ physical.Backend = (*RaftBackend)(nil)
Expand Down Expand Up @@ -364,9 +366,10 @@ func NewRaftBackend(conf map[string]string, logger log.Logger) (physical.Backend
}

// Create the backend raft store for logs and stable storage.
opts := boltOptions()
dbPath := filepath.Join(path, "raft.db")
opts := boltOptions(dbPath)
raftOptions := raftboltdb.Options{
Path: filepath.Join(path, "raft.db"),
Path: dbPath,
BoltOptions: opts,
}
store, err := raftboltdb.New(raftOptions)
Expand Down Expand Up @@ -1649,11 +1652,12 @@ func (s sealer) Open(ctx context.Context, ct []byte) ([]byte, error) {

// boltOptions returns a bolt.Options struct, suitable for passing to
// bolt.Open(), pre-configured with all of our preferred defaults.
func boltOptions() *bolt.Options {
func boltOptions(path string) *bolt.Options {
o := &bolt.Options{
Timeout: 1 * time.Second,
FreelistType: bolt.FreelistMapType,
NoFreelistSync: true,
MmapFlags: getMmapFlags(path),
}

if os.Getenv("VAULT_RAFT_FREELIST_TYPE") == "array" {
Expand Down