From 44d7402dc100711b5d42b696c756c4f4e5f1750f Mon Sep 17 00:00:00 2001 From: David VIEJO Date: Tue, 14 Jan 2025 14:38:46 +0100 Subject: [PATCH] Refactor orderer block fetching with retry logic and update inspect command to use ledger client - Replaced manual retry logic in `fetchOrdererChannelBlock` and `fetchConfigBlock` methods with a retry option from the Fabric SDK, improving code readability and maintainability. - Updated the `inspectChannelCmd` to utilize the new ledger client for querying the configuration block, enhancing the command's functionality and performance. - Removed deprecated code related to resource management client in the inspect command. These changes streamline the process of fetching orderer blocks and improve the overall robustness of the channel inspection functionality. Signed-off-by: David VIEJO --- .../mainchannel/mainchannel_controller.go | 34 +++++++--------- kubectl-hlf/cmd/channel/inspect.go | 40 +++++++++++-------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/controllers/mainchannel/mainchannel_controller.go b/controllers/mainchannel/mainchannel_controller.go index 82f2eed7..c4b365b3 100644 --- a/controllers/mainchannel/mainchannel_controller.go +++ b/controllers/mainchannel/mainchannel_controller.go @@ -23,6 +23,7 @@ import ( cb "github.com/hyperledger/fabric-protos-go/common" sb "github.com/hyperledger/fabric-protos-go/orderer/smartbft" "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" + "github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry" fab2 "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" @@ -397,18 +398,12 @@ func (r *FabricMainChannelReconciler) joinInternalOrderers(ctx context.Context, func (r *FabricMainChannelReconciler) fetchOrdererChannelBlock(resClient *resmgmt.Client, fabricMainChannel *hlfv1alpha1.FabricMainChannel, resmgmtOptions []resmgmt.RequestOption) (*common.Block, error) { var ordererChannelBlock *common.Block var err error - attemptsLeft := 5 - for { - ordererChannelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...) - if err == nil || attemptsLeft == 0 { - break - } - if err != nil { - attemptsLeft-- - } - log.Infof("Failed to get block %v, attempts left %d", err, attemptsLeft) - time.Sleep(1500 * time.Millisecond) - } + resmgmtOptions = append(resmgmtOptions, resmgmt.WithRetry(retry.Opts{ + Attempts: 5, + InitialBackoff: 1000 * time.Millisecond, + MaxBackoff: 10 * time.Second, + })) + ordererChannelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...) if err != nil { return nil, errors.Wrapf(err, "failed to get block from channel %s", fabricMainChannel.Spec.Name) } @@ -500,14 +495,15 @@ func (r *FabricMainChannelReconciler) setupResmgmtOptions(fabricMainChannel *hlf func (r *FabricMainChannelReconciler) fetchConfigBlock(resClient *resmgmt.Client, fabricMainChannel *hlfv1alpha1.FabricMainChannel, resmgmtOptions []resmgmt.RequestOption) ([]byte, error) { var channelBlock *cb.Block var err error + resmgmtOptions = append(resmgmtOptions, resmgmt.WithRetry(retry.Opts{ + Attempts: 5, + InitialBackoff: 1000 * time.Millisecond, + MaxBackoff: 10 * time.Second, + })) - for i := 0; i < 5; i++ { - channelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...) - if err == nil { - break - } - log.Warnf("Attempt %d failed to query config block from orderer: %v retrying in 1 second", i+1, err) - time.Sleep(1 * time.Second) + channelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...) + if err != nil { + return nil, errors.Wrapf(err, "failed to query config block from orderer %s", fabricMainChannel.Spec.Name) } if err != nil { diff --git a/kubectl-hlf/cmd/channel/inspect.go b/kubectl-hlf/cmd/channel/inspect.go index ab3ecc33..9a3fba61 100644 --- a/kubectl-hlf/cmd/channel/inspect.go +++ b/kubectl-hlf/cmd/channel/inspect.go @@ -3,14 +3,15 @@ package channel import ( "bytes" "fmt" - "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" + "io" + "io/ioutil" + + "github.com/hyperledger/fabric-config/protolator" + "github.com/hyperledger/fabric-sdk-go/pkg/client/ledger" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/resource" "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" - "github.com/hyperledger/fabric/common/tools/protolator" "github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers" "github.com/spf13/cobra" - "io" ) type inspectChannelCmd struct { @@ -42,26 +43,31 @@ func (c *inspectChannelCmd) run(out io.Writer) error { if err != nil { return err } - org1AdminClientContext := sdk.Context( - fabsdk.WithUser(c.userName), - fabsdk.WithOrg(mspID), - ) - resClient, err := resmgmt.New(org1AdminClientContext) + // org1AdminClientContext := sdk.Context( + // fabsdk.WithUser(c.userName), + // fabsdk.WithOrg(mspID), + // ) + // resClient, err := resmgmt.New(org1AdminClientContext) + // if err != nil { + // return err + // } + chContext := sdk.ChannelContext(c.channelName, fabsdk.WithUser(c.userName), fabsdk.WithOrg(mspID)) + ledgerClient, err := ledger.New(chContext) if err != nil { return err } - block, err := resClient.QueryConfigBlockFromOrderer( - c.channelName, - ) - if err != nil { - return err - } - cmnConfig, err := resource.ExtractConfigFromBlock(block) + block, err := ledgerClient.QueryConfigBlock() if err != nil { return err } + ioutil.WriteFile("block.json", []byte(block.String()), 0644) + + // cmnConfig, err := resource.ExtractConfigFromBlock(block) + // if err != nil { + // return err + // } var buf bytes.Buffer - err = protolator.DeepMarshalJSON(&buf, cmnConfig) + err = protolator.DeepMarshalJSON(&buf, block) if err != nil { return err }