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

Txn is too big to fit into one request #441

Closed
rof20004 opened this issue Mar 16, 2018 · 8 comments
Closed

Txn is too big to fit into one request #441

rof20004 opened this issue Mar 16, 2018 · 8 comments

Comments

@rof20004
Copy link

I am trying to insert one million register but I got this error: Txn is too big to fit into one request

How can I solve this?

This is my code.

package main

import (
	"log"
	"strconv"

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

var database = &badger.DB{}

func initDB() {
	// Open the Badger database located in the /tmp/badger directory.
	// It will be created if it doesn't exist.
	opts := badger.DefaultOptions
	opts.Dir = "./badger"
	opts.ValueDir = "./badger"
	db, err := badger.Open(opts)
	if err != nil {
		log.Fatal(err)
	}
	database = db
}

func main() {
	initDB()

	log.Println("Writing to database...")

	database.Update(func(txn *badger.Txn) error {
		for i := 0; i < 1000000; i++ {
			key := "Character" + strconv.Itoa(i)
			value := strconv.Itoa(i)
			err := txn.Set([]byte(key), []byte(value))
			if err != nil {
				log.Fatalln(err)
			}
		}
		return nil
	})
}

@manishrjain
Copy link
Contributor

You can stop adding more keys and just commit the transaction. A single txn can't be too big, which is what the error is indicating.

@rof20004
Copy link
Author

@manishrjain Thanks and sorry.

@manishrjain
Copy link
Contributor

No worries, mate!

@AaronVasquez
Copy link

I'm running into this issue too. What is the recommended txn size?

@manishrjain
Copy link
Contributor

Badger would tell you when the txn size is too big by returning that error. In general, a txn should not exceed the size of a single memtable.

@leighmcculloch
Copy link

What's the right way to determine that error has occurred? From what I can see the error returned is errors.fundamental which isn't exported, so there's no way of doing if err == badger.ErrTxnTooBig to make a programatic decision to try again.

@manishrjain
Copy link
Contributor

ErrTxnTooBig = errors.New("Txn is too big to fit into one request")

This is the error that gets returned. It is exported. Here's the documentation around retrying:

https://github.com/dgraph-io/badger#read-write-transactions

@leighmcculloch
Copy link

Ah, thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants