go.temporal.io/server/client/client_bean.go
248 LOC · 79 covered · 169 uncovered · 14 ranges · 64 concepts · 2 introducers · 12 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination client_bean_mock.go
package client
import (
"fmt"
"sync"
"sync/atomic"
"go.temporal.io/api/serviceerror"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/server/api/adminservice/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/api/matchingservice/v1"
"go.temporal.io/server/client/admin"
"go.temporal.io/server/client/frontend"
"go.temporal.io/server/client/history"
"go.temporal.io/server/client/matching"
"go.temporal.io/server/common/cluster"
"google.golang.org/grpc"
)
type (
// Bean is a collection of clients
Bean interface {
GetHistoryClient() historyservice.HistoryServiceClient
GetMatchingClient(namespaceIDToName NamespaceIDToNameFunc) (matchingservice.MatchingServiceClient, error)
GetFrontendClient() workflowservice.WorkflowServiceClient
GetRemoteAdminClient(string) (adminservice.AdminServiceClient, error)
GetRemoteFrontendClient(string) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient, error)
// Close deterministically releases bean-held resources on shutdown: it
// stops the history and matching clients (their daemon goroutines and
// cached gRPC connections) and unregisters the cluster metadata change
// callback.
Close()
}
frontendClient struct {
connection grpc.ClientConnInterface
workflowservice.WorkflowServiceClient
}
clientBeanImpl struct {
sync.Mutex
historyClient historyservice.HistoryServiceClient
matchingClient atomic.Value
clusterMetadata cluster.Metadata
factory Factory
adminClientsLock sync.RWMutex
adminClients map[string]adminservice.AdminServiceClient
frontendClientsLock sync.RWMutex
frontendClients map[string]frontendClient
}
)
// NewClientBean provides a collection of clients
func NewClientBean(factory Factory, clusterMetadata cluster.Metadata) (Bean, error) {
fx.go ×44
historyClient, err := factory.NewHistoryClientWithTimeout(history.DefaultTimeout)
if err != nil {
return nil, err
}
frontendClients := map[string]frontendClient{}
currentClusterName := clusterMetadata.GetCurrentClusterName()
// Init local cluster client with membership info
adminClient, err := factory.NewLocalAdminClientWithTimeout(
admin.DefaultTimeout,
admin.DefaultLargeTimeout,
)
if err != nil {
return nil, err
}
frontend.DefaultTimeout,
frontend.DefaultLongPollTimeout,
)
if err != nil {
return nil, err
}
frontendClients[currentClusterName] = frontendClient{
connection: conn,
WorkflowServiceClient: client,
}
bean := &clientBeanImpl{
factory: factory,
historyClient: historyClient,
clusterMetadata: clusterMetadata,
adminClients: adminClients,
frontendClients: frontendClients,
}
bean.registerClientEviction()
return bean, nil
}
currentCluster := h.clusterMetadata.GetCurrentClusterName()
h.clusterMetadata.RegisterMetadataChangeCallback(
h,
func(oldClusterMetadata map[string]*cluster.ClusterInformation, newClusterMetadata map[string]*cluster.ClusterInformation) {
for clusterName := range newClusterMetadata {
if clusterName == currentCluster {
continue
}
h.adminClientsLock.Lock()
delete(h.adminClients, clusterName)
h.adminClientsLock.Unlock()
h.frontendClientsLock.Lock()
delete(h.frontendClients, clusterName)
h.frontendClientsLock.Unlock()
}
})
}
// Close releases the resources held by the bean's clients. See the Bean
// interface for details. It is safe to call more than once.
h.clusterMetadata.UnRegisterMetadataChangeCallback(h)
// The history and matching client wrapper chains implement Stop();
// stopping them releases their daemon goroutines and cached gRPC
// connections.
if s, ok := h.historyClient.(interface{ Stop() }); ok {
s.Stop()
}
if mc := h.matchingClient.Load(); mc != nil {
if s, ok := mc.(interface{ Stop() }); ok {
s.Stop()
}
}
}
return h.historyClient
}
func (h *clientBeanImpl) GetMatchingClient(namespaceIDToName NamespaceIDToNameFunc) (matchingservice.MatchingServiceClient, error) {
fx.go ×44
if client := h.matchingClient.Load(); client != nil {
return client.(matchingservice.MatchingServiceClient), nil
}
}
func (h *clientBeanImpl) GetFrontendClient() workflowservice.WorkflowServiceClient {
fx.go ×44
return h.frontendClients[h.clusterMetadata.GetCurrentClusterName()]
}
func (h *clientBeanImpl) GetRemoteAdminClient(cluster string) (adminservice.AdminServiceClient, error) {
fx.go ×44
h.adminClientsLock.RLock()
client, ok := h.adminClients[cluster]
h.adminClientsLock.RUnlock()
if ok {
return client, nil
}
clusterInfo, clusterFound := h.clusterMetadata.GetAllClusterInfo()[cluster]
if !clusterFound {
// We intentionally return internal error here.
// This error could only happen with internal mis-configuration.
// This can happen when a namespace is config for multiple clusters. But those clusters are not connected.
// We also have logic in task processing to drop tasks when namespace cluster exclude a local cluster.
return nil, &serviceerror.Internal{
Message: fmt.Sprintf(
"Unknown cluster name: %v with given cluster information map: %v.",
cluster,
clusterInfo,
),
}
}
h.adminClientsLock.Lock()
defer h.adminClientsLock.Unlock()
client, ok = h.adminClients[cluster]
if ok {
return client, nil
}
client = h.factory.NewRemoteAdminClientWithTimeout(
clusterInfo.RPCAddress,
admin.DefaultTimeout,
admin.DefaultLargeTimeout,
)
h.adminClients[cluster] = client
return client, nil
}
func (h *clientBeanImpl) GetRemoteFrontendClient(clusterName string) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient, error) {
h.frontendClientsLock.RLock()
client, ok := h.frontendClients[clusterName]
h.frontendClientsLock.RUnlock()
if ok {
return client.connection, client, nil
}
clusterInfo, clusterFound := h.clusterMetadata.GetAllClusterInfo()[clusterName]
if !clusterFound {
// We intentionally return internal error here.
// This error could only happen with internal mis-configuration.
// This can happen when a namespace is config for multiple clusters. But those clusters are not connected.
// We also have logic in task processing to drop tasks when namespace cluster exclude a local cluster.
return nil, nil, &serviceerror.Internal{
Message: fmt.Sprintf(
"Unknown clusterName name: %v with given clusterName information map: %v.",
clusterName,
clusterInfo,
),
}
}
h.frontendClientsLock.Lock()
defer h.frontendClientsLock.Unlock()
client, ok = h.frontendClients[clusterName]
if ok {
return client.connection, client, nil
}
conn, fClient := h.factory.NewRemoteFrontendClientWithTimeout(
clusterInfo.RPCAddress,
frontend.DefaultTimeout,
frontend.DefaultLongPollTimeout,
)
client = frontendClient{
connection: conn,
WorkflowServiceClient: fClient,
}
h.frontendClients[clusterName] = client
return client.connection, client, nil
}
func (h *clientBeanImpl) lazyInitMatchingClient(namespaceIDToName NamespaceIDToNameFunc) (matchingservice.MatchingServiceClient, error) {
fx.go ×44
h.Lock()
defer h.Unlock()
if cached := h.matchingClient.Load(); cached != nil {
return cached.(matchingservice.MatchingServiceClient), nil
}
client, err := h.factory.NewMatchingClientWithTimeout(namespaceIDToName, matching.DefaultTimeout, matching.DefaultLongPollTimeout)
fx.go ×44
if err != nil {
return nil, err
}
return client, nil
}