Skip to content
New issue

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

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

Already on GitHub? # to your account

Cannot open encrypted DB after writing with StreamWriter #1144

Closed
danielmai opened this issue Dec 5, 2019 · 0 comments · Fixed by #1146
Closed

Cannot open encrypted DB after writing with StreamWriter #1144

danielmai opened this issue Dec 5, 2019 · 0 comments · Fixed by #1146
Assignees
Labels
area/crash This issue causes a panic or some other of exception that causes a crash. kind/bug Something is broken. priority/P0 Critical issue that requires immediate attention. status/accepted We accept to investigate or work on it.

Comments

@danielmai
Copy link
Contributor

What version of Go are you using (go version)?

$ go version
go version go1.13.3 linux/amd64

What version of Badger are you using?

Badger v2.0.0

Does this issue reproduce with the latest master?

Yes (407e5bd)

What are the hardware specifications of the machine (RAM, OS, Disk)?

Ubuntu Linux

What did you do?

I'm using StreamWriter to write to badger with encryption enabled.

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/dgraph-io/badger/v2"
	"github.com/dgraph-io/badger/v2/pb"
)

var (
	encrypt = flag.Bool("encrypt", true, "Enable encryption")
)

func check(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %v\n", msg, err)
	}
}

func main() {
	flag.Parse()
	opts := badger.DefaultOptions("/tmp/badger-test")
	log.Printf("Using encryption: %v\n", *encrypt)
	if *encrypt {
		opts = opts.WithEncryptionKey([]byte("badgerkey16bytes"))
	}
	db, err := badger.Open(opts)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	key := []byte("mykey")
	value := []byte("myvalue")

	list := &pb.KVList{}
	list.Kv = append(list.Kv, &pb.KV{
		Key:     key,
		Value:   value,
		Version: 20,
	})
	fmt.Printf("%+v\n", list.Kv)

	sw := db.NewStreamWriter()
	check(sw.Prepare(), "Prepare failed")
	check(sw.Write(list), "Write failed")
	check(sw.Flush(), "Flush failed")

	var val string
	err = db.View(func(txn *badger.Txn) error {
		item, err := txn.Get(key)
		if err != nil {
			return err
		}
		return item.Value(func(b []byte) error { val = string(b); return nil })
	})
	check(err, "Error while retrieving key")
	fmt.Printf("Value: %s\n", val)
}

What did you expect to see?

Running the program once works. Running it a second time to re-open the existing encrypted DB gives an error. I expect to successfully open the DB the second time around.

What did you see instead?

After a second run, the DB fails to open with an error message related to protos:

2019/12/05 15:00:45 proto: illegal wireType 7

github.com/dgraph-io/badger/v2/y.Wrap
	/home/dmai/go/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0/y/error.go:71
github.com/dgraph-io/badger/v2/y.Check
	/home/dmai/go/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0/y/error.go:43
github.com/dgraph-io/badger/v2/table.(*Table).readIndex
	/home/dmai/go/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0/table/table.go:349
github.com/dgraph-io/badger/v2/table.(*Table).initBiggestAndSmallest
	/home/dmai/go/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0/table/table.go:260
github.com/dgraph-io/badger/v2/table.OpenTable
	/home/dmai/go/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0/table/table.go:227
github.com/dgraph-io/badger/v2.newLevelsController.func1
	/home/dmai/go/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0/levels.go:162
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:1357

Doing this for several runs can give different error messages:

$ rm -r /tmp/badger-test; go run main.go; go run main.go
...
2019/12/05 15:02:15 proto: TableIndex: wiretype end group for non-group
$ rm -r /tmp/badger-test; go run main.go; go run main.go
...
2019/12/05 15:03:01 proto: TableIndex: illegal tag -483489005 (wire type 901633650128607389)
$ rm -r /tmp/badger-test; go run main.go; go run main.go
...
2019/12/05 15:03:13 proto: illegal wireType 6
$ rm -r /tmp/badger-test; go run main.go; go run main.go
...
2019/12/05 15:03:45 unexpected EOF

Additional notes

If encryption is enabled (run the sample program with --encrypt=false), then re-opening the DB works as expected.

@danielmai danielmai added the kind/bug Something is broken. label Dec 5, 2019
@danielmai danielmai changed the title Cannot re-open encrypted DB after writing with StreamWriter Cannot open encrypted DB after writing with StreamWriter Dec 5, 2019
@lgalatin lgalatin assigned lgalatin and jarifibrahim and unassigned lgalatin Dec 5, 2019
@jarifibrahim jarifibrahim added area/crash This issue causes a panic or some other of exception that causes a crash. priority/P0 Critical issue that requires immediate attention. status/accepted We accept to investigate or work on it. labels Dec 6, 2019
jarifibrahim pushed a commit that referenced this issue Dec 6, 2019
We store information about tables in the table manifest. When stream
writer would create a new table, it wouldn't store the key ID for the
respective table in the manifest file. This commit fixes it.

Fixes #1144
manishrjain pushed a commit to outcaste-io/outserv that referenced this issue Jul 6, 2022
We store information about tables in the table manifest. When stream
writer would create a new table, it wouldn't store the key ID for the
respective table in the manifest file. This commit fixes it.

Fixes hypermodeinc/badger#1144
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area/crash This issue causes a panic or some other of exception that causes a crash. kind/bug Something is broken. priority/P0 Critical issue that requires immediate attention. status/accepted We accept to investigate or work on it.
Development

Successfully merging a pull request may close this issue.

3 participants