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

Integrate shard distributor client in matching client #6622

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions client/clientBean.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type (
Bean interface {
GetHistoryClient() history.Client
GetHistoryPeers() history.PeerResolver
GetMatchingClient(domainIDToName DomainIDToNameFunc) (matching.Client, error)
GetMatchingClient(domainIDToName DomainIDToNameFunc, shardDistributorClient sharddistributor.Client) (matching.Client, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the other comment. this interface change shouldn't be needed when sharddistributor.Client is an implementation detail of existing peer resolver.

GetFrontendClient() frontend.Client
GetShardDistributorClient() sharddistributor.Client
GetRemoteAdminClient(cluster string) admin.Client
Expand Down Expand Up @@ -123,11 +123,11 @@ func (h *clientBeanImpl) GetHistoryPeers() history.PeerResolver {
return h.historyPeers
}

func (h *clientBeanImpl) GetMatchingClient(domainIDToName DomainIDToNameFunc) (matching.Client, error) {
func (h *clientBeanImpl) GetMatchingClient(domainIDToName DomainIDToNameFunc, shardDistributorClient sharddistributor.Client) (matching.Client, error) {
if client := h.matchingClient.Load(); client != nil {
return client.(matching.Client), nil
}
return h.lazyInitMatchingClient(domainIDToName)
return h.lazyInitMatchingClient(domainIDToName, shardDistributorClient)
}

func (h *clientBeanImpl) GetFrontendClient() frontend.Client {
Expand Down Expand Up @@ -170,13 +170,13 @@ func (h *clientBeanImpl) GetRemoteFrontendClient(cluster string) frontend.Client
return client
}

func (h *clientBeanImpl) lazyInitMatchingClient(domainIDToName DomainIDToNameFunc) (matching.Client, error) {
func (h *clientBeanImpl) lazyInitMatchingClient(domainIDToName DomainIDToNameFunc, shardDistributorClient sharddistributor.Client) (matching.Client, error) {
h.Lock()
defer h.Unlock()
if cached := h.matchingClient.Load(); cached != nil {
return cached.(matching.Client), nil
}
client, err := h.factory.NewMatchingClient(domainIDToName)
client, err := h.factory.NewMatchingClient(domainIDToName, shardDistributorClient)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions client/clientBean_mock.go

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

12 changes: 8 additions & 4 deletions client/clientfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ type (
// Factory can be used to create RPC clients for cadence services
Factory interface {
NewHistoryClient() (history.Client, history.PeerResolver, error)
NewMatchingClient(domainIDToName DomainIDToNameFunc) (matching.Client, error)
NewMatchingClient(domainIDToName DomainIDToNameFunc, shardDistributorClient sharddistributor.Client) (matching.Client, error)

NewHistoryClientWithTimeout(timeout time.Duration) (history.Client, history.PeerResolver, error)
NewMatchingClientWithTimeout(domainIDToName DomainIDToNameFunc, timeout time.Duration, longPollTimeout time.Duration) (matching.Client, error)
NewMatchingClientWithTimeout(domainIDToName DomainIDToNameFunc, shardDistributorClient sharddistributor.Client, timeout time.Duration, longPollTimeout time.Duration) (matching.Client, error)

NewAdminClientWithTimeoutAndConfig(config transport.ClientConfig, timeout time.Duration, largeTimeout time.Duration) (admin.Client, error)
NewFrontendClientWithTimeoutAndConfig(config transport.ClientConfig, timeout time.Duration, longPollTimeout time.Duration) (frontend.Client, error)
Expand Down Expand Up @@ -108,8 +108,8 @@ func (cf *rpcClientFactory) NewHistoryClient() (history.Client, history.PeerReso
return cf.NewHistoryClientWithTimeout(timeoutwrapper.HistoryDefaultTimeout)
}

func (cf *rpcClientFactory) NewMatchingClient(domainIDToName DomainIDToNameFunc) (matching.Client, error) {
return cf.NewMatchingClientWithTimeout(domainIDToName, timeoutwrapper.MatchingDefaultTimeout, timeoutwrapper.MatchingDefaultLongPollTimeout)
func (cf *rpcClientFactory) NewMatchingClient(domainIDToName DomainIDToNameFunc, shardDistributorClient sharddistributor.Client) (matching.Client, error) {
return cf.NewMatchingClientWithTimeout(domainIDToName, shardDistributorClient, timeoutwrapper.MatchingDefaultTimeout, timeoutwrapper.MatchingDefaultLongPollTimeout)
}

func (cf *rpcClientFactory) NewHistoryClientWithTimeout(timeout time.Duration) (history.Client, history.PeerResolver, error) {
Expand Down Expand Up @@ -145,6 +145,7 @@ func (cf *rpcClientFactory) NewHistoryClientWithTimeout(timeout time.Duration) (

func (cf *rpcClientFactory) NewMatchingClientWithTimeout(
domainIDToName DomainIDToNameFunc,
shardDistributorClient sharddistributor.Client,
timeout time.Duration,
longPollTimeout time.Duration,
) (matching.Client, error) {
Expand Down Expand Up @@ -174,8 +175,11 @@ func (cf *rpcClientFactory) NewMatchingClientWithTimeout(
client := matching.NewClient(
rawClient,
peerResolver,
shardDistributorClient,
cf.dynConfig.GetStringProperty(dynamicconfig.MatchingShardDistributionMode),
matching.NewMultiLoadBalancer(defaultLoadBalancer, loadBalancers, domainIDToName, cf.dynConfig, cf.logger),
partitionConfigProvider,
cf.logger,
)
client = timeoutwrapper.NewMatchingClient(client, longPollTimeout, timeout)
if errorRate := cf.dynConfig.GetFloat64Property(dynamicconfig.MatchingErrorInjectionRate)(); errorRate != 0 {
Expand Down
84 changes: 65 additions & 19 deletions client/matching/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,51 @@ package matching

import (
"context"
"fmt"

"go.uber.org/yarpc"

"github.com/uber/cadence/client/sharddistributor"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/dynamicconfig"
"github.com/uber/cadence/common/future"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/service/sharddistributor/constants"
)

var _ Client = (*clientImpl)(nil)

type clientImpl struct {
client Client
peerResolver PeerResolver
loadBalancer LoadBalancer
provider PartitionConfigProvider
client Client
peerResolver PeerResolver
shardDistributorClient sharddistributor.Client
shardDistributionMode dynamicconfig.StringPropertyFn
loadBalancer LoadBalancer
provider PartitionConfigProvider
logger log.Logger
}

// NewClient creates a new history service TChannel client
func NewClient(
client Client,
peerResolver PeerResolver,
shardDistributorClient sharddistributor.Client,
hashDistributionMode dynamicconfig.StringPropertyFn,
lb LoadBalancer,
provider PartitionConfigProvider,
logger log.Logger,
) Client {
return &clientImpl{
client: client,
peerResolver: peerResolver,
loadBalancer: lb,
provider: provider,
client: client,
peerResolver: peerResolver,
shardDistributorClient: shardDistributorClient,
shardDistributionMode: hashDistributionMode,
loadBalancer: lb,
provider: provider,
logger: logger,
}
}

Expand All @@ -65,7 +81,7 @@ func (c *clientImpl) AddActivityTask(
)
originalTaskListName := request.TaskList.GetName()
request.TaskList.Name = partition
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,7 +110,7 @@ func (c *clientImpl) AddDecisionTask(
)
originalTaskListName := request.TaskList.GetName()
request.TaskList.Name = partition
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -124,7 +140,7 @@ func (c *clientImpl) PollForActivityTask(
)
originalTaskListName := request.PollRequest.GetTaskList().GetName()
request.PollRequest.TaskList.Name = partition
peer, err := c.peerResolver.FromTaskList(request.PollRequest.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.PollRequest.TaskList.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -162,7 +178,7 @@ func (c *clientImpl) PollForDecisionTask(
)
originalTaskListName := request.PollRequest.GetTaskList().GetName()
request.PollRequest.TaskList.Name = partition
peer, err := c.peerResolver.FromTaskList(request.PollRequest.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.PollRequest.TaskList.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -199,7 +215,7 @@ func (c *clientImpl) QueryWorkflow(
"",
)
request.TaskList.Name = partition
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return nil, err
}
Expand All @@ -211,7 +227,7 @@ func (c *clientImpl) RespondQueryTaskCompleted(
request *types.MatchingRespondQueryTaskCompletedRequest,
opts ...yarpc.CallOption,
) error {
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return err
}
Expand All @@ -223,7 +239,7 @@ func (c *clientImpl) CancelOutstandingPoll(
request *types.CancelOutstandingPollRequest,
opts ...yarpc.CallOption,
) error {
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return err
}
Expand All @@ -235,7 +251,7 @@ func (c *clientImpl) DescribeTaskList(
request *types.MatchingDescribeTaskListRequest,
opts ...yarpc.CallOption,
) (*types.DescribeTaskListResponse, error) {
peer, err := c.peerResolver.FromTaskList(request.DescRequest.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.DescRequest.TaskList.GetName())
if err != nil {
return nil, err
}
Expand All @@ -247,7 +263,7 @@ func (c *clientImpl) ListTaskListPartitions(
request *types.MatchingListTaskListPartitionsRequest,
opts ...yarpc.CallOption,
) (*types.ListTaskListPartitionsResponse, error) {
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -305,7 +321,7 @@ func (c *clientImpl) UpdateTaskListPartitionConfig(
request *types.MatchingUpdateTaskListPartitionConfigRequest,
opts ...yarpc.CallOption,
) (*types.MatchingUpdateTaskListPartitionConfigResponse, error) {
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return nil, err
}
Expand All @@ -317,9 +333,39 @@ func (c *clientImpl) RefreshTaskListPartitionConfig(
request *types.MatchingRefreshTaskListPartitionConfigRequest,
opts ...yarpc.CallOption,
) (*types.MatchingRefreshTaskListPartitionConfigResponse, error) {
peer, err := c.peerResolver.FromTaskList(request.TaskList.GetName())
peer, err := c.getShardOwner(ctx, request.TaskList.GetName())
if err != nil {
return nil, err
}
return c.client.RefreshTaskListPartitionConfig(ctx, request, append(opts, yarpc.WithShardKey(peer))...)
}

func (c *clientImpl) getShardOwner(ctx context.Context, taskListName string) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the peer resolver interface.
Could you consider implementing a new type of peer resolver and put the logic in the new implementation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Maybe introduce a MultiPeerResolver object which satisfies PeerResolver interface. It can do take this responsibility (either calls legacy peerResolver or shardDistributor)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points, will work on making this. Thanks!

sharddistributorMode := c.shardDistributionMode()
if sharddistributorMode == common.ShardModeShardDistributor && c.shardDistributorClient != nil {
request := &types.GetShardOwnerRequest{
ShardKey: taskListName,
Namespace: constants.MatchingNamespace,
}

resp, err := c.shardDistributorClient.GetShardOwner(ctx, request)
if err != nil {
return "", fmt.Errorf("find shard in shard distributor: %w", err)
jakobht marked this conversation as resolved.
Show resolved Hide resolved
}

return resp.Owner, nil
}

if sharddistributorMode == common.ShardModeShardDistributor && c.shardDistributorClient == nil {
c.logger.Warn("ShardDistributor mode enabled, but shard distributor is not available, falling back to hash-ring")
} else if c.shardDistributionMode() != common.ShardModeHashRing {
c.logger.Warn("Unknown hash distribution mode, falling back to hash-ring", tag.Mode(c.shardDistributionMode()))
}

owner, err := c.peerResolver.FromTaskList(taskListName)
if err != nil {
return "", fmt.Errorf("find shard in hash ring: %w", err)
}

return owner, nil
}
Loading
Loading