Skip to content

Commit ca5be29

Browse files
authored
Reimplement DC redirection handler (#3887)
1 parent 008f056 commit ca5be29

22 files changed

+745
-3154
lines changed

client/clientBean.go

+77-82
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"go.temporal.io/api/serviceerror"
3535
"go.temporal.io/api/workflowservice/v1"
36+
"google.golang.org/grpc"
3637

3738
"go.temporal.io/server/api/adminservice/v1"
3839
"go.temporal.io/server/api/historyservice/v1"
@@ -48,14 +49,16 @@ type (
4849
// Bean is a collection of clients
4950
Bean interface {
5051
GetHistoryClient() historyservice.HistoryServiceClient
51-
SetHistoryClient(client historyservice.HistoryServiceClient)
5252
GetMatchingClient(namespaceIDToName NamespaceIDToNameFunc) (matchingservice.MatchingServiceClient, error)
53-
SetMatchingClient(client matchingservice.MatchingServiceClient)
5453
GetFrontendClient() workflowservice.WorkflowServiceClient
55-
SetFrontendClient(client workflowservice.WorkflowServiceClient)
56-
GetRemoteAdminClient(cluster string) (adminservice.AdminServiceClient, error)
57-
SetRemoteAdminClient(cluster string, client adminservice.AdminServiceClient)
58-
GetRemoteFrontendClient(cluster string) (workflowservice.WorkflowServiceClient, error)
54+
GetRemoteAdminClient(string) (adminservice.AdminServiceClient, error)
55+
SetRemoteAdminClient(string, adminservice.AdminServiceClient)
56+
GetRemoteFrontendClient(string) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient, error)
57+
}
58+
59+
frontendClient struct {
60+
connection grpc.ClientConnInterface
61+
workflowservice.WorkflowServiceClient
5962
}
6063

6164
clientBeanImpl struct {
@@ -68,7 +71,7 @@ type (
6871
adminClientsLock sync.RWMutex
6972
adminClients map[string]adminservice.AdminServiceClient
7073
frontendClientsLock sync.RWMutex
71-
frontendClients map[string]workflowservice.WorkflowServiceClient
74+
frontendClients map[string]frontendClient
7275
}
7376
)
7477

@@ -81,7 +84,7 @@ func NewClientBean(factory Factory, clusterMetadata cluster.Metadata) (Bean, err
8184
}
8285

8386
adminClients := map[string]adminservice.AdminServiceClient{}
84-
frontendClients := map[string]workflowservice.WorkflowServiceClient{}
87+
frontendClients := map[string]frontendClient{}
8588

8689
currentClusterName := clusterMetadata.GetCurrentClusterName()
8790
// Init local cluster client with membership info
@@ -92,15 +95,18 @@ func NewClientBean(factory Factory, clusterMetadata cluster.Metadata) (Bean, err
9295
if err != nil {
9396
return nil, err
9497
}
95-
frontendClient, err := factory.NewLocalFrontendClientWithTimeout(
98+
conn, client, err := factory.NewLocalFrontendClientWithTimeout(
9699
frontend.DefaultTimeout,
97100
frontend.DefaultLongPollTimeout,
98101
)
99102
if err != nil {
100103
return nil, err
101104
}
102105
adminClients[currentClusterName] = adminClient
103-
frontendClients[currentClusterName] = frontendClient
106+
frontendClients[currentClusterName] = frontendClient{
107+
connection: conn,
108+
WorkflowServiceClient: client,
109+
}
104110

105111
for clusterName, info := range clusterMetadata.GetAllClusterInfo() {
106112
if !info.Enabled || clusterName == currentClusterName {
@@ -111,13 +117,16 @@ func NewClientBean(factory Factory, clusterMetadata cluster.Metadata) (Bean, err
111117
admin.DefaultTimeout,
112118
admin.DefaultLargeTimeout,
113119
)
114-
frontendClient = factory.NewRemoteFrontendClientWithTimeout(
120+
conn, client = factory.NewRemoteFrontendClientWithTimeout(
115121
info.RPCAddress,
116122
frontend.DefaultTimeout,
117123
frontend.DefaultLongPollTimeout,
118124
)
119125
adminClients[clusterName] = adminClient
120-
frontendClients[clusterName] = frontendClient
126+
frontendClients[clusterName] = frontendClient{
127+
connection: conn,
128+
WorkflowServiceClient: client,
129+
}
121130
}
122131

123132
bean := &clientBeanImpl{
@@ -154,64 +163,49 @@ func (h *clientBeanImpl) GetHistoryClient() historyservice.HistoryServiceClient
154163
return h.historyClient
155164
}
156165

157-
func (h *clientBeanImpl) SetHistoryClient(
158-
client historyservice.HistoryServiceClient,
159-
) {
160-
h.historyClient = client
161-
}
162-
163166
func (h *clientBeanImpl) GetMatchingClient(namespaceIDToName NamespaceIDToNameFunc) (matchingservice.MatchingServiceClient, error) {
164167
if client := h.matchingClient.Load(); client != nil {
165168
return client.(matchingservice.MatchingServiceClient), nil
166169
}
167170
return h.lazyInitMatchingClient(namespaceIDToName)
168171
}
169172

170-
func (h *clientBeanImpl) SetMatchingClient(
171-
client matchingservice.MatchingServiceClient,
172-
) {
173-
h.matchingClient.Store(client)
174-
}
175-
176173
func (h *clientBeanImpl) GetFrontendClient() workflowservice.WorkflowServiceClient {
177174
return h.frontendClients[h.clusterMetadata.GetCurrentClusterName()]
178175
}
179176

180-
func (h *clientBeanImpl) SetFrontendClient(
181-
client workflowservice.WorkflowServiceClient,
182-
) {
183-
h.frontendClients[h.clusterMetadata.GetCurrentClusterName()] = client
184-
}
185-
186177
func (h *clientBeanImpl) GetRemoteAdminClient(cluster string) (adminservice.AdminServiceClient, error) {
187178
h.adminClientsLock.RLock()
188179
client, ok := h.adminClients[cluster]
189180
h.adminClientsLock.RUnlock()
181+
if ok {
182+
return client, nil
183+
}
190184

191-
if !ok {
192-
clusterInfo, clusterFound := h.clusterMetadata.GetAllClusterInfo()[cluster]
193-
if !clusterFound {
194-
return nil, &serviceerror.NotFound{
195-
Message: fmt.Sprintf(
196-
"Unknown cluster name: %v with given cluster information map: %v.",
197-
cluster,
198-
clusterInfo,
199-
),
200-
}
185+
clusterInfo, clusterFound := h.clusterMetadata.GetAllClusterInfo()[cluster]
186+
if !clusterFound {
187+
return nil, &serviceerror.NotFound{
188+
Message: fmt.Sprintf(
189+
"Unknown cluster name: %v with given cluster information map: %v.",
190+
cluster,
191+
clusterInfo,
192+
),
201193
}
194+
}
202195

203-
h.adminClientsLock.Lock()
204-
defer h.adminClientsLock.Unlock()
205-
client, ok = h.adminClients[cluster]
206-
if !ok {
207-
client = h.factory.NewRemoteAdminClientWithTimeout(
208-
clusterInfo.RPCAddress,
209-
admin.DefaultTimeout,
210-
admin.DefaultLargeTimeout,
211-
)
212-
h.setRemoteAdminClientLocked(cluster, client)
213-
}
196+
h.adminClientsLock.Lock()
197+
defer h.adminClientsLock.Unlock()
198+
client, ok = h.adminClients[cluster]
199+
if ok {
200+
return client, nil
214201
}
202+
203+
client = h.factory.NewRemoteAdminClientWithTimeout(
204+
clusterInfo.RPCAddress,
205+
admin.DefaultTimeout,
206+
admin.DefaultLargeTimeout,
207+
)
208+
h.adminClients[cluster] = client
215209
return client, nil
216210
}
217211

@@ -222,46 +216,47 @@ func (h *clientBeanImpl) SetRemoteAdminClient(
222216
h.adminClientsLock.Lock()
223217
defer h.adminClientsLock.Unlock()
224218

225-
h.setRemoteAdminClientLocked(cluster, client)
219+
h.adminClients[cluster] = client
226220
}
227221

228-
func (h *clientBeanImpl) GetRemoteFrontendClient(cluster string) (workflowservice.WorkflowServiceClient, error) {
222+
func (h *clientBeanImpl) GetRemoteFrontendClient(clusterName string) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient, error) {
229223
h.frontendClientsLock.RLock()
230-
client, ok := h.frontendClients[cluster]
224+
client, ok := h.frontendClients[clusterName]
231225
h.frontendClientsLock.RUnlock()
226+
if ok {
227+
return client.connection, client, nil
228+
}
232229

233-
if !ok {
234-
clusterInfo, clusterFound := h.clusterMetadata.GetAllClusterInfo()[cluster]
235-
if !clusterFound {
236-
return nil, &serviceerror.NotFound{
237-
Message: fmt.Sprintf(
238-
"Unknown cluster name: %v with given cluster information map: %v.",
239-
cluster,
240-
clusterInfo,
241-
),
242-
}
230+
clusterInfo, clusterFound := h.clusterMetadata.GetAllClusterInfo()[clusterName]
231+
if !clusterFound {
232+
return nil, nil, &serviceerror.NotFound{
233+
Message: fmt.Sprintf(
234+
"Unknown clusterName name: %v with given clusterName information map: %v.",
235+
clusterName,
236+
clusterInfo,
237+
),
243238
}
239+
}
244240

245-
h.frontendClientsLock.Lock()
246-
defer h.frontendClientsLock.Unlock()
247-
client, ok = h.frontendClients[cluster]
248-
if !ok {
249-
client = h.factory.NewRemoteFrontendClientWithTimeout(
250-
clusterInfo.RPCAddress,
251-
frontend.DefaultTimeout,
252-
frontend.DefaultLongPollTimeout,
253-
)
254-
h.setRemoteFrontendClientLocked(cluster, client)
255-
}
241+
h.frontendClientsLock.Lock()
242+
defer h.frontendClientsLock.Unlock()
243+
244+
client, ok = h.frontendClients[clusterName]
245+
if ok {
246+
return client.connection, client, nil
256247
}
257-
return client, nil
258-
}
259248

260-
func (h *clientBeanImpl) setRemoteFrontendClientLocked(
261-
cluster string,
262-
client workflowservice.WorkflowServiceClient,
263-
) {
264-
h.frontendClients[cluster] = client
249+
conn, fClient := h.factory.NewRemoteFrontendClientWithTimeout(
250+
clusterInfo.RPCAddress,
251+
frontend.DefaultTimeout,
252+
frontend.DefaultLongPollTimeout,
253+
)
254+
client = frontendClient{
255+
connection: conn,
256+
WorkflowServiceClient: fClient,
257+
}
258+
h.frontendClients[clusterName] = client
259+
return client.connection, client, nil
265260
}
266261

267262
func (h *clientBeanImpl) setRemoteAdminClientLocked(

client/clientBean_mock.go

+17-51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)