Ejemplo n.º 1
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
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
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
}