diff --git a/go/vt/vttime/clock.go b/go/vt/vttime/clock.go deleted file mode 100644 index 1177be2f285..00000000000 --- a/go/vt/vttime/clock.go +++ /dev/null @@ -1,57 +0,0 @@ -/* -Copyright 2019 The Vitess Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package vttime - -import ( - "flag" - - "vitess.io/vitess/go/vt/log" -) - -var ( - // clockTypes maps implementation name to Clock object. - // Should only be written to at init() time. - clockTypes = make(map[string]Clock) - - // defaultClockType is the flag used to define the runtime clock type. - defaultClockType = flag.String("vttime_default_clock_type", "time", "The type of clock to be used by default by vttime library.") -) - -// Clock returns the current time. -type Clock interface { - // Now returns the current time as Interval. - // This method should be thread safe (i.e. multiple go routines can - // safely call this at the same time). - // The returned interval is guaranteed to have earliest <= latest, - // and all implementations enforce it. - Now() (Interval, error) -} - -// GetClock returns the global Clock object. -// Since it depends on flags, be sure to call this after they have been parsed -// (i.e. *not* in init() functions), otherwise this will panic. -func GetClock() Clock { - if !flag.Parsed() { - panic("GetClock() called before flags are parsed") - } - - c, ok := clockTypes[*defaultClockType] - if !ok { - log.Fatalf("No Clock type named %v", *defaultClockType) - } - return c -} diff --git a/go/vt/vttime/doc.go b/go/vt/vttime/doc.go deleted file mode 100644 index c7c6b621b27..00000000000 --- a/go/vt/vttime/doc.go +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright 2019 The Vitess Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package vttime contains the definitions and implementations for the Vitess -// time library. This package is based on Google's TrueTime, as described -// in this Spanner paper for instance: -// http://static.googleusercontent.com/media/research.google.com/en//archive/spanner-osdi2012.pdf -// -// The idea is that a timestamp is not enough, as clocks will drift -// apart between computers. However, it is usually possible to know -// how much drift happens. So instead of returning a timestamp that -// may be wrong, we return an interval [earliest, latest] with the -// following guarantees: -// - current time is greater or equal to 'earliest'. -// - current time is less or equal to 'latest'. -// -// When comparing two intervals, we know one of them is smaller if -// there is no overlap and it is before: -// [--------] -// [-----------] -// If there is overlap, we can't say for sure: -// [--------] -// [----------] -// -// However, if the goal is to be sure we are producing events that -// clients will know are chonologically ordered, it is then possible -// to sleep for a few milliseconds and guarantee that. This becomes -// handy in Paxos-like algorithms, for instance. See the paper for -// more details. -package vttime diff --git a/go/vt/vttime/interval.go b/go/vt/vttime/interval.go deleted file mode 100644 index 9446c17edb0..00000000000 --- a/go/vt/vttime/interval.go +++ /dev/null @@ -1,66 +0,0 @@ -/* -Copyright 2019 The Vitess Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package vttime - -import ( - "fmt" - "time" -) - -// Interval describes a time interval -type Interval struct { - earliest time.Time - latest time.Time -} - -// NewInterval creates a new Interval from the provided times. -// earliest has to be smaller or equal to latest, or an error is returned. -func NewInterval(earliest, latest time.Time) (Interval, error) { - if latest.Sub(earliest) < 0 { - return Interval{}, fmt.Errorf("NewInterval: earliest has to be smaller or equal to latest, but got: earliest=%v latest=%v", earliest, latest) - } - return Interval{ - earliest: earliest, - latest: latest, - }, nil -} - -// Earliest returns the earliest time in the interval. If Interval was -// from calling Now(), it is guaranteed the real time was greater or -// equal than Earliest(). -func (i Interval) Earliest() time.Time { - return i.earliest -} - -// Latest returns the latest time in the interval. If Interval was -// from calling Now(), it is guaranteed the real time was lesser or -// equal than Latest(). -func (i Interval) Latest() time.Time { - return i.latest -} - -// Less returns true if the provided interval is earlier than the parameter. -// Since both intervals are inclusive, comparison has to be strict. -func (i Interval) Less(other Interval) bool { - return i.latest.Sub(other.earliest) < 0 -} - -// IsValid returns true iff latest >= earliest, meaning the interval -// is actually a real valid interval. -func (i Interval) IsValid() bool { - return i.latest.Sub(i.earliest) >= 0 -} diff --git a/go/vt/vttime/interval_test.go b/go/vt/vttime/interval_test.go deleted file mode 100644 index 0fa6fcb2421..00000000000 --- a/go/vt/vttime/interval_test.go +++ /dev/null @@ -1,112 +0,0 @@ -/* -Copyright 2019 The Vitess Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package vttime - -import ( - "testing" - "time" -) - -func TestNewInterval(t *testing.T) { - e := time.Now() - l := e.Add(10 * time.Millisecond) - - // earliest < latest - i, err := NewInterval(e, l) - if err != nil { - t.Errorf("unexpected error in NewInterval: %v", err) - } - if got := i.Earliest(); got != e { - t.Errorf("invalid Earliest, got %v expected %v", got, e) - } - if got := i.Latest(); got != l { - t.Errorf("invalid Latest, got %v expected %v", got, l) - } - - // earliest == latest - l = e - if _, err := NewInterval(e, l); err != nil { - t.Errorf("unexpected error in NewInterval(l=e): %v", err) - } - - // earliest > latest -> error - l = e.Add(-10 * time.Millisecond) - if _, err := NewInterval(e, l); err == nil { - t.Errorf("unexpected nil error in NewInterval(l 1 || d.Seconds() < -1 { - t.Errorf("now very late: %v %v %v", i, now, d) - } - d = now.Sub(i.Latest()) - if d.Seconds() > 1 || d.Seconds() < -1 { - t.Errorf("now very early %v %v %v", now, i, d) - } -}