Example #1
0
// Initialize proxy and tribserver
func initTribserver(storage string, server string, myhostport string) 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 tribserver
	ts = tribimpl.NewTribserver(server, myhostport)
	if ts == nil {
		fmt.Println("Could not start tribserver")
		return nil
	}
	rpc.Register(ts)
	return l
}
func main() {
	flag.Parse()
	if *storageMasterNodePort == "" {
		if *portnum == 0 {
			*portnum = 9009
		}
		// Single node execution
		*storageMasterNodePort = fmt.Sprintf("localhost:%d", *portnum)
		if *numNodes == 0 {
			*numNodes = 1
			log.Println("Self-mastering, setting nodes to 1")
		}
	}
	l, e := net.Listen("tcp", fmt.Sprintf(":%d", *portnum))
	if e != nil {
		log.Fatal("listen error:", e)
	}
	_, listenport, _ := net.SplitHostPort(l.Addr().String())
	log.Println("Server starting on ", listenport)
	*portnum, _ = strconv.Atoi(listenport)
	ss := storageimpl.NewStorageserver(*storageMasterNodePort, *numNodes, *portnum, uint32(*nodeID))

	srpc := storagerpc.NewStorageRPC(ss)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	http.Serve(l, nil)
}
Example #3
0
func main() {
	var masterPort string = "127.0.0.1:9009"
	var numNodes int = 2
	var portnum int = 9011
	var nodeID uint32 = 1

	//create slave node
	l, e := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", portnum))
	if e != nil {
		log.Fatal("listen error:", e)
	}

	ss := storageimpl.NewStorageserver(masterPort, numNodes, portnum, nodeID)
	srpc := storagerpc.NewStorageRPC(ss)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	http.Serve(l, nil)
}
Example #4
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
}
Example #5
0
func NewStorageserver(master string, numnodes int, portnum int, nodeid uint32) *Storageserver {

	//fmt.Println("called new storage server")

	ss := &Storageserver{}

	//if no nodeid is provided, choose one randomly
	if nodeid == 0 {
		reallySeedTheDamnRNG()
		ss.nodeid = rand.Uint32()
	} else {
		//otherwise just take the one they gave you
		ss.nodeid = nodeid
	}

	ss.portnum = portnum

	if numnodes != 0 {
		ss.isMaster = true
	} else {
		ss.isMaster = false
	}

	ss.nodeList = []storageproto.Node{}
	ss.nodeListM = make(chan int, 1)
	ss.nodeListM <- 1

	ss.nodeMap = make(map[storageproto.Node]int)
	ss.nodeMapM = make(chan int, 1)
	ss.nodeMapM <- 1

	ss.leaseMap = make(map[string]*LeaseInfo)
	ss.leaseMapM = make(chan int, 1)
	ss.leaseMapM <- 1

	ss.clientLeaseMap = make(map[string]int64)
	ss.clientLeaseMapM = make(chan int, 1)
	ss.clientLeaseMapM <- 1

	ss.stopRevoke = make(chan string, 1)

	ss.listMap = make(map[string][]string)
	ss.listMapM = make(chan int, 1)
	ss.listMapM <- 1

	ss.connMap = make(map[string]*rpc.Client)
	ss.connMapM = make(chan int, 1)
	ss.connMapM <- 1

	ss.valMap = make(map[string]string)
	ss.valMapM = make(chan int, 1)
	ss.valMapM <- 1

	if ss.isMaster == false {
		ss.numNodes = 0
		//connect to the master server
		masterClient, err := rpc.DialHTTP("tcp", master)
		for err != nil {
			//keep retrying until we can actually conenct
			//(Master may not have started yet)
			masterClient, err = rpc.DialHTTP("tcp", master)
			//fmt.Println("Trying to connect to master...")
			time.Sleep(time.Duration(3) * time.Second)
		}

		//set up args for registering ourselves
		info := storageproto.Node{HostPort: "localhost:" + strconv.Itoa(portnum), NodeID: ss.nodeid}
		args := storageproto.RegisterArgs{ServerInfo: info}
		reply := storageproto.RegisterReply{}

		for err != nil || reply.Ready != true {
			//call register on the master node with our info as the args. Kinda weird
			err = masterClient.Call("StorageRPC.Register", &args, &reply)
			//keep retrying until all things are registered
			//fmt.Println("Trying to register with master...")
			time.Sleep(time.Duration(3) * time.Second)
		}

		//gotta still set up some other shits
		//like get list of servers from reply maybe?
		//spec is pretty vague...
		<-ss.nodeListM
		//fmt.Println("Aquired nodeList lock NewStorageserver")
		ss.nodeList = reply.Servers
		log.Println("Successfully joined storage node cluster.")
		slist := ""
		for i := 0; i < len(ss.nodeList); i++ {
			res := fmt.Sprintf("{localhost:%v %v}", ss.portnum, ss.nodeid)
			slist += res
			if i < len(ss.nodeList)-1 {
				slist += " "
			}
		}
		log.Printf("Server List: [%s]", slist)
		ss.nodeListM <- 1
		//fmt.Println("released nodeList lock NewStorageserver")

		//non master doesn't keep a node map cause f**k you

	} else {
		//otherwise we are the master so we have to do some other shits
		//maybe these are the same shits as the other dudes got to do so maybe we don't gotta bother? WHAT WHO KNOWS

		//set up numNodes:
		if numnodes == 0 {
			numnodes = 1
		}
		ss.numNodes = numnodes
		<-ss.nodeListM
		//fmt.Println("aquired nodelist lock NewStorageserver")
		//append self to nodeList and put self in map
		me := storageproto.Node{HostPort: "localhost:" + strconv.Itoa(portnum), NodeID: ss.nodeid}
		ss.nodeList = append(ss.nodeList, me) //some shit here})
		ss.nodeListM <- 1
		//fmt.Println("released nodelist lock NewStorageserver")
		<-ss.nodeMapM
		//fmt.Println("aquired nodeMap lock NewStorageserver")
		ss.nodeMap[me] = 0
		ss.nodeMapM <- 1
		//fmt.Println("released nodeMap lock NewStorageserver")
	}

	ss.srpc = storagerpc.NewStorageRPC(ss)
	rpc.Register(ss.srpc)
	go ss.GarbageCollector()

	/*fmt.Println("started new server")
	fmt.Println(storageproto.Node{HostPort: "localhost:" + strconv.Itoa(portnum), NodeID: ss.nodeid})
	fmt.Printf("master? %v\n", ss.isMaster)
	fmt.Printf("numnodes? %v\n", ss.numNodes)*/

	return ss
}