codenotary / immudb

immudb - immutable database based on zero trust, SQL/Key-Value/Document model, tamperproof, data change history
https://immudb.io
Other
8.54k stars 341 forks source link

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

Closed mitar closed 7 months ago

mitar commented 7 months ago

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).

jeroiraz commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

I made https://github.com/codenotary/immudb.io/pull/506 to update docs. Thanks.