forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat.go
68 lines (62 loc) · 2.61 KB
/
heartbeat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2014 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package rpc
import (
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/util/hlc"
)
// A HeartbeatService exposes a method to echo its request params. It doubles
// as a way to measure the offset of the server from other nodes. It uses the
// clock to return the server time every heartbeat. It also keeps track of
// remote clocks sent to it by storing them in the remoteClockMonitor.
type HeartbeatService struct {
// Provides the nanosecond unix epoch timestamp of the processor.
clock *hlc.Clock
// A pointer to the RemoteClockMonitor configured in the RPC Context,
// shared by rpc clients, to keep track of remote clock measurements.
remoteClockMonitor *RemoteClockMonitor
}
// Ping echos the contents of the request to the response, and returns the
// server's current clock value, allowing the requester to measure its clock.
// The requester should also estimate its offset from this server along
// with the requester's address.
func (hs *HeartbeatService) Ping(args *proto.PingRequest, reply *proto.PingResponse) error {
reply.Pong = args.Ping
serverOffset := args.Offset
// The server offset should be the opposite of the client offset.
serverOffset.Offset = -serverOffset.Offset
hs.remoteClockMonitor.UpdateOffset(args.Addr, serverOffset)
reply.ServerTime = hs.clock.PhysicalNow()
return nil
}
// A ManualHeartbeatService allows manual control of when heartbeats occur, to
// facilitate testing.
type ManualHeartbeatService struct {
clock *hlc.Clock
remoteClockMonitor *RemoteClockMonitor
// Heartbeats are processed when a value is sent here.
ready chan struct{}
}
// Ping waits until the heartbeat service is ready to respond to a Heartbeat.
func (mhs *ManualHeartbeatService) Ping(args *proto.PingRequest, reply *proto.PingResponse) error {
<-mhs.ready
hs := HeartbeatService{
clock: mhs.clock,
remoteClockMonitor: mhs.remoteClockMonitor,
}
return hs.Ping(args, reply)
}