mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
<!-- Describe what has changed in this PR --> **What changed?** This PR changes the behavior of `getAnyClientConn` to round-robin through the list of members in the history pool, instead of looking in the connection pool. <!-- Tell your future self why have you made these changes --> **Why?** This fixes an issue where DLQ commands like merge would end up using zombie connections to history hosts that no longer exist. Why? This was part of a small optimization I was making. I figured that we would end up making the client pool large if we kept making connections to different hosts by running DLQ commands over and over when we can instead use any valid connection. However, we never remove connections to dead hosts from this pool, so that strategy doesn't work. Instead, we now round-robin through the list of members in the pool. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** I added unit tests for both the connection pool and the history client. I also fixed a broken unit test in `TestErrGetAnyHistoryHost`. In addition, I deployed these changes to a test environment, and I verified that we no longer get errors, even after following this sequence: 1. Deploy the change to the frontend 2. Run a bunch of DLQ admin commands (to fill up the connection pool on the frontend) 3. Restart the history service to get all new history hosts 4. Repeat step 2 and verify that there are no more "no route to host" errors <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?**
198 lines
6.2 KiB
Go
198 lines
6.2 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.
|
|
|
|
package history_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.temporal.io/api/serviceerror"
|
|
"go.temporal.io/server/api/historyservice/v1"
|
|
"go.temporal.io/server/client/history"
|
|
"go.temporal.io/server/common/dynamicconfig"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/membership"
|
|
"go.temporal.io/server/internal/nettest"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type (
|
|
testHistoryService struct {
|
|
historyservice.UnimplementedHistoryServiceServer
|
|
}
|
|
testRPCFactory struct {
|
|
base *nettest.RPCFactory
|
|
dialedAddresses []string
|
|
}
|
|
)
|
|
|
|
func TestErrLookup(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
serviceResolver := membership.NewMockServiceResolver(ctrl)
|
|
serviceResolver.EXPECT().Lookup("1").Return(nil, assert.AnError).AnyTimes()
|
|
client := history.NewClient(
|
|
dynamicconfig.NewNoopCollection(),
|
|
serviceResolver,
|
|
log.NewTestLogger(),
|
|
1,
|
|
nil,
|
|
time.Duration(0),
|
|
)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
fn func() error
|
|
}{
|
|
{
|
|
name: "GetDLQTasks",
|
|
fn: func() error {
|
|
_, err := client.GetDLQTasks(context.Background(), &historyservice.GetDLQTasksRequest{})
|
|
return err
|
|
},
|
|
},
|
|
{
|
|
name: "DeleteDLQTasks",
|
|
fn: func() error {
|
|
_, err := client.DeleteDLQTasks(context.Background(), &historyservice.DeleteDLQTasksRequest{})
|
|
return err
|
|
},
|
|
},
|
|
} {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
err := tc.fn()
|
|
var unavailableErr *serviceerror.Unavailable
|
|
require.ErrorAs(t, err, &unavailableErr, "Should return an 'Unavailable' error when there "+
|
|
"are no history hosts available to serve the request")
|
|
assert.ErrorContains(t, err, assert.AnError.Error())
|
|
})
|
|
}
|
|
}
|
|
|
|
// This tests our strategy for getting history hosts to serve shard-agnostic requests, like those interacting with the
|
|
// DLQ. For such requests, we should round-robin over all available history shards. In addition, we should re-use any
|
|
// available connections when the round-robin wraps around.
|
|
func TestShardAgnosticConnectionStrategy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
fn func(client historyservice.HistoryServiceClient) error
|
|
}{
|
|
{
|
|
name: "GetDLQTasks",
|
|
fn: func(client historyservice.HistoryServiceClient) error {
|
|
_, err := client.GetDLQTasks(context.Background(), &historyservice.GetDLQTasksRequest{})
|
|
return err
|
|
},
|
|
},
|
|
{
|
|
name: "DeleteDLQTasks",
|
|
fn: func(client historyservice.HistoryServiceClient) error {
|
|
_, err := client.DeleteDLQTasks(context.Background(), &historyservice.DeleteDLQTasksRequest{})
|
|
return err
|
|
},
|
|
},
|
|
} {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
ctrl := gomock.NewController(t)
|
|
|
|
// Create a service resolver that just returns 2 hosts for the first 3 requests. We want to send 3 requests
|
|
// with 2 hosts so that we can verify that we re-use the connection of "test1" on the last request.
|
|
serviceResolver := membership.NewMockServiceResolver(ctrl)
|
|
serviceResolver.EXPECT().Lookup("1").Return(membership.NewHostInfoFromAddress("test1"), nil)
|
|
serviceResolver.EXPECT().Lookup("2").Return(membership.NewHostInfoFromAddress("test2"), nil)
|
|
serviceResolver.EXPECT().Lookup("1").Return(membership.NewHostInfoFromAddress("test1"), nil)
|
|
|
|
// Create an in-memory gRPC server.
|
|
listener := nettest.NewListener(nettest.NewPipe())
|
|
rpcFactory := &testRPCFactory{
|
|
base: nettest.NewRPCFactory(listener),
|
|
}
|
|
grpcServer := grpc.NewServer()
|
|
testSvc := &testHistoryService{}
|
|
historyservice.RegisterHistoryServiceServer(grpcServer, testSvc)
|
|
errs := make(chan error)
|
|
go func() {
|
|
errs <- grpcServer.Serve(listener)
|
|
}()
|
|
defer func() {
|
|
grpcServer.Stop()
|
|
require.NoError(t, <-errs)
|
|
}()
|
|
|
|
// Send 3 requests to verify that we re-use a connection for the last request.
|
|
client := history.NewClient(
|
|
dynamicconfig.NewNoopCollection(),
|
|
serviceResolver,
|
|
log.NewTestLogger(),
|
|
2,
|
|
rpcFactory,
|
|
time.Duration(0),
|
|
)
|
|
for i := 0; i < 3; i++ {
|
|
err := tc.fn(client)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Verify that there are no repeated dialed addresses (indicating that we re-used the connection).
|
|
assert.Equal(
|
|
t,
|
|
[]string{"test1", "test2"},
|
|
rpcFactory.dialedAddresses,
|
|
"Should cache the client connection and reuse it for subsequent requests",
|
|
)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *testHistoryService) GetDLQTasks(
|
|
context.Context,
|
|
*historyservice.GetDLQTasksRequest,
|
|
) (*historyservice.GetDLQTasksResponse, error) {
|
|
return &historyservice.GetDLQTasksResponse{}, nil
|
|
}
|
|
|
|
func (s *testHistoryService) DeleteDLQTasks(
|
|
context.Context,
|
|
*historyservice.DeleteDLQTasksRequest,
|
|
) (*historyservice.DeleteDLQTasksResponse, error) {
|
|
return &historyservice.DeleteDLQTasksResponse{}, nil
|
|
}
|
|
|
|
func (t *testRPCFactory) CreateInternodeGRPCConnection(rpcAddress string) *grpc.ClientConn {
|
|
t.dialedAddresses = append(t.dialedAddresses, rpcAddress)
|
|
return t.base.CreateInternodeGRPCConnection(rpcAddress)
|
|
}
|