6.5840 Lab-Key/Value Server 思路

6.824 Lab 2: Key/Value Server

在本实验中,需要构建一个键/值服务器, 以确保每个操作在网络故障 的情况下都只执行一次, 并且操作是线性化的。

网络故障

示例代码

Go 复制代码
type Clerk struct {
	server   *labrpc.ClientEnd
	workerId int64
	seq      int64
}
Go 复制代码
func (ck *Clerk) PutAppend(key string, value string, op string) string {
	reply := new(PutAppendReply)
	for !reply.Ok {
		ck.server.Call("KVServer."+op, &PutAppendArgs{key, value, ck.workerId, ck.seq}, reply)
	}
	ck.seq++
	v := reply.Value
	reply.Ok = false
	return v
}
Go 复制代码
type KVServer struct {
	mu sync.RWMutex

	m      map[string]string
	seqSet map[string]string
}
Go 复制代码
func (kv *KVServer) Append(args *PutAppendArgs, reply *PutAppendReply) {
	kv.mu.Lock()
	defer kv.mu.Unlock()
	
	key := strconv.Itoa(int(args.WorkerId)) + strconv.Itoa(int(args.Seq))
	//检查是不是执行过
	if v, ok := kv.seqSet[key]; ok {
		reply.Ok = true
		reply.Value = v
		return
	}
	//删除上一次的缓存
	delete(kv.seqSet, strconv.Itoa(int(args.WorkerId))+strconv.Itoa(int(args.Seq-1)))
	//执行Append
	s := kv.m[args.Key]
	kv.seqSet[key] = s
	kv.m[args.Key] = s + args.Value
	reply.Value = s
	reply.Ok = true
}

tips

  • 测试文件运行在linux下比windows快不少,建议在linux测试
相关推荐
程序猿DD26 分钟前
OctaFuse 2.19.0:重做管理后台界面与交互,模型目录同步更新
后端
金銀銅鐵1 小时前
带您看懂字节码指令(一):一帧里有什么
java·后端·ai编程
码事漫谈2 小时前
Agent 测评是什么?大白话讲一讲
后端
皮皮林5513 小时前
在 IDEA 里斗个地主不过分吧!
后端
SomeOtherTime3 小时前
Rust所有权(AI回答)
开发语言·后端·rust
程序员cxuan5 小时前
离谱,AI 大佬顺手搓了个 YouTube ,而我顺手给他搓了个中文插件
人工智能·后端·程序员
学长毕设5 小时前
基于springboot科创竞赛信息平台-附源码
java·spring boot·后端
深蓝AI5 小时前
Gemini Decisions API 来了:一次前向打分取代逐字解码,校准误差 0.0250 才是 agent 分支调用的真门槛
人工智能·后端
学长毕设5 小时前
基于SpringBoot公务员考试交流平台-附源码
java·spring boot·后端
大然Ryan6 小时前
我用 Go 和 AI 做了一个 macOS 磁盘清理工具:Vole(开源)
go