From bae89a63c78ffa588743b59a56ee1bdd9e998c59 Mon Sep 17 00:00:00 2001
From: Goutham Veeramachaneni <gouthamve@gmail.com>
Date: Fri, 11 Jan 2019 11:48:51 +0530
Subject: [PATCH] Review feedback

Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com>
---
 pkg/ingester/flush.go      | 20 +++++++-------------
 pkg/ingester/flush_test.go |  2 +-
 pkg/ingester/ingester.go   |  4 ++++
 3 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/pkg/ingester/flush.go b/pkg/ingester/flush.go
index e22f0045..d50186c3 100644
--- a/pkg/ingester/flush.go
+++ b/pkg/ingester/flush.go
@@ -81,16 +81,12 @@ func (i *Ingester) sweepInstance(instance *instance, immediate bool) {
 }
 
 func (i *Ingester) sweepStream(instance *instance, stream *stream, immediate bool) {
-	old := false
-	if len(stream.chunks) == 1 {
-		chunk := stream.chunks[0]
-		if i.cfg.MaxChunkIdle > 0 &&
-			time.Since(chunk.lastUpdated) > i.cfg.MaxChunkIdle {
-			old = true
-		}
+	if len(stream.chunks) == 0 {
+		return
 	}
 
-	if !old && len(stream.chunks) <= 1 && !immediate {
+	lastChunk := stream.chunks[len(stream.chunks)-1]
+	if time.Since(lastChunk.lastUpdated) < i.cfg.MaxChunkIdle && len(stream.chunks) <= 1 && !immediate {
 		return
 	}
 
@@ -171,13 +167,11 @@ func (i *Ingester) collectChunksToFlush(instance *instance, fp model.Fingerprint
 	if len(stream.chunks) == 1 {
 		chunk := &stream.chunks[0]
 
-		if i.cfg.MaxChunkIdle > 0 &&
-			time.Since(chunk.lastUpdated) > i.cfg.MaxChunkIdle {
+		if time.Since(chunk.lastUpdated) > i.cfg.MaxChunkIdle &&
+			chunk.flushed.IsZero() {
 			chunk.closed = true
 
-			if chunk.flushed.IsZero() {
-				return []*chunkDesc{chunk}, stream.labels
-			}
+			return []*chunkDesc{chunk}, stream.labels
 		}
 	}
 
diff --git a/pkg/ingester/flush_test.go b/pkg/ingester/flush_test.go
index 2af46ffa..1b497f6e 100644
--- a/pkg/ingester/flush_test.go
+++ b/pkg/ingester/flush_test.go
@@ -97,7 +97,7 @@ func (s *testStore) checkData(t *testing.T, userIDs []string, testData map[strin
 			streams = append(streams, stream)
 		}
 		sort.Slice(streams, func(i, j int) bool {
-			return streams[i].Labels < streams[i].Labels
+			return streams[i].Labels < streams[j].Labels
 		})
 
 		require.Equal(t, testData[userID], streams)
diff --git a/pkg/ingester/ingester.go b/pkg/ingester/ingester.go
index 9fd5b887..e678f3e8 100644
--- a/pkg/ingester/ingester.go
+++ b/pkg/ingester/ingester.go
@@ -71,6 +71,10 @@ type ChunkStore interface {
 
 // New makes a new Ingester.
 func New(cfg Config, store ChunkStore) (*Ingester, error) {
+	if cfg.MaxChunkIdle == 0 {
+		cfg.MaxChunkIdle = 30 * time.Minute
+	}
+
 	i := &Ingester{
 		cfg:         cfg,
 		instances:   map[string]*instance{},
-- 
GitLab