hanahmily commented on code in PR #918: URL: https://github.com/apache/skywalking-banyandb/pull/918#discussion_r2656056402
########## fodc/proxy/internal/grpc/service.go: ########## @@ -0,0 +1,359 @@ +// 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 + defer s.cleanupConnection(registeredAgentID) Review Comment: Place it outside the loop. Please remember never put a defer in a loop. ########## fodc/proxy/internal/grpc/service.go: ########## @@ -0,0 +1,359 @@ +// 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 { Review Comment: The fields "context," "stream," "cancel," "agentID," and "identity" are not used. It might be a good idea to consider removing them. -- 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]
