@@ -58,6 +58,16 @@ func (i *Ingester) sweepUsers(immediate bool) {
5858 }
5959}
6060
61+ type flushReason int
62+
63+ const (
64+ noFlush = iota
65+ reasonImmediate
66+ reasonMultipleChunksInSeries
67+ reasonAged
68+ reasonIdle
69+ )
70+
6171// sweepSeries schedules a series for flushing based on a set of criteria
6272//
6373// NB we don't close the head chunk here, as the series could wait in the queue
@@ -70,39 +80,42 @@ func (i *Ingester) sweepSeries(userID string, fp model.Fingerprint, series *memo
7080 firstTime := series .firstTime ()
7181 flush := i .shouldFlushSeries (series , immediate )
7282
73- if flush {
83+ if flush != noFlush {
7484 flushQueueIndex := int (uint64 (fp ) % uint64 (i .cfg .ConcurrentFlushes ))
75- util .Event ().Log ("msg" , "add to flush queue" , "userID" , userID , "numChunks " , len ( series . chunkDescs ) , "firstTime" , firstTime , "fp" , fp , "series" , series .metric )
85+ util .Event ().Log ("msg" , "add to flush queue" , "userID" , userID , "reason " , flush , "firstTime" , firstTime , "fp" , fp , "series" , series .metric )
7686 i .flushQueues [flushQueueIndex ].Enqueue (& flushOp {firstTime , userID , fp , immediate })
7787 }
7888}
7989
80- func (i * Ingester ) shouldFlushSeries (series * memorySeries , immediate bool ) bool {
90+ func (i * Ingester ) shouldFlushSeries (series * memorySeries , immediate bool ) flushReason {
91+ if immediate {
92+ return reasonImmediate
93+ }
8194 // Series should be scheduled for flushing if they have more than one chunk
82- if immediate || len (series .chunkDescs ) > 1 {
83- return true
95+ if len (series .chunkDescs ) > 1 {
96+ return reasonMultipleChunksInSeries
8497 }
8598
8699 // Or if the only existing chunk need flushing
87100 if len (series .chunkDescs ) > 0 {
88101 return i .shouldFlushChunk (series .chunkDescs [0 ])
89102 }
90103
91- return false
104+ return noFlush
92105}
93106
94- func (i * Ingester ) shouldFlushChunk (c * desc ) bool {
107+ func (i * Ingester ) shouldFlushChunk (c * desc ) flushReason {
95108 // Chunks should be flushed if they span longer than MaxChunkAge
96109 if c .LastTime .Sub (c .FirstTime ) > i .cfg .MaxChunkAge {
97- return true
110+ return reasonAged
98111 }
99112
100113 // Chunk should be flushed if their last update is older then MaxChunkIdle
101114 if model .Now ().Sub (c .LastUpdate ) > i .cfg .MaxChunkIdle {
102- return true
115+ return reasonIdle
103116 }
104117
105- return false
118+ return noFlush
106119}
107120
108121func (i * Ingester ) flushLoop (j int ) {
@@ -148,14 +161,15 @@ func (i *Ingester) flushUserSeries(userID string, fp model.Fingerprint, immediat
148161 }
149162
150163 userState .fpLocker .Lock (fp )
151- if ! i .shouldFlushSeries (series , immediate ) {
164+ reason := i .shouldFlushSeries (series , immediate )
165+ if reason == noFlush {
152166 userState .fpLocker .Unlock (fp )
153167 return nil
154168 }
155169
156170 // Assume we're going to flush everything, and maybe don't flush the head chunk if it doesn't need it.
157171 chunks := series .chunkDescs
158- if immediate || (len (chunks ) > 0 && i .shouldFlushChunk (series .head ())) {
172+ if immediate || (len (chunks ) > 0 && i .shouldFlushChunk (series .head ()) != noFlush ) {
159173 series .closeHead ()
160174 } else {
161175 chunks = chunks [:len (chunks )- 1 ]
@@ -171,7 +185,7 @@ func (i *Ingester) flushUserSeries(userID string, fp model.Fingerprint, immediat
171185 ctx , cancel := context .WithTimeout (ctx , i .cfg .FlushOpTimeout )
172186 defer cancel () // releases resources if slowOperation completes before timeout elapses
173187
174- util .Event ().Log ("msg" , "flush chunks" , "userID" , userID , "numChunks" , len (chunks ), "firstTime" , chunks [0 ].FirstTime , "fp" , fp , "series" , series .metric )
188+ util .Event ().Log ("msg" , "flush chunks" , "userID" , userID , "reason" , reason , " numChunks" , len (chunks ), "firstTime" , chunks [0 ].FirstTime , "fp" , fp , "series" , series .metric )
175189 err := i .flushChunks (ctx , fp , series .metric , chunks )
176190 if err != nil {
177191 util .Event ().Log ("msg" , "flush error" , "userID" , userID , "err" , err , "fp" , fp , "series" , series .metric )
0 commit comments