Beispiel #1
0
func responseReq(myNode *raft.RaftMachine, clHandl *Handler) {
	var msg *fs.Msg
	for {
		select {
		case commData := <-myNode.SM.CommMedium.CommitCh:
			cmData := (commData).(sm.Commit)

			clHandl.RLock()
			conn := clHandl.ClientMap[cmData.Data.Id]
			clHandl.RUnlock()

			redir := strings.Fields(string(cmData.Err))
			if cmData.Err != nil {
				if redir[0] == "ERR_REDIRECT" {
					conn = clHandl.ClientMap[redir[1]]
					reply(conn, &fs.Msg{Kind: 'R', Contents: []byte(strconv.Itoa(int(cmData.Index)))})
					conn.Close()
					clHandl.Lock()
					delete(clHandl.ClientMap, cmData.Data.Id)
					clHandl.Unlock()
					break
				}
				reply(conn, &fs.Msg{Kind: 'M'})
				conn.Close()
				clHandl.Lock()
				delete(clHandl.ClientMap, cmData.Data.Id)
				clHandl.Unlock()
				break
			}

			data := []byte(cmData.Data.Logg)
			json.Unmarshal(data, &msg)
			response := fs.ProcessMsg(msg)

			if !reply(conn, response) {
				conn.Close()
				clHandl.Lock()
				delete(clHandl.ClientMap, cmData.Data.Id)
				clHandl.Unlock()
				break
			}

		case commData := <-myNode.SM.CommMedium.CommitInfoCh:
			cmData := (commData).(sm.CommitInfo)
			data := []byte(cmData.Data.Logg)
			json.Unmarshal(data, &msg)
			response := fs.ProcessMsg(msg)
			_ = response
		}
	}
}
Beispiel #2
0
func serve(clientId string, myNode *raft.RaftMachine, clHandl *Handler) {
	clHandl.RLock()
	conn := clHandl.ClientMap[clientId]
	clHandl.RUnlock()
	reader := bufio.NewReader(conn)
	for {
		msg, msgerr, fatalerr := fs.GetMsg(reader)
		if fatalerr != nil || msgerr != nil {
			reply(conn, &fs.Msg{Kind: 'M'})
			conn.Close()
			clHandl.Lock()
			delete(clHandl.ClientMap, clientId)
			clHandl.Unlock()
			break
		}

		if msgerr != nil {
			if (!reply(conn, &fs.Msg{Kind: 'M'})) {
				conn.Close()
				clHandl.Lock()
				delete(clHandl.ClientMap, clientId)
				clHandl.Unlock()
				break
			}
		}
		//msg other than read send to raft cluster.
		if string(msg.Kind) != "r" {
			msgByt, _ := json.Marshal(msg)
			raft.ClientAppend(myNode, clientId, msgByt)
		} else {
			//serve read request directly.
			time.Sleep(1 * time.Second)
			response := fs.ProcessMsg(msg)
			if !reply(conn, response) {
				conn.Close()
				clHandl.Lock()
				delete(clHandl.ClientMap, clientId)
				clHandl.Unlock()
				break
			}
		}
	}
}