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

Improvements: Manual Memory allocation via Calloc #1459

Merged
merged 28 commits into from
Aug 19, 2020
Merged

Conversation

jarifibrahim
Copy link
Contributor

@jarifibrahim jarifibrahim commented Aug 14, 2020

This PR contains a bunch of improvements

  1. This PR reverts the following reverts (adds them back)
    • Revert "Compress/Encrypt Blocks in the background 6001230
    • Revert "Buffer pool for decompression 800305e
    • Revert "fix: Fix race condition in block.incRef 63d9309
    • Revert "add assert to check integer overflow for table size e0d058c
  2. Calloc and Free for memory allocation
    • The block buffers for decompression and encryption are now allocated using calloc and free. The y/calloc.go file contains the code.

The compaction changes are moved to a separate PR for easier cherry-picking #1466


This change is Reviewable

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Run a whole bunch of tests, really test this thoroughly.
Bring it to Dgraph master and test there too.
Test Streams, maybe it has some leaks with table builders.
And do add the onReject func in Ristretto.

Reviewed 13 of 23 files at r1, 12 of 12 files at r2, 1 of 1 files at r3.
Reviewable status: all files reviewed, 13 unresolved discussions (waiting on @ashish-goswami and @jarifibrahim)


db.go, line 197 at r2 (raw file):

	// It's okay to have zero compactors which will disable all compactions but
	// we cannot have just one compactor otherwise we will end up with all data
	// one level 2.

on level 2.


levels.go, line 1040 at r3 (raw file):

		var timeStart time.Time
		{
			s.kv.opt.Infof("STALLED STALLED STALLED: %v\n", time.Since(s.lastUnstalled))

Debugf.


options.go, line 464 at r3 (raw file):

// Setting this to zero stops compactions, which could eventually cause writes to block forever.
//
// The default value of NumCompactors is 2. One is dedicated just for L0.

L0 and L1.


value.go, line 109 at r3 (raw file):

	hash := crc32.New(y.CastagnoliCrcTable)

Remove vert space


value.go, line 124 at r3 (raw file):

		eBuf = append(eBuf, e.Key...)
		eBuf = append(eBuf, e.Value...)
		if err := y.XORBlockStream(writer, eBuf, lf.dataKey.Data, lf.generateIV(offset)); err != nil {

100 chars.


badger/cmd/write_bench.go, line 311 at r3 (raw file):

			return
		case <-t.C:
			// txn := db.NewTransaction(false)

Remove these changes.


table/builder.go, line 44 at r3 (raw file):

	// When a block is encrypted, it's length increases. We add 200 bytes of padding to
	// handle cases when block size increases. This is an approximate number.
	padding = 200

256


table/builder.go, line 98 at r3 (raw file):

func NewTableBuilder(opts Options) *Builder {
	b := &Builder{
		// Additional 5 MB to store index (approximate).

Update this line.


table/builder.go, line 492 at r3 (raw file):

	y.AssertTrue(cap(dst)-len(dst) >= len(iv))
	dst = append(dst, iv...)
	// if !viaC || cap(data)-len(data) >= len(iv) {

Remove.


table/table.go, line 599 at r3 (raw file):

		y.AssertTrue(blk.incrRef())

		// Manish: Set is not guaranteed to actually tell you whether block went into cache or not.

Add a TODO tag here. Also, a warning that this would result in memory leak.


table/table.go, line 813 at r3 (raw file):

	case options.Snappy:
		if sz, err := snappy.DecodedLen(b.data); err == nil {
			dst = y.Calloc(sz)

Irrespective of the error, always allocate dst.

err == nil { } else { dst = y.Calloc(len(b.data)*4) // Take a guess.


y/y.go, line 170 at r3 (raw file):

type Slice struct {
	buf []byte
	// viaC bool

Remove.


y/y.go, line 182 at r3 (raw file):

}

// func (s *Slice) Append(dst []byte) []byte {

Remove.

Copy link
Contributor Author

@jarifibrahim jarifibrahim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed all the review comments.

Reviewable status: 17 of 27 files reviewed, 13 unresolved discussions (waiting on @ashish-goswami and @manishrjain)


db.go, line 197 at r2 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

on level 2.

Done.


levels.go, line 1040 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Debugf.

Done.


options.go, line 464 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

L0 and L1.

Done.


value.go, line 109 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Remove vert space

Done.


value.go, line 124 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

100 chars.

Done.


badger/cmd/write_bench.go, line 311 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Remove these changes.

Done.


table/builder.go, line 44 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

256

Done.


table/builder.go, line 98 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Update this line.

Done.


table/builder.go, line 492 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Remove.

Done.


table/table.go, line 599 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Add a TODO tag here. Also, a warning that this would result in memory leak.

Done.


table/table.go, line 813 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Irrespective of the error, always allocate dst.

err == nil { } else { dst = y.Calloc(len(b.data)*4) // Take a guess.

Done.


y/y.go, line 170 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Remove.

Done.


y/y.go, line 182 at r3 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Remove.

Done.

@jarifibrahim jarifibrahim changed the title Improvements: Memory, Compaction, Builder Improvements: Memory, Builder Aug 19, 2020
@manishrjain manishrjain changed the title Improvements: Memory, Builder Improvements: Manual Memory allocation Aug 19, 2020
jarifibrahim pushed a commit that referenced this pull request Aug 19, 2020
Related to #1459

This PR contains the following changes to compactions
- Use a separate thread for compacting Level 0 and 1 and a separate one for other levels
- Pick levels to compact based on score.
- Stall Level 0 if compactions cannot keep up (we had added this in #1186)
- Limit the number of open table builders to 5 in compactions.
@manishrjain manishrjain changed the title Improvements: Manual Memory allocation Improvements: Manual Memory allocation via Calloc Aug 19, 2020
Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 13 of 25 files reviewed, 13 unresolved discussions (waiting on @ashish-goswami and @manishrjain)

@jarifibrahim jarifibrahim merged commit 5c1b4c8 into master Aug 19, 2020
@jarifibrahim jarifibrahim deleted the mrjn/calloc branch August 19, 2020 13:24
jarifibrahim pushed a commit that referenced this pull request Aug 20, 2020
Related to #1459

This PR contains the following changes to compactions
- Use a separate thread for compacting Level 0 and 1 and a separate one for other levels
- Pick levels to compact based on score.
- Stall Level 0 if compactions cannot keep up (we had added this in #1186)
- Limit the number of open table builders to 5 in compactions.

(cherry picked from commit 0b8eb4c)
jarifibrahim pushed a commit that referenced this pull request Aug 25, 2020
Related to #1459

This PR contains the following changes to compactions
- Use a separate thread for compacting Level 0 and 1 and a separate one for other levels
- Pick levels to compact based on score.
- Stall Level 0 if compactions cannot keep up (we had added this in #1186)
- Limit the number of open table builders to 5 in compactions.

(cherry picked from commit 0b8eb4c)
danielmai added a commit that referenced this pull request Aug 28, 2020
Building Badger without Cgo fails because of references to Zstd in
table/builder.go introduced in #1459 .

     $ CGO_ENABLED=0 go build -v .
     github.com/DataDog/zstd
     go build github.com/DataDog/zstd: build constraints exclude all Go files in
     /home/dmai/go/pkg/mod/github.com/!data!dog/zstd@v1.4.1

Moving zstd.CompressBound to y/zstd_cgo.go and y/zstd_nocgo.go fixes non-Cgo
builds. As is already the case, non-Cgo builds with compression must use snappy,
not Zstd, or calling y.ZSTDCompressBound without Cgo will panic.
danielmai added a commit that referenced this pull request Aug 28, 2020
Building Badger without Cgo fails because of references to Zstd in
table/builder.go introduced in #1459 .

     $ CGO_ENABLED=0 go build -v .
     github.com/DataDog/zstd
     go build github.com/DataDog/zstd: build constraints exclude all Go files in
     /home/dmai/go/pkg/mod/github.com/!data!dog/zstd@v1.4.1

Moving zstd.CompressBound to y/zstd_cgo.go and y/zstd_nocgo.go fixes non-Cgo
builds. As is already the case, non-Cgo builds with compression must use snappy,
not Zstd, or calling y.ZSTDCompressBound without Cgo will panic.
jarifibrahim pushed a commit that referenced this pull request Oct 2, 2020
Related to #1459

This PR contains the following changes to compactions
- Use a separate thread for compacting Level 0 and 1 and a separate one for other levels
- Pick levels to compact based on score.
- Stall Level 0 if compactions cannot keep up (we had added this in #1186)
- Limit the number of open table builders to 5 in compactions.
jarifibrahim pushed a commit that referenced this pull request Oct 2, 2020
This PR contains a  bunch of improvements
1. This PR reverts the following reverts (adds them back)
    - Revert "Compress/Encrypt Blocks in the background  6001230
    - Revert "Buffer pool for decompression 800305e
   - Revert "fix: Fix race condition in block.incRef 63d9309
    - Revert "add assert to check integer overflow for table size e0d058c
2. Calloc and Free for memory allocation
    - The block buffers for decompression and encryption are now allocated using calloc and free.
      The `y/calloc.go` file contains the code.

The compaction changes are moved to a separate PR for easier cherry-picking
#1466
jarifibrahim pushed a commit that referenced this pull request Oct 2, 2020
Building Badger without Cgo fails because of references to Zstd in
table/builder.go introduced in #1459 .

     $ CGO_ENABLED=0 go build -v .
     github.com/DataDog/zstd
     go build github.com/DataDog/zstd: build constraints exclude all Go files in
     /home/dmai/go/pkg/mod/github.com/!data!dog/zstd@v1.4.1

Moving zstd.CompressBound to y/zstd_cgo.go and y/zstd_nocgo.go fixes non-Cgo
builds. As is already the case, non-Cgo builds with compression must use snappy,
not Zstd, or calling y.ZSTDCompressBound without Cgo will panic.
manishrjain pushed a commit that referenced this pull request Oct 15, 2020
Related to #1459

This PR contains the following changes to compactions
- Use a separate thread for compacting Level 0 and 1 and a separate one for other levels
- Pick levels to compact based on score.
- Stall Level 0 if compactions cannot keep up (we had added this in #1186)
- Limit the number of open table builders to 5 in compactions.
manishrjain pushed a commit to outcaste-io/outserv that referenced this pull request Jul 6, 2022
Related to dgraph-io/badger#1459

This PR contains the following changes to compactions
- Use a separate thread for compacting Level 0 and 1 and a separate one for other levels
- Pick levels to compact based on score.
- Stall Level 0 if compactions cannot keep up (we had added this in #1186)
- Limit the number of open table builders to 5 in compactions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants