Skip to content

Commit 129c8c4

Browse files
authored
switch to monotonic clocks for component check-in (#5284)
* switch to monotonic clocks for component check-in * add comment on monotonic clock stripping
1 parent 337df51 commit 129c8c4

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: use monotonic clock for component check-in
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; a word indicating the component this changeset affects.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/5284
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/5277

pkg/component/runtime/command.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ func (c *commandRuntime) Run(ctx context.Context, comm Communicator) error {
195195
// first check-in
196196
sendExpected = true
197197
}
198-
c.lastCheckin = time.Now().UTC()
198+
// Warning lastCheckin must contain a
199+
// monotonic clock. Functions like Local(),
200+
// UTC(), Round(), AddDate(), etc. remove the
201+
// monotonic clock. See
202+
// https://pkg.go.dev/time
203+
c.lastCheckin = time.Now()
199204
if c.state.syncCheckin(checkin) {
200205
changed = true
201206
}
@@ -222,7 +227,13 @@ func (c *commandRuntime) Run(ctx context.Context, comm Communicator) error {
222227
}
223228
} else {
224229
// running and should be running
225-
now := time.Now().UTC()
230+
//
231+
// Warning now must contain a
232+
// monotonic clock. Functions like Local(),
233+
// UTC(), Round(), AddDate(), etc. remove the
234+
// monotonic clock. See
235+
// https://pkg.go.dev/time
236+
now := time.Now()
226237
if now.Sub(c.lastCheckin) <= checkinPeriod {
227238
c.missedCheckins = 0
228239
} else {

pkg/component/runtime/service.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,11 @@ func (s *serviceRuntime) processCheckin(checkin *proto.CheckinObserved, comm Com
478478
// first check-in
479479
sendExpected = true
480480
}
481-
*lastCheckin = time.Now().UTC()
481+
// Warning lastCheckin must contain a monotonic clock.
482+
// Functions like Local(), UTC(), Round(), AddDate(),
483+
// etc. remove the monotonic clock. See
484+
// https://pkg.go.dev/time
485+
*lastCheckin = time.Now()
482486
if s.state.syncCheckin(checkin) {
483487
changed = true
484488
}
@@ -505,7 +509,11 @@ func (s *serviceRuntime) isRunning() bool {
505509
// checkStatus checks check-ins state, called on timer
506510
func (s *serviceRuntime) checkStatus(checkinPeriod time.Duration, lastCheckin *time.Time, missedCheckins *int) {
507511
if s.isRunning() {
508-
now := time.Now().UTC()
512+
// Warning now must contain a monotonic clock.
513+
// Functions like Local(), UTC(), Round(), AddDate(),
514+
// etc. remove the monotonic clock. See
515+
// https://pkg.go.dev/time
516+
now := time.Now()
509517
if lastCheckin.IsZero() {
510518
// never checked-in
511519
*missedCheckins++

0 commit comments

Comments
 (0)