|
| 1 | +package spectatorclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "go.uber.org/fx" |
| 9 | + "go.uber.org/yarpc/api/peer" |
| 10 | + "go.uber.org/yarpc/api/transport" |
| 11 | + "go.uber.org/yarpc/peer/hostport" |
| 12 | + "go.uber.org/yarpc/yarpcerrors" |
| 13 | + |
| 14 | + "github.com/uber/cadence/common/log" |
| 15 | + "github.com/uber/cadence/common/log/tag" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + NamespaceHeader = "x-shard-distributor-namespace" |
| 20 | + grpcAddressMetadataKey = "grpc_address" |
| 21 | +) |
| 22 | + |
| 23 | +// SpectatorPeerChooserInterface extends peer.Chooser with SetSpectators method |
| 24 | +type SpectatorPeerChooserInterface interface { |
| 25 | + peer.Chooser |
| 26 | + SetSpectators(spectators Spectators) |
| 27 | +} |
| 28 | + |
| 29 | +// SpectatorPeerChooser is a peer.Chooser that uses the Spectator to route requests |
| 30 | +// to the correct executor based on shard ownership. |
| 31 | +// This is the shard distributor equivalent of Cadence's RingpopPeerChooser. |
| 32 | +// |
| 33 | +// Flow: |
| 34 | +// 1. Client calls RPC with yarpc.WithShardKey("shard-key") |
| 35 | +// 2. Choose() is called with req.ShardKey = "shard-key" |
| 36 | +// 3. Query Spectator for shard owner |
| 37 | +// 4. Extract grpc_address from owner metadata |
| 38 | +// 5. Create/reuse peer for that address |
| 39 | +// 6. Return peer to YARPC for connection |
| 40 | +type SpectatorPeerChooser struct { |
| 41 | + spectators Spectators |
| 42 | + transport peer.Transport |
| 43 | + logger log.Logger |
| 44 | + namespace string |
| 45 | + |
| 46 | + mu sync.RWMutex |
| 47 | + peers map[string]peer.Peer // grpc_address -> peer |
| 48 | +} |
| 49 | + |
| 50 | +type SpectatorPeerChooserParams struct { |
| 51 | + fx.In |
| 52 | + Transport peer.Transport |
| 53 | + Logger log.Logger |
| 54 | +} |
| 55 | + |
| 56 | +// NewSpectatorPeerChooser creates a new peer chooser that routes based on shard distributor ownership |
| 57 | +func NewSpectatorPeerChooser( |
| 58 | + params SpectatorPeerChooserParams, |
| 59 | +) SpectatorPeerChooserInterface { |
| 60 | + return &SpectatorPeerChooser{ |
| 61 | + transport: params.Transport, |
| 62 | + logger: params.Logger, |
| 63 | + peers: make(map[string]peer.Peer), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Start satisfies the peer.Chooser interface |
| 68 | +func (c *SpectatorPeerChooser) Start() error { |
| 69 | + c.logger.Info("Starting shard distributor peer chooser", tag.ShardNamespace(c.namespace)) |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// Stop satisfies the peer.Chooser interface |
| 74 | +func (c *SpectatorPeerChooser) Stop() error { |
| 75 | + c.logger.Info("Stopping shard distributor peer chooser", tag.ShardNamespace(c.namespace)) |
| 76 | + |
| 77 | + // Release all peers |
| 78 | + c.mu.Lock() |
| 79 | + defer c.mu.Unlock() |
| 80 | + |
| 81 | + for addr, p := range c.peers { |
| 82 | + if err := c.transport.ReleasePeer(p, &noOpSubscriber{}); err != nil { |
| 83 | + c.logger.Error("Failed to release peer", tag.Error(err), tag.Address(addr)) |
| 84 | + } |
| 85 | + } |
| 86 | + c.peers = make(map[string]peer.Peer) |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// IsRunning satisfies the peer.Chooser interface |
| 92 | +func (c *SpectatorPeerChooser) IsRunning() bool { |
| 93 | + return true |
| 94 | +} |
| 95 | + |
| 96 | +// Choose returns a peer for the given shard key by: |
| 97 | +// 0. Looking up the spectator for the namespace using the x-shard-distributor-namespace header |
| 98 | +// 1. Looking up the shard owner via the Spectator |
| 99 | +// 2. Extracting the grpc_address from the owner's metadata |
| 100 | +// 3. Creating/reusing a peer for that address |
| 101 | +// |
| 102 | +// The ShardKey in the request is the actual shard key (e.g., workflow ID, shard ID), |
| 103 | +// NOT the ip:port address. This is the key distinction from directPeerChooser. |
| 104 | +func (c *SpectatorPeerChooser) Choose(ctx context.Context, req *transport.Request) (peer.Peer, func(error), error) { |
| 105 | + if req.ShardKey == "" { |
| 106 | + return nil, nil, yarpcerrors.InvalidArgumentErrorf("chooser requires ShardKey to be non-empty") |
| 107 | + } |
| 108 | + |
| 109 | + // Get the spectator for the namespace |
| 110 | + namespace, ok := req.Headers.Get(NamespaceHeader) |
| 111 | + if !ok || namespace == "" { |
| 112 | + return nil, nil, yarpcerrors.InvalidArgumentErrorf("chooser requires x-shard-distributor-namespace header to be non-empty") |
| 113 | + } |
| 114 | + |
| 115 | + spectator, err := c.spectators.ForNamespace(namespace) |
| 116 | + if err != nil { |
| 117 | + return nil, nil, yarpcerrors.InvalidArgumentErrorf("failed to get spectator for namespace %s: %w", namespace, err) |
| 118 | + } |
| 119 | + |
| 120 | + // Query spectator for shard owner |
| 121 | + owner, err := spectator.GetShardOwner(ctx, req.ShardKey) |
| 122 | + if err != nil { |
| 123 | + return nil, nil, yarpcerrors.UnavailableErrorf("failed to get shard owner for key %s: %v", req.ShardKey, err) |
| 124 | + } |
| 125 | + |
| 126 | + // Extract GRPC address from owner metadata |
| 127 | + grpcAddress, ok := owner.Metadata[grpcAddressMetadataKey] |
| 128 | + if !ok || grpcAddress == "" { |
| 129 | + return nil, nil, yarpcerrors.InternalErrorf("no grpc_address in metadata for executor %s owning shard %s", owner.ExecutorID, req.ShardKey) |
| 130 | + } |
| 131 | + |
| 132 | + // Check if we already have a peer for this address |
| 133 | + c.mu.RLock() |
| 134 | + p, ok := c.peers[grpcAddress] |
| 135 | + if ok { |
| 136 | + c.mu.RUnlock() |
| 137 | + return p, func(error) {}, nil |
| 138 | + } |
| 139 | + c.mu.RUnlock() |
| 140 | + |
| 141 | + // Create new peer for this address |
| 142 | + p, err = c.addPeer(grpcAddress) |
| 143 | + if err != nil { |
| 144 | + return nil, nil, yarpcerrors.InternalErrorf("failed to add peer for address %s: %v", grpcAddress, err) |
| 145 | + } |
| 146 | + |
| 147 | + return p, func(error) {}, nil |
| 148 | +} |
| 149 | + |
| 150 | +func (c *SpectatorPeerChooser) SetSpectators(spectators Spectators) { |
| 151 | + c.spectators = spectators |
| 152 | +} |
| 153 | + |
| 154 | +func (c *SpectatorPeerChooser) addPeer(grpcAddress string) (peer.Peer, error) { |
| 155 | + c.mu.Lock() |
| 156 | + defer c.mu.Unlock() |
| 157 | + |
| 158 | + // Check again in case another goroutine added it |
| 159 | + if p, ok := c.peers[grpcAddress]; ok { |
| 160 | + return p, nil |
| 161 | + } |
| 162 | + |
| 163 | + p, err := c.transport.RetainPeer(hostport.Identify(grpcAddress), &noOpSubscriber{}) |
| 164 | + if err != nil { |
| 165 | + return nil, fmt.Errorf("retain peer failed: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + c.peers[grpcAddress] = p |
| 169 | + c.logger.Info("Added peer to shard distributor peer chooser", tag.Address(grpcAddress)) |
| 170 | + return p, nil |
| 171 | +} |
| 172 | + |
| 173 | +// noOpSubscriber is a no-op implementation of peer.Subscriber |
| 174 | +type noOpSubscriber struct{} |
| 175 | + |
| 176 | +func (*noOpSubscriber) NotifyStatusChanged(peer.Identifier) {} |
0 commit comments