From 1ae0334e17c9d1f0b234eb0048a44eae902aecca Mon Sep 17 00:00:00 2001
From: Seva Poliakov <ctrlok@gmail.com>
Date: Wed, 8 Jul 2020 13:59:19 +0300
Subject: [PATCH] Fix graphite minus one timestamp (#609)

* fix graphite -1 timestamp

* format the graphite fix -1 timestamp
---
 lib/protoparser/graphite/parser_test.go  | 26 ++++++++++++++++++++++++
 lib/protoparser/graphite/streamparser.go |  2 +-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/lib/protoparser/graphite/parser_test.go b/lib/protoparser/graphite/parser_test.go
index ca8bcccb1..3eefef35a 100644
--- a/lib/protoparser/graphite/parser_test.go
+++ b/lib/protoparser/graphite/parser_test.go
@@ -1,7 +1,9 @@
 package graphite
 
 import (
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
 	"reflect"
+	"strings"
 	"testing"
 )
 
@@ -161,3 +163,27 @@ func TestRowsUnmarshalSuccess(t *testing.T) {
 		},
 	})
 }
+
+func Test_streamContext_Read(t *testing.T) {
+	f := func(s string, rowsExpected *Rows) {
+		t.Helper()
+		ctx := &streamContext{}
+		ctx.Read(strings.NewReader(s))
+		if len(ctx.Rows.Rows) != len(rowsExpected.Rows) {
+			t.Fatalf("different len of expected rows;\ngot\n%+v;\nwant\n%+v", ctx.Rows, rowsExpected.Rows)
+		}
+		if !reflect.DeepEqual(ctx.Rows.Rows, rowsExpected.Rows) {
+			t.Fatalf("unexpected rows;\ngot\n%+v;\nwant\n%+v", ctx.Rows.Rows, rowsExpected.Rows)
+		}
+	}
+
+	// -1 timestamp
+	currentTimestamp := int64(fasttime.UnixTimestamp())
+	f("aaa 1123", &Rows{
+		Rows: []Row{{
+			Metric:    "aaa",
+			Value:     1123,
+			Timestamp: currentTimestamp * 1000,
+		}},
+	})
+}
diff --git a/lib/protoparser/graphite/streamparser.go b/lib/protoparser/graphite/streamparser.go
index 825ffdcb7..cdca2a3ed 100644
--- a/lib/protoparser/graphite/streamparser.go
+++ b/lib/protoparser/graphite/streamparser.go
@@ -75,7 +75,7 @@ func (ctx *streamContext) Read(r io.Reader) bool {
 	currentTimestamp := int64(fasttime.UnixTimestamp())
 	for i := range rows {
 		r := &rows[i]
-		if r.Timestamp == 0 {
+		if r.Timestamp == 0 || r.Timestamp == -1 {
 			r.Timestamp = currentTimestamp
 		}
 	}