mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
273 lines
8.0 KiB
Go
273 lines
8.0 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
// Generates all three generated files in this package:
|
|
//go:generate go run ../../cmd/tools/rpcwrappers -service history
|
|
|
|
package history
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.temporal.io/api/serviceerror"
|
|
"google.golang.org/grpc"
|
|
|
|
"go.temporal.io/server/api/historyservice/v1"
|
|
replicationspb "go.temporal.io/server/api/replication/v1"
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/convert"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
serviceerrors "go.temporal.io/server/common/serviceerror"
|
|
)
|
|
|
|
var _ historyservice.HistoryServiceClient = (*clientImpl)(nil)
|
|
|
|
const (
|
|
// DefaultTimeout is the default timeout used to make calls
|
|
DefaultTimeout = time.Second * 30
|
|
)
|
|
|
|
type clientImpl struct {
|
|
numberOfShards int32
|
|
tokenSerializer common.TaskTokenSerializer
|
|
timeout time.Duration
|
|
clients common.ClientCache
|
|
logger log.Logger
|
|
}
|
|
|
|
// NewClient creates a new history service gRPC client
|
|
func NewClient(
|
|
numberOfShards int32,
|
|
timeout time.Duration,
|
|
clients common.ClientCache,
|
|
logger log.Logger,
|
|
) historyservice.HistoryServiceClient {
|
|
return &clientImpl{
|
|
numberOfShards: numberOfShards,
|
|
tokenSerializer: common.NewProtoTaskTokenSerializer(),
|
|
timeout: timeout,
|
|
clients: clients,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (c *clientImpl) DescribeHistoryHost(
|
|
ctx context.Context,
|
|
request *historyservice.DescribeHistoryHostRequest,
|
|
opts ...grpc.CallOption) (*historyservice.DescribeHistoryHostResponse, error) {
|
|
|
|
var err error
|
|
var client historyservice.HistoryServiceClient
|
|
|
|
if request.GetShardId() != 0 {
|
|
client, err = c.getClientForShardID(request.GetShardId())
|
|
} else if request.GetWorkflowExecution() != nil {
|
|
client, err = c.getClientForWorkflowID(request.GetNamespaceId(), request.GetWorkflowExecution().GetWorkflowId())
|
|
} else {
|
|
ret, err := c.clients.GetClientForClientKey(request.GetHostAddress())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
client = ret.(historyservice.HistoryServiceClient)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response *historyservice.DescribeHistoryHostResponse
|
|
op := func(ctx context.Context, client historyservice.HistoryServiceClient) error {
|
|
var err error
|
|
ctx, cancel := c.createContext(ctx)
|
|
defer cancel()
|
|
response, err = client.DescribeHistoryHost(ctx, request, opts...)
|
|
return err
|
|
}
|
|
err = c.executeWithRedirect(ctx, client, op)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (c *clientImpl) GetReplicationMessages(
|
|
ctx context.Context,
|
|
request *historyservice.GetReplicationMessagesRequest,
|
|
opts ...grpc.CallOption,
|
|
) (*historyservice.GetReplicationMessagesResponse, error) {
|
|
requestsByClient := make(map[historyservice.HistoryServiceClient]*historyservice.GetReplicationMessagesRequest)
|
|
|
|
for _, token := range request.Tokens {
|
|
client, err := c.getClientForShardID(token.GetShardId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, ok := requestsByClient[client]; !ok {
|
|
requestsByClient[client] = &historyservice.GetReplicationMessagesRequest{
|
|
ClusterName: request.ClusterName,
|
|
}
|
|
}
|
|
|
|
req := requestsByClient[client]
|
|
req.Tokens = append(req.Tokens, token)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(len(requestsByClient))
|
|
respChan := make(chan *historyservice.GetReplicationMessagesResponse, len(requestsByClient))
|
|
errChan := make(chan error, 1)
|
|
for client, req := range requestsByClient {
|
|
go func(client historyservice.HistoryServiceClient, request *historyservice.GetReplicationMessagesRequest) {
|
|
defer wg.Done()
|
|
|
|
ctx, cancel := c.createContext(ctx)
|
|
defer cancel()
|
|
resp, err := client.GetReplicationMessages(ctx, request, opts...)
|
|
if err != nil {
|
|
c.logger.Warn("Failed to get replication tasks from client", tag.Error(err))
|
|
// Returns service busy error to notify replication
|
|
if _, ok := err.(*serviceerror.ResourceExhausted); ok {
|
|
select {
|
|
case errChan <- err:
|
|
default:
|
|
}
|
|
}
|
|
return
|
|
}
|
|
respChan <- resp
|
|
}(client, req)
|
|
}
|
|
|
|
wg.Wait()
|
|
close(respChan)
|
|
close(errChan)
|
|
|
|
response := &historyservice.GetReplicationMessagesResponse{ShardMessages: make(map[int32]*replicationspb.ReplicationMessages)}
|
|
for resp := range respChan {
|
|
for shardID, tasks := range resp.ShardMessages {
|
|
response.ShardMessages[shardID] = tasks
|
|
}
|
|
}
|
|
var err error
|
|
if len(errChan) > 0 {
|
|
err = <-errChan
|
|
}
|
|
return response, err
|
|
}
|
|
|
|
func (c *clientImpl) GetReplicationStatus(
|
|
ctx context.Context,
|
|
request *historyservice.GetReplicationStatusRequest,
|
|
opts ...grpc.CallOption,
|
|
) (*historyservice.GetReplicationStatusResponse, error) {
|
|
clients, err := c.clients.GetAllClients()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
respChan := make(chan *historyservice.GetReplicationStatusResponse, len(clients))
|
|
errChan := make(chan error, 1)
|
|
var wg sync.WaitGroup
|
|
wg.Add(len(clients))
|
|
for _, client := range clients {
|
|
historyClient := client.(historyservice.HistoryServiceClient)
|
|
go func(client historyservice.HistoryServiceClient) {
|
|
defer wg.Done()
|
|
resp, err := historyClient.GetReplicationStatus(ctx, request, opts...)
|
|
if err != nil {
|
|
select {
|
|
case errChan <- err:
|
|
default:
|
|
}
|
|
} else {
|
|
respChan <- resp
|
|
}
|
|
}(historyClient)
|
|
}
|
|
wg.Wait()
|
|
close(respChan)
|
|
close(errChan)
|
|
|
|
response := &historyservice.GetReplicationStatusResponse{}
|
|
for resp := range respChan {
|
|
response.Shards = append(response.Shards, resp.Shards...)
|
|
}
|
|
|
|
if len(errChan) > 0 {
|
|
err = <-errChan
|
|
return response, err
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (c *clientImpl) createContext(parent context.Context) (context.Context, context.CancelFunc) {
|
|
return context.WithTimeout(parent, c.timeout)
|
|
}
|
|
|
|
func (c *clientImpl) getClientForWorkflowID(namespaceID, workflowID string) (historyservice.HistoryServiceClient, error) {
|
|
key := common.WorkflowIDToHistoryShard(namespaceID, workflowID, c.numberOfShards)
|
|
return c.getClientForShardID(key)
|
|
}
|
|
|
|
func (c *clientImpl) getClientForShardID(shardID int32) (historyservice.HistoryServiceClient, error) {
|
|
if shardID <= 0 {
|
|
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf("Invalid ShardID: %d", shardID))
|
|
}
|
|
client, err := c.clients.GetClientForKey(convert.Int32ToString(shardID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return client.(historyservice.HistoryServiceClient), nil
|
|
}
|
|
|
|
func (c *clientImpl) executeWithRedirect(ctx context.Context,
|
|
client historyservice.HistoryServiceClient,
|
|
op func(ctx context.Context, client historyservice.HistoryServiceClient) error,
|
|
) error {
|
|
|
|
for {
|
|
err := common.IsValidContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = op(ctx, client)
|
|
if s, ok := err.(*serviceerrors.ShardOwnershipLost); ok && len(s.OwnerHost) != 0 {
|
|
// TODO: consider emitting a metric for number of redirects
|
|
ret, err := c.clients.GetClientForClientKey(s.OwnerHost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
client = ret.(historyservice.HistoryServiceClient)
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
}
|