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

Key commited in a transaction is not visible in the next transaction #1912

Closed
mitar opened this issue Jan 5, 2024 · 3 comments
Closed

Key commited in a transaction is not visible in the next transaction #1912

mitar opened this issue Jan 5, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@mitar
Copy link

mitar commented Jan 5, 2024

What happened

I made a transaction and committed a value under a key. In the next transaction that key is not visible. But it is visible outside of the transaction (when using db.Get).

What you expected to happen

I expect that committed data is available in transactions.

How to reproduce it (as minimally and precisely as possible)

package main

import (
	"context"
	"log"

	"github.com/codenotary/immudb/embedded/appendable"
	"github.com/codenotary/immudb/embedded/store"
)

func main() {
	opts := store.DefaultOptions()
	opts = opts.WithSyncFrequency(0)
	opts = opts.WithCompressionFormat(appendable.NoCompression)
	db, err := store.Open("/tmp/data", opts)
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	tx, err := db.NewTx(context.Background(), &store.TxOptions{Mode: store.ReadWriteTx})
	if err != nil {
		log.Fatal(err)
	}
	defer tx.Cancel()

	key := []byte("foo")
	value := []byte("bar")

	err = tx.Set(key, nil, value)
	if err != nil {
		log.Fatal(err)
	}

	_, err = tx.Commit(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	tx, err = db.NewTx(context.Background(), &store.TxOptions{Mode: store.ReadOnlyTx})
	if err != nil {
		log.Fatal(err)
	}

	ref, err := tx.Get(context.Background(), key)
	if err != nil {
		log.Fatal(err)
	}
	value, err = ref.Resolve()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%v", string(value))
}

Environment

I use Go with immudb embedded at version v1.9.0-RC2.0.20231220125802-d143b42683b7.

Additional info (any other context about the problem)

It might be that transactions in immudb work differently than they do in traditional databases. But there you can fetch data committed in prior transactions when you are inside a new transaction. They behave like a snapshot (also provide isolation against changes in other parallel transactions).

@mitar mitar added the bug Something isn't working label Jan 5, 2024
@jeroiraz
Copy link
Contributor

jeroiraz commented Jan 5, 2024

What happened

I made a transaction and committed a value under a key. In the next transaction that key is not visible. But it is visible outside of the transaction (when using db.Get).

What you expected to happen

I expect that committed data is available in transactions.

How to reproduce it (as minimally and precisely as possible)

package main

import (
	"context"
	"log"

	"github.com/codenotary/immudb/embedded/appendable"
	"github.com/codenotary/immudb/embedded/store"
)

func main() {
	opts := store.DefaultOptions()
	opts = opts.WithSyncFrequency(0)
	opts = opts.WithCompressionFormat(appendable.NoCompression)
	db, err := store.Open("/tmp/data", opts)
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	tx, err := db.NewTx(context.Background(), &store.TxOptions{Mode: store.ReadWriteTx})
	if err != nil {
		log.Fatal(err)
	}
	defer tx.Cancel()

	key := []byte("foo")
	value := []byte("bar")

	err = tx.Set(key, nil, value)
	if err != nil {
		log.Fatal(err)
	}

	_, err = tx.Commit(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	tx, err = db.NewTx(context.Background(), &store.TxOptions{Mode: store.ReadOnlyTx})
	if err != nil {
		log.Fatal(err)
	}

	ref, err := tx.Get(context.Background(), key)
	if err != nil {
		log.Fatal(err)
	}
	value, err = ref.Resolve()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%v", string(value))
}

Environment

I use Go with immudb embedded at version v1.9.0-RC2.0.20231220125802-d143b42683b7.

Additional info (any other context about the problem)

It might be that transactions in immudb work differently than they do in traditional databases. But there you can fetch data committed in prior transactions when you are inside a new transaction. They behave like a snapshot (also provide isolation against changes in other parallel transactions).

Hi @mitar, transactions in immudb work pretty much as you would expect from any other database. Transactions are full ACID compliant with strict isolation.

I think the only issue with the code above is that the transactions options struct is built in a raw manner without a helpful constructor. Then it's not imposing any requirement for the snapshot used for the second transaction

tx, err = immuStore.NewTx(context.Background(), store.DefaultTxOptions().WithMode(store.ReadOnlyTx))

Please take a look at here https://github.com/jeroiraz/immudb/blob/9cfa35003ce361c3df4d53465d41b724bcee5018/embedded/store/immustore_test.go#L3739

@mitar
Copy link
Author

mitar commented Jan 6, 2024

I see. Thanks.

Then docs should be updated. In docs, there is:

tx, err := st.NewTx(context.Background(), &store.TxOptions{Mode: store.ReadWriteTx})

As we see, this is not a problem in this concrete example in docs, but it leads to the error like I have made. Example should use store.DefaultTxOptions() to tell the (future) user about this.

@mitar
Copy link
Author

mitar commented Jan 6, 2024

I made codenotary/immudb.io#506 to update docs. Thanks.

@mitar mitar closed this as completed Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants