Skip to content

Commit 2f8cd09

Browse files
committed
timeutil/ptp: fix conversion from seconds to Time
Epic: none Release note (bug fix): since 22.2.0, using a PTP clock device (enabled by the --clock-device flag) would generate timestamps in the far future. It now generates the correct time. This could cause nodes to crash due to incorrect timestamps, or in the worst case irreversibly advance the cluster's HLC clock into the far future.
1 parent 36665f6 commit 2f8cd09

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

pkg/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ ALL_TESTS = [
694694
"//pkg/util/timeofday:timeofday_test",
695695
"//pkg/util/timetz:timetz_test",
696696
"//pkg/util/timeutil/pgdate:pgdate_test",
697+
"//pkg/util/timeutil/ptp:ptp_test",
697698
"//pkg/util/timeutil:timeutil_test",
698699
"//pkg/util/tochar:tochar_test",
699700
"//pkg/util/tracing/collector:collector_test",
@@ -2416,6 +2417,7 @@ GO_TARGETS = [
24162417
"//pkg/util/timeutil/pgdate:pgdate",
24172418
"//pkg/util/timeutil/pgdate:pgdate_test",
24182419
"//pkg/util/timeutil/ptp:ptp",
2420+
"//pkg/util/timeutil/ptp:ptp_test",
24192421
"//pkg/util/timeutil:timeutil",
24202422
"//pkg/util/timeutil:timeutil_test",
24212423
"//pkg/util/tochar:tochar",

pkg/util/timeutil/ptp/BUILD.bazel

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "ptp",
@@ -59,3 +59,19 @@ go_library(
5959
"//conditions:default": [],
6060
}),
6161
)
62+
63+
go_test(
64+
name = "ptp_test",
65+
srcs = ["ptp_clock_linux_test.go"],
66+
args = ["-test.timeout=295s"],
67+
embed = [":ptp"],
68+
deps = select({
69+
"@io_bazel_rules_go//go/platform:android": [
70+
"//pkg/util/timeutil",
71+
],
72+
"@io_bazel_rules_go//go/platform:linux": [
73+
"//pkg/util/timeutil",
74+
],
75+
"//conditions:default": [],
76+
}),
77+
)

pkg/util/timeutil/ptp/ptp_clock_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,10 @@ func (p Clock) Now() time.Time {
7979
panic(err)
8080
}
8181

82-
return timeutil.Unix(int64(ts.tv_sec)*1e9, int64(ts.tv_nsec))
82+
return timeutil.Unix(int64(ts.tv_sec), int64(ts.tv_nsec))
83+
}
84+
85+
// realtime returns a clock using the system CLOCK_REALTIME device. For testing.
86+
func realtime() Clock {
87+
return Clock{clockDeviceID: uintptr(C.CLOCK_REALTIME)}
8388
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the file licenses/BSL.txt.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0, included in the file
9+
// licenses/APL.txt.
10+
11+
//go:build linux
12+
// +build linux
13+
14+
package ptp
15+
16+
import (
17+
"testing"
18+
"time"
19+
20+
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
21+
)
22+
23+
// TestClockNow sanity checks that Clock.Now() sourced from the CLOCK_REALTIME
24+
// device returns time close to timeutil.Now(). This ensures that the conversion
25+
// from the time returned by a clock device to Go's time.Time is correct.
26+
func TestClockNow(t *testing.T) {
27+
if got, want := realtime().Now(), timeutil.Now(); want.Sub(got).Abs() > 10*time.Second {
28+
t.Errorf("clock mismatch: got %v; timeutil says %v", got, want)
29+
}
30+
}

0 commit comments

Comments
 (0)