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

add pebble version check #127

Merged
merged 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/cockroachdb/pebble"
Expand Down Expand Up @@ -141,6 +142,43 @@ func Open(dirname string, opts *Options) (DB, error) {
opts.PebbleOptions = DefaultPebbleOptions()
}

_, err := os.Stat(dirname)
poonai marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if err != nil && os.IsNotExist(err) {
// create dir if db dir didn't exit.
if err := os.MkdirAll(dirname, os.ModePerm); err != nil {
return nil, err
}
}

// retive the pebble version.
version, err := os.ReadFile(filepath.Join(dirname, "VERSION"))
if err != nil && !os.IsNotExist(err) {
return nil, err
}

if err != nil && os.IsNotExist(err) {
// create version for to check invariant in the
// next open.
if err := os.WriteFile(filepath.Join(dirname, "VERSION"),
[]byte(fmt.Sprintf("%d", opts.PebbleOptions.FormatMajorVersion)),
os.ModePerm); err != nil {
return nil, err
}
} else {
existingVersion, err := strconv.ParseUint(string(version), 10, 64)
if err != nil {
return nil, err
}
if existingVersion != uint64(opts.PebbleOptions.FormatMajorVersion) {
return nil, fmt.Errorf("the user trying to open pebble version in %d. but db is in %d",
opts.PebbleOptions.FormatMajorVersion,
existingVersion)
}
}

opts.PebbleOptions.Comparer = DefaultKeyComparer()

pdb, err := pebble.Open(dirname, opts.PebbleOptions)
Expand Down
44 changes: 44 additions & 0 deletions bond_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package bond

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/cockroachdb/pebble"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -42,3 +45,44 @@ func TestBond_Open(t *testing.T) {
err = db.Close()
require.NoError(t, err)
}

func TestBond_VersionCheck(t *testing.T) {
defer func() { _ = os.RemoveAll(dbName) }()

pebbleOpts := DefaultPebbleOptions()
pebbleOpts.FormatMajorVersion = pebble.FormatPrePebblev1MarkedCompacted
opts := DefaultOptions()
opts.PebbleOptions = pebbleOpts

db, err := Open(dbName, opts)
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)

// simluate the db where VERSION file don't exist.
err = os.Remove(filepath.Join(dbName, "VERSION"))
require.NoError(t, err)

// opening db should create a version file
db, err = Open(dbName, opts)
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)

_, err = os.Stat(filepath.Join(dbName, "VERSION"))
require.NoError(t, err)
buf, err := os.ReadFile(filepath.Join(dbName, "VERSION"))
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("%d", opts.PebbleOptions.FormatMajorVersion), string(buf))

// rewrite the version with some other version.
err = os.Remove(filepath.Join(dbName, "VERSION"))
require.NoError(t, err)
err = os.WriteFile(filepath.Join(dbName, "VERSION"),
[]byte(fmt.Sprintf("%d", opts.PebbleOptions.FormatMajorVersion-1)), os.ModePerm)
require.NoError(t, err)

// throw an error since db is written in different version.
_, err = Open(dbName, opts)
require.Error(t, err)
}
Loading