Exemplo n.º 1
0
// Test libstore returns nil when it cannot connect to the server
func testNonexistentServer() {
	if l, err := libstore.NewLibstore(fmt.Sprintf("localhost:%d", *portnum), fmt.Sprintf("localhost:%d", *portnum), libstore.NONE); l == nil || err != nil {
		fmt.Fprintln(output, "PASS")
		passCount++
	} else {
		fmt.Fprintln(output, "FAIL: libstore does not return nil when it cannot connect to nonexistent storage server")
		failCount++
	}
	cleanupLibstore(nil)
}
Exemplo n.º 2
0
func NewTribserver(master, myhostport string) *Tribserver {
	lsplog.SetVerbose(3)
	lsplog.Vlogf(1, "[Tribserver] new server, master:%s, local:%s\n", master, myhostport)

	srv := new(Tribserver)
	store, err := libstore.NewLibstore(master, myhostport, libstore.NONE)
	if lsplog.CheckReport(1, err) {
		return nil
	}

	atomic.StoreInt32(&srv.id, 0)
	srv.store = store
	srv.hostport = myhostport

	return srv
}
Exemplo n.º 3
0
// Initialize proxy and libstore
func initLibstore(storage string, server string, myhostport string, flags int) net.Listener {
	// Start proxy
	l, err := net.Listen("tcp", server)
	if err != nil {
		log.Fatal("listen error:", err)
	}
	pc = proxycounter.NewProxyCounter(storage, server)
	if pc == nil {
		fmt.Println("Could not start proxy")
		return nil
	}
	srpc := storagerpc.NewStorageRPC(pc)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	go http.Serve(l, nil)

	// Start libstore
	ls, err = libstore.NewLibstore(server, myhostport, flags)
	if ls == nil || err != nil {
		fmt.Println("Could not start libstore")
		return nil
	}
	return l
}
Exemplo n.º 4
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s  <command>:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "   commands:  p  key val   (put)\n")
		fmt.Fprintf(os.Stderr, "              g  key       (get)\n")
		fmt.Fprintf(os.Stderr, "              la key val   (list append)\n")
		fmt.Fprintf(os.Stderr, "              lr key val   (list remove)\n")
		fmt.Fprintf(os.Stderr, "              lg key       (list get)\n")
	}

	flag.Parse()
	if flag.NArg() < 2 {
		log.Fatal("Insufficient arguments to client")
	}

	cmd := flag.Arg(0)

	cmdlist := []cmd_info{
		{"p", 2},
		{"g", 1},
		{"la", 2},
		{"lr", 2},
		{"lg", 1},
	}

	cmdmap := make(map[string]cmd_info)
	for _, j := range cmdlist {
		cmdmap[j.cmdline] = j
	}

	ci, found := cmdmap[cmd]
	if !found {
		log.Fatal("Unknown command ", cmd)
	}
	if flag.NArg() < (ci.nargs + 1) {
		log.Fatal("Insufficient arguments for ", cmd)
	}

	leaseCBaddr := ""
	flags := 0
	if *handleLeases {
		l, e := net.Listen("tcp", ":0")
		if e != nil {
			log.Fatal("listen error:", e)
		}
		_, listenport, _ := net.SplitHostPort(l.Addr().String())
		leaseCBaddr = net.JoinHostPort("localhost", listenport)
		log.Println("Starting HTTP server on ", listenport)
		rpc.HandleHTTP()
		go http.Serve(l, nil)
	}

	if *handleLeases && *forceLease {
		flags |= libstore.ALWAYS_LEASE
	}
	ls, err := libstore.NewLibstore(net.JoinHostPort(*serverAddress, fmt.Sprintf("%d", *portnum)), leaseCBaddr, flags)
	if err != nil {
		log.Fatal("Could not create a libstore")
	}

	for i := 0; i < *numTimes; i++ {
		switch cmd {
		case "g":
			val, err := ls.Get(flag.Arg(1))
			if err != nil {
				fmt.Println("error: ", err)
			} else {
				fmt.Println("  ", val)
			}
		case "lg":
			val, err := ls.GetList(flag.Arg(1))
			if err != nil {
				fmt.Println("error: ", err)
			} else {
				for _, i := range val {
					fmt.Printf("%s  ", i)
				}
				fmt.Printf("\n")
			}
		case "p", "la", "lr":
			var err error
			switch cmd {
			case "p":
				err = ls.Put(flag.Arg(1), flag.Arg(2))
			case "la":
				err = ls.AppendToList(flag.Arg(1), flag.Arg(2))
			case "lr":
				err = ls.RemoveFromList(flag.Arg(1), flag.Arg(2))
			}
			switch err {
			case nil:
				fmt.Println("OK")
			default:
				fmt.Println("Error: ", err)
			}
		}
	}
	if *handleLeases {
		fmt.Println("waiting 20 seconds for lease callbacks")
		time.Sleep(20 * time.Second)
	}
}