// ClientRequestSendMessages checks we are the leader and then queues the messages
// Results are only sent once the messages are on the queue
func (node *Node) ClientRequestSendMessages(args *rapi.SendMessagesArgs, results *rapi.SendMessagesResults) error {
	//log.Printf("Starting request send message\n")
	state := node.getState()
	if state == SHUTDOWN_NODE {
		results.Result = rapi.Get_RI_NODE_IN_SHUTDOWN(node.name)
		return nil
	} else if state != LEADER_NODE {
		results.Result = rapi.Get_RI_NODE_NOT_LEADER(node.name, node.getLastSeenLeader())
		return nil
	}
	//log.Printf("Sending to queue aggregator.\n")
	//IDs, err := node.log.Queue(node.getTerm(), args.SentMessages)
	IDs, err := node.writeAggregator.Queue(args.SentMessages)
	//log.Printf("Got response from write aggregator\n")
	results.IDs = IDs
	if err == nil {
		results.Result = rapi.Get_RI_SUCCESS()
		if !args.WaitForCommit {
			return nil
		}
		if len(IDs) > 0 {
			// See if we are waiting on commit
			waitForIndex := IDs[len(IDs)-1]
			for node.getState() == LEADER_NODE {
				if node.commitIndex.WaitOnCommitChange(waitForIndex) >= waitForIndex {
					return nil
				}
			}
			// If we get here then we are no longer the leader - return an error
			results.Result = rapi.Get_RI_NODE_NOT_LEADER(node.name, node.getLastSeenLeader())
		}
	}
	return err
}
示例#2
0
/*
SendMessages is a Client RPC method allowing clients to send new messages to a topic on the leader node.
*/
func (handler *RPCHandler) SendMessages(args *rapi.SendMessagesArgs, results *rapi.SendMessagesResults) error {
	node := handler.server.GetNode(args.Topic)
	if node != nil {
		return node.ClientRequestSendMessages(args, results)
	} else {
		results.Result = rapi.Get_RI_TOPIC_NOT_FOUND(handler.server.address, args.Topic)
		return nil
	}
}