Fine0830 commented on code in PR #918:
URL: 
https://github.com/apache/skywalking-banyandb/pull/918#discussion_r2653206836


##########
fodc/proxy/internal/grpc/service.go:
##########
@@ -0,0 +1,385 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package grpc
+
+import (
+       "context"
+       "errors"
+       "fmt"
+       "io"
+       "strings"
+       "sync"
+       "time"
+
+       "google.golang.org/grpc"
+       "google.golang.org/grpc/codes"
+       "google.golang.org/grpc/metadata"
+       "google.golang.org/grpc/peer"
+       "google.golang.org/grpc/status"
+       "google.golang.org/protobuf/types/known/timestamppb"
+
+       fodcv1 
"github.com/apache/skywalking-banyandb/api/proto/banyandb/fodc/v1"
+       "github.com/apache/skywalking-banyandb/fodc/proxy/internal/metrics"
+       "github.com/apache/skywalking-banyandb/fodc/proxy/internal/registry"
+       "github.com/apache/skywalking-banyandb/pkg/logger"
+)
+
+// AgentConnection represents a connection to an agent.
+type AgentConnection struct {
+       MetricsStream fodcv1.FODCService_StreamMetricsServer
+       Context       context.Context
+       Stream        grpc.ServerStream
+       Cancel        context.CancelFunc
+       LastActivity  time.Time
+       AgentID       string
+       Identity      registry.AgentIdentity
+       mu            sync.RWMutex
+}
+
+// UpdateActivity updates the last activity time.
+func (ac *AgentConnection) UpdateActivity() {
+       ac.mu.Lock()
+       defer ac.mu.Unlock()
+       ac.LastActivity = time.Now()
+}
+
+// GetLastActivity returns the last activity time.
+func (ac *AgentConnection) GetLastActivity() time.Time {
+       ac.mu.RLock()
+       defer ac.mu.RUnlock()
+       return ac.LastActivity
+}
+
+// FODCService implements the FODC gRPC service.
+type FODCService struct {
+       fodcv1.UnimplementedFODCServiceServer
+       registry          *registry.AgentRegistry
+       metricsAggregator *metrics.Aggregator
+       logger            *logger.Logger
+       connections       map[string]*AgentConnection
+       connectionsMu     sync.RWMutex
+       heartbeatInterval time.Duration
+}
+
+// NewFODCService creates a new FODCService instance.
+func NewFODCService(registry *registry.AgentRegistry, metricsAggregator 
*metrics.Aggregator, logger *logger.Logger, heartbeatInterval time.Duration) 
*FODCService {
+       return &FODCService{
+               registry:          registry,
+               metricsAggregator: metricsAggregator,
+               logger:            logger,
+               connections:       make(map[string]*AgentConnection),
+               heartbeatInterval: heartbeatInterval,
+       }
+}
+
+// RegisterAgent handles bi-directional agent registration stream.
+func (s *FODCService) RegisterAgent(stream 
fodcv1.FODCService_RegisterAgentServer) error {
+       ctx, cancel := context.WithCancel(stream.Context())
+       defer cancel()
+
+       var agentID string
+       var agentConn *AgentConnection
+       initialRegistration := true
+
+       for {
+               req, recvErr := stream.Recv()
+               if errors.Is(recvErr, io.EOF) {
+                       s.logger.Debug().Str("agent_id", 
agentID).Msg("Registration stream closed by agent")
+                       break
+               }
+               if recvErr != nil {
+                       s.logger.Error().Err(recvErr).Str("agent_id", 
agentID).Msg("Error receiving registration request")
+                       return recvErr
+               }
+
+               if initialRegistration {
+                       identity := registry.AgentIdentity{
+                               IP:     req.PrimaryAddress.Ip,
+                               Port:   int(req.PrimaryAddress.Port),
+                               Role:   req.NodeRole,
+                               Labels: req.Labels,
+                       }
+
+                       primaryAddr := registry.Address{
+                               IP:   req.PrimaryAddress.Ip,
+                               Port: int(req.PrimaryAddress.Port),
+                       }
+
+                       registeredAgentID, registerErr := 
s.registry.RegisterAgent(ctx, identity, primaryAddr)
+                       if registerErr != nil {
+                               resp := &fodcv1.RegisterAgentResponse{
+                                       Success: false,
+                                       Message: registerErr.Error(),
+                               }
+                               if sendErr := stream.Send(resp); sendErr != nil 
{
+                                       
s.logger.Error().Err(sendErr).Msg("Failed to send registration error response")
+                               }
+                               return registerErr
+                       }
+
+                       agentID = registeredAgentID
+                       agentConn = &AgentConnection{
+                               AgentID:      agentID,
+                               Identity:     identity,
+                               Stream:       stream,
+                               Context:      ctx,
+                               Cancel:       cancel,
+                               LastActivity: time.Now(),
+                       }
+
+                       s.connectionsMu.Lock()
+                       s.connections[agentID] = agentConn
+                       s.connectionsMu.Unlock()
+
+                       resp := &fodcv1.RegisterAgentResponse{
+                               Success:                  true,
+                               Message:                  "Agent registered 
successfully",
+                               HeartbeatIntervalSeconds: 
int64(s.heartbeatInterval.Seconds()),
+                               AgentId:                  agentID,
+                       }
+
+                       if sendErr := stream.Send(resp); sendErr != nil {
+                               s.logger.Error().Err(sendErr).Str("agent_id", 
agentID).Msg("Failed to send registration response")
+                               s.cleanupConnection(agentID)
+                               // Unregister agent since we couldn't send 
confirmation
+                               if unregisterErr := 
s.registry.UnregisterAgent(agentID); unregisterErr != nil {
+                                       
s.logger.Error().Err(unregisterErr).Str("agent_id", agentID).Msg("Failed to 
unregister agent after send error")
+                               }
+                               return sendErr
+                       }
+
+                       initialRegistration = false
+                       s.logger.Info().
+                               Str("agent_id", agentID).
+                               Str("ip", identity.IP).
+                               Int("port", identity.Port).
+                               Str("role", identity.Role).
+                               Msg("Agent registration stream established")
+               } else {
+                       if updateErr := s.registry.UpdateHeartbeat(agentID); 
updateErr != nil {
+                               s.logger.Error().Err(updateErr).Str("agent_id", 
agentID).Msg("Failed to update heartbeat")
+                               s.cleanupConnection(agentID)
+                               return updateErr
+                       }
+
+                       if agentConn != nil {
+                               agentConn.UpdateActivity()
+                       }
+               }
+       }
+
+       s.cleanupConnection(agentID)
+       return nil
+}
+
+// StreamMetrics handles bi-directional metrics streaming.
+func (s *FODCService) StreamMetrics(stream 
fodcv1.FODCService_StreamMetricsServer) error {
+       ctx := stream.Context()
+
+       agentID := s.getAgentIDFromContext(ctx)
+       if agentID == "" {
+               agentID = s.getAgentIDFromPeer(ctx)
+               if agentID != "" {
+                       s.logger.Warn().
+                               Str("agent_id", agentID).
+                               Msg("Agent ID not found in metadata, using peer 
address fallback (this may be unreliable)")
+               }
+       }
+
+       if agentID == "" {
+               s.logger.Error().Msg("Agent ID not found in context metadata or 
peer address")
+               return status.Errorf(codes.Unauthenticated, "agent ID not found 
in context or peer address")
+       }
+
+       s.connectionsMu.Lock()
+       existingConn, exists := s.connections[agentID]
+       if exists {
+               existingConn.MetricsStream = stream
+               existingConn.UpdateActivity()
+       } else {
+               agentConn := &AgentConnection{
+                       AgentID:       agentID,
+                       Stream:        stream,
+                       MetricsStream: stream,
+                       Context:       ctx,
+                       LastActivity:  time.Now(),
+               }
+               s.connections[agentID] = agentConn
+       }
+       s.connectionsMu.Unlock()
+
+       defer s.cleanupConnection(agentID)
+
+       recvCh := make(chan *fodcv1.StreamMetricsRequest, 1)
+       recvErrCh := make(chan error, 1)

Review Comment:
   Actually, this is an extension based on [official 
methods](https://grpc.io/docs/languages/go/basics/), but our scenario is more 
complex. With background goroutine + channel, main loop can respond to 
ctx.Done(), can handle timeouts if agent stops sending, can handle multiple 
event sources, and non-blocking main event loop.
   
   <img width="1006" height="803" alt="1" 
src="https://github.com/user-attachments/assets/bc09b262-a228-43fa-a6aa-b2d5c3ba28f4";
 />
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to