Skip to content

Commit f9b6e76

Browse files
committed
don't put all load in the last orderer
Signed-off-by: David VIEJO <[email protected]>
1 parent 6b56f8a commit f9b6e76

File tree

1 file changed

+56
-13
lines changed

1 file changed

+56
-13
lines changed

controllers/mainchannel/mainchannel_controller.go

+56-13
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func (r *FabricMainChannelReconciler) Reconcile(ctx context.Context, req ctrl.Re
116116
return r.handleReconcileError(ctx, fabricMainChannel, err)
117117
}
118118

119-
resmgmtOptions := r.setupResmgmtOptions(fabricMainChannel)
119+
options, _ := r.setupResmgmtOptions(fabricMainChannel)
120120

121-
blockBytes, err := r.fetchConfigBlock(resClient, fabricMainChannel, resmgmtOptions)
121+
blockBytes, err := r.fetchConfigBlock(resClient, fabricMainChannel, options)
122122
if err != nil {
123123
return r.handleReconcileError(ctx, fabricMainChannel, err)
124124
}
@@ -127,11 +127,11 @@ func (r *FabricMainChannelReconciler) Reconcile(ctx context.Context, req ctrl.Re
127127
return r.handleReconcileError(ctx, fabricMainChannel, err)
128128
}
129129

130-
if err := r.updateChannelConfig(ctx, fabricMainChannel, resClient, resmgmtOptions, blockBytes, sdk, clientSet); err != nil {
130+
if err := r.updateChannelConfig(ctx, fabricMainChannel, resClient, options, blockBytes, sdk, clientSet); err != nil {
131131
return r.handleReconcileError(ctx, fabricMainChannel, err)
132132
}
133133
time.Sleep(3 * time.Second)
134-
if err := r.saveChannelConfig(ctx, fabricMainChannel, resClient, resmgmtOptions); err != nil {
134+
if err := r.saveChannelConfig(ctx, fabricMainChannel, resClient, options); err != nil {
135135
return r.handleReconcileError(ctx, fabricMainChannel, err)
136136
}
137137

@@ -395,6 +395,43 @@ func (r *FabricMainChannelReconciler) joinInternalOrderers(ctx context.Context,
395395
return nil
396396
}
397397

398+
func (r *FabricMainChannelReconciler) queryConfigBlockFromOrdererWithRoundRobin(resClient *resmgmt.Client, channelID string, ordererEndpoints []string, resmgmtOptions []resmgmt.RequestOption) (*common.Block, error) {
399+
if len(ordererEndpoints) == 0 {
400+
return nil, fmt.Errorf("no orderer endpoints available")
401+
}
402+
403+
// Try each orderer in sequence until one succeeds
404+
var lastErr error
405+
for _, endpoint := range ordererEndpoints {
406+
// Create options for this specific orderer
407+
ordererOpts := []resmgmt.RequestOption{
408+
resmgmt.WithOrdererEndpoint(endpoint),
409+
resmgmt.WithRetry(retry.Opts{
410+
Attempts: 3,
411+
InitialBackoff: 1 * time.Second,
412+
MaxBackoff: 10 * time.Second,
413+
}),
414+
}
415+
416+
// Add any other options that were passed in (except orderer endpoints)
417+
for _, opt := range resmgmtOptions {
418+
ordererOpts = append(ordererOpts, opt)
419+
}
420+
421+
log.Infof("Attempting to query config block from orderer %s", endpoint)
422+
block, err := resClient.QueryConfigBlockFromOrderer(channelID, ordererOpts...)
423+
if err != nil {
424+
log.Warnf("Failed to query config block from orderer %s: %v", endpoint, err)
425+
lastErr = err
426+
continue
427+
}
428+
log.Infof("Successfully queried config block from orderer %s", endpoint)
429+
return block, nil
430+
}
431+
432+
return nil, fmt.Errorf("failed to query config block from all orderers, last error: %v", lastErr)
433+
}
434+
398435
func (r *FabricMainChannelReconciler) fetchOrdererChannelBlock(resClient *resmgmt.Client, fabricMainChannel *hlfv1alpha1.FabricMainChannel, resmgmtOptions []resmgmt.RequestOption) (*common.Block, error) {
399436
var ordererChannelBlock *common.Block
400437
var err error
@@ -403,7 +440,9 @@ func (r *FabricMainChannelReconciler) fetchOrdererChannelBlock(resClient *resmgm
403440
InitialBackoff: 1000 * time.Millisecond,
404441
MaxBackoff: 10 * time.Second,
405442
}))
406-
ordererChannelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...)
443+
444+
options, endpoints := r.setupResmgmtOptions(fabricMainChannel)
445+
ordererChannelBlock, err = r.queryConfigBlockFromOrdererWithRoundRobin(resClient, fabricMainChannel.Spec.Name, endpoints, options)
407446
if err != nil {
408447
return nil, errors.Wrapf(err, "failed to get block from channel %s", fabricMainChannel.Spec.Name)
409448
}
@@ -478,18 +517,25 @@ func (r *FabricMainChannelReconciler) handleReconcileError(ctx context.Context,
478517
return r.updateCRStatusOrFailReconcile(ctx, r.Log, fabricMainChannel)
479518
}
480519

481-
func (r *FabricMainChannelReconciler) setupResmgmtOptions(fabricMainChannel *hlfv1alpha1.FabricMainChannel) []resmgmt.RequestOption {
520+
func (r *FabricMainChannelReconciler) setupResmgmtOptions(fabricMainChannel *hlfv1alpha1.FabricMainChannel) ([]resmgmt.RequestOption, []string) {
482521
resmgmtOptions := []resmgmt.RequestOption{
483522
resmgmt.WithTimeout(fab2.ResMgmt, 30*time.Second),
523+
resmgmt.WithRetry(retry.Opts{
524+
Attempts: 3,
525+
InitialBackoff: 1 * time.Second,
526+
MaxBackoff: 10 * time.Second,
527+
}),
484528
}
485529

530+
var ordererEndpoints []string
486531
for _, ordOrg := range fabricMainChannel.Spec.OrdererOrganizations {
487532
for _, endpoint := range ordOrg.OrdererEndpoints {
488-
resmgmtOptions = append(resmgmtOptions, resmgmt.WithOrdererEndpoint(endpoint))
533+
ordererEndpoints = append(ordererEndpoints, endpoint)
534+
// resmgmtOptions = append(resmgmtOptions)
489535
}
490536
}
491537

492-
return resmgmtOptions
538+
return resmgmtOptions, ordererEndpoints
493539
}
494540

495541
func (r *FabricMainChannelReconciler) fetchConfigBlock(resClient *resmgmt.Client, fabricMainChannel *hlfv1alpha1.FabricMainChannel, resmgmtOptions []resmgmt.RequestOption) ([]byte, error) {
@@ -501,11 +547,8 @@ func (r *FabricMainChannelReconciler) fetchConfigBlock(resClient *resmgmt.Client
501547
MaxBackoff: 10 * time.Second,
502548
}))
503549

504-
channelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...)
505-
if err != nil {
506-
return nil, errors.Wrapf(err, "failed to query config block from orderer %s", fabricMainChannel.Spec.Name)
507-
}
508-
550+
options, endpoints := r.setupResmgmtOptions(fabricMainChannel)
551+
channelBlock, err = r.queryConfigBlockFromOrdererWithRoundRobin(resClient, fabricMainChannel.Spec.Name, endpoints, options)
509552
if err != nil {
510553
log.Infof("Channel %s does not exist, creating it: %v", fabricMainChannel.Spec.Name, err)
511554
return r.createNewChannel(fabricMainChannel)

0 commit comments

Comments
 (0)