def run(arg):
    cswithcer = codecSwitcher()
    sConf = staticConfig()
    path = sConf.getPath()
    infoCLocation = sConf.getInfoCLocation()
    cHelper = configHelper( infoCLocation.get("ipInfoC"), infoCLocation.get("portInfoC"))
    consumerLocation = arg[0]
    stepSize = arg[1]
    tagList = cswithcer.getEncode(arg[2:])

    gchooser = groupChooser()
    groupList = gchooser.chooseGroup(tagList)
    if groupList == []:
        print "No storage resource available"
        print "failed1failed2failed"
        sys.exit(1)
    groupName = None
    for groupN in groupList:
        currentUsageInfo = getUsageInfo.run([])
        gsize = currentUsageInfo.get(groupN).get('groupSize')
        usize = currentUsageInfo.get(groupN).get('usedSize')
        asize = long(gsize) - long(usize) - 100
        ssize = long(stepSize)
        if ssize <= asize:
            groupName = groupN
            break
    if groupName == None:
        print "Do not have enough storage space requestSize:%d and asize:%d" % (ssize,asize)
        print "failed1failed2failed"
        sys.exit(1)
    requestStorageCmd = "ssh -t root@"+consumerLocation+" \"python "+path+"main.py requestStorage "+groupName+" "+str(stepSize)+" extra\""
    out = executeCmd(requestStorageCmd)
    if out.find("706errorKEY") >= 0:
        print "failed1failed2failed"
        sys.exit(1)
Exemplo n.º 2
0
def run(arg):
    os.environ['TZ']="Asia/Shanghai"
    time.tzset()
    cswitcher = codecSwitcher()
    logger = autoscaleLog(__file__)
    logger.writeLog(arg)
    sConf = staticConfig()
    path = sConf.getPath()
    infoCLocation = sConf.getInfoCLocation()
    cHelper = configHelper( infoCLocation.get("ipInfoC"), infoCLocation.get("portInfoC"))
    userName = arg[0]
    stepSize = arg[1]
    startTime = int(arg[2])
    endTime = int(arg[3])
    #tagList = []
    #for a in arg[4:]:
    #    tagList.append(unicode(a))
    tagList = cswitcher.getEncode(arg[4:])
    gchooser = groupChooser()
    groupList = gchooser.chooseGroup(tagList)
    if groupList == []:
        print "No storage resource available"
        print "failed1failed2failed"
        return False
    groupName = groupList[0]
    res = gchooser.applyTimeslot(groupName,startTime,endTime,stepSize)
    if res == False:
        print "Do not have enough space, booking failed"
        print "failed1failed2failed"
        return False

    # update user booking table

    userBooking = cHelper.getUserBookingTable()
    print userBooking
    userBookingForUser = userBooking.get(userName)
    if userBookingForUser == None:
        userBookingForUser = {}
    userBookingForUserForGroup = userBookingForUser.get(groupName)
    if userBookingForUserForGroup == None:
        userBookingForUserForGroup = {}
    stKey = startTime/3600
    etKey = endTime/3600
    for t in xrange(stKey,etKey):
        s = userBookingForUserForGroup.get(str(t))
        if s == None:
            s = 0
        userBookingForUserForGroup[str(t)] = s + int(stepSize)
    userBookingForUser[groupName] = userBookingForUserForGroup
    print userBookingForUserForGroup
    userBooking[userName] = userBookingForUser
    print userBookingForUser
    print userBooking
    cHelper.setUserBookingTable(userBooking)
    print "booking succeded"
    return True
def run(arg):
    cswitcher = codecSwitcher()
    logger = autoscaleLog(__file__)
    logger.writeLog(arg)
    sConf = staticConfig()
    path = sConf.getPath()
    infoCLocation = sConf.getInfoCLocation()
    cHelper = configHelper( infoCLocation.get("ipInfoC"), infoCLocation.get("portInfoC"))
    print arg
    userName = arg[0]
    consumerLocation = arg[1]
    stepSize = int(arg[2])
    tagList = cswitcher.getEncode(arg[3:])
    gchooser = groupChooser()
    groupList = gchooser.chooseGroup(tagList)
    if groupList == []:
        print "No storage resource available"
        print "failed1failed2failed"
        return False
    groupName = groupList[0]
    # currentTime = time.time()
    timeM = timeManager()
    currentTime = timeM.getTime()
    # print "currentTime"
    # print currentTime
    ctKey = str(currentTime/3600)
    # print "ctKey"
    # print ctKey
    userBooking = cHelper.getUserBookingTable()
    # print "userBooking GET"
    # print userBooking
    userBookingForUser = userBooking.get(userName)
    # print "userBookingForUser"
    # print userBookingForUser
    if userBookingForUser == None:
        print "User did not book any storage"
        print "failed1failed2failed"
        return False
    userBookingForUserForGroup = userBookingForUser.get(groupName)
    # print "userBookingForUserForGroup"
    # print userBookingForUserForGroup
    if userBookingForUserForGroup == None:
        print "User did not book storage for "+str(tagList)
        print "failed1failed2failed"
        return False
    bookedStorageSize = userBookingForUserForGroup.get(ctKey)
    # print "bookedStorageSize"
    # print bookedStorageSize
    if bookedStorageSize == None:
        print "User did not book storage for this period"
        print "failed1failed2failed"
        return False
    bookedStorageSize = int(bookedStorageSize)
    if bookedStorageSize < stepSize:
        print "User do not have enough booked storage space"
        print "failed1failed2failed"
        return False

    # check current available space, if not enough , release

    currentUsageInfo = getUsageInfo.run([])
    groupSize = currentUsageInfo.get(groupName).get('groupSize')
    usedSize = currentUsageInfo.get(groupName).get('usedSize')
    asize = groupSize - usedSize
    neededSize = 0
    if stepSize > asize:
        neededSize = stepSize - asize
    if neededSize > 0:
        releaseConsumerList = []
        releaseCandidates = cHelper.getReleaseCandidates()
        for deviceMapConsumerIP,size in releaseCandidates.items():
            neededSize = neededSize - size
            releaseConsumerList.append(deviceMapConsumerIP)
            if neededSize <= 0:
                break
        if neededSize > 0:
            print "Do not have enough space sorry"
            return False
        # release consumers
        for deviceMapConsumerIP in releaseConsumerList:
            deviceMap, consumerIP = deviceMapConsumerIP.split("@")
            releaseExtraStorage.run([consumerIP,deviceMap])

    requestStorageCmd = "ssh -t root@"+consumerLocation+" \"python "+path+"main.py requestStorage "+groupName+" "+str(stepSize)+" booked\""
    print requestStorageCmd
    out = executeCmd(requestStorageCmd)
    if out.find("706errorKEY") >= 0:
        print "failed1failed2failed"
        sys.exit(1)

    # userBookingForUserForGroup[ctKey] = bookedStorageSize - stepSize
    startKeyInt = int(ctKey)
    timeKeys = userBookingForUserForGroup.keys()
    timeKeysInt = []
    for timeKey in timeKeys:
        timeKeysInt.append(int(timeKey))
    timeKeysInt.sort()
    for i in xrange(len(timeKeysInt)):
        timeKeyInt = timeKeysInt[i]
        if timeKeyInt < startKeyInt:
            userBookingForUserForGroup.pop(cswitcher.getEncode(timeKeyInt))
        elif timeKeyInt == startKeyInt:
            timeKey = cswitcher.getEncode(timeKeyInt)
            timeKeyBookedSize = userBookingForUserForGroup[timeKey]
            userBookingForUserForGroup[timeKey] = timeKeyBookedSize - stepSize
            startKeyInt += 1
        else:
            break
    cHelper.setUserBookingTable(userBooking)
    print "get booked storage succeded"
    return True