Skip to content

Commit

Permalink
Update errors (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetomir Smiljkovic authored May 29, 2020
1 parent ec77e50 commit b539a3f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 46 deletions.
8 changes: 3 additions & 5 deletions pkg/bee/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

const maxChunkSize = 4096

var errChunkSize = fmt.Errorf("chunk size too big (max %d)", maxChunkSize)

// Chunk represents Bee chunk
type Chunk struct {
address swarm.Address
Expand All @@ -20,7 +18,7 @@ type Chunk struct {
// NewChunk returns new chunk
func NewChunk(data []byte) (Chunk, error) {
if len(data) > maxChunkSize {
return Chunk{}, errChunkSize
return Chunk{}, fmt.Errorf("create chunk: size too big (max %d)", maxChunkSize)
}

return Chunk{data: data}, nil
Expand All @@ -30,7 +28,7 @@ func NewChunk(data []byte) (Chunk, error) {
func NewRandomChunk(r *rand.Rand) (c Chunk, err error) {
c = Chunk{data: make([]byte, r.Intn(maxChunkSize))}
if _, err := r.Read(c.data); err != nil {
return Chunk{}, err
return Chunk{}, fmt.Errorf("create random chunk: %w", err)
}

return
Expand All @@ -57,7 +55,7 @@ func (c *Chunk) ClosestNode(nodes []swarm.Address) (closest swarm.Address, err e
for _, a := range nodes[1:] {
dcmp, err := swarm.DistanceCmp(c.Address().Bytes(), closest.Bytes(), a.Bytes())
if err != nil {
return swarm.Address{}, err
return swarm.Address{}, fmt.Errorf("find closest node: %w", err)
}
switch dcmp {
case 0:
Expand Down
20 changes: 10 additions & 10 deletions pkg/bee/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func NewCluster(o ClusterOptions) (c Cluster, err error) {
for i := 0; i < o.Size; i++ {
a, err := createURL(o.APIScheme, o.APIHostnamePattern, o.Namespace, o.APIDomain, i)
if err != nil {
return Cluster{}, err
return Cluster{}, fmt.Errorf("create cluster: %w", err)
}

d, err := createURL(o.DebugAPIScheme, o.DebugAPIHostnamePattern, o.Namespace, o.DebugAPIDomain, i)
if err != nil {
return Cluster{}, err
return Cluster{}, fmt.Errorf("create cluster: %w", err)
}

n := NewNode(NodeOptions{
Expand All @@ -66,9 +66,9 @@ func (c *Cluster) Addresses(ctx context.Context) (addrs []Addresses, err error)
return msgs[i].Index < msgs[j].Index
})

for _, m := range msgs {
for i, m := range msgs {
if m.Error != nil {
return nil, m.Error
return nil, fmt.Errorf("node %d: %w", i, m.Error)
}
addrs = append(addrs, m.Addresses)
}
Expand Down Expand Up @@ -121,9 +121,9 @@ func (c *Cluster) Overlays(ctx context.Context) (overlays []swarm.Address, err e
return msgs[i].Index < msgs[j].Index
})

for _, m := range msgs {
for i, m := range msgs {
if m.Error != nil {
return nil, m.Error
return nil, fmt.Errorf("node %d: %w", i, m.Error)
}
overlays = append(overlays, m.Address)
}
Expand Down Expand Up @@ -177,9 +177,9 @@ func (c *Cluster) Peers(ctx context.Context) (peers [][]swarm.Address, err error
return msgs[i].Index < msgs[j].Index
})

for _, m := range msgs {
for i, m := range msgs {
if m.Error != nil {
return nil, m.Error
return nil, fmt.Errorf("node %d: %w", i, m.Error)
}
peers = append(peers, m.Peers)
}
Expand Down Expand Up @@ -237,9 +237,9 @@ func (c *Cluster) Topologies(ctx context.Context) (topologies []Topology, err er
return msgs[i].Index < msgs[j].Index
})

for _, m := range msgs {
for i, m := range msgs {
if m.Error != nil {
return nil, m.Error
return nil, fmt.Errorf("node %d: %w", i, m.Error)
}
topologies = append(topologies, m.Topology)
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/bee/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -49,7 +50,7 @@ type Addresses struct {
func (n *Node) Addresses(ctx context.Context) (resp Addresses, err error) {
a, err := n.debug.Node.Addresses(ctx)
if err != nil {
return Addresses{}, err
return Addresses{}, fmt.Errorf("get addresses: %w", err)
}

return Addresses{
Expand All @@ -62,7 +63,7 @@ func (n *Node) Addresses(ctx context.Context) (resp Addresses, err error) {
func (n *Node) DownloadChunk(ctx context.Context, a swarm.Address) (data []byte, err error) {
r, err := n.api.Bzz.Download(ctx, a)
if err != nil {
return nil, err
return nil, fmt.Errorf("download chunk %s: %w", a, err)
}

return ioutil.ReadAll(r)
Expand All @@ -77,7 +78,7 @@ func (n *Node) HasChunk(ctx context.Context, c Chunk) (bool, error) {
func (n *Node) Overlay(ctx context.Context) (swarm.Address, error) {
a, err := n.debug.Node.Addresses(ctx)
if err != nil {
return swarm.Address{}, err
return swarm.Address{}, fmt.Errorf("get overlay: %w", err)
}

return a.Overlay, nil
Expand All @@ -87,7 +88,7 @@ func (n *Node) Overlay(ctx context.Context) (swarm.Address, error) {
func (n *Node) Peers(ctx context.Context) (peers []swarm.Address, err error) {
ps, err := n.debug.Node.Peers(ctx)
if err != nil {
return nil, err
return nil, fmt.Errorf("get peers: %w", err)
}

for _, p := range ps.Peers {
Expand All @@ -101,7 +102,7 @@ func (n *Node) Peers(ctx context.Context) (peers []swarm.Address, err error) {
func (n *Node) Ping(ctx context.Context, node swarm.Address) (rtt string, err error) {
r, err := n.api.PingPong.Ping(ctx, node)
if err != nil {
return "", err
return "", fmt.Errorf("ping node %s: %w", node, err)
}
return r.RTT, nil
}
Expand All @@ -128,7 +129,7 @@ type Bin struct {
func (n *Node) Topology(ctx context.Context) (topology Topology, err error) {
t, err := n.debug.Node.Topology(ctx)
if err != nil {
return Topology{}, err
return Topology{}, fmt.Errorf("get topology: %w", err)
}

topology = Topology{
Expand Down Expand Up @@ -158,7 +159,7 @@ func (n *Node) Topology(ctx context.Context) (topology Topology, err error) {
func (n *Node) Underlay(ctx context.Context) ([]string, error) {
a, err := n.debug.Node.Addresses(ctx)
if err != nil {
return nil, err
return nil, fmt.Errorf("get underlay: %w", err)
}

return a.Underlay, nil
Expand All @@ -168,7 +169,7 @@ func (n *Node) Underlay(ctx context.Context) ([]string, error) {
func (n *Node) UploadChunk(ctx context.Context, c *Chunk) (err error) {
r, err := n.api.Bzz.Upload(ctx, bytes.NewReader(c.Data()))
if err != nil {
return err
return fmt.Errorf("upload chunk: %w", err)
}

c.address = r.Hash
Expand Down
2 changes: 1 addition & 1 deletion pkg/check/fullconnectivity/fullconnectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Check(cluster bee.Cluster) (err error) {
for i, n := range cluster.Nodes {
peers, err := n.Peers(ctx)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

if len(peers) != expectedPeerCount {
Expand Down
4 changes: 2 additions & 2 deletions pkg/check/kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Check(cluster bee.Cluster) (err error) {
for i, n := range cluster.Nodes {
t, err := n.Topology(ctx)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

if t.Depth == 0 {
Expand All @@ -44,7 +44,7 @@ func Check(cluster bee.Cluster) (err error) {
for k, b := range t.Bins {
binDepth, err := strconv.Atoi(strings.Split(k, "_")[1])
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}
fmt.Printf("Bin %d. Population: %d. Connected: %d.\n", binDepth, b.Population, b.Connected)
if binDepth < t.Depth && b.Connected < 2 {
Expand Down
14 changes: 3 additions & 11 deletions pkg/check/peercount/peercount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package peercount

import (
"context"
"errors"
"fmt"

"github.com/ethersphere/beekeeper/pkg/bee"
)

var errPeerCount = errors.New("peer count")

// Check executes peer count check on cluster
func Check(cluster bee.Cluster) (err error) {
var expectedPeerCount = cluster.Size() - 1
Expand All @@ -18,20 +15,15 @@ func Check(cluster bee.Cluster) (err error) {
for i, n := range cluster.Nodes {
o, err := n.Overlay(ctx)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

peers, err := n.Peers(ctx)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

if len(peers) == expectedPeerCount {
fmt.Printf("Node %d. Passed. Peers %d/%d. Node: %s\n", i, len(peers), expectedPeerCount, o.String())
} else {
fmt.Printf("Node %d. Failed. Peers %d/%d. Node: %s\n", i, len(peers), expectedPeerCount, o.String())
return errPeerCount
}
fmt.Printf("Node %d. Peers %d/%d. Node: %s\n", i, len(peers), expectedPeerCount, o.String())
}

return
Expand Down
4 changes: 2 additions & 2 deletions pkg/check/pingpong/pingpong.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ func Check(cluster bee.Cluster) (err error) {
for i, n := range cluster.Nodes {
o, err := n.Overlay(ctx)
if err != nil {
return fmt.Errorf("error getting overlay for node %d: %w", i, err)
return fmt.Errorf("node %d: %w", i, err)
}

peers, err := n.Peers(ctx)
if err != nil {
return fmt.Errorf("error getting peers for node %d: %w", i, err)
return fmt.Errorf("node %d: %w", i, err)
}

for j, p := range peers {
Expand Down
8 changes: 4 additions & 4 deletions pkg/check/pushsync/pushsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ func Check(c bee.Cluster, o Options) (err error) {
for j := 0; j < o.ChunksPerNode; j++ {
chunk, err := bee.NewRandomChunk(rnd)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

if err := c.Nodes[i].UploadChunk(ctx, &chunk); err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

closest, err := chunk.ClosestNode(overlays)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}
index := findIndex(overlays, closest)

time.Sleep(1 * time.Second)
synced, err := c.Nodes[index].HasChunk(ctx, chunk)
if err != nil {
return err
return fmt.Errorf("node %d: %w", index, err)
}
if !synced {
fmt.Printf("Node %d. Chunk %d not found on the closest node. Node: %s Chunk: %s Closest: %s\n", i, j, overlays[i].String(), chunk.Address().String(), closest.String())
Expand Down
6 changes: 3 additions & 3 deletions pkg/check/retrieval/retrieval.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ func Check(c bee.Cluster, o Options) (err error) {
for j := 0; j < o.ChunksPerNode; j++ {
chunk, err := bee.NewRandomChunk(rnd)
if err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

if err := c.Nodes[i].UploadChunk(ctx, &chunk); err != nil {
return err
return fmt.Errorf("node %d: %w", i, err)
}

data, err := c.Nodes[c.Size()-1].DownloadChunk(ctx, chunk.Address())
if err != nil {
return err
return fmt.Errorf("node %d: %w", c.Size()-1, err)
}

if !bytes.Equal(chunk.Data(), data) {
Expand Down

0 comments on commit b539a3f

Please sign in to comment.