func TestRewrite(t *testing.T) { servers, mds := ringN(t, 3) client := newServer(t, mds) err := distributor.OpenReplication(client) if err != nil { t.Fatal(err) } defer client.Close() size := BlockSize * 100 data := makeTestData(size) input := bytes.NewReader(data) f := createVol(t, client, "testvol", uint64(size)) for i := 0; i <= 100; i++ { input.Seek(int64(i%100), 0) f.Seek(int64(i%100), 0) _, err := io.Copy(f, input) if err != nil { t.Fatalf("couldn't copy: %v", err) } err = f.Sync() if err != nil { t.Fatalf("couldn't sync: %v", err) } } err = f.Close() if err != nil { t.Fatalf("couldn't close: %v", err) } compareBytes(t, mds, data, "testvol") closeAll(t, servers...) }
func TestLoadAndDump(t *testing.T) { servers, mds := ringN(t, 3) client := newServer(t, mds) err := distributor.OpenReplication(client) if err != nil { t.Fatal(err) } defer client.Close() size := BlockSize * 100 data := makeTestData(size) input := bytes.NewReader(data) f := createVol(t, client, "testvol", uint64(size)) copied, err := io.Copy(f, input) if err != nil { t.Fatalf("couldn't copy: %v", err) } err = f.Sync() if err != nil { t.Fatalf("couldn't sync: %v", err) } err = f.Close() if err != nil { t.Fatalf("couldn't close: %v", err) } fmt.Printf("copied %d bytes\n", copied) compareBytes(t, mds, data, "testvol") closeAll(t, servers...) }
func BenchmarkLoadThree(b *testing.B) { b.StopTimer() servers, mds := ringN(b, 3) client := newServer(b, mds) err := distributor.OpenReplication(client) if err != nil { b.Fatal(err) } defer client.Close() size := BlockSize * 100 data := makeTestData(size) b.StartTimer() for i := 0; i < b.N; i++ { input := bytes.NewReader(data) volname := fmt.Sprintf("testvol-%d", i) err = block.CreateBlockVolume(client.MDS, volname, uint64(size)) if err != nil { b.Fatalf("couldn't create block volume %s: %v", volname, err) } blockvol, err := block.OpenBlockVolume(client, volname) if err != nil { b.Fatalf("couldn't open block volume %s: %v", volname, err) } f, err := blockvol.OpenBlockFile() if err != nil { b.Fatalf("couldn't open blockfile %s: %v", volname, err) } copied, err := io.Copy(f, input) if err != nil { b.Fatalf("couldn't copy: %v", err) } err = f.Sync() if err != nil { b.Fatalf("couldn't sync: %v", err) } err = f.Close() if err != nil { b.Fatalf("couldn't close: %v", err) } b.Logf("copied %d bytes", copied) } b.SetBytes(int64(size)) b.StopTimer() closeAll(b, servers...) b.StartTimer() }
func createServer() *torus.Server { cfg := flagconfig.BuildConfigFromFlags() srv, err := torus.NewServer(cfg, "etcd", "temp") if err != nil { die("Couldn't start: %s\n", err) } err = distributor.OpenReplication(srv) if err != nil { die("Couldn't start: %s", err) } return srv }
func createServer() *torus.Server { srv, err := torus.NewServer(cfg, "etcd", "temp") if err != nil { 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) os.Exit(1) } if httpAddr != "" { go http.ServeHTTP(httpAddr, srv) } return srv }
func compareBytes(t *testing.T, mds *temp.Server, data []byte, volume string) { reader := newServer(t, mds) err := distributor.OpenReplication(reader) if err != nil { t.Fatal(err) } defer reader.Close() f := openVol(t, reader, "testvol") output := &bytes.Buffer{} _, err = io.Copy(output, f) if err != nil { t.Fatalf("couldn't copy: %v", err) } if !bytes.Equal(output.Bytes(), data) { t.Error("bytes not equal") } f.Close() }
func runServer(cmd *cobra.Command, args []string) { if completion { cmd.Root().GenBashCompletion(os.Stdout) os.Exit(0) } var ( srv *torus.Server err error ) switch { case cfg.MetadataAddress == "": srv, err = torus.NewServer(cfg, "temp", "mfile") case debugInit: err = torus.InitMDS("etcd", cfg, torus.GlobalMetadata{ BlockSize: 512 * 1024, DefaultBlockSpec: blockset.MustParseBlockLayerSpec("crc,base"), }, ring.Ketama) if err != nil { if err == torus.ErrExists { fmt.Println("debug-init: Already exists") } else { fmt.Printf("Couldn't debug-init: %s\n", err) os.Exit(1) } } fallthrough default: srv, err = torus.NewServer(cfg, "etcd", "mfile") } if err != nil { fmt.Printf("Couldn't start: %s\n", err) os.Exit(1) } if autojoin { err = doAutojoin(srv) if err != nil { fmt.Printf("Couldn't auto-join: %s\n", err) os.Exit(1) } } mainClose := make(chan bool) signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, os.Interrupt) if peerAddress != "" { var u *url.URL u, err = url.Parse(peerAddress) if err != nil { fmt.Printf("Couldn't parse peer address %s: %s\n", peerAddress, err) os.Exit(1) } if u.Scheme == "" { fmt.Printf("Peer address %s does not have URL scheme (http:// or tdp://)\n", peerAddress) os.Exit(1) } err = distributor.ListenReplication(srv, u) } else { err = distributor.OpenReplication(srv) } defer srv.Close() go func() { for _ = range signalChan { fmt.Println("\nReceived an interrupt, stopping services...") close(mainClose) os.Exit(0) } }() if err != nil { fmt.Println("couldn't use server:", err) os.Exit(1) } if httpAddress != "" { http.ServeHTTP(httpAddress, srv) } // Wait <-mainClose }