Skip to content

Commit

Permalink
Use MAP_POPULATE for our bbolt mmaps (hashicorp#13573)
Browse files Browse the repository at this point in the history
* Use MAP_POPULATE for our bbolt mmaps, assuming the files fit in memory.  This should improve startup times when freelist sync is disabled.
  • Loading branch information
ncabatoff authored and heppu committed Jan 13, 2022
1 parent e33856b commit 20ce083
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
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

0 comments on commit 20ce083

Please sign in to comment.