-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is an automated cherry-pick of #8005
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
- Loading branch information
1 parent
38f1d35
commit b949269
Showing
16 changed files
with
279 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2022 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" | ||
"time" | ||
|
||
"github.com/pingcap/tiflow/pkg/errors" | ||
"github.com/pingcap/tiflow/pkg/p2p" | ||
"github.com/pingcap/tiflow/pkg/security" | ||
) | ||
|
||
// MessageSender is used to send a message of a given topic to a given node. | ||
type MessageSender interface { | ||
// TODO investigate whether we need to implement a barrier mechanism | ||
|
||
// SendToNode sends a message to a given node. Returns whether it is successful and a possible error. | ||
// A `would-block` error will not be returned. (false, nil) would be returned instead. | ||
SendToNode(ctx context.Context, targetNodeID NodeID, topic Topic, message interface{}) (bool, error) | ||
|
||
// SendToNodeB sends a message to a given node in a blocking way | ||
SendToNodeB(ctx context.Context, targetNodeID NodeID, topic Topic, message interface{}) error | ||
} | ||
|
||
type messageSenderImpl struct { | ||
router MessageRouter | ||
} | ||
|
||
// NewMessageSender returns a new message sender. | ||
func NewMessageSender(router MessageRouter) MessageSender { | ||
return &messageSenderImpl{router: router} | ||
} | ||
|
||
// SendToNodeB implements MessageSender.SendToNodeB | ||
// Note the blocking send may have performance issue, BE CAUTION when using this function. | ||
func (m *messageSenderImpl) SendToNodeB( | ||
ctx context.Context, targetNodeID NodeID, topic Topic, message interface{}, | ||
) error { | ||
client := m.router.GetClient(targetNodeID) | ||
if client == nil { | ||
return errors.ErrExecutorNotFoundForMessage.GenWithStackByArgs() | ||
} | ||
|
||
// TODO: blocking send in p2p library may have performance issue | ||
_, err := client.SendMessage(ctx, topic, message) | ||
return err | ||
} | ||
|
||
func (m *messageSenderImpl) SendToNode(ctx context.Context, targetNodeID NodeID, topic Topic, message interface{}) (bool, error) { | ||
client := m.router.GetClient(targetNodeID) | ||
if client == nil { | ||
return false, nil | ||
} | ||
|
||
_, err := client.TrySendMessage(ctx, topic, message) | ||
if err != nil { | ||
if errors.Is(err, errors.ErrPeerMessageSendTryAgain) { | ||
return false, nil | ||
} | ||
return false, errors.Trace(err) | ||
} | ||
return true, nil | ||
} | ||
|
||
// MessageRouter alias to p2p.MessageRouter | ||
type MessageRouter = p2p.MessageRouter | ||
|
||
var defaultClientConfig = &p2p.MessageClientConfig{ | ||
SendChannelSize: 128, | ||
BatchSendInterval: 100 * time.Millisecond, // essentially disables flushing | ||
MaxBatchBytes: 8 * 1024 * 1024, // 8MB | ||
MaxBatchCount: 4096, | ||
RetryRateLimitPerSecond: 1.0, // once per second | ||
ClientVersion: "v5.4.0", // a fake version | ||
MaxRecvMsgSize: 4 * 1024 * 1024, // 4MB | ||
} | ||
|
||
// NewMessageRouter creates a new MessageRouter instance via tiflow p2p API | ||
func NewMessageRouter(nodeID NodeID, advertisedAddr string) MessageRouter { | ||
config := *defaultClientConfig // copy | ||
config.AdvertisedAddr = advertisedAddr | ||
return p2p.NewMessageRouter( | ||
nodeID, | ||
&security.Credential{ /* TLS not supported for now */ }, | ||
&config, | ||
) | ||
} |
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,97 @@ | ||
// Copyright 2022 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" | ||
"fmt" | ||
"math" | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/phayes/freeport" | ||
p2pImpl "github.com/pingcap/tiflow/pkg/p2p" | ||
"github.com/pingcap/tiflow/pkg/security" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func makeListenerForServerTests(t *testing.T) (l net.Listener, addr string) { | ||
port := freeport.GetPort() | ||
addr = fmt.Sprintf("127.0.0.1:%d", port) | ||
l, err := net.Listen("tcp", addr) | ||
require.NoError(t, err) | ||
return | ||
} | ||
|
||
// read only | ||
var clientConfigForUnitTesting = &p2pImpl.MessageClientConfig{ | ||
SendChannelSize: 1, | ||
BatchSendInterval: time.Second, | ||
MaxBatchBytes: math.MaxInt64, | ||
MaxBatchCount: math.MaxInt64, | ||
RetryRateLimitPerSecond: 999.0, | ||
ClientVersion: "v5.4.0", // a fake version | ||
AdvertisedAddr: "fake-addr:8300", | ||
MaxRecvMsgSize: 4 * 1024 * 1024, // 4MB | ||
} | ||
|
||
func TestMessageRPCServiceBasics(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
l, addr := makeListenerForServerTests(t) | ||
messageSrvc, err := NewMessageRPCService("test-node-1", &security.Credential{} /* no TLS */) | ||
require.NoError(t, err) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := messageSrvc.Serve(ctx, l) | ||
require.Error(t, err) | ||
require.Regexp(t, ".*canceled.*", err.Error()) | ||
}() | ||
|
||
var called atomic.Bool | ||
handlerManager := messageSrvc.MakeHandlerManager() | ||
ok, err := handlerManager.RegisterHandler(ctx, "test-topic-1", &msgContent{}, func(sender NodeID, value MessageValue) error { | ||
require.Equal(t, "test-client-1", sender) | ||
require.IsType(t, &msgContent{}, value) | ||
require.False(t, called.Swap(true)) | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, ok) | ||
|
||
client := p2pImpl.NewMessageClient("test-client-1", clientConfigForUnitTesting) | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := client.Run(ctx, "tcp", addr, "test-node-1", &security.Credential{} /* no TLS */) | ||
require.Error(t, err) | ||
require.Regexp(t, ".*canceled.*", err.Error()) | ||
}() | ||
|
||
_, err = client.SendMessage(ctx, "test-topic-1", &msgContent{}) | ||
require.NoError(t, err) | ||
require.Eventually(t, func() bool { | ||
return called.Load() | ||
}, 5*time.Second, 10*time.Millisecond) | ||
|
||
cancel() | ||
wg.Wait() | ||
} |
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
Oops, something went wrong.