func (ss *Storageserver) Get(args *storageproto.GetArgs, reply *storageproto.GetReply) error { if !ss.CheckWithinRange(args.Key) { reply.Status = storageproto.EWRONGSERVER lsplog.Vlogf(1, "[Get] The key is given to the wrong server") return nil } if args.WantLease { ss.modifyingLock.Lock() if ss.modifying[args.Key] != nil { reply.Lease.Granted = false } else { reply.Lease.Granted = true ss.leasesLock.Lock() currentLeaseList := ss.leases[args.Key] if currentLeaseList == nil { currentLeaseList = list.New() } currentLeaseList.PushBack(&ClientLease{args.LeaseClient, time.Now().Add((storageproto.LEASE_SECONDS + storageproto.LEASE_GUARD_SECONDS) * time.Second)}) ss.leasesLock.Unlock() } ss.modifyingLock.Unlock() } ss.storageLock.Lock() if ss.storage[args.Key] == nil { reply.Status = storageproto.EKEYNOTFOUND } else { reply.Value = *ss.storage[args.Key] reply.Status = storageproto.OK } ss.storageLock.Unlock() return nil }
// RPC-able interfaces, bridged via StorageRPC. // These should do something! :-) func (ss *Storageserver) Get(args *storageproto.GetArgs, reply *storageproto.GetReply) error { //ss.rwlock.RLock() fmt.Printf("try to GET key %s\n", args.Key) val, present := ss.hash[args.Key] if !present { //if the whole system only have one storage node if ss.numnodes == 1 { reply.Status = storageproto.EKEYNOTFOUND } else { //reply.Status = storageproto.EKEYNOTFOUND reply.Status = storageproto.EWRONGSERVER } fmt.Printf("storage GET key %s failed, nonexist\n", args.Key) //ss.rwlock.RUnlock() return nil } err := json.Unmarshal([]byte(val), &(reply.Value)) if err != nil { lsplog.Vlogf(0, "WARNING: unmarshal data generate an error") } if args.WantLease { ss.addLeasePool(args, &(reply.Lease)) } fmt.Printf("Storage Get key %s, val %s, lease %t\n", args.Key, reply.Value, reply.Lease.Granted) reply.Status = storageproto.OK //ss.rwlock.RUnlock() return nil }
func (pc *ProxyCounter) Get(args *storageproto.GetArgs, reply *storageproto.GetReply) error { if pc.override { reply.Status = pc.overrideStatus return pc.overrideErr } byteCount := len(args.Key) if args.WantLease { atomic.AddUint32(&pc.leaseRequestCount, 1) } if pc.disableLease { args.WantLease = false } err := pc.srv.Call("StorageRPC.Get", args, reply) byteCount += len(reply.Value) if reply.Lease.Granted { if pc.overrideLeaseSeconds > 0 { reply.Lease.ValidSeconds = pc.overrideLeaseSeconds } atomic.AddUint32(&pc.leaseGrantedCount, 1) } atomic.AddUint32(&pc.rpcCount, 1) atomic.AddUint32(&pc.byteCount, uint32(byteCount)) return err }
func (ss *Storageserver) Get(args *storageproto.GetArgs, reply *storageproto.GetReply) error { //fmt.Println("called get") //fmt.Printf("key: %v\n", args.Key) rightServer := ss.checkServer(args.Key) if rightServer == false { //fmt.Println("wrong server Get") reply.Status = storageproto.EWRONGSERVER return nil } <-ss.leaseMapM leaseInfo, exists := ss.leaseMap[args.Key] ss.leaseMapM <- 1 if exists == true { list := *leaseInfo.ClientKeys for i := 0; i < len(list); i++ { if list[i] == args.LeaseClient { args.WantLease = false break } } } if args.WantLease == true { reply.Lease.Granted = true reply.Lease.ValidSeconds = storageproto.LEASE_SECONDS var keys []string if exists == true { <-leaseInfo.Mutex *leaseInfo.ClientKeys = append(*leaseInfo.ClientKeys, args.LeaseClient) leaseInfo.Mutex <- 1 // fmt.Printf("**GET added %v to %v lease list\n", args.LeaseClient, args.Key) ss.PrintLeaseMap() } else { keys = []string{} keys = append(keys, args.LeaseClient) <-ss.leaseMapM mutex := make(chan int, 1) mutex <- 1 ss.leaseMap[args.Key] = &LeaseInfo{Mutex: mutex, ClientKeys: &keys} ss.leaseMapM <- 1 // fmt.Printf("**GET added Lease %v to %v\n", args.LeaseClient, args.Key) ss.PrintLeaseMap() } <-ss.clientLeaseMapM leaseExpiration := time.Now().Add((storageproto.LEASE_SECONDS + storageproto.LEASE_GUARD_SECONDS) * time.Second).UnixNano() ss.clientLeaseMap[args.LeaseClient+"@"+args.Key] = leaseExpiration ss.clientLeaseMapM <- 1 } else { reply.Lease.Granted = false reply.Lease.ValidSeconds = 0 } <-ss.valMapM val, ok := ss.valMap[args.Key] if ok != true { reply.Status = storageproto.EKEYNOTFOUND reply.Value = "" } else { reply.Status = storageproto.OK reply.Value = val } ss.valMapM <- 1 //fmt.Printf("Get val for key %v: %v\n", args.Key, reply.Value) return nil }