mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-01 01:40:44 +01:00
* mq/kafka: wait for a filer to register instead of panicking on an empty list NewSeaweedMQBrokerHandler queried the masters for filers once, ~2 s after start, and handed an empty list to NewFilerClientAccessor, which panics. A fresh filer registers only after its empty-membership confirm window (10 s), so a gateway started with a fresh cluster crashed (Kafka Quick Test, kafka-gateway exit 2). Retry while the list is empty for up to 60 s, then return an error. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * mq/kafka: wait out a transient-empty broker listing too A fresh SMQ broker registers with the masters on the same announce delay as a filer, so the one-shot broker lookup could fail startup the same way. Generalize waitForFilers into waitForClusterNodes and bound both discoveries by the same window; the empty-broker check becomes unreachable and drops away. * mq/kafka: wait out the broker health check like node discovery A broker announces to the masters before it has acquired its balancer lock, so the gateway's three-attempt probe could still fail fast on a fresh cluster. Retry FindBrokerLeader for the same window the caller waits out empty listings. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: user.email <chrislusf@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
244 lines
8.7 KiB
Go
244 lines
8.7 KiB
Go
package integration
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer_client"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
)
|
|
|
|
// NewSeaweedMQBrokerHandler creates a new handler with SeaweedMQ broker integration
|
|
func NewSeaweedMQBrokerHandler(masters string, filerGroup string, clientHost string) (*SeaweedMQHandler, error) {
|
|
if masters == "" {
|
|
return nil, fmt.Errorf("masters required - SeaweedMQ infrastructure must be configured")
|
|
}
|
|
|
|
// Parse master addresses using SeaweedFS utilities
|
|
masterServerAddresses := pb.ServerAddresses(masters).ToAddresses()
|
|
if len(masterServerAddresses) == 0 {
|
|
return nil, fmt.Errorf("no valid master addresses provided")
|
|
}
|
|
|
|
// Load security configuration for gRPC connections
|
|
util.LoadSecurityConfiguration()
|
|
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.mq")
|
|
masterDiscovery := pb.ServerAddresses(masters).ToServiceDiscovery()
|
|
|
|
// Use provided client host for proper gRPC connection
|
|
// This is critical for MasterClient to establish streaming connections
|
|
clientHostAddr := pb.ServerAddress(clientHost)
|
|
|
|
masterClient := wdclient.NewMasterClient(grpcDialOption, filerGroup, "kafka-gateway", clientHostAddr, "", "", *masterDiscovery)
|
|
|
|
glog.V(1).Infof("Created MasterClient with clientHost=%s, masters=%s", clientHost, masters)
|
|
|
|
// Start KeepConnectedToMaster in background to maintain connection
|
|
glog.V(1).Infof("Starting KeepConnectedToMaster background goroutine...")
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go func() {
|
|
defer cancel()
|
|
masterClient.KeepConnectedToMaster(ctx)
|
|
}()
|
|
|
|
// Give the connection a moment to establish
|
|
time.Sleep(2 * time.Second)
|
|
glog.V(1).Infof("Initial connection delay completed")
|
|
|
|
// Discover brokers and filers from masters using master client. A fresh
|
|
// node registers only after its announce delay (a filer also waits out its
|
|
// empty-membership confirm window), so an empty list right after startup
|
|
// is normal: wait rather than fail fast.
|
|
glog.V(1).Infof("About to call discoverBrokersWithMasterClient...")
|
|
brokerAddresses, err := waitForClusterNodes(ctx, "brokers", clusterNodeDiscoveryTimeout, func() ([]string, error) {
|
|
return discoverBrokersWithMasterClient(ctx, masterClient, filerGroup)
|
|
})
|
|
if err != nil {
|
|
glog.Errorf("Broker discovery failed: %v", err)
|
|
return nil, fmt.Errorf("failed to discover brokers: %v", err)
|
|
}
|
|
glog.V(1).Infof("Broker discovery returned: %v", brokerAddresses)
|
|
|
|
filerAddresses, err := waitForClusterNodes(ctx, "filers", clusterNodeDiscoveryTimeout, func() ([]pb.ServerAddress, error) {
|
|
return discoverFilersWithMasterClient(ctx, masterClient, filerGroup)
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to discover filers: %v", err)
|
|
}
|
|
|
|
// Create shared filer client accessor for all components
|
|
sharedFilerAccessor := filer_client.NewFilerClientAccessor(
|
|
filerAddresses,
|
|
grpcDialOption,
|
|
)
|
|
|
|
// For now, use the first broker (can be enhanced later for load balancing)
|
|
brokerAddress := brokerAddresses[0]
|
|
|
|
// Create broker client with shared filer accessor
|
|
brokerClient, err := NewBrokerClientWithFilerAccessor(brokerAddress, sharedFilerAccessor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create broker client: %v", err)
|
|
}
|
|
|
|
// Test the connection
|
|
if err := brokerClient.HealthCheck(); err != nil {
|
|
brokerClient.Close()
|
|
return nil, fmt.Errorf("broker health check failed: %v", err)
|
|
}
|
|
|
|
return &SeaweedMQHandler{
|
|
filerClientAccessor: sharedFilerAccessor,
|
|
brokerClient: brokerClient,
|
|
masterClient: masterClient,
|
|
// topics map removed - always read from filer directly
|
|
// ledgers removed - SMQ broker handles all offset management
|
|
brokerAddresses: brokerAddresses, // Store all discovered broker addresses
|
|
hwmCache: make(map[string]*hwmCacheEntry),
|
|
hwmCacheTTL: 100 * time.Millisecond, // 100ms cache TTL for fresh HWM reads (critical for Schema Registry)
|
|
topicExistsCache: make(map[string]*topicExistsCacheEntry),
|
|
topicExistsCacheTTL: 5 * time.Second, // 5 second cache TTL for topic existence
|
|
}, nil
|
|
}
|
|
|
|
// discoverBrokersWithMasterClient queries masters for available brokers using reusable master client
|
|
func discoverBrokersWithMasterClient(ctx context.Context, masterClient *wdclient.MasterClient, filerGroup string) ([]string, error) {
|
|
var brokers []string
|
|
|
|
err := masterClient.WithClient(ctx, false, func(client master_pb.SeaweedClient) error {
|
|
glog.V(1).Infof("Inside MasterClient.WithClient callback - client obtained successfully")
|
|
resp, err := client.ListClusterNodes(ctx, &master_pb.ListClusterNodesRequest{
|
|
ClientType: cluster.BrokerType,
|
|
FilerGroup: filerGroup,
|
|
Limit: 1000,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
glog.V(1).Infof("list cluster nodes successful - found %d cluster nodes", len(resp.ClusterNodes))
|
|
|
|
// Extract broker addresses from response
|
|
for _, node := range resp.ClusterNodes {
|
|
if node.Address != "" {
|
|
brokers = append(brokers, node.Address)
|
|
glog.V(1).Infof("discovered broker: %s", node.Address)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
glog.Errorf("MasterClient.WithClient failed: %v", err)
|
|
} else {
|
|
glog.V(1).Infof("Broker discovery completed successfully - found %d brokers: %v", len(brokers), brokers)
|
|
}
|
|
|
|
return brokers, err
|
|
}
|
|
|
|
// clusterNodeDiscoveryTimeout bounds how long the gateway waits for the
|
|
// masters to list a node; well above a fresh node's announce delay.
|
|
var clusterNodeDiscoveryTimeout = 60 * time.Second
|
|
|
|
// waitForClusterNodes calls discover until it returns at least one node, an
|
|
// error, or timeout passes, retrying once a second while the list is empty.
|
|
func waitForClusterNodes[T any](ctx context.Context, nodeType string, timeout time.Duration, discover func() ([]T, error)) ([]T, error) {
|
|
deadline := time.Now().Add(timeout)
|
|
for {
|
|
nodes, err := discover()
|
|
if err != nil || len(nodes) > 0 {
|
|
return nodes, err
|
|
}
|
|
if !time.Now().Before(deadline) {
|
|
return nil, fmt.Errorf("no %s registered with the masters after %v", nodeType, timeout)
|
|
}
|
|
glog.V(1).Infof("no %s registered with the masters yet; retrying", nodeType)
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case <-time.After(time.Second):
|
|
}
|
|
}
|
|
}
|
|
|
|
func discoverFilersWithMasterClient(ctx context.Context, masterClient *wdclient.MasterClient, filerGroup string) ([]pb.ServerAddress, error) {
|
|
var filers []pb.ServerAddress
|
|
|
|
err := masterClient.WithClient(ctx, false, func(client master_pb.SeaweedClient) error {
|
|
resp, err := client.ListClusterNodes(ctx, &master_pb.ListClusterNodesRequest{
|
|
ClientType: cluster.FilerType,
|
|
FilerGroup: filerGroup,
|
|
Limit: 1000,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Extract filer addresses from response - return as HTTP addresses (pb.ServerAddress)
|
|
for _, node := range resp.ClusterNodes {
|
|
if node.Address != "" {
|
|
// Return HTTP address as pb.ServerAddress (no pre-conversion to gRPC)
|
|
httpAddr := pb.ServerAddress(node.Address)
|
|
filers = append(filers, httpAddr)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return filers, err
|
|
}
|
|
|
|
// GetFilerClientAccessor returns the shared filer client accessor
|
|
func (h *SeaweedMQHandler) GetFilerClientAccessor() *filer_client.FilerClientAccessor {
|
|
return h.filerClientAccessor
|
|
}
|
|
|
|
// SetProtocolHandler sets the protocol handler reference for accessing connection context
|
|
func (h *SeaweedMQHandler) SetProtocolHandler(handler ProtocolHandler) {
|
|
h.protocolHandler = handler
|
|
}
|
|
|
|
// GetBrokerAddresses returns the discovered SMQ broker addresses
|
|
func (h *SeaweedMQHandler) GetBrokerAddresses() []string {
|
|
return h.brokerAddresses
|
|
}
|
|
|
|
// Close shuts down the handler and all connections
|
|
func (h *SeaweedMQHandler) Close() error {
|
|
if h.brokerClient != nil {
|
|
return h.brokerClient.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreatePerConnectionBrokerClient creates a new BrokerClient instance for a specific connection
|
|
// CRITICAL: Each Kafka TCP connection gets its own BrokerClient to prevent gRPC stream interference
|
|
// This fixes the deadlock where CreateFreshSubscriber would block all connections
|
|
func (h *SeaweedMQHandler) CreatePerConnectionBrokerClient() (*BrokerClient, error) {
|
|
// Use the same broker addresses as the shared client
|
|
if len(h.brokerAddresses) == 0 {
|
|
return nil, fmt.Errorf("no broker addresses available")
|
|
}
|
|
|
|
// Use the first broker address (in production, could use load balancing)
|
|
brokerAddress := h.brokerAddresses[0]
|
|
|
|
// Create a new client with the shared filer accessor
|
|
client, err := NewBrokerClientWithFilerAccessor(brokerAddress, h.filerClientAccessor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create broker client: %w", err)
|
|
}
|
|
|
|
return client, nil
|
|
}
|