func NewTribserver(agencyid, master, ownstorage, myhostport string, prefer int) *Tribserver {
	lsplog.SetVerbose(3)

	//Connect to master
	lc, err := libconn.NewLibconn(agencyid, master, myhostport, prefer)
	if lsplog.CheckReport(1, err) {
		return nil
	}

	//Init libstore : Agency storage server
	ls, err := libstore.NewLibstore(agencyid, ownstorage, myhostport, libstore.NONE)
	if lsplog.CheckReport(1, err) {
		log.Printf("[%s:%s] Fail to start", agencyid, myhostport)
		return nil
	}

	//Init libairline : Airline storage server
	la, err := libairline.NewLibairline(lc, agencyid, myhostport)
	if lsplog.CheckReport(1, err) {
		log.Printf("[%s:%s] Fail to start", agencyid, myhostport)
		return nil
	}

	//lsplog.Vlogf(5, "Server active... %s", myhostport);
	log.Printf("[%s:%s] Start", agencyid, myhostport)
	return &Tribserver{ls, la, agencyid, myhostport}
}
func main() {
	lsplog.SetVerbose(3)
	lsplog.Vlogf(3, "[Request] Args: %s", os.Args)
	var e error
	ww := new(Worker)
	ww.load = 0
	ww.endFailureRecovery = make(chan bool, 1)
	ww.startFailureRecovery = make(chan bool, 1)
	ww.reqLock = new(sync.Mutex)
	monitor := os.Args[1]
	port, _ := strconv.Atoi(os.Args[2])
	l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatal("listen error:", e)
	}
	_, _, _ = net.SplitHostPort(l.Addr().String())
	//port, _ = strconv.Atoi(listenport)
	rpc.Register(ww)
	rpc.HandleHTTP()
	go http.Serve(l, nil)
	// Register to monitor and get load balancer hostport and its buddy
	ww.cliM, _ = rpc.DialHTTP("tcp", monitor)
	name, _ := os.Hostname()
	addrs, _ := net.LookupHost(name)
	args := &commproto.RegisterWorkerArgs{fmt.Sprintf("%s:%s", addrs[0], port)}
	var reply commproto.RegisterWorkerReply
	ww.cliM.Call("MonitorRPC.RegisterWorker", args, &reply)
	lsplog.Vlogf(3, "[Worker] Received LoadBalancerHostPort: %s", reply.LoadBalancerHostPort)
	// connect to main LB
	parts := strings.Split(reply.LoadBalancerHostPort, ":")
	fakePort, _ := strconv.Atoi(parts[1])
	fakeHostPort := fmt.Sprintf("%s:%d", parts[0], fakePort+10)
	ww.cli, e = lsp12.NewLspClient(fakeHostPort, &lsp12.LspParams{5, 2000})
	for e != nil || ww.cli == nil {
		ww.cli, e = lsp12.NewLspClient(reply.LoadBalancerHostPort, &lsp12.LspParams{5, 2000})
		lsplog.Vlogf(3, "[Worker] Connection to load balancer failed. \n")
	}
	// queue of requests
	ww.reqList = list.New()

	// lauch the request handler
	go requestHandler(ww)

	// listen to requests
	for {
		req, err := ww.cli.Read()
		if err != nil {
			ww.startFailureRecovery <- true
			<-ww.endFailureRecovery
			continue
		}
		lsplog.Vlogf(3, "[Worker] Recieved: %s", string(req))
		responseParts := strings.Split(string(req), " ")
		i, _ := strconv.Atoi(responseParts[1])
		ww.reqLock.Lock()
		ww.load += i
		ww.reqList.PushBack(i)
		ww.reqLock.Unlock()
	}
}
Exemple #3
0
func NewLibpaxos(self string, replicas []string, q int, nodepos int, pc PaxosClient) *Libpaxos {
	lsplog.SetVerbose(4)

	lp := &Libpaxos{}
	lp.nodepos = nodepos

	numnodes := len(replicas)
	lp.self = self
	lp.replicas = replicas

	lp.highestLearnt = 0

	//Initialize all 'roles'
	lp.c = &Coordinator{nodepos: nodepos, nodes: numnodes, quorum: q}
	lp.c.init()

	lp.a = &Acceptor{}
	lp.a.init()

	lp.l = &Learner{quorum: q}
	lp.l.init()

	lp.pc = pc
	lp.lastCommandExecuted = 0
	//rpc.Register(lp)

	lp.proposeCounter = 0
	lp.unsentValues = make(map[string]*Packet)
	lp.executeHistory = make(map[string]bool)

	lp.operateReqC = make(chan *Packet)
	go lp.operateHandler()

	//Epoch
	go func() {
		for {
			d := time.Duration(1000) * time.Millisecond
			if d.Nanoseconds() > 0 {
				time.Sleep(d)
			}

			p := &Packet{}
			p.PacketFrom = lp.self
			p.PacketMsg = Msg{}
			p.PacketMsg.MsgType = EPOCH
			p.PacketMsg.Value = ""

			lp.operateReqC <- p
		}
	}()

	return lp
}
func main() {
	lsplog.SetVerbose(3)
	lsplog.Vlogf(3, "[Request] Args: %s", os.Args)
	hostport := os.Args[1]
	rep, _ := strconv.Atoi(os.Args[2])
	var e error
	lsplog.Vlogf(3, "[Client] Trying to connect to switch.")
	client, e = lsp12.NewLspClient(hostport, &lsp12.LspParams{5, 2000})
	if e != nil {
		lsplog.Vlogf(3, "[Client] Connection to switch failed.")
	}
	for i := 0; i < rep; i++ {
		r := random(1, 100)
		request := fmt.Sprintf("Request %f", r)
		client.Write([]byte(request))
		fmt.Printf("Sending request number %d: time to sleep %d \n", i, r)
		time.Sleep(time.Duration(100) * time.Millisecond)
	}
}
func main() {
	lsplog.SetVerbose(1)
	flag.Parse()
	if *portnum == 0 {
		*portnum = 9009
	}
	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 := switchimpl.NewSwitch(*portnum)

	srpc := switchrpc.NewSwitchRPC(ss)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	http.Serve(l, nil)
}
func NewAirlineServer(airlineID, myhostport, masterStorage, ownStorage string, replicas []string, pos int) (*AirlineServer, *libpaxos.Libpaxos, error) {
	lsplog.SetVerbose(4)
	as := &AirlineServer{}
	as.cmdNO = 0
	as.reqChan = make(chan *Request)
	as.airlineID = airlineID
	as.myhostport = myhostport
	as.logBuf = make(map[string](*bufi.Buf))
	as.flightBuf = make(map[string](*airlineproto.FlightInfo))
	as.flightQueryBuf = make(map[string](string))
	as.lockBuf = make(map[string](bool))
	as.oldLog = make(map[string]int)
	as.oldReplycMap = make(map[int](chan interface{}))

	q := len(replicas)/2 + len(replicas)%2
	as.lp = libpaxos.NewLibpaxos(myhostport, replicas, q, pos, as)

	lc, err := libconn.NewLibconn(airlineID, masterStorage, myhostport, 0)
	if lsplog.CheckReport(2, err) {
		return nil, nil, err
	}
	as.lib_conn = lc

	//
	//	// Create RPC connection to own storage server
	//	conn2, err := rpc.DialHTTP("tcp", ownStorage)
	//	if err != nil {
	//		return nil, err
	//	}
	//	as.ownStorage = conn2
	//
	// Declare existense
	lc.PublishAirline()
	lc.DeclareExistence()

	go as.storeHandler()
	log.Printf("[%s:%s] Start", airlineID, myhostport)
	return as, as.lp, nil
}
// NewStorageserver
// parameters:
// - server: master sever host port
// - numnodes: total number of storage servers
// - portnum: manually set port number
// - nodeid: hash value for server id
// return:
// - pointer to new storage server
// function:
// - initialize storage server
// - register to the master server for slave server
// - get storage servers' infomation
// - set up appHandler
func NewStorageserver(master string, numnodes int, portnum int, nodeid uint32) *Storageserver {
	// Initialize storage server
	lsplog.SetVerbose(3)
	//runtime.GOMAXPROCS(5)
	ss := &Storageserver{}
	ss.cacheReqC = make(chan interface{})
	ss.serverListLock = new(sync.Mutex)
	ss.nodeid = nodeid
	ss.connLock = new(sync.Mutex)
	ss.connMap = make(map[string]*rpc.Client)

	// register to master for slave
	lsplog.Vlogf(5, "Master:"+master+" Nodeid:%d", nodeid)
	port := fmt.Sprintf("%d", portnum)
	hostport := fmt.Sprintf("localhost:%d", portnum)
	masterport := strings.Split(master, ":")[1]
	if masterport == port {
		//This is the master storage server
		lsplog.Vlogf(5, "Master")

		ss.numnodes = numnodes
		ss.servers = make([]storageproto.Node, numnodes)
		ss.servers[0] = storageproto.Node{master, nodeid}
		ss.cntnodes = 1
	} else {
		lsplog.Vlogf(5, "Slave")
		if nodeid == 0 {
			//Choose random nodeid
			reallySeedTheDamnRNG()
			nodeid = rand.Uint32()
		}
		ss.nodeid = nodeid

		//Connect to master server and wait until ready
		connectedToServer := false
		serverAddress := master
		sleepTime := 1000

		var masterRpcClient *rpc.Client
		var err error
		var reply storageproto.RegisterReply

		for true {
			// Create RPC connection to storage server
			if !connectedToServer {
				masterRpcClient, err = rpc.DialHTTP("tcp", serverAddress)
				if err != nil {
					fmt.Printf("Could not connect to master server %s\n", serverAddress)
				} else {
					connectedToServer = true
					lsplog.Vlogf(5, "Connected to master %s\n", serverAddress)
				}
			}

			if connectedToServer {
				err = masterRpcClient.Call("StorageRPC.Register",
					storageproto.RegisterArgs{storageproto.Node{hostport, nodeid}}, &reply)
				if err == nil && reply.Ready {
					ss.numnodes = len(reply.Servers)
					ss.servers = reply.Servers
					ss.cntnodes = ss.numnodes
					break
				}
			}

			time.Sleep(time.Duration(sleepTime) * time.Millisecond)
		}
	}

	go ss.appHandler()
	return ss
}