Skip to content

Commit 57345d8

Browse files
committed
addressing static code issue
1 parent e1c46b6 commit 57345d8

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

server/internal/orchestrator/swarm/rag_service_user_role.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,11 @@ func (r *RAGServiceUserRole) connectToColocatedPrimary(ctx context.Context, rc *
203203
return nil, err
204204
}
205205

206-
db, err := dbSvc.GetDatabase(ctx, r.DatabaseID)
206+
primaryInstanceID, err := r.resolveColocatedPrimary(ctx, dbSvc, logger)
207207
if err != nil {
208-
if errors.Is(err, database.ErrDatabaseNotFound) {
209-
return nil, fmt.Errorf("database not found: %w", err)
210-
}
211-
return nil, fmt.Errorf("failed to get database: %w", err)
212-
}
213-
if len(db.Instances) == 0 {
214-
return nil, fmt.Errorf("database has no instances")
208+
return nil, err
215209
}
216210

217-
candidates := r.colocatedInstances(db.Instances, logger)
218-
primaryInstanceID := r.findPrimaryAmong(ctx, dbSvc, candidates, logger)
219-
220211
connInfo, err := dbSvc.GetInstanceConnectionInfo(ctx, r.DatabaseID, primaryInstanceID)
221212
if err != nil {
222213
return nil, fmt.Errorf("failed to get instance connection info: %w", err)
@@ -243,6 +234,23 @@ func (r *RAGServiceUserRole) connectToColocatedPrimary(ctx context.Context, rc *
243234
return conn, nil
244235
}
245236

237+
// resolveColocatedPrimary fetches the database, selects co-located instances,
238+
// and returns the primary instance ID via Patroni.
239+
func (r *RAGServiceUserRole) resolveColocatedPrimary(ctx context.Context, dbSvc *database.Service, logger zerolog.Logger) (string, error) {
240+
db, err := dbSvc.GetDatabase(ctx, r.DatabaseID)
241+
if err != nil {
242+
if errors.Is(err, database.ErrDatabaseNotFound) {
243+
return "", fmt.Errorf("database not found: %w", err)
244+
}
245+
return "", fmt.Errorf("failed to get database: %w", err)
246+
}
247+
if len(db.Instances) == 0 {
248+
return "", fmt.Errorf("database has no instances")
249+
}
250+
candidates := r.colocatedInstances(db.Instances, logger)
251+
return r.findPrimaryAmong(ctx, dbSvc, candidates, logger), nil
252+
}
253+
246254
// colocatedInstances returns the subset of instances that share r.HostID.
247255
// Falls back to all instances if none are co-located.
248256
func (r *RAGServiceUserRole) colocatedInstances(all []*database.Instance, logger zerolog.Logger) []*database.Instance {

0 commit comments

Comments
 (0)