Esempio n. 1
0
func NewStorageserver(master string, numnodes int, port int, nodeid uint32) *StorageServer {
	lsplog.SetVerbose(3)

	if nodeid == 0 {
		rand.Seed(time.Now().UnixNano())
		nodeid = rand.Uint32()
	}

	ss := new(StorageServer)
	ss.nodeid = nodeid
	ss.port = port
	ss.hashmap = make(map[string][]byte)
	ss.leasePool = make(map[string]*leaseEntry)

	addr := fmt.Sprintf("localhost:%d", port)
	if master == addr {
		lsplog.Vlogf(1, "[StorageServer] create master node: %v\n", master)

		ss.ismaster = true
		ss.numnodes = numnodes
		ss.nodes = make(map[sp.Node]bool)

		node := sp.Node{master, nodeid}
		ss.nodes[node] = true
	} else {
		lsplog.Vlogf(1, "[StorageServer] create non-master node: %v\n", addr)

		ss.ismaster = false

		cli, err := rpc.DialHTTP("tcp", master)
		if err != nil {
			lsplog.Vlogf(1, "[StorageServer] create node failed: %v\n", err)
			return nil
		}

		var args sp.RegisterArgs
		var reply sp.RegisterReply

		args.ServerInfo.HostPort = fmt.Sprintf("localhost:%d", port)
		args.ServerInfo.NodeID = nodeid
		for i := 0; i < 10; i++ {
			cli.Call("StorageRPC.Register", &args, &reply)
			if reply.Ready == true {
				break
			}
			time.Sleep(1000 * time.Millisecond)
		}
	}
	return ss
}
Esempio 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
}
Esempio n. 3
0
func iNewLibstore(master, myhostport string, flags int) (*Libstore, error) {
	lsplog.SetVerbose(3)

	if myhostport == "" {
		panic("[Libstore] hostport shouldn't be null")
	}

	cli, err := rpc.DialHTTP("tcp", master)
	if err != nil {
		lsplog.Vlogf(1, "[Libstore] DialHTTP error: %v\n", err)
		return nil, err
	}
	defer cli.Close()

	var args sp.GetServersArgs
	var reply sp.RegisterReply

	for i := 0; i < 5; i++ {
		cli.Call("StorageRPC.GetServers", &args, &reply)
		if reply.Ready {
			break
		}
	}
	if !reply.Ready {
		return nil, lsplog.MakeErr("StorageServer not ready")
	}

	ls := new(Libstore)

	ls.hostport = myhostport
	ls.flags = flags
	ls.nodes = reply.Servers
	ls.leases = cache.NewCache()
	ls.rpcConn = make([]*rpc.Client, len(ls.nodes))

	sort.Sort(ls.nodes)

	return ls, nil
}