Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

clean up errors in torusd #329

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/torusblk/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func attachAction(cmd *cobra.Command, args []string) {
<-ch
status := sysd.wait(svc)
if status == "failed" {
onErr(errors.New("Couldn't attach"))
onErr(errors.New("couldn't attach"))
} else if status == "active" {
writeResponse(Response{
Status: "Success",
Expand Down Expand Up @@ -278,7 +278,7 @@ func mountAction(cmd *cobra.Command, args []string) {
// }
// status := sysd.wait(mountsvc)
// if status == "failed" {
// onErr(errors.New("Couldn't attach"))
// onErr(errors.New("couldn't attach"))
// } else if status == "active" {
// writeResponse(Response{
// Status: "Success",
Expand Down
4 changes: 2 additions & 2 deletions cmd/torusblk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func configureServer(cmd *cobra.Command, args []string) {
func createServer() *torus.Server {
srv, err := torus.NewServer(cfg, "etcd", "temp")
if err != nil {
fmt.Printf("Couldn't start: %s\n", err)
fmt.Printf("couldn't start: %s\n", err)
os.Exit(1)
}
err = distributor.OpenReplication(srv)
if err != nil {
fmt.Printf("Couldn't start: %s", err)
fmt.Printf("couldn't start: %s", err)
os.Exit(1)
}
if httpAddr != "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/torusctl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func createServer() *torus.Server {
cfg := flagconfig.BuildConfigFromFlags()
srv, err := torus.NewServer(cfg, "etcd", "temp")
if err != nil {
die("Couldn't start: %s\n", err)
die("couldn't start: %s", err)
}
err = distributor.OpenReplication(srv)
if err != nil {
die("Couldn't start: %s", err)
die("couldn't start: %s", err)
}
return srv
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/torusctl/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func volumeListAction(cmd *cobra.Command, args []string) {
mds := mustConnectToMDS()
vols, _, err := mds.GetVolumes()
if err != nil {
die("error listing volumes: %v\n", err)
die("error listing volumes: %v", err)
}
table := NewTableWriter(os.Stdout)
table.SetHeader([]string{"Volume Name", "Size", "Type", "Status"})
Expand Down
54 changes: 26 additions & 28 deletions cmd/torusd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -52,7 +51,12 @@ var rootCommand = &cobra.Command{
Short: "Torus distributed storage",
Long: `The torus distributed storage server.`,
PreRun: configureServer,
Run: runServer,
Run: func(cmd *cobra.Command, args []string) {
err := runServer(cmd, args)
if err != nil {
die("%v", err)
}
},
}

func init() {
Expand Down Expand Up @@ -93,8 +97,7 @@ func configureServer(cmd *cobra.Command, args []string) {
rl := capnslog.MustRepoLogger("github.com/coreos/torus")
llc, err := rl.ParseLogLevelConfig(logpkg)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing logpkg: %s\n", err)
os.Exit(1)
die("error parsing logpkg: %s", err)
}
rl.SetLogLevel(llc)
}
Expand All @@ -110,16 +113,14 @@ func configureServer(cmd *cobra.Command, args []string) {
if strings.Contains(sizeStr, "%") {
percent, err := parsePercentage(sizeStr)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing size %s: %s\n", sizeStr, err)
os.Exit(1)
die("error parsing size %s: %s", sizeStr, err)
}
directory, _ := filepath.Abs(dataDir)
size = du.NewDiskUsage(directory).Size() * percent / 100
} else {
size, err = humanize.ParseBytes(sizeStr)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing size %s: %s\n", sizeStr, err)
os.Exit(1)
die("error parsing size %s: %s", sizeStr, err)
}
}

Expand All @@ -135,12 +136,12 @@ func parsePercentage(percentString string) (uint64, error) {
return 0, err
}
if sizeNumber < 1 || sizeNumber > 100 {
return 0, errors.New(fmt.Sprintf("invalid size %d; must be between 1%% and 100%%", sizeNumber))
return 0, fmt.Errorf("invalid size %d; must be between 1%% and 100%%", sizeNumber)
}
return uint64(sizeNumber), nil
}

func runServer(cmd *cobra.Command, args []string) {
func runServer(cmd *cobra.Command, args []string) error {
if completion {
cmd.Root().GenBashCompletion(os.Stdout)
os.Exit(0)
Expand All @@ -162,24 +163,21 @@ func runServer(cmd *cobra.Command, args []string) {
if err == torus.ErrExists {
fmt.Println("debug-init: Already exists")
} else {
fmt.Printf("Couldn't debug-init: %s\n", err)
os.Exit(1)
return fmt.Errorf("couldn't debug-init: %s", err)
}
}
fallthrough
default:
srv, err = torus.NewServer(cfg, "etcd", "mfile")
}
if err != nil {
fmt.Printf("Couldn't start: %s\n", err)
os.Exit(1)
return fmt.Errorf("couldn't start: %s", err)
}

if autojoin {
err = doAutojoin(srv)
if err != nil {
fmt.Printf("Couldn't auto-join: %s\n", err)
os.Exit(1)
return fmt.Errorf("couldn't auto-join: %s", err)
}
}

Expand All @@ -192,13 +190,11 @@ func runServer(cmd *cobra.Command, args []string) {

u, err = url.Parse(peerAddress)
if err != nil {
fmt.Printf("Couldn't parse peer address %s: %s\n", peerAddress, err)
os.Exit(1)
return fmt.Errorf("couldn't parse peer address %s: %s", peerAddress, err)
}

if u.Scheme == "" {
fmt.Printf("Peer address %s does not have URL scheme (http:// or tdp://)\n", peerAddress)
os.Exit(1)
return fmt.Errorf("Peer address %s does not have URL scheme (http:// or tdp://)", peerAddress)
}

err = distributor.ListenReplication(srv, u)
Expand All @@ -216,22 +212,21 @@ func runServer(cmd *cobra.Command, args []string) {
}()

if err != nil {
fmt.Println("couldn't use server:", err)
os.Exit(1)
return fmt.Errorf("couldn't use server: %s", err)
}
if httpAddress != "" {
http.ServeHTTP(httpAddress, srv)
}
// Wait
<-mainClose
return nil
}

func doAutojoin(s *torus.Server) error {
for {
ring, err := s.MDS.GetRing()
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't get ring: %v\n", err)
return err
return fmt.Errorf("couldn't get ring: %v", err)
}
var newRing torus.Ring
if r, ok := ring.(torus.RingAdder); ok {
Expand All @@ -242,16 +237,14 @@ func doAutojoin(s *torus.Server) error {
},
})
} else {
fmt.Fprintf(os.Stderr, "current ring type cannot support auto-adding\n")
return err
return fmt.Errorf("current ring type cannot support auto-adding")
}
if err == torus.ErrExists {
// We're already a member; we're coming back up.
return nil
}
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't add peer to ring: %v", err)
return err
return fmt.Errorf("couldn't add peer to ring: %v", err)
}
err = s.MDS.SetRing(newRing)
if err == torus.ErrNonSequentialRing || err == torus.ErrAgain {
Expand All @@ -261,3 +254,8 @@ func doAutojoin(s *torus.Server) error {
return err
}
}

func die(why string, args ...interface{}) {
fmt.Fprintf(os.Stderr, why+"\n", args...)
os.Exit(1)
}
2 changes: 1 addition & 1 deletion distributor/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (d *Distributor) WriteBlock(ctx context.Context, i torus.BlockRef, data []b
if err == nil {
return nil
}
clog.Debugf("Couldn't write locally; writing to cluster: %s", err)
clog.Debugf("couldn't write locally; writing to cluster: %s", err)
fallthrough
case torus.WriteOne:
for _, p := range peers.Peers[:peers.Replication] {
Expand Down
4 changes: 2 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
return n, err
} else if wrote != frontlen {
promFileWrittenBytes.WithLabelValues(f.volume.Name).Add(float64(n))
return n, errors.New("Couldn't write all of the first block at the offset")
return n, errors.New("couldn't write all of the first block at the offset")
}
b = b[frontlen:]
n += wrote
Expand Down Expand Up @@ -241,7 +241,7 @@ func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
return n, err
} else if wrote != toWrite {
promFileWrittenBytes.WithLabelValues(f.volume.Name).Add(float64(n))
return n, errors.New("Couldn't write all of the last block")
return n, errors.New("couldn't write all of the last block")
}
n += wrote
off += int64(wrote)
Expand Down
2 changes: 1 addition & 1 deletion internal/nbd/nbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (nbd *NBD) Serve() error {
time.Sleep(time.Microsecond * 500)
err := nbd.SetBlockSize(nbd.blocksize)
if err != nil {
clog.Printf("Couldn't upgrade blocksize: %s", err)
clog.Printf("couldn't upgrade blocksize: %s", err)
}
}(nbd)
}
Expand Down