This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Fix deadlock when write queue full #1569
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"strconv" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/grafana/metrictank/cluster" | ||
|
@@ -43,7 +44,6 @@ type AggMetric struct { | |
ingestFromT0 uint32 | ||
ttl uint32 | ||
lastSaveStart uint32 // last chunk T0 that was added to the write Queue. | ||
lastSaveFinish uint32 // last chunk T0 successfully written to Cassandra. | ||
lastWrite uint32 // wall clock time of when last point was successfully added (possibly to the ROB) | ||
firstTs uint32 // timestamp of first point seen | ||
} | ||
|
@@ -92,14 +92,11 @@ func NewAggMetric(store Store, cachePusher cache.CachePusher, key schema.AMKey, | |
// Sync the saved state of a chunk by its T0. | ||
func (a *AggMetric) SyncChunkSaveState(ts uint32, sendPersist bool) ChunkSaveCallback { | ||
return func() { | ||
a.Lock() | ||
if ts > a.lastSaveFinish { | ||
a.lastSaveFinish = ts | ||
lastSaveStart := atomic.LoadUint32(&a.lastSaveStart) | ||
if ts > lastSaveStart { | ||
atomic.StoreUint32(&a.lastSaveStart, ts) | ||
} | ||
if ts > a.lastSaveStart { | ||
a.lastSaveStart = ts | ||
} | ||
a.Unlock() | ||
|
||
log.Debugf("AM: metric %s at chunk T0=%d has been saved.", a.key, ts) | ||
if sendPersist { | ||
SendPersistMessage(a.key.String(), ts) | ||
|
@@ -360,7 +357,8 @@ func (a *AggMetric) persist(pos int) { | |
chunk := a.chunks[pos] | ||
pre := time.Now() | ||
|
||
if a.lastSaveStart >= chunk.Series.T0 { | ||
lastSaveStart := atomic.LoadUint32(&a.lastSaveStart) | ||
if lastSaveStart >= chunk.Series.T0 { | ||
// this can happen if | ||
// a) there are 2 primary MT nodes both saving chunks to Cassandra | ||
// b) a primary failed and this node was promoted to be primary but metric consuming is lagging. | ||
|
@@ -391,7 +389,7 @@ func (a *AggMetric) persist(pos int) { | |
previousPos += len(a.chunks) | ||
} | ||
previousChunk := a.chunks[previousPos] | ||
for (previousChunk.Series.T0 < chunk.Series.T0) && (a.lastSaveStart < previousChunk.Series.T0) { | ||
for (previousChunk.Series.T0 < chunk.Series.T0) && (lastSaveStart < previousChunk.Series.T0) { | ||
Dieterbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Debugf("AM: persist(): old chunk needs saving. Adding %s:%d to writeQueue", a.key, previousChunk.Series.T0) | ||
cwr := NewChunkWriteRequest( | ||
a.SyncChunkSaveState(previousChunk.Series.T0, true), | ||
|
@@ -410,7 +408,7 @@ func (a *AggMetric) persist(pos int) { | |
} | ||
|
||
// Every chunk with a T0 <= this chunks' T0 is now either saved, or in the writeQueue. | ||
a.lastSaveStart = chunk.Series.T0 | ||
atomic.StoreUint32(&a.lastSaveStart, chunk.Series.T0) | ||
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. similar to my comment in SyncChunkSaveState, we need to account for others setting higher values using a swap loop. |
||
|
||
log.Debugf("AM: persist(): sending %d chunks to write queue", len(pending)) | ||
|
||
|
@@ -492,8 +490,7 @@ func (a *AggMetric) add(ts uint32, val float64) { | |
log.Debugf("AM: %s Add(): created first chunk with first point: %v", a.key, a.chunks[0]) | ||
a.lastWrite = uint32(time.Now().Unix()) | ||
if a.dropFirstChunk { | ||
a.lastSaveStart = t0 | ||
a.lastSaveFinish = t0 | ||
atomic.StoreUint32(&a.lastSaveStart, t0) | ||
} | ||
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. same here. need swap loop to account for chunksaves coming in concurrently. |
||
a.addAggregators(ts, val) | ||
return | ||
|
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.
race condition!
I think you need something like
(i took this from idx/memory.bumpLastUpdate())
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.
good catch, i'll update that