Esempio n. 1
0
// 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
}
Esempio n. 2
0
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
}
Esempio n. 3
0
/**@brief Get value given a key for storage server
 * @param key
 * @return value
 * @return error
 */
func (ls *Libstore) iGet(key string) (string, error) {
	var cli *rpc.Client
	var args storageproto.GetArgs = storageproto.GetArgs{key, false, ls.Addr}
	var reply storageproto.GetReply
	var err error

	//try cache first
	if tmp, err := ls.Leases.Get(key, &args); err == nil {
		reply.Value = tmp.(string)
		return reply.Value, nil
	}

	if (ls.Flags & ALWAYS_LEASE) != 0 {
		args.WantLease = true
	}

	//lsplog.Vlogf(0, "libstore Get %s\n", key)

	cli, err = ls.GetServer(key)
	if lsplog.CheckReport(1, err) {
		return "", err
	}

	//listen on no port to accept revoke
	if ls.Addr == "" {
		args.WantLease = false
	}

	//lsplog.Vlogf(0, "Get args:%v\n", args)

	err = cli.Call("StorageRPC.Get", &args, &reply)
	if lsplog.CheckReport(1, err) {
		return "", err
	}

	//lsplog.Vlogf(0, "Get reply:%v#!!\n", reply)
	//fmt.Printf("Get reply granted:%v#!#\n", reply.Lease.Granted)

	if reply.Lease.Granted {
		ls.Leases.LeaseGranted(key, reply.Value, reply.Lease)
	}

	if reply.Status != storageproto.OK {
		return "", MakeErr("Get()", reply.Status)
	}

	return reply.Value, nil
}
Esempio n. 4
0
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
}
Esempio n. 5
0
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
}