-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p2p: implement pkg/p2p (Part I) (#3156)
- Loading branch information
Showing
13 changed files
with
2,313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package p2p | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pingcap/ticdc/pkg/leakutil" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
leakutil.SetUpLeakTest(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package p2p | ||
|
||
import "github.com/pingcap/ticdc/proto/p2p" | ||
|
||
type ( | ||
// NodeID represents the identifier of a sender node. | ||
// Using IP address is not enough because of possible restarts. | ||
NodeID = string | ||
// Topic represents the topic for a peer-to-peer message | ||
Topic = string | ||
// Seq represents the serial number of a message for a given topic. | ||
Seq = int64 | ||
// MessageServerStream is an alias for the protobuf-generated interface for the message service. | ||
MessageServerStream = p2p.CDCPeerToPeer_SendMessageServer | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package p2p | ||
|
||
import "encoding/json" | ||
|
||
// Serializable is an interface for defining custom serialization methods | ||
// for peer messages. | ||
type Serializable interface { | ||
Marshal() ([]byte, error) | ||
Unmarshal(data []byte) error | ||
} | ||
|
||
func marshalMessage(value interface{}) ([]byte, error) { | ||
if value, ok := value.(Serializable); ok { | ||
return value.Marshal() | ||
} | ||
return json.Marshal(value) | ||
} | ||
|
||
func unmarshalMessage(data []byte, value interface{}) error { | ||
if value, ok := value.(Serializable); ok { | ||
return value.Unmarshal(data) | ||
} | ||
return json.Unmarshal(data, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package p2p | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/vmihailenco/msgpack/v5" | ||
) | ||
|
||
type jsonSerializableMessage struct { | ||
A int | ||
B float64 | ||
C string | ||
} | ||
|
||
type msgpackSerializableMessage struct { | ||
A int | ||
B float64 | ||
C string | ||
D []int | ||
} | ||
|
||
func (m *msgpackSerializableMessage) Marshal() ([]byte, error) { | ||
return msgpack.Marshal(m) | ||
} | ||
|
||
func (m *msgpackSerializableMessage) Unmarshal(data []byte) error { | ||
return msgpack.Unmarshal(data, m) | ||
} | ||
|
||
func TestJsonSerializable(t *testing.T) { | ||
msg := &jsonSerializableMessage{ | ||
A: 1, | ||
B: 2, | ||
C: "test", | ||
} | ||
|
||
data, err := marshalMessage(msg) | ||
require.NoError(t, err) | ||
|
||
msg1 := &jsonSerializableMessage{} | ||
err = unmarshalMessage(data, msg1) | ||
require.NoError(t, err) | ||
|
||
require.True(t, reflect.DeepEqual(msg, msg1)) | ||
} | ||
|
||
func TestMsgpackSerializable(t *testing.T) { | ||
msg := &msgpackSerializableMessage{ | ||
A: 1, | ||
B: 2, | ||
C: "test", | ||
D: []int{1, 2, 3, 4, 5, 6}, | ||
} | ||
data, err := marshalMessage(msg) | ||
require.NoError(t, err) | ||
|
||
data1, err := msgpack.Marshal(msg) | ||
require.NoError(t, err) | ||
require.Equal(t, data1, data) | ||
|
||
msg1 := &msgpackSerializableMessage{} | ||
err = unmarshalMessage(data, msg1) | ||
require.NoError(t, err) | ||
require.True(t, reflect.DeepEqual(msg, msg1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package p2p | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/modern-go/reflect2" | ||
"github.com/pingcap/failpoint" | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/ticdc/proto/p2p" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
gRPCPeer "google.golang.org/grpc/peer" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type streamWrapper struct { | ||
MessageServerStream | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
} | ||
|
||
func wrapStream(stream MessageServerStream) *streamWrapper { | ||
ctx, cancel := context.WithCancel(stream.Context()) | ||
return &streamWrapper{ | ||
MessageServerStream: stream, | ||
ctx: ctx, | ||
cancel: cancel, | ||
} | ||
} | ||
|
||
func (w *streamWrapper) Context() context.Context { | ||
return w.ctx | ||
} | ||
|
||
// ServerWrapper implements a CDCPeerToPeerServer, and it | ||
// maintains an inner CDCPeerToPeerServer instance that can | ||
// be replaced as needed. | ||
type ServerWrapper struct { | ||
rwMu sync.RWMutex | ||
innerServer p2p.CDCPeerToPeerServer | ||
|
||
wrappedStreamsMu sync.Mutex | ||
wrappedStreams map[*streamWrapper]struct{} | ||
} | ||
|
||
// NewServerWrapper creates a new ServerWrapper | ||
func NewServerWrapper() *ServerWrapper { | ||
return &ServerWrapper{ | ||
wrappedStreams: map[*streamWrapper]struct{}{}, | ||
} | ||
} | ||
|
||
// SendMessage implements p2p.CDCPeerToPeerServer | ||
func (s *ServerWrapper) SendMessage(stream p2p.CDCPeerToPeer_SendMessageServer) error { | ||
s.rwMu.RLock() | ||
innerServer := s.innerServer | ||
s.rwMu.RUnlock() | ||
|
||
if innerServer == nil { | ||
var addr string | ||
peer, ok := gRPCPeer.FromContext(stream.Context()) | ||
if ok { | ||
addr = peer.Addr.String() | ||
} | ||
log.Debug("gRPC server received request while CDC capture is not running.", zap.String("addr", addr)) | ||
return status.New(codes.Unavailable, "CDC capture is not running").Err() | ||
} | ||
|
||
wrappedStream := wrapStream(stream) | ||
s.wrappedStreamsMu.Lock() | ||
s.wrappedStreams[wrappedStream] = struct{}{} | ||
s.wrappedStreamsMu.Unlock() | ||
defer func() { | ||
s.wrappedStreamsMu.Lock() | ||
delete(s.wrappedStreams, wrappedStream) | ||
s.wrappedStreamsMu.Unlock() | ||
wrappedStream.cancel() | ||
}() | ||
|
||
// Used in unit tests to simulate a race situation between `SendMessage` and `Reset`. | ||
// TODO think of another way to make tests parallelizable. | ||
failpoint.Inject("ServerWrapperSendMessageDelay", func() {}) | ||
return innerServer.SendMessage(wrappedStream) | ||
} | ||
|
||
// Reset resets the inner server object in the ServerWrapper | ||
func (s *ServerWrapper) Reset(inner p2p.CDCPeerToPeerServer) { | ||
s.rwMu.Lock() | ||
defer s.rwMu.Unlock() | ||
|
||
s.wrappedStreamsMu.Lock() | ||
defer s.wrappedStreamsMu.Unlock() | ||
|
||
for wrappedStream := range s.wrappedStreams { | ||
wrappedStream.cancel() | ||
} | ||
|
||
// reflect2.IsNil handles two cases for us: | ||
// 1) null value | ||
// 2) an interface with a null value but a not-null type info. | ||
if reflect2.IsNil(inner) { | ||
s.innerServer = nil | ||
return | ||
} | ||
s.innerServer = inner | ||
} |
Oops, something went wrong.