Commit e0b19d5d authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent c91c63be
...@@ -24,12 +24,12 @@ import ( ...@@ -24,12 +24,12 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"log"
"io" "io"
"os" "os"
"lab.nexedi.com/kirr/neo/go/neo/server" "lab.nexedi.com/kirr/neo/go/neo/server"
"lab.nexedi.com/kirr/neo/go/xcommon/xnet" "lab.nexedi.com/kirr/neo/go/xcommon/xnet"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
) )
const masterSummary = "run master node" const masterSummary = "run master node"
...@@ -56,14 +56,13 @@ func masterMain(argv []string) { ...@@ -56,14 +56,13 @@ func masterMain(argv []string) {
if *cluster == "" { if *cluster == "" {
// XXX vvv -> die or log.Fatalf ? // XXX vvv -> die or log.Fatalf ?
log.Fatal(os.Stderr, "cluster name must be provided") zt.Fatal("cluster name must be provided")
os.Exit(2)
} }
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) zt.Exit(2)
} }
net := xnet.NetPlain("tcp") // TODO + TLS; not only "tcp" ? net := xnet.NetPlain("tcp") // TODO + TLS; not only "tcp" ?
...@@ -81,6 +80,6 @@ func masterMain(argv []string) { ...@@ -81,6 +80,6 @@ func masterMain(argv []string) {
err := masterSrv.Run(ctx) err := masterSrv.Run(ctx)
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
} }
...@@ -24,14 +24,14 @@ import ( ...@@ -24,14 +24,14 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"log"
"io" "io"
"os" "os"
"strings" "strings"
"lab.nexedi.com/kirr/neo/go/neo/server" "lab.nexedi.com/kirr/neo/go/neo/server"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
"lab.nexedi.com/kirr/neo/go/xcommon/xnet" "lab.nexedi.com/kirr/neo/go/xcommon/xnet"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
) )
const storageSummary = "run storage node" const storageSummary = "run storage node"
...@@ -54,19 +54,18 @@ func storageMain(argv []string) { ...@@ -54,19 +54,18 @@ func storageMain(argv []string) {
flags.Parse(argv[1:]) flags.Parse(argv[1:])
if *cluster == "" { if *cluster == "" {
// XXX vvv -> die or log.Fatalf ? // XXX vvv -> die or Fatalf ?
log.Fatal(os.Stderr, "cluster name must be provided") zt.Fatal(os.Stderr, "cluster name must be provided")
os.Exit(2)
} }
masterv := strings.Split(*masters, ",") masterv := strings.Split(*masters, ",")
if len(masterv) == 0 { if len(masterv) == 0 {
fmt.Fprintf(os.Stderr, "master list must be provided") fmt.Fprintf(os.Stderr, "master list must be provided")
os.Exit(2) zt.Exit(2)
} }
if len(masterv) > 1 { if len(masterv) > 1 {
fmt.Fprintf(os.Stderr, "BUG neo/go POC currently supports only 1 master") fmt.Fprintf(os.Stderr, "BUG neo/go POC currently supports only 1 master")
os.Exit(2) zt.Exit(2)
} }
master := masterv[0] master := masterv[0]
...@@ -74,13 +73,13 @@ func storageMain(argv []string) { ...@@ -74,13 +73,13 @@ func storageMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) zt.Exit(2)
} }
// XXX hack to use existing zodb storage for data // XXX hack to use existing zodb storage for data
zstor, err := fs1.Open(context.Background(), argv[0]) // XXX context.Background -> ? zstor, err := fs1.Open(context.Background(), argv[0]) // XXX context.Background -> ?
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
net := xnet.NetPlain("tcp") // TODO + TLS; not only "tcp" ? net := xnet.NetPlain("tcp") // TODO + TLS; not only "tcp" ?
...@@ -98,6 +97,6 @@ func storageMain(argv []string) { ...@@ -98,6 +97,6 @@ func storageMain(argv []string) {
err = storSrv.Run(ctx) err = storSrv.Run(ctx)
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
} }
...@@ -25,11 +25,11 @@ import ( ...@@ -25,11 +25,11 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1" "lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
"lab.nexedi.com/kirr/go123/xbytes" "lab.nexedi.com/kirr/go123/xbytes"
"lab.nexedi.com/kirr/go123/xerr" "lab.nexedi.com/kirr/go123/xerr"
...@@ -313,7 +313,7 @@ func dumpMain(argv []string) { ...@@ -313,7 +313,7 @@ func dumpMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) zt.Exit(2)
} }
storPath := argv[0] storPath := argv[0]
...@@ -326,7 +326,7 @@ func dumpMain(argv []string) { ...@@ -326,7 +326,7 @@ func dumpMain(argv []string) {
err := Dump(os.Stdout, storPath, fs1.IterForward, d) err := Dump(os.Stdout, storPath, fs1.IterForward, d)
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
} }
...@@ -411,12 +411,12 @@ func tailMain(argv []string) { ...@@ -411,12 +411,12 @@ func tailMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) zt.Exit(2)
} }
storPath := argv[0] storPath := argv[0]
err := Dump(os.Stdout, storPath, fs1.IterBackward, &DumperFsTail{Ntxn: ntxn}) err := Dump(os.Stdout, storPath, fs1.IterBackward, &DumperFsTail{Ntxn: ntxn})
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
} }
...@@ -25,9 +25,9 @@ import ( ...@@ -25,9 +25,9 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"log"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1" "lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
) )
// Reindex rebuilds index for FileStorage file @ path // Reindex rebuilds index for FileStorage file @ path
...@@ -69,13 +69,13 @@ func reindexMain(argv []string) { ...@@ -69,13 +69,13 @@ func reindexMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) zt.Exit(2)
} }
storPath := argv[0] storPath := argv[0]
err := Reindex(context.Background(), storPath) err := Reindex(context.Background(), storPath)
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
} }
...@@ -122,12 +122,12 @@ func verifyIdxMain(argv []string) { ...@@ -122,12 +122,12 @@ func verifyIdxMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) zt.Exit(2)
} }
storPath := argv[0] storPath := argv[0]
err := VerifyIndexFor(context.Background(), storPath) err := VerifyIndexFor(context.Background(), storPath)
if err != nil { if err != nil {
log.Fatal(err) zt.Fatal(err)
} }
} }
...@@ -25,7 +25,6 @@ import ( ...@@ -25,7 +25,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
...@@ -97,31 +96,31 @@ func catobjMain(argv []string) { ...@@ -97,31 +96,31 @@ func catobjMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 2 { if len(argv) < 2 {
flags.Usage() flags.Usage()
os.Exit(2) Exit(2)
} }
storUrl := argv[0] storUrl := argv[0]
if hashOnly && raw { if hashOnly && raw {
log.Fatal("-hashonly & -raw are incompatible") Fatal("-hashonly & -raw are incompatible")
} }
xidv := []zodb.Xid{} xidv := []zodb.Xid{}
for _, arg := range argv[1:] { for _, arg := range argv[1:] {
xid, err := zodb.ParseXid(arg) xid, err := zodb.ParseXid(arg)
if err != nil { if err != nil {
log.Fatal(err) // XXX recheck Fatal(err) // XXX recheck
} }
xidv = append(xidv, xid) xidv = append(xidv, xid)
} }
if raw && len(xidv) > 1 { if raw && len(xidv) > 1 {
log.Fatal("only 1 object allowed with -raw") Fatal("only 1 object allowed with -raw")
} }
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) Fatal(err)
} }
// TODO defer stor.Close() // TODO defer stor.Close()
...@@ -136,7 +135,7 @@ func catobjMain(argv []string) { ...@@ -136,7 +135,7 @@ func catobjMain(argv []string) {
for _, xid := range xidv { for _, xid := range xidv {
err = catobj(xid) err = catobj(xid)
if err != nil { if err != nil {
log.Fatal(err) Fatal(err)
} }
} }
} }
...@@ -81,8 +81,41 @@ type MainProg struct { ...@@ -81,8 +81,41 @@ type MainProg struct {
HelpTopics HelpRegistry // provided help topics HelpTopics HelpRegistry // provided help topics
} }
// Exit is like os.Exit but makes sure deferred functions are run.
// Exit should be called from main goroutine.
func Exit(code int) {
panic(&programExit{code})
}
// Fatal is like log.Fatal but makes sure deferred functions are run.
// Fatal should be called from main goroutine.
func Fatal(v ...interface{}) {
log.Print(v...)
Exit(1)
}
// programExit is thrown when Exit or Fatal are called
type programExit struct {
code int
}
// Main is the main entry point for the program. Call it from main. // Main is the main entry point for the program. Call it from main.
func (prog *MainProg) Main() { func (prog *MainProg) Main() {
// handle exit throw-requests
defer func() {
r := recover()
if e, _ := r.(*programExit); e != nil {
os.Exit(e.code)
}
if r != nil {
panic(r)
}
}()
prog.main()
}
func (prog *MainProg) main() {
flag.Usage = prog.usage flag.Usage = prog.usage
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`") cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`") memprofile := flag.String("memprofile", "", "write memory profile to `file`")
...@@ -91,7 +124,7 @@ func (prog *MainProg) Main() { ...@@ -91,7 +124,7 @@ func (prog *MainProg) Main() {
if len(argv) == 0 { if len(argv) == 0 {
prog.usage() prog.usage()
os.Exit(2) Exit(2)
} }
command := argv[0] command := argv[0]
...@@ -100,10 +133,10 @@ func (prog *MainProg) Main() { ...@@ -100,10 +133,10 @@ func (prog *MainProg) Main() {
if *cpuprofile != "" { if *cpuprofile != "" {
f, err := os.Create(*cpuprofile) f, err := os.Create(*cpuprofile)
if err != nil { if err != nil {
log.Fatal("could not create CPU profile: ", err) Fatal("could not create CPU profile: ", err)
} }
if err := pprof.StartCPUProfile(f); err != nil { if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err) Fatal("could not start CPU profile: ", err)
} }
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
...@@ -112,11 +145,11 @@ func (prog *MainProg) Main() { ...@@ -112,11 +145,11 @@ func (prog *MainProg) Main() {
if *memprofile != "" { if *memprofile != "" {
f, err := os.Create(*memprofile) f, err := os.Create(*memprofile)
if err != nil { if err != nil {
log.Fatal("could not create memory profile: ", err) Fatal("could not create memory profile: ", err)
} }
runtime.GC() // get up-to-date statistics runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil { if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err) Fatal("could not write memory profile: ", err)
} }
f.Close() f.Close()
} }
...@@ -134,7 +167,7 @@ func (prog *MainProg) Main() { ...@@ -134,7 +167,7 @@ func (prog *MainProg) Main() {
if cmd == nil { if cmd == nil {
fmt.Fprintf(os.Stderr, "%s: unknown subcommand \"%s\"\n", prog.Name, command) fmt.Fprintf(os.Stderr, "%s: unknown subcommand \"%s\"\n", prog.Name, command)
fmt.Fprintf(os.Stderr, "Run '%s help' for usage.\n", prog.Name) fmt.Fprintf(os.Stderr, "Run '%s help' for usage.\n", prog.Name)
os.Exit(2) Exit(2)
} }
cmd.Main(argv) cmd.Main(argv)
...@@ -201,7 +234,7 @@ Use "%s help [topic]" for more information about that topic. ...@@ -201,7 +234,7 @@ Use "%s help [topic]" for more information about that topic.
func (prog *MainProg) help(argv []string) { func (prog *MainProg) help(argv []string) {
if len(argv) < 2 { // help topic ... if len(argv) < 2 { // help topic ...
prog.usage() prog.usage()
os.Exit(2) Exit(2)
} }
topic := argv[1] topic := argv[1]
...@@ -210,17 +243,17 @@ func (prog *MainProg) help(argv []string) { ...@@ -210,17 +243,17 @@ func (prog *MainProg) help(argv []string) {
command := prog.Commands.Lookup(topic) command := prog.Commands.Lookup(topic)
if command != nil { if command != nil {
command.Usage(os.Stdout) command.Usage(os.Stdout)
os.Exit(0) Exit(0)
} }
helpTopic := prog.helpTopics().Lookup(topic) helpTopic := prog.helpTopics().Lookup(topic)
if helpTopic != nil { if helpTopic != nil {
fmt.Println(helpTopic.Text) fmt.Println(helpTopic.Text)
os.Exit(0) Exit(0)
} }
fmt.Fprintf(os.Stderr, "Unknown help topic `%s`. Run '%s help'.\n", topic, prog.Name) fmt.Fprintf(os.Stderr, "Unknown help topic `%s`. Run '%s help'.\n", topic, prog.Name)
os.Exit(2) Exit(2)
} }
// helpTopics returns provided help topics augmented with help on common topics // helpTopics returns provided help topics augmented with help on common topics
......
...@@ -44,7 +44,6 @@ import ( ...@@ -44,7 +44,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"lab.nexedi.com/kirr/go123/xfmt" "lab.nexedi.com/kirr/go123/xfmt"
...@@ -235,7 +234,7 @@ func dumpMain(argv []string) { ...@@ -235,7 +234,7 @@ func dumpMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) Exit(2)
} }
storUrl := argv[0] storUrl := argv[0]
...@@ -245,17 +244,17 @@ func dumpMain(argv []string) { ...@@ -245,17 +244,17 @@ func dumpMain(argv []string) {
tidMin, tidMax, err := zodb.ParseTidRange(tidRange) tidMin, tidMax, err := zodb.ParseTidRange(tidRange)
if err != nil { if err != nil {
log.Fatal(err) // XXX recheck Fatal(err) // XXX recheck
} }
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) Fatal(err)
} }
// TODO defer stor.Close() // TODO defer stor.Close()
err = Dump(os.Stdout, stor, tidMin, tidMax, hashOnly) err = Dump(os.Stdout, stor, tidMin, tidMax, hashOnly)
if err != nil { if err != nil {
log.Fatal(err) Fatal(err)
} }
} }
...@@ -26,7 +26,6 @@ import ( ...@@ -26,7 +26,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
...@@ -112,17 +111,17 @@ func infoMain(argv []string) { ...@@ -112,17 +111,17 @@ func infoMain(argv []string) {
argv = flags.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
flags.Usage() flags.Usage()
os.Exit(2) Exit(2)
} }
storUrl := argv[0] storUrl := argv[0]
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) Fatal(err)
} }
err = Info(os.Stdout, stor, argv[1:]) err = Info(os.Stdout, stor, argv[1:])
if err != nil { if err != nil {
log.Fatal(err) Fatal(err)
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment