Ejemplo n.º 1
0
    def scaleDownInAZWithVMs(self, amount):
        totalDownCount = 0
        for az in self.azList:
            if az.holdVMs:
                #留一台不能全部关,所以-1
                vmsDownCount = min(len(az.holdVMs) - 1, amount)

                #本az只剩下一台机器了,在本函数中不能继续往下减,由后面的工作完成
                if vmsDownCount == 0:
                    continue

                #有几次出错,调试用
                logger.debug('scaleDownInAZWithVMs: downNumber:' +
                             str(vmsDownCount) + ' az:' + az.name +
                             ' len az.holdVMs:' + str(len(az.holdVMs)) +
                             ' amount:' + str(amount))

                TomcatInstanceUtil.deleteSpecifyNumberInstancesWithSpecifyAZ(
                    vmsDownCount, az.name)
                self.updateCloudInfo(self.topoFilePath)
                amount -= vmsDownCount
                totalDownCount += vmsDownCount

                if amount == 0:
                    break
        return totalDownCount
Ejemplo n.º 2
0
    def scaleDownMakesAZEmpty(self, amount):
        totalDownCount = 0
        #按distance由大到小排列,从通讯开销最大的开始尝试,直到可用性不满足要求,或者需要scale down的数目已经满足
        reversedAzList = sorted(self.azList,
                                key=attrgetter('distance'),
                                reverse=True)

        azNameList = []
        for az in reversedAzList:
            azNameList.append(az.name)

        for azn in azNameList:
            az = self.findNewAZWithAZName(azn)
            if az.holdVMs:
                # No matter the result is true or false, the structure of the TopoTree has been changed after
                # this function call, so following updateCloudInfo() should be invoked anyway.
                if self.canScaleDownOnlyVMInAZOrNot(az):
                    logger.debug('scale down the only vm in ' + az.name)
                    TomcatInstanceUtil.deleteSpecifyNumberInstancesWithSpecifyAZ(
                        1, az.name)
                    amount -= 1
                    totalDownCount += 1

                self.updateCloudInfo(self.topoFilePath)

                if amount == 0:
                    break

        return totalDownCount
Ejemplo n.º 3
0
 def scaleUpInAZWithoutVMs(self, amount, comd):
     createdCount = 0
     for az in self.azList:
         if not az.holdVMs and az.distance >= comd:
             vmsLaunchCount = min(az.maxVMCount, amount)
             TomcatInstanceUtil.createSpecifyNumberInstancesInAZ(
                 vmsLaunchCount, az.name)
             self.updateCloudInfo(self.topoFilePath)
             amount -= vmsLaunchCount
             createdCount += vmsLaunchCount
             if amount == 0:
                 break
     return createdCount
Ejemplo n.º 4
0
    def create(self, req):
        rc = req.params.get('requireCount')
        resetFlag = req.params.get('reset')
        if rc and isNumber(rc) and resetFlag and isNumber(resetFlag):
            rc = int(rc)
            resetFlag = int(resetFlag)

            if resetFlag:
                TomcatInstanceUtil.resetAllUsingInstances()

            uic = UsingInstancesDBUtil.getUsingInstancesCount()
            if rc > uic:
                needC = rc - uic
                while needC > 0:
                    TomcatInstanceUtil.createTomcatInstance()
                    needC -= 1
            elif rc < uic:
                deleteC = uic - rc
                TomcatInstanceUtil.deleteSpecifyNumberInstances(deleteC)

            TomcatInstanceUtil.ensureAllUsingInstancesActive()
            return UsingInstancesDBUtil.getAllUsingInstancesInfo()
        else:
            result = errorResultJson('The Post Body Must be {requireCount:x, reset:y} (ps:x must be number, reset must be 0 or 1)')
        return result
Ejemplo n.º 5
0
    def doMigration(self, req):
        vmId = req.params.get('givenVM')
        pmId = req.params.get('targetPM')

        if not vmId or not pmId:
            return errorResultJson('please pass the givenVM and the givenPM!')

        azName = PMAndAZDBUtil.getAZNameByResourceId(pmId)
        if TomcatInstanceUtil.migrate(vmId, azName):
            return successResultJson(True)
        else:
            return errorResultJson(False)
Ejemplo n.º 6
0
    def getInitialScheme(self):
        periodDB = shelve.open(periodRecoderFile)
        periodDB[periodRecoder] = None
        periodDB.close()

        fiboDB = shelve.open(fiboDataFile)
        fiboDB[fiboDataName] = None
        fiboDB.close()

        clearAllData()

        PerformanceDBUtil.clearPerformanceDataTable()
        WorkloadDBUtil.clearWorkloadTable()

        uiCount = UsingInstancesDBUtil.getUsingInstancesCount()

        if uiCount < initUsingInstancesNumbers:
            TomcatInstanceUtil.resetAllUsingInstances()
            self.acrcPlacementComponent.getPlacementScheme(
                initUsingInstancesNumbers - uiCount, True)
        elif uiCount > initUsingInstancesNumbers:
            self.acrcPlacementComponent.getPlacementScheme(
                uiCount - initUsingInstancesNumbers, False)
            TomcatInstanceUtil.resetAllUsingInstances()
        else:
            TomcatInstanceUtil.resetAllUsingInstances()
Ejemplo n.º 7
0
    def initExperimentThree(self, req):
        vmNum = req.params.get('requireCount')

        if vmNum and isNumber(vmNum):
            rc = int(vmNum)

            TomcatInstanceUtil.deleteAllTestingInstance()

            azList = ['az1', 'az2', 'az3', 'az4']
            azLen = len(azList)
            for i in range(rc):
                TomcatInstanceUtil.createTomcatInstance(azName=azList[i % azLen])

            TomcatInstanceUtil.ensureAllUsingInstancesActive()

            for ipEnd in ipEndOfComputes:
                os.system('/home/sk/cloudEx/shellScript/initAllPMUtil.sh ' + str(ipEnd) + ' > /dev/null')

            # 清空与预测相关的数据
            clearAllData()
            PMCPUDBUtil.clearPMCPUTable()
            VMCPUDBUtil.clearVMCPUTable()

            return UsingInstancesDBUtil.getAllUsingInstancesInfo()
        else:
            result = errorResultJson('The Post Body Must be {requireCount:x} (ps:x must be number)')
        return result
Ejemplo n.º 8
0
    def autonomicPeriodHandler(self):
        scaleContent = self.provisionComponent.getNumberOfVMsShouldBeScaled()

        # re-organize the code, maintaining the logic
        if scaleContent['vmNumbers'] == 0:
            TomcatInstanceUtil.resetAllUsingInstances()
        elif scaleContent['isUp']:
            TomcatInstanceUtil.resetAllUsingInstances()
            self.placementComponent.getPlacementScheme(scaleContent['vmNumbers'], scaleContent['isUp'])
        else:
            self.placementComponent.getPlacementScheme(scaleContent['vmNumbers'], scaleContent['isUp'])
            TomcatInstanceUtil.resetAllUsingInstances()
Ejemplo n.º 9
0
    def getNumberOfVMsShouldBeScaled(self):
        if not self.predictor or not self.ruleChecker:
            raise Exception('no predictor or ruleChecker')

        newestHD = WorkloadDBUtil.getNewestWorkload()

        if not newestHD:
            raise Exception('Can not get newest workload')

        addWLToACRCWindow(newestHD['realWL'])
        predictWL = self.predictor.getNextPeriodWorkload()
        addPWLToACRCWindow(predictWL)

        # 预测写入Workload表
        periodNoDB = shelve.open(periodRecoderFile)
        periodNo = periodNoDB.get(periodRecoder, None)
        if not periodNo:
            raise Exception('Can not get periodNo')

        # do linear regression in given windowSize
        # TODO: temporarily use default windowSize = sys.maxint
        pairs = WorkloadDBUtil.getRealWorkloadAndRealTotalCalculationInPairs()
        wl = pairs[0]
        tc = pairs[1]
        # TODO: do not fix the interception temporarily, might let the line cross the origin later
        regression = np.polyfit(wl, tc, 1)
        k = float(regression[0])
        b = float(regression[1])

        if k < 0 or wl.__len__() == 1:
            # actually k should be positive, here to eliminate corner case when there's not much log
            logger.warning("k = " + str(k) +
                           " is negative or length of log is 0!")
            k = tc[-1] / wl[-1]
            b = 0
            logger.warning("using k = " + str(k) + " b = 0 instead")

        predictVMNumbers = math.ceil(
            k * predictWL /
            (TomcatInstanceUtil.getCalculationCapacityPerInstance() - b))

        predictTC = k * predictWL + predictVMNumbers * b

        WorkloadDBUtil.addPredictWorkloadAndPredictTotalCalculationToSpecificPeriod(
            periodNo, predictWL, predictTC)

        # 记录供给的虚拟机数目的分布
        provisionInfoDB = shelve.open(provisionInfoFile)

        provisionInfoDB[predictProvisionVMNumbers] = predictVMNumbers

        logger.info('PredictWorkload:' + str(predictWL) +
                    ' predictVMNumbers:' + str(predictVMNumbers) +
                    ' predictTC: ' + str(predictTC))

        addedVMNumbers = self.ruleChecker.getNextAddedVMs()

        provisionInfoDB[reactiveProvisionVMNumbers] = addedVMNumbers

        logger.info('addedVMNumbers:' + str(addedVMNumbers))

        provisionInfoDB.close()

        nextPeriodVMNumbers = predictVMNumbers + addedVMNumbers

        usingInstancesCount = UsingInstancesDBUtil.getUsingInstancesCount()
        gab = abs(usingInstancesCount - nextPeriodVMNumbers)

        if usingInstancesCount > nextPeriodVMNumbers:
            rt = {'vmNumbers': gab, 'isUp': False}
        elif usingInstancesCount < nextPeriodVMNumbers:
            rt = {'vmNumbers': gab, 'isUp': True}
        else:
            rt = {'vmNumbers': gab}

        logger.info('return with:' + str(rt))

        return rt
Ejemplo n.º 10
0
#!/usr/bin/env python
# encoding: utf-8

#import NovaUtil.TomcatInstanceUtil
from NovaUtil.TomcatInstanceUtil import TomcatInstanceUtil
from opsdkUtil.getClientUtil import GetClientUtil
from NovaUtil.FlavorUtil import FlavorUtil
from NovaUtil.InstanceUtil import InstanceUtil
from DBUtil.UsingInstancesDBUtil import UsingInstancesDBUtil
#nova = GetClientUtil.getNovaClient()
#instance = nova.servers.list(search_opts = {'name':'sjyvm-1'})
#print instance
#smallFlavor = FlavorUtil.findFlavorByName('m1.small')
#print 'get' + smallFlavor.name
#instance = nova.servers.create('sjy-x', 'skvmModel', smallFlavor)
#print nova.servers.ips(nova)
#instance = TomcatInstanceUtil.createTomcatInstance()

#instance = InstanceUtil.findInstaceByName('sjyvm-e2c9f0ce-b8fd-11e5-9899-00e04c680ae0')
#instance = InstanceUtil.findInstanceById('3c366418-8a4e-440a-b474-cf4dacd5e6c3')
#print dir(instance)
#print instance.availability-zone

print TomcatInstanceUtil.migrate('34223c71-33b5-4616-a818-2027812dc83b', 'az4')
Ejemplo n.º 11
0
    def periodPerformanceDataHandler2(self, req):
        minResponseTime = req.params.get('minResponseTime')
        avgResponseTime = req.params.get('avgResponseTime')
        maxResponseTime = req.params.get('maxResponseTime')
        totalRequestCount = req.params.get('totalRequestCount')
        breakSLACount = req.params.get('breakSLACount')
        jumpServer = req.params.get('jumpServer')
        infos = SampleUtil.getThreadInfosOverAllUsingInstances(jumpServer)
        avgCpuUtil = round(infos[0] / 100.0, 4)
        totalCalculation = round(infos[1] / 100.0, 4)
        avgMemoryUtil = round(SampleUtil.getAllUsingInstancesPeriodAVGMemoryUtil() / 100.0, 4)

        if not (isDecimal(minResponseTime) or isNumber(minResponseTime)) \
                or not (isDecimal(maxResponseTime) or isNumber(maxResponseTime)) \
                or not (isDecimal(avgResponseTime) or isNumber(avgResponseTime)) \
                or not isNumber(totalRequestCount) \
                or not isNumber(breakSLACount):
            return errorResultJson('Please pass the params correctly')
        elif avgCpuUtil == None or avgMemoryUtil == None or totalCalculation == None:
            raise Exception("can not get avgCpuUtil or avgMemoryUtil or totalCalculation data")
        else:
            minResponseTime = float(minResponseTime)
            avgResponseTime = float(avgResponseTime)
            maxResponseTime = float(maxResponseTime)
            totalRequestCount = int(totalRequestCount)
            breakSLACount = int(breakSLACount)

            # 确认periodNo
            periodNoDB = shelve.open(periodRecoderFile)
            periodNo = periodNoDB.get(periodRecoder, None)

            if not periodNo:
                periodNo = 1

            periodNoDB[periodRecoder] = periodNo + 1
            periodNoDB.close()

            # 计算breakSLAPercent
            breakSLAPercent = float(breakSLACount) / totalRequestCount
            breakSLAPercent = round(breakSLAPercent, 4)

            # 计算刚刚过去的这个周期的可用性
            placementTool = ACRCPlacementComponent()
            availabilityData = placementTool.calculateAvailability()

            # 得到刚刚过去这个周期的虚拟机数目
            vmNumbers = UsingInstancesDBUtil.getUsingInstancesCount()

            # 添加上个周期应该提供的虚拟机数目
            shouldVMNumbers = WorkloadVMMapDBUtil.getTargetVMsToSpecificWorkload(totalRequestCount)

            if periodNo == 1:
                ppVMNumbers = vmNumbers
                rpVMNumbers = 0
            else:
                provisionInfoDB = shelve.open(provisionInfoFile)
                ppVMNumbers = provisionInfoDB.get(predictProvisionVMNumbers, None)
                rpVMNumbers = provisionInfoDB.get(reactiveProvisionVMNumbers, None)

            # 添加performanceData
            performanceData = {'minResponseTime': minResponseTime, 'maxResponseTime': maxResponseTime,
                               'avgResponseTime': avgResponseTime, 'breakSLAPercent': breakSLAPercent,
                               'avgCpuUtil': avgCpuUtil, 'avgMemoryUtil': avgMemoryUtil,
                               'availability': availabilityData, 'vmNumbers': vmNumbers,
                               'shouldVMNumbers': shouldVMNumbers, 'predictProvisionVMNumbers': ppVMNumbers,
                               'reactiveProvisionVMNumbers': rpVMNumbers}
            PerformanceDBUtil.addPerformanceDataToSpecificPeriod(periodNo, performanceData)

            # 向数据库中添加workload信息
            if periodNo == 1:
                WorkloadDBUtil.addFirstPeriodRealWorkloadAndRealTotalCalculation(totalRequestCount, totalCalculation)
            else:
                WorkloadDBUtil.addRealWorkloadAndRealTotalCalculationToSpecificPeriod(periodNo, totalRequestCount,
                                                                                      totalCalculation)

            acrCtl = ACRController()
            acrCtl.autonomicPeriodHandler()

            TomcatInstanceUtil.ensureAllUsingInstancesActive()
            return UsingInstancesDBUtil.getAllUsingInstancesInfo()