@@ -95,24 +95,25 @@ def writeCachedDataPoints():
9595
9696 cache = MetricCache ()
9797 while cache :
98- (metric , datapoints ) = cache .drain_metric ()
99- if metric is None :
100- # end the loop
101- break
98+ # First, create new metrics files, which is helpful for graphite-web
99+ while cache .new_metrics and (not CREATE_BUCKET or CREATE_BUCKET .peek (1 )):
100+ metric = cache .new_metrics .popleft ()
102101
103- dbFileExists = state .database .exists (metric )
102+ if metric not in cache :
103+ # This metric has already been drained. There's no sense in creating it.
104+ continue
104105
105- if not dbFileExists :
106- if CREATE_BUCKET and not CREATE_BUCKET .drain (1 ):
107- # If our tokenbucket doesn't have enough tokens available to create a new metric
108- # file then we'll just drop the metric on the ground and move on to the next
109- # metric.
110- # XXX This behavior should probably be configurable to no tdrop metrics
111- # when rate limiting unless our cache is too big or some other legit
112- # reason.
113- instrumentation .increment ('droppedCreates' )
106+ if state .database .exists (metric ):
114107 continue
115108
109+ if CREATE_BUCKET and not CREATE_BUCKET .drain (1 ):
110+ # This should never actually happen as no other thread should be
111+ # draining our tokens, and we just checked for a token.
112+ # Just put the new metric back in the create list and we'll try again
113+ # after writing an update.
114+ cache .new_metrics .appendleft (metric )
115+ break
116+
116117 archiveConfig = None
117118 xFilesFactor , aggregationMethod = None , None
118119
@@ -150,6 +151,18 @@ def writeCachedDataPoints():
150151 instrumentation .increment ('errors' )
151152 continue
152153
154+ # now drain and persist some data
155+ (metric , datapoints ) = cache .drain_metric ()
156+ if metric is None :
157+ # end the loop
158+ break
159+
160+ if not state .database .exists (metric ):
161+ # If we get here, the metric must still be in new_metrics. We're
162+ # creating too fast, and we'll drop this data.
163+ instrumentation .increment ('droppedCreates' )
164+ continue
165+
153166 # If we've got a rate limit configured lets makes sure we enforce it
154167 waitTime = 0
155168 if UPDATE_BUCKET :
0 commit comments