Exemplo n.º 1
0
func NewPaxosWrap(myHostPort string, ID int, serverHostPorts []string, flags []string) (PaxosWrap, error) {
	Wrapper := new(paxosWrap)
	paxos, err := paxos.NewPaxos(myHostPort, ID, serverHostPorts, true)
	Wrapper.timeout = 0
	Wrapper.die = false
	Wrapper.sleep = false
	if err != nil {
		return nil, err
	}
	Wrapper.paxos = paxos
	err = rpc.RegisterName("Paxos", paxoswraprpc.Wrap(Wrapper))
	if err != nil {
		return nil, err
	}
	for _, value := range flags {
		split := strings.Split(value, ":")
		if split[0] == "s" {
			Wrapper.sleep = true
			timer, err := strconv.Atoi(split[1])
			if err != nil {
				return nil, err
			}
			Wrapper.timeout = time.Duration(timer)
		} else if split[0] == "d" {
			Wrapper.die = true
		} else if split[0] == "p" {
			Wrapper.pDelay = true
			timer, err := strconv.Atoi(split[1])
			if err != nil {
				return nil, err
			}
			Wrapper.pTimeout = time.Duration(timer)
		} else if split[0] == "a" {
			Wrapper.aDelay = true
			timer, err := strconv.Atoi(split[1])
			if err != nil {
				return nil, err
			}
			Wrapper.aTimeout = time.Duration(timer)
		} else if split[0] == "g" {
			Wrapper.gDelay = true
			timer, err := strconv.Atoi(split[1])
			if err != nil {
				return nil, err
			}
			Wrapper.gTimeout = time.Duration(timer)
		} else if split[0] == "c" {
			Wrapper.cDelay = true
			timer, err := strconv.Atoi(split[1])
			if err != nil {
				return nil, err
			}
			Wrapper.cTimeout = time.Duration(timer)
		} else if split[0] == "r" {
			Wrapper.rDelay = true
			timer, err := strconv.Atoi(split[1])
			if err != nil {
				return nil, err
			}
			Wrapper.rTimeout = time.Duration(timer)
		}
	}

	return Wrapper, nil

}
Exemplo n.º 2
0
//if masterServerHostPort isn't empty, then it's a masterclient,
//else it's a slaveclient to start with
func NewServer(masterServerHostPort string, port int, nodeID int, test bool, flags []string) (PacmanServer, error) {
	pacmanServer := new(pacmanServer)
	pacmanServer.selfNode = new(loadbalancerrpc.Node)
	pacmanServer.nodes = make([]loadbalancerrpc.Node, loadbalancerrpc.InitCliNum)
	pacmanServer.masterServerHostPort = masterServerHostPort
	pacmanServer.ID = nodeID
	pacmanServer.lock = new(sync.Mutex)
	//if it's the slave client
	conn, err := rpc.DialHTTP("tcp", masterServerHostPort)
	if err != nil {
		return nil, err
	}
	pacmanServer.masterConn = conn
	args := &loadbalancerrpc.RegisterArgs{ServerInfo: loadbalancerrpc.Node{HostPort: net.JoinHostPort("localhost", strconv.Itoa(port)), NodeID: nodeID}}
	var reply loadbalancerrpc.RegisterReply

	err = pacmanServer.masterConn.Call("LoadBalancer.RegisterServer", args, &reply)
	fmt.Println("trying to connect , Host port:", port, " ready :", reply.Status)
	if err != nil {
		fmt.Println(err)
	}
	if reply.Status == loadbalancerrpc.INVALID {
		return nil, errors.New("you're an invalid server")
	}
	for reply.Status == loadbalancerrpc.NotReady {
		//if master server is still busy
		fmt.Println("retrying to connect")
		time.Sleep(1000 * time.Millisecond)
		err = pacmanServer.masterConn.Call("LoadBalancer.RegisterServer", args, &reply)
	}

	pacmanServer.nodes = reply.Servers
	hostPorts := make([]string, len(pacmanServer.nodes))
	i := 0
	for _, node := range pacmanServer.nodes {
		hostPorts[i] = node.HostPort
		i++
	}

	listener, err := net.Listen("tcp", net.JoinHostPort("localhost", strconv.Itoa(port)))
	if err != nil {
		return nil, err
	}

	err = rpc.RegisterName("PacmanServer", serverrpc.Wrap(pacmanServer))
	if err != nil {
		return nil, err
	}
	rpc.HandleHTTP()
	go http.Serve(listener, nil)
	if test {
		fmt.Println("Test Mode : On")
		pacmanServer.paxos, err = paxosWrap.NewPaxosWrap(net.JoinHostPort("localhost", strconv.Itoa(port)), nodeID, hostPorts, flags)
	} else {
		pacmanServer.paxos, err = paxos.NewPaxos(net.JoinHostPort("localhost", strconv.Itoa(port)), nodeID, hostPorts, false)
	}

	if err != nil {
		return nil, err
	}

	if reply.Recovering == true {
		//nop
		fmt.Println("I'M DOING NOP")
		err = pacmanServer.paxos.RequestValue("")

		if err != nil {
			fmt.Println("Can't get logs from other servers by NOP")
		}
	}

	return pacmanServer, nil

}