예제 #1
0
def handle(client, addr):
    data = client.recv(1024)
    # greeting
    if data == "hello":
        client.send("hello")
        decision, request = handshake(client)
        if decision == 'Rejected':
            print 'Request is being rejected due to no resources available'
            # notify Cloud Manager about the rejection
            client.send("Reject")
        if decision == 'Accepted':
            app = ''.join(request['App'])
            ports = [int(x) for x in request['Ports']]
            request['lxcIP'] = launch_lxc(app, ports)
            request['cloudIP'] = addr
            lxc_give_access(client, ports, app)
            print 'Edge server running, ready for scaling.'
            with open("lxcList.txt", 'wb') as wfp:
                pickle.dump(lxcList, wfp)
        client.close()
        print 'Connection to %s closed.' % addr[0]
    elif data.split()[0] == "terminate":
        app = data.split()[1]
        lxc = next((item for item in lxcList if item['App'] == app), None)
        if lxc:
            terminate(lxc)
            # remove from list
            lxcList[:] = [d for d in lxcList if d.get('App') != lxc['App']]
            client.send("Terminated %s" % app)
        else:
            client.send("Request ignored, %s not existed." % app)
    else:
        print 'Invalid greeting'
예제 #2
0
def auto_scale():
    lxcList = []
    if os.path.exists("lxcList.txt"):
        with open("lxcList.txt", "rb") as f:
            lxcList = pickle.load(f)
    if len(lxcList) > 0:
        print "%d LXCs to scale" % len(lxcList)
        sortedLXC = sorted(lxcList, key=itemgetter('Priority'), reverse=True)
        for lxc in sortedLXC:
            print "Scaling %s" % lxc['App']
            if (check_activeness(lxc['App'][0])
                    and networkLatency < lxc['Objective'][0]):
                appLatency = networkLatency + lxc['computeLatency']
                if appLatency > lxc['Objective']:
                    scale_up(lxc['App'], sortedLXC)
                else:
                    scale_down(lxc['App'], sortedLXC)
            else:
                print "App %s will be terminated due to inactivity or long network delay." % lxc[
                    'App']
                terminate(lxc)
                # remove from list
                lxcList[:] = [d for d in lxcList if d.get('App') != lxc['App']]
    else:
        print "No running LXCs to scale."
예제 #3
0
def scale_up(app, appList):
    if check_resource():
        # add one unit resource to the app container
        os.system('. scaleLXC.sh %s up' % app)
    else:
        # terminate other containers from the one with lowest priority
        while not check_resource() and len(appList) > 1:
            no_containers = len(appList)
            terminate(appList[no_containers - 1])
            # remove from list
            appList[:] = [d for d in appList if d.get('App') != app]
        if check_resource():
            # add one unit resource to the app container
            os.system('. scaleLXC.sh %s up' % app)
        else:
            print "Scaling up aborted, lack of resources."
예제 #4
0
def scale_up(appName, sortedRunning, record):
    # measure current allocation
    cmdCPU = "lxc-cgroup -n %s cpu.cfs_quota_us" % appName
    cmdMemory = "lxc-cgroup -n %s memory.limit_in_bytes)/1024/1024" % appName
    currentCPU = float(subprocess.check_output(cmdCPU, shell=True))
    currrentMemory = float(subprocess.check_output(cmdMemory, shell=True))
    # only act if lxc has not been allocated the maximum resources
    if currentCPU < psutil.cpu_count() and currrentMemory < psutil.virtual_memory().total:
        # calculate resources to add
        indApp, lxc = next(((index, d) for (index, d) in enumerate(record) if d['App'][0] == appName), None)
        cpu2Add = currentCPU * record[indApp]['violationRate']
        memory2Add = currrentMemory * record[indApp]['violationRate']
        if has_enough_resource(cpu2Add, memory2Add):
            # add required resource to the app container
            os.system('lxc-cgroup -n %s cpu.cfs_quota_us %s' % (appName, currentCPU + cpu2Add))
            os.system('lxc-cgroup -n %s memory.limit_in_bytes %sM' % (appName, currrentMemory + memory2Add))
            # update Scale in registry
            if record[indApp]['Scale']:
                record[indApp]['Scale'] += 1
            else:
                record[indApp]['Scale'] = 1
        else:
            # terminate other containers from the one with lowest priority
            while not has_enough_resource(cpu2Add, memory2Add) and len(sortedRunning) > 1:
                terminate(sortedRunning[-1])
                # update status in registry
                ind = next((index for (index, d) in enumerate(record) if d['App'] == sortedRunning[-1]['App']), None)
                record[ind]['Status'] = 'off'
                # remove this lxc from sorted list
                del sortedRunning[-1]
            if has_enough_resource(cpu2Add, memory2Add):
                # add required resource to the app container
                os.system('lxc-cgroup -n %s cpu.cfs_quota_us %s' % (appName, currentCPU + cpu2Add))
                os.system('lxc-cgroup -n %s memory.limit_in_bytes %sM' % (appName, currrentMemory + memory2Add))
                # update Scale in registry
                if record[indApp]['Scale']:
                    record[indApp]['Scale'] += 1
                else:
                    record[indApp]['Scale'] = 1
            else:
                print "Scaling up aborted, lack of resources."
    else:
        print "Scaling up aborted, maximum resources allocated already."
예제 #5
0
def auto_scale():
    if os.path.exists("lxcList.txt"):
        with open("lxcList.txt", 'rb') as f:
            lxcList = pickle.load(f)
        # scale running LXC only
        runningLXC = [d for d in lxcList if d['Status'] == 'on']
        if len(runningLXC) > 0:
            print "%d LXCs to scale" % len(lxcList)
            if pmApproach == "SPS":
                for lxc in runningLXC:
                    if not lxc.get('Priority'):
                        lxc['Priority'] = calculate_ps(lxc)
            else:
                for lxc in runningLXC:
                    lxc['Priority'] = calculate_ps(lxc)
            # sort by priority
            sortedLXC = sorted(runningLXC, key=itemgetter('Priority'), reverse=True)
            for lxc in sortedLXC:
                ind = next((index for (index, d) in enumerate(lxcList) if d['App'] == lxc['App']), None)
                # check activeness
                if lxc['Requests'] > 0 and networkLatency < float(lxc['Objective'][0]):
                    print "Scaling %s" % lxc['App'][0]
                    appLatency = networkLatency + lxc['computeLatency']
                    if appLatency > float(lxc['Objective'][0]):
                        scale_up(lxc['App'][0], sortedLXC, lxcList)
                    elif appLatency > float(lxc['Threshold'][0]) * float(lxc['Objective'][0]):
                        if lxc['Donation'][0] == 'Yes':
                            scale_down(lxc['App'][0], lxcList)
                            # update Reward in registry
                            lxcList[ind]['Reward'] += 1
                    else:
                        scale_down(lxc['App'][0], lxcList)
                else:
                    print "App %s will be terminated due to inactivity or long network delay." % lxc['App']
                    terminate(lxc)
                    # update status in registry
                    lxcList[ind]['Status'] = 'off'
                    # remove this lxc from sorted list
                    del sortedLXC[-1]
        else:
            print "No running LXCs to scale."
예제 #6
0
                for factor in ['lxcIP', 'cloudIP', 'ID', 'Status', 'Premium', 'Ports', 'Objective', 'Database', 'Donation', 'Threshold']:
                    lxcList[index][factor] = request[factor]
                if lxcList[index]['Loyalty']:
                    lxcList[index]['Loyalty'] += 1
                else:
                    lxcList[index]['Loyalty'] = 1
            else:
                request['Loyalty'] = 1
                lxcList.append(request)
            lxc_give_access(client, ports, app)
            print 'Edge server running, ready for scaling.'
    elif data.split()[0] == "terminate":
        app = data.split()[1]
        index, lxc = next(((index, d) for (index, d) in enumerate(lxcList) if d['App'][0] == app), None)
        if index:
            terminate(lxc)
            # update status
            lxcList[index]['Status'] = 'off'
            client.send("Terminated %s" % app)
    	else:
            client.send("Request ignored, %s not existed." % app)
    else:
        print 'Invalid greeting'
    # update record
    with open("lxcList.txt",'wb') as wfp:
        pickle.dump(lxcList, wfp)
    client.close()
    print 'Connection to %s closed.' % addr[0]


def start_server():