-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix deadlock in discard stats #1070
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1496998
Decouple compaction from flushing of discardStats
ashish-goswami 5870c76
Fix breaking tests
ashish-goswami 34237a9
Fix races in vlog tests
ashish-goswami 3a16d54
Address review comments
ashish-goswami c8f0d4e
Simplify code for flushing DiscardStats
ashish-goswami 12cea56
Fix race in discard tests
ashish-goswami 054ac4e
Address review comments
ashish-goswami ecadd42
Address review comments
ashish-goswami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package badger | |
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
|
@@ -470,23 +471,32 @@ func TestPersistLFDiscardStats(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
// wait for compaction to complete | ||
time.Sleep(1 * time.Second) | ||
time.Sleep(1 * time.Second) // wait for compaction to complete | ||
|
||
persistedMap := make(map[uint32]int64) | ||
db.vlog.lfDiscardStats.Lock() | ||
require.True(t, len(db.vlog.lfDiscardStats.m) > 0, "some discardStats should be generated") | ||
for k, v := range db.vlog.lfDiscardStats.m { | ||
persistedMap[k] = v | ||
} | ||
db.vlog.lfDiscardStats.updatesSinceFlush = discardStatsFlushThreshold + 1 | ||
db.vlog.lfDiscardStats.Unlock() | ||
|
||
// db.vlog.lfDiscardStats.updatesSinceFlush is already > discardStatsFlushThreshold, | ||
// send empty map to flushChan, so that latest discardStats map can be persisted. | ||
db.vlog.lfDiscardStats.flushChan <- map[uint32]int64{} | ||
time.Sleep(1 * time.Second) // Wait for map to be persisted. | ||
err = db.Close() | ||
require.NoError(t, err) | ||
|
||
db, err = Open(opt) | ||
require.NoError(t, err) | ||
defer db.Close() | ||
time.Sleep(1 * time.Second) // Wait for discardStats to be populated by populateDiscardStats(). | ||
db.vlog.lfDiscardStats.Lock() | ||
require.True(t, reflect.DeepEqual(persistedMap, db.vlog.lfDiscardStats.m), | ||
"Discard maps are not equal") | ||
db.vlog.lfDiscardStats.Unlock() | ||
} | ||
|
||
func TestChecksums(t *testing.T) { | ||
|
@@ -630,7 +640,6 @@ func TestPartialAppendToValueLog(t *testing.T) { | |
kv, err = Open(opts) | ||
require.NoError(t, err) | ||
checkKeys(t, kv, [][]byte{k3}) | ||
|
||
// Replay value log from beginning, badger head is past k2. | ||
require.NoError(t, kv.vlog.Close()) | ||
require.NoError(t, | ||
|
@@ -1003,14 +1012,12 @@ func TestTruncatedDiscardStat(t *testing.T) { | |
for i := uint32(0); i < uint32(20); i++ { | ||
stat[i] = 0 | ||
} | ||
// Set discard stats. | ||
db.vlog.lfDiscardStats = &lfDiscardStats{ | ||
m: stat, | ||
} | ||
db.vlog.lfDiscardStats.m = stat | ||
encodedDS, _ := json.Marshal(db.vlog.lfDiscardStats.m) | ||
entries := []*Entry{{ | ||
Key: y.KeyWithTs(lfDiscardStatsKey, 1), | ||
// Insert truncated discard stats. This is important. | ||
Value: db.vlog.encodedDiscardStats()[:10], | ||
Value: encodedDS[:10], | ||
}} | ||
// Push discard stats entry to the write channel. | ||
req, err := db.sendToWriteCh(entries) | ||
|
@@ -1059,14 +1066,14 @@ func TestDiscardStatsMove(t *testing.T) { | |
stat[i] = 0 | ||
} | ||
|
||
// Set discard stats. | ||
db.vlog.lfDiscardStats = &lfDiscardStats{ | ||
m: stat, | ||
} | ||
db.vlog.lfDiscardStats.Lock() | ||
db.vlog.lfDiscardStats.m = stat | ||
encodedDS, _ := json.Marshal(db.vlog.lfDiscardStats.m) | ||
db.vlog.lfDiscardStats.Unlock() | ||
entries := []*Entry{{ | ||
Key: y.KeyWithTs(lfDiscardStatsKey, 1), | ||
// The discard stat value is more than value threshold. | ||
Value: db.vlog.encodedDiscardStats(), | ||
Value: encodedDS, | ||
}} | ||
// Push discard stats entry to the write channel. | ||
req, err := db.sendToWriteCh(entries) | ||
|
@@ -1076,7 +1083,9 @@ func TestDiscardStatsMove(t *testing.T) { | |
// Unset discard stats. We've already pushed the stats. If we don't unset it then it will be | ||
// pushed again on DB close. Also, the first insertion was in vlog file 1, this insertion would | ||
// be in value log file 3. | ||
db.vlog.lfDiscardStats.Lock() | ||
db.vlog.lfDiscardStats.m = nil | ||
db.vlog.lfDiscardStats.Unlock() | ||
|
||
// Push more entries so that we get more than 1 value log files. | ||
require.NoError(t, db.Update(func(txn *Txn) error { | ||
|
@@ -1086,7 +1095,6 @@ func TestDiscardStatsMove(t *testing.T) { | |
require.NoError(t, db.Update(func(txn *Txn) error { | ||
e := NewEntry([]byte("ff"), []byte("1")) | ||
return txn.SetEntry(e) | ||
|
||
})) | ||
|
||
tr := trace.New("Badger.ValueLog", "GC") | ||
|
@@ -1096,8 +1104,13 @@ func TestDiscardStatsMove(t *testing.T) { | |
require.NoError(t, db.Close()) | ||
|
||
db, err = Open(ops) | ||
// discardStats will be populate using vlog.populateDiscardStats(), which pushes discard stats | ||
// to vlog.lfDiscardStats.flushChan. Hence wait for some time, for discard stats to be updated. | ||
time.Sleep(1 * time.Second) | ||
require.NoError(t, err) | ||
db.vlog.lfDiscardStats.Lock() | ||
require.Equal(t, stat, db.vlog.lfDiscardStats.m) | ||
db.vlog.lfDiscardStats.Unlock() | ||
require.NoError(t, db.Close()) | ||
} | ||
|
||
|
@@ -1109,11 +1122,13 @@ func TestBlockedDiscardStats(t *testing.T) { | |
db, err := Open(getTestOptions(dir)) | ||
require.NoError(t, err) | ||
// Set discard stats. | ||
db.vlog.lfDiscardStats = &lfDiscardStats{ | ||
m: map[uint32]int64{0: 0}, | ||
} | ||
db.vlog.lfDiscardStats.m = map[uint32]int64{0: 0} | ||
db.blockWrite() | ||
require.NoError(t, db.vlog.flushDiscardStats()) | ||
// Push discard stats more than the capacity of flushChan. This ensures at least one flush | ||
// operation completes successfully after the writes were blocked. | ||
for i := 0; i < cap(db.vlog.lfDiscardStats.flushChan)+2; i++ { | ||
db.vlog.lfDiscardStats.flushChan <- db.vlog.lfDiscardStats.m | ||
} | ||
db.unblockWrite() | ||
require.NoError(t, db.Close()) | ||
} | ||
|
@@ -1126,11 +1141,8 @@ func TestBlockedDiscardStatsOnClose(t *testing.T) { | |
|
||
db, err := Open(getTestOptions(dir)) | ||
require.NoError(t, err) | ||
// Set discard stats. | ||
db.vlog.lfDiscardStats = &lfDiscardStats{ | ||
m: map[uint32]int64{0: 0}, | ||
} | ||
// This is important. Set updateSinceFlush to discardStatsFlushThresold so | ||
db.vlog.lfDiscardStats.m = map[uint32]int64{0: 0} | ||
// This is important. Set updateSinceFlush to discardStatsFlushThreshold so | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also add a test that replicates the deadlock, and make sure it does not deadlock with this change? |
||
// that the next update call flushes the discard stats. | ||
db.vlog.lfDiscardStats.updatesSinceFlush = discardStatsFlushThreshold + 1 | ||
require.NoError(t, db.Close()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
2
here?