李度、马也驰 25spring数据库系统 p1仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
828 B

package nodes
import "strconv"
// leader node作为server为client注册的方法
type ServerReply struct{
Isconnect bool
HaveValue bool
Value string
}
// RPC call
func (node *Node) WriteKV(kv LogEntry, reply *ServerReply) error {
logId := node.maxLogId
node.maxLogId++
node.log[logId] = kv
// 广播给其它节点
log.Info("server write : logId = " + strconv.Itoa(logId) + ", key = " + kv.Key)
node.BroadCastKV(logId, kv)
reply.Isconnect = true
return nil
}
// RPC call
func (node *Node) ReadKey(key string, reply *ServerReply) error {
log.Info("server read : " + key)
// 先只读leader自己
for _, kv := range node.log {
if kv.Key == key {
reply.Value = kv.Value
reply.HaveValue = true
reply.Isconnect = true
return nil
}
}
reply.HaveValue = false
reply.Isconnect = true
return nil
}