From e392dc5bf7fe4acfc8c1c1cf535f227cb95617d8 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Tue, 3 Mar 2020 18:02:10 +0100 Subject: [PATCH 01/12] Skip hitting the store if the query is in future. (#1929) Signed-off-by: Goutham Veeramachaneni --- pkg/util/time.go | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 pkg/util/time.go diff --git a/pkg/util/time.go b/pkg/util/time.go new file mode 100644 index 000000000..f0e8f39b7 --- /dev/null +++ b/pkg/util/time.go @@ -0,0 +1,7 @@ +package util + +import "time" + +func TimeMilliseconds(t time.Time) int64 { + return t.UnixNano() / int64(time.Millisecond/time.Nanosecond) +} From 20501e233456da219c4e5729bd15037dd5960ed4 Mon Sep 17 00:00:00 2001 From: Sandeep Sukhani Date: Fri, 13 Mar 2020 22:44:36 +0530 Subject: [PATCH 02/12] query time filtering of chunks for pending delete requests (#2214) * query time filtering of chunks for pending delete requests Signed-off-by: Sandeep Sukhani * changes suggested from PR review moved code for query time filtering from stores to querier moved DeleteStore and TombstonesLoader to purger package Signed-off-by: Sandeep Sukhani * simplified DeletedSeriesIterator Signed-off-by: Sandeep Sukhani * checking whether seeked point is deleted before calling Next in DeletedSeriesIterator.Seek Signed-off-by: Sandeep Sukhani * add tests for DeletedSeriesIterator Signed-off-by: Sandeep Sukhani * changed tombstonesReloadDuration to 5 mins Signed-off-by: Sandeep Sukhani --- pkg/util/time.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/util/time.go b/pkg/util/time.go index f0e8f39b7..3d2b22bd6 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -1,7 +1,27 @@ package util -import "time" +import ( + "math" + "net/http" + "strconv" + "time" + + "github.com/weaveworks/common/httpgrpc" +) func TimeMilliseconds(t time.Time) int64 { return t.UnixNano() / int64(time.Millisecond/time.Nanosecond) } + +// ParseTime parses the string into an int64, milliseconds since epoch. +func ParseTime(s string) (int64, error) { + if t, err := strconv.ParseFloat(s, 64); err == nil { + s, ns := math.Modf(t) + tm := time.Unix(int64(s), int64(ns*float64(time.Second))) + return TimeMilliseconds(tm), nil + } + if t, err := time.Parse(time.RFC3339Nano, s); err == nil { + return TimeMilliseconds(t), nil + } + return 0, httpgrpc.Errorf(http.StatusBadRequest, "cannot parse %q to a valid timestamp", s) +} From 0777c77044806ac5b24a543cd8f282be9e5980dd Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Mon, 25 May 2020 11:06:06 +0200 Subject: [PATCH 03/12] Add blocks storage consistency check in the querier (#2593) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added blocks storage consistency check in the querier Signed-off-by: Marco Pracucci * Log each time a consistency check fails Signed-off-by: Marco Pracucci * Log the trace ID too when logging consistency check failure Signed-off-by: Marco Pracucci * Update pkg/querier/blocks_consistency_checker.go Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update pkg/querier/blocks_consistency_checker.go Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update pkg/querier/blocks_consistency_checker.go Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update pkg/querier/blocks_consistency_checker.go Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Merged TimeMillisecond() and TimeToMillis() utility functions together Signed-off-by: Marco Pracucci * Disabled consistency check by default Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný --- pkg/util/time.go | 17 +++++++++++++---- pkg/util/time_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 pkg/util/time_test.go diff --git a/pkg/util/time.go b/pkg/util/time.go index 3d2b22bd6..fc2b46a1f 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -9,8 +9,17 @@ import ( "github.com/weaveworks/common/httpgrpc" ) -func TimeMilliseconds(t time.Time) int64 { - return t.UnixNano() / int64(time.Millisecond/time.Nanosecond) +const ( + nanosecondsInMillisecond = int64(time.Millisecond / time.Nanosecond) +) + +func TimeToMillis(t time.Time) int64 { + return t.UnixNano() / nanosecondsInMillisecond +} + +// TimeFromMillis is a helper to turn milliseconds -> time.Time +func TimeFromMillis(ms int64) time.Time { + return time.Unix(0, ms*nanosecondsInMillisecond) } // ParseTime parses the string into an int64, milliseconds since epoch. @@ -18,10 +27,10 @@ func ParseTime(s string) (int64, error) { if t, err := strconv.ParseFloat(s, 64); err == nil { s, ns := math.Modf(t) tm := time.Unix(int64(s), int64(ns*float64(time.Second))) - return TimeMilliseconds(tm), nil + return TimeToMillis(tm), nil } if t, err := time.Parse(time.RFC3339Nano, s); err == nil { - return TimeMilliseconds(t), nil + return TimeToMillis(t), nil } return 0, httpgrpc.Errorf(http.StatusBadRequest, "cannot parse %q to a valid timestamp", s) } diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go new file mode 100644 index 000000000..9899c4bdb --- /dev/null +++ b/pkg/util/time_test.go @@ -0,0 +1,25 @@ +package util + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestTimeFromMillis(t *testing.T) { + var testExpr = []struct { + input int64 + expected time.Time + }{ + {input: 1000, expected: time.Unix(1, 0)}, + {input: 1500, expected: time.Unix(1, 500*nanosecondsInMillisecond)}, + } + + for i, c := range testExpr { + t.Run(string(i), func(t *testing.T) { + res := TimeFromMillis(c.input) + require.Equal(t, c.expected, res) + }) + } +} From 8c4799c57e86df8450a6bac462f7c5923b6c74ff Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 4 Jun 2020 16:47:28 +0200 Subject: [PATCH 04/12] Added jitter to blocks storage period bucket scans (#2693) Signed-off-by: Marco Pracucci --- pkg/util/time.go | 8 ++++++++ pkg/util/time_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index fc2b46a1f..aeed6ab70 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -2,6 +2,7 @@ package util import ( "math" + "math/rand" "net/http" "strconv" "time" @@ -34,3 +35,10 @@ func ParseTime(s string) (int64, error) { } return 0, httpgrpc.Errorf(http.StatusBadRequest, "cannot parse %q to a valid timestamp", s) } + +func DurationWithJitter(input time.Duration, variancePerc float64) time.Duration { + variance := int64(float64(input) * variancePerc) + jitter := rand.Int63n(variance*2) - variance + + return input + time.Duration(jitter) +} diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index 9899c4bdb..ba160145a 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -23,3 +24,13 @@ func TestTimeFromMillis(t *testing.T) { }) } } + +func TestDurationWithJitter(t *testing.T) { + const numRuns = 1000 + + for i := 0; i < numRuns; i++ { + actual := DurationWithJitter(time.Minute, 0.5) + assert.GreaterOrEqual(t, int64(actual), int64(30*time.Second)) + assert.LessOrEqual(t, int64(actual), int64(90*time.Second)) + } +} From 85f685f077a777b26d6cac34dffb8a07789105b1 Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 2 Jul 2020 18:39:58 +0200 Subject: [PATCH 05/12] Make store-gateway mandatory when running the blocks storage (#2822) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Removed blocks bucket store support from querier Signed-off-by: Marco Pracucci * Updated documentation Signed-off-by: Marco Pracucci * Added PR number to CHANGELOG entry Signed-off-by: Marco Pracucci * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Updated doc Signed-off-by: Marco Pracucci * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Updated doc Signed-off-by: Marco Pracucci * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Update docs/operations/blocks-storage.template Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný * Updated doc Signed-off-by: Marco Pracucci Co-authored-by: Peter Štibraný --- pkg/util/time.go | 5 +++++ pkg/util/time_test.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index aeed6ab70..7a9a840b8 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -37,6 +37,11 @@ func ParseTime(s string) (int64, error) { } func DurationWithJitter(input time.Duration, variancePerc float64) time.Duration { + // No duration? No jitter. + if input == 0 { + return 0 + } + variance := int64(float64(input) * variancePerc) jitter := rand.Int63n(variance*2) - variance diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index ba160145a..a088a8706 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -34,3 +34,7 @@ func TestDurationWithJitter(t *testing.T) { assert.LessOrEqual(t, int64(actual), int64(90*time.Second)) } } + +func TestDurationWithJitter_ZeroInputDuration(t *testing.T) { + assert.Equal(t, time.Duration(0), DurationWithJitter(time.Duration(0), 0.5)) +} From 60ff35835c18de906b67672a242f2411ec03d6da Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Mon, 10 Aug 2020 14:25:44 +0200 Subject: [PATCH 06/12] Fixed query start/end timestamp rounding (#2990) * Fixed query start/end timestamp rounding Signed-off-by: Marco Pracucci * Added integration test Signed-off-by: Marco Pracucci * Fixed linter Signed-off-by: Marco Pracucci * Fixed integration test Signed-off-by: Marco Pracucci * Improved tests Signed-off-by: Marco Pracucci --- pkg/util/time.go | 1 + pkg/util/time_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index 7a9a840b8..45a462456 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -27,6 +27,7 @@ func TimeFromMillis(ms int64) time.Time { func ParseTime(s string) (int64, error) { if t, err := strconv.ParseFloat(s, 64); err == nil { s, ns := math.Modf(t) + ns = math.Round(ns*1000) / 1000 tm := time.Unix(int64(s), int64(ns*float64(time.Second))) return TimeToMillis(tm), nil } diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index a088a8706..ecccb7e0b 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -38,3 +38,53 @@ func TestDurationWithJitter(t *testing.T) { func TestDurationWithJitter_ZeroInputDuration(t *testing.T) { assert.Equal(t, time.Duration(0), DurationWithJitter(time.Duration(0), 0.5)) } + +func TestParseTime(t *testing.T) { + var tests = []struct { + input string + fail bool + result time.Time + }{ + { + input: "", + fail: true, + }, { + input: "abc", + fail: true, + }, { + input: "30s", + fail: true, + }, { + input: "123", + result: time.Unix(123, 0), + }, { + input: "123.123", + result: time.Unix(123, 123000000), + }, { + input: "2015-06-03T13:21:58.555Z", + result: time.Unix(1433337718, 555*time.Millisecond.Nanoseconds()), + }, { + input: "2015-06-03T14:21:58.555+01:00", + result: time.Unix(1433337718, 555*time.Millisecond.Nanoseconds()), + }, { + // Test nanosecond rounding. + input: "2015-06-03T13:21:58.56789Z", + result: time.Unix(1433337718, 567*1e6), + }, { + // Test float rounding. + input: "1543578564.705", + result: time.Unix(1543578564, 705*1e6), + }, + } + + for _, test := range tests { + ts, err := ParseTime(test.input) + if test.fail { + require.Error(t, err) + continue + } + + require.NoError(t, err) + assert.Equal(t, TimeToMillis(test.result), ts) + } +} From ba5b4b7c832e8b784a277c2951d507ca75c5ec8b Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Tue, 18 Aug 2020 12:56:00 +0200 Subject: [PATCH 07/12] Make tests build on go1.15 (#3052) My machine was upgraded to go1.15 and tests were failing with "conversion from int to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)" Signed-off-by: Goutham Veeramachaneni --- pkg/util/time_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index ecccb7e0b..ea10ac748 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -1,6 +1,7 @@ package util import ( + "fmt" "testing" "time" @@ -18,7 +19,7 @@ func TestTimeFromMillis(t *testing.T) { } for i, c := range testExpr { - t.Run(string(i), func(t *testing.T) { + t.Run(fmt.Sprint(i), func(t *testing.T) { res := TimeFromMillis(c.input) require.Equal(t, c.expected, res) }) From 1ea4052d47ee0c1fefe8600b552476e74a597d45 Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Fri, 6 Nov 2020 08:39:30 +0100 Subject: [PATCH 08/12] Enforced -querier.max-query-lookback in the query-frontend for range queries (#3458) * Enforced -querier.max-query-lookback in the query-frontend for range queries Signed-off-by: Marco Pracucci * Addressed review comments Signed-off-by: Marco Pracucci --- pkg/util/time.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index 45a462456..7b55613e6 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/prometheus/common/model" "github.com/weaveworks/common/httpgrpc" ) @@ -23,6 +24,16 @@ func TimeFromMillis(ms int64) time.Time { return time.Unix(0, ms*nanosecondsInMillisecond) } +// FormatTimeMillis returns a human readable version of the input time (in milliseconds). +func FormatTimeMillis(ms int64) string { + return TimeFromMillis(ms).String() +} + +// FormatTimeModel returns a human readable version of the input time. +func FormatTimeModel(t model.Time) string { + return TimeFromMillis(int64(t)).String() +} + // ParseTime parses the string into an int64, milliseconds since epoch. func ParseTime(s string) (int64, error) { if t, err := strconv.ParseFloat(s, 64); err == nil { From d82c95881fac3456f55012ea2b583cda1e1eb21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0tibran=C3=BD?= Date: Mon, 15 Feb 2021 13:20:23 +0100 Subject: [PATCH 09/12] HA tracker KV store cleanup (#3809) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Cleanup obsolete HA replicas from KV store. Signed-off-by: Peter Štibraný * CHANGELOG.md entry. Signed-off-by: Peter Štibraný * Add metric to see how many cleanup cycles run. Signed-off-by: Peter Štibraný * Don't forget to start cleanup loop. Signed-off-by: Peter Štibraný * Before returning from loop, wait for cleanup loop to stop. Signed-off-by: Peter Štibraný * Fix wait group usage. Signed-off-by: Peter Štibraný * Keep cleanup disabled by default. Signed-off-by: Peter Štibraný * Address review feedback. Signed-off-by: Peter Štibraný * Remove flag to enable HA pairs cleanup. Signed-off-by: Peter Štibraný * Improve metric names. Signed-off-by: Peter Štibraný --- pkg/util/time.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index 7b55613e6..24054a342 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -48,6 +48,7 @@ func ParseTime(s string) (int64, error) { return 0, httpgrpc.Errorf(http.StatusBadRequest, "cannot parse %q to a valid timestamp", s) } +// DurationWithJitter returns random duration from "input - input*variance" to "input + input*variance" interval. func DurationWithJitter(input time.Duration, variancePerc float64) time.Duration { // No duration? No jitter. if input == 0 { From b613c5a63ac138366739e87c807d1eec69e774af Mon Sep 17 00:00:00 2001 From: Steve Simpson <78375245+stevesg@users.noreply.github.com> Date: Tue, 23 Feb 2021 11:02:34 +0100 Subject: [PATCH 10/12] Apply jitter to the configured timeout for performing idle compactions. (#3850) * Apply jitter to the configured timeout for performing idle compactions. Attempt to prevent idle compaction from happening concurrently by introducing a 25% jitter to the configured timeout. This jitter only increases the timeout, such that the configured minimum timeout is always adhered to. Signed-off-by: Steve Simpson * Update Changelog. Signed-off-by: Steve Simpson * Fix race detected in unit test TestIngesterCompactIdleBlock. Move the setting of TSDB.compactionIdleTimeout just after struct creation. This avoids a race condition in the unit test because it makes calls to compactBlocks directly. Signed-off-by: Steve Simpson * Update configuration flag documentation. Signed-off-by: Steve Simpson Co-authored-by: Marco Pracucci --- pkg/util/time.go | 13 +++++++++++++ pkg/util/time_test.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index 24054a342..58f951a8b 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -60,3 +60,16 @@ func DurationWithJitter(input time.Duration, variancePerc float64) time.Duration return input + time.Duration(jitter) } + +// DurationWithPositiveJitter returns random duration from "input" to "input + input*variance" interval. +func DurationWithPositiveJitter(input time.Duration, variancePerc float64) time.Duration { + // No duration? No jitter. + if input == 0 { + return 0 + } + + variance := int64(float64(input) * variancePerc) + jitter := rand.Int63n(variance) + + return input + time.Duration(jitter) +} diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index ea10ac748..700fe55fa 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -40,6 +40,20 @@ func TestDurationWithJitter_ZeroInputDuration(t *testing.T) { assert.Equal(t, time.Duration(0), DurationWithJitter(time.Duration(0), 0.5)) } +func TestDurationWithPositiveJitter(t *testing.T) { + const numRuns = 1000 + + for i := 0; i < numRuns; i++ { + actual := DurationWithPositiveJitter(time.Minute, 0.5) + assert.GreaterOrEqual(t, int64(actual), int64(60*time.Second)) + assert.LessOrEqual(t, int64(actual), int64(90*time.Second)) + } +} + +func TestDurationWithPositiveJitter_ZeroInputDuration(t *testing.T) { + assert.Equal(t, time.Duration(0), DurationWithPositiveJitter(time.Duration(0), 0.5)) +} + func TestParseTime(t *testing.T) { var tests = []struct { input string From 82beec39793774d06d54621c5780e82a9f060f3f Mon Sep 17 00:00:00 2001 From: Steve Simpson Date: Fri, 9 Jul 2021 14:19:55 +0200 Subject: [PATCH 11/12] Allow disabling of ring heartbeats by setting relevant options to zero. (#4344) * Allow disabling of ring heartbeats by setting relevant options to zero. Signed-off-by: Steve Simpson * Review comments. Signed-off-by: Steve Simpson --- pkg/util/time.go | 11 +++++++++++ pkg/util/time_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index 58f951a8b..8816b1d7d 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -73,3 +73,14 @@ func DurationWithPositiveJitter(input time.Duration, variancePerc float64) time. return input + time.Duration(jitter) } + +// NewDisableableTicker essentially wraps NewTicker but allows the ticker to be disabled by passing +// zero duration as the interval. Returns a function for stopping the ticker, and the ticker channel. +func NewDisableableTicker(interval time.Duration) (func(), <-chan time.Time) { + if interval == 0 { + return func() {}, nil + } + + tick := time.NewTicker(interval) + return func() { tick.Stop() }, tick.C +} diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index 700fe55fa..ab1da4a85 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -103,3 +103,31 @@ func TestParseTime(t *testing.T) { assert.Equal(t, TimeToMillis(test.result), ts) } } + +func TestNewDisableableTicker_Enabled(t *testing.T) { + stop, ch := NewDisableableTicker(10 * time.Millisecond) + defer stop() + + time.Sleep(100 * time.Millisecond) + + select { + case <-ch: + break + default: + t.Error("ticker should have ticked when enabled") + } +} + +func TestNewDisableableTicker_Disabled(t *testing.T) { + stop, ch := NewDisableableTicker(0) + defer stop() + + time.Sleep(100 * time.Millisecond) + + select { + case <-ch: + t.Error("ticker should not have ticked when disabled") + default: + break + } +} From 4a74065da271221a3d827c83a819861e8b7e331c Mon Sep 17 00:00:00 2001 From: Tyler Reid Date: Thu, 2 Sep 2021 16:12:57 -0500 Subject: [PATCH 12/12] Move time to it's own package and make generic Signed-off-by: Tyler Reid --- {pkg/util => time}/time.go | 16 ++++++++-------- {pkg/util => time}/time_test.go | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) rename {pkg/util => time}/time.go (88%) rename {pkg/util => time}/time_test.go (96%) diff --git a/pkg/util/time.go b/time/time.go similarity index 88% rename from pkg/util/time.go rename to time/time.go index 8816b1d7d..05e89da9d 100644 --- a/pkg/util/time.go +++ b/time/time.go @@ -1,4 +1,4 @@ -package util +package time import ( "math" @@ -15,23 +15,23 @@ const ( nanosecondsInMillisecond = int64(time.Millisecond / time.Nanosecond) ) -func TimeToMillis(t time.Time) int64 { +func ToMillis(t time.Time) int64 { return t.UnixNano() / nanosecondsInMillisecond } -// TimeFromMillis is a helper to turn milliseconds -> time.Time -func TimeFromMillis(ms int64) time.Time { +// FromMillis is a helper to turn milliseconds -> time.Time +func FromMillis(ms int64) time.Time { return time.Unix(0, ms*nanosecondsInMillisecond) } // FormatTimeMillis returns a human readable version of the input time (in milliseconds). func FormatTimeMillis(ms int64) string { - return TimeFromMillis(ms).String() + return FromMillis(ms).String() } // FormatTimeModel returns a human readable version of the input time. func FormatTimeModel(t model.Time) string { - return TimeFromMillis(int64(t)).String() + return FromMillis(int64(t)).String() } // ParseTime parses the string into an int64, milliseconds since epoch. @@ -40,10 +40,10 @@ func ParseTime(s string) (int64, error) { s, ns := math.Modf(t) ns = math.Round(ns*1000) / 1000 tm := time.Unix(int64(s), int64(ns*float64(time.Second))) - return TimeToMillis(tm), nil + return ToMillis(tm), nil } if t, err := time.Parse(time.RFC3339Nano, s); err == nil { - return TimeToMillis(t), nil + return ToMillis(t), nil } return 0, httpgrpc.Errorf(http.StatusBadRequest, "cannot parse %q to a valid timestamp", s) } diff --git a/pkg/util/time_test.go b/time/time_test.go similarity index 96% rename from pkg/util/time_test.go rename to time/time_test.go index ab1da4a85..b5b572d8e 100644 --- a/pkg/util/time_test.go +++ b/time/time_test.go @@ -1,4 +1,4 @@ -package util +package time import ( "fmt" @@ -20,7 +20,7 @@ func TestTimeFromMillis(t *testing.T) { for i, c := range testExpr { t.Run(fmt.Sprint(i), func(t *testing.T) { - res := TimeFromMillis(c.input) + res := FromMillis(c.input) require.Equal(t, c.expected, res) }) } @@ -100,7 +100,7 @@ func TestParseTime(t *testing.T) { } require.NoError(t, err) - assert.Equal(t, TimeToMillis(test.result), ts) + assert.Equal(t, ToMillis(test.result), ts) } }