Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: enforce max clock offset across nodes in a cluster #9612

Merged
merged 1 commit into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rpc/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ func (ctx *Context) IsConnHealthy(remoteAddr string) bool {
}

func (ctx *Context) runHeartbeat(cc *grpc.ClientConn, remoteAddr string) error {
request := PingRequest{Addr: ctx.Addr}
request := PingRequest{
Addr: ctx.Addr,
MaxOffsetNanos: ctx.localClock.MaxOffset().Nanoseconds(),
}
heartbeatClient := NewHeartbeatClient(cc)

var heartbeatTimer timeutil.Timer
Expand Down
8 changes: 8 additions & 0 deletions rpc/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ type HeartbeatService struct {
// The requester should also estimate its offset from this server along
// with the requester's address.
func (hs *HeartbeatService) Ping(ctx context.Context, args *PingRequest) (*PingResponse, error) {
// Enforce that clock max offsets are identical between nodes.
// Commit suicide in the event that this is ever untrue.
// This check is ignored if either offset is set to 0 (for unittests).
mo, amo := hs.clock.MaxOffset(), time.Duration(args.MaxOffsetNanos)
if mo != 0 && amo != 0 && mo != amo {
panic(fmt.Sprintf("locally configured maximum clock offset (%s) "+
"does not match that of node %s (%s)", mo, args.Addr, amo))
}
serverOffset := args.Offset
// The server offset should be the opposite of the client offset.
serverOffset.Offset = -serverOffset.Offset
Expand Down
71 changes: 49 additions & 22 deletions rpc/heartbeat.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/heartbeat.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ message PingRequest {
optional RemoteOffset offset = 2 [(gogoproto.nullable) = false];
// The address of the client.
optional string addr = 3 [(gogoproto.nullable) = false];
// The configured maximum clock offset (in nanoseconds) on the server.
optional int64 max_offset_nanos = 4 [(gogoproto.nullable) = false];
}

// A PingResponse contains the echoed ping request string.
Expand Down
30 changes: 30 additions & 0 deletions rpc/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package rpc

import (
"fmt"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -103,3 +105,31 @@ func TestManualHeartbeat(t *testing.T) {
manualResponse.ServerTime, regularResponse.ServerTime)
}
}

func TestClockOffsetMismatch(t *testing.T) {
defer leaktest.AfterTest(t)()
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
if match, _ := regexp.MatchString("locally configured maximum clock offset", r.(string)); !match {
t.Errorf("expected clock mismatch error")
}
}
}()

clock := hlc.NewClock(hlc.UnixNano)
clock.SetMaxOffset(250 * time.Millisecond)
hs := &HeartbeatService{
clock: clock,
remoteClockMonitor: newRemoteClockMonitor(context.TODO(), clock, time.Hour),
}

request := &PingRequest{
Ping: "testManual",
Addr: "test",
MaxOffsetNanos: (500 * time.Millisecond).Nanoseconds(),
}
ctx := context.Background()
_, _ = hs.Ping(ctx, request)
t.Fatal("should not reach")
}