diff --git a/pkg/check/manifest/manifest.go b/pkg/check/manifest/manifest.go index a0cafc99..e903d61c 100644 --- a/pkg/check/manifest/manifest.go +++ b/pkg/check/manifest/manifest.go @@ -69,19 +69,13 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int } rnd := random.PseudoGenerator(o.Seed) - names := cluster.FullNodeNames() - perm := rnd.Perm(len(names)) + clients, err := cluster.ShuffledFullNodeClients(ctx, rnd) - if len(names) < 2 { + if len(clients) < 2 { return fmt.Errorf("not enough nodes to run feed check") } - - clients, err := cluster.NodesClients(ctx) - if err != nil { - return err - } - upClient := clients[names[perm[0]]] - downClient := clients[names[perm[1]]] + upClient := clients[0] + downClient := clients[1] err = c.checkWithoutSubDirs(ctx, rnd, o, upClient, downClient) if err != nil { @@ -229,38 +223,37 @@ func (c *Check) download(client *bee.Client, address swarm.Address, file *bee.Fi } c.logger.Infof("downloading file: %s/%s", address, fName) - try := 0 -DOWNLOAD: - time.Sleep(5 * time.Second) - try++ - if try > 5 { - return fmt.Errorf("failed getting manifest files after too many retries") - } - _, hash, err := client.DownloadManifestFile(context.Background(), address, fName) - if err != nil { + var hash []byte + for i := 0; i < 5; i++ { + _, h, err := client.DownloadManifestFile(context.Background(), address, fName) + hash = h + if err == nil { + break + } c.logger.Infof("node %s. Error retrieving file: %s", client.Name(), err.Error()) - goto DOWNLOAD + time.Sleep(5 * time.Second) + } + if hash == nil { + return fmt.Errorf("failed getting manifest files after too many retries") } + expectedHash := indexFile.Hash() if file != nil { - if !bytes.Equal(file.Hash(), hash) { - c.logger.Infof("node %s. File hash does not match", client.Name()) - return errManifest - } - } else { - if !bytes.Equal(indexFile.Hash(), hash) { - c.logger.Infof("node %s. Index hash does not match", client.Name()) - return errManifest - } + expectedHash = file.Hash() } - c.logger.Infof("node %s. File retrieved successfully", client.Name()) + + if !bytes.Equal(expectedHash, hash) { + c.logger.Infof("node %s: file hash does not match", client.Name()) + return errManifest + } + + c.logger.Infof("node %s: file retrieved successfully", client.Name()) return nil } func generateFilesWithPaths(r *rand.Rand, paths []string, maxSize int) ([]bee.File, error) { files := make([]bee.File, len(paths)) - for i := 0; i < len(paths); i++ { - path := paths[i] + for i, path := range paths { size := int64(r.Intn(maxSize)) + 1 file := bee.NewRandomFile(r, path, size) err := file.CalculateHash() @@ -306,6 +299,8 @@ func tarFiles(files []bee.File) (*bytes.Buffer, error) { var buf bytes.Buffer tw := tar.NewWriter(&buf) + defer tw.Close() + for _, file := range files { // create tar header and write it hdr := &tar.Header{ @@ -327,10 +322,6 @@ func tarFiles(files []bee.File) (*bytes.Buffer, error) { return nil, err } } - - if err := tw.Close(); err != nil { - return nil, err - } - + return &buf, nil } diff --git a/pkg/orchestration/cluster.go b/pkg/orchestration/cluster.go index 9bb7b808..da50f4a6 100644 --- a/pkg/orchestration/cluster.go +++ b/pkg/orchestration/cluster.go @@ -23,6 +23,7 @@ type Cluster interface { FlattenSettlements(ctx context.Context) (settlements NodeGroupSettlements, err error) FlattenTopologies(ctx context.Context) (topologies map[string]bee.Topology, err error) FullNodeNames() (names []string) + ShuffledFullNodeClients(ctx context.Context, r *rand.Rand) ([]*bee.Client, error) GlobalReplicationFactor(ctx context.Context, a swarm.Address) (grf int, err error) LightNodeNames() (names []string) Name() string diff --git a/pkg/orchestration/k8s/cluster.go b/pkg/orchestration/k8s/cluster.go index 98b9967d..57b42093 100644 --- a/pkg/orchestration/k8s/cluster.go +++ b/pkg/orchestration/k8s/cluster.go @@ -233,6 +233,22 @@ func (c *Cluster) FullNodeNames() (names []string) { return } +// ShuffledFullNodeClients returns a list of full node clients shuffled +func (c *Cluster) ShuffledFullNodeClients(ctx context.Context, r *rand.Rand) ([]*bee.Client, error) { + cls, err := c.NodesClients(ctx) + if err != nil { + return nil, err + } + var res []*bee.Client + for _, cl := range cls { + res = append(res, cl) + } + r.Shuffle(len(res), func(i, j int) { + res[i], res[j] = res[j], res[i] + }) + return res, nil +} + // NodesClients returns map of node's clients in the cluster excluding stopped nodes func (c *Cluster) NodesClients(ctx context.Context) (map[string]*bee.Client, error) { clients := make(map[string]*bee.Client)