@@ -58,6 +58,16 @@ func (i *Ingester) sweepUsers(immediate bool) {
58
58
}
59
59
}
60
60
61
+ type flushReason int
62
+
63
+ const (
64
+ noFlush = iota
65
+ reasonImmediate
66
+ reasonMultipleChunksInSeries
67
+ reasonAged
68
+ reasonIdle
69
+ )
70
+
61
71
// sweepSeries schedules a series for flushing based on a set of criteria
62
72
//
63
73
// 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
70
80
firstTime := series .firstTime ()
71
81
flush := i .shouldFlushSeries (series , immediate )
72
82
73
- if flush {
83
+ if flush != noFlush {
74
84
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 )
76
86
i .flushQueues [flushQueueIndex ].Enqueue (& flushOp {firstTime , userID , fp , immediate })
77
87
}
78
88
}
79
89
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
+ }
81
94
// 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
84
97
}
85
98
86
99
// Or if the only existing chunk need flushing
87
100
if len (series .chunkDescs ) > 0 {
88
101
return i .shouldFlushChunk (series .chunkDescs [0 ])
89
102
}
90
103
91
- return false
104
+ return noFlush
92
105
}
93
106
94
- func (i * Ingester ) shouldFlushChunk (c * desc ) bool {
107
+ func (i * Ingester ) shouldFlushChunk (c * desc ) flushReason {
95
108
// Chunks should be flushed if they span longer than MaxChunkAge
96
109
if c .LastTime .Sub (c .FirstTime ) > i .cfg .MaxChunkAge {
97
- return true
110
+ return reasonAged
98
111
}
99
112
100
113
// Chunk should be flushed if their last update is older then MaxChunkIdle
101
114
if model .Now ().Sub (c .LastUpdate ) > i .cfg .MaxChunkIdle {
102
- return true
115
+ return reasonIdle
103
116
}
104
117
105
- return false
118
+ return noFlush
106
119
}
107
120
108
121
func (i * Ingester ) flushLoop (j int ) {
@@ -148,14 +161,15 @@ func (i *Ingester) flushUserSeries(userID string, fp model.Fingerprint, immediat
148
161
}
149
162
150
163
userState .fpLocker .Lock (fp )
151
- if ! i .shouldFlushSeries (series , immediate ) {
164
+ reason := i .shouldFlushSeries (series , immediate )
165
+ if reason == noFlush {
152
166
userState .fpLocker .Unlock (fp )
153
167
return nil
154
168
}
155
169
156
170
// Assume we're going to flush everything, and maybe don't flush the head chunk if it doesn't need it.
157
171
chunks := series .chunkDescs
158
- if immediate || (len (chunks ) > 0 && i .shouldFlushChunk (series .head ())) {
172
+ if immediate || (len (chunks ) > 0 && i .shouldFlushChunk (series .head ()) != noFlush ) {
159
173
series .closeHead ()
160
174
} else {
161
175
chunks = chunks [:len (chunks )- 1 ]
@@ -171,7 +185,7 @@ func (i *Ingester) flushUserSeries(userID string, fp model.Fingerprint, immediat
171
185
ctx , cancel := context .WithTimeout (ctx , i .cfg .FlushOpTimeout )
172
186
defer cancel () // releases resources if slowOperation completes before timeout elapses
173
187
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 )
175
189
err := i .flushChunks (ctx , fp , series .metric , chunks )
176
190
if err != nil {
177
191
util .Event ().Log ("msg" , "flush error" , "userID" , userID , "err" , err , "fp" , fp , "series" , series .metric )
0 commit comments