예제 #1
0
    def request(self, request):
        newRequest = Request()
        newRequest.originalRequest = request
        newRequest.onCompleted = lambda: self.onCompleted(newRequest)
        self.loadBalancer.request(newRequest)

        action = self.controller.onRequest(newRequest)
        self.scaleBy(action)
예제 #2
0
    def request(self, request):
        #self.sim.log(self, "Got request {0}", request)
        request.arrival = self.sim.now
        if self.algorithm in [
                'weighted-RR', 'theta-diff', 'theta-diff-plus', 'equal-thetas',
                'ctl-simplify'
        ]:
            chosenBackendIndex = \
             weightedChoice(zip(range(0, len(self.backends)), self.weights), self.random)
        elif self.algorithm == 'equal-thetas-SQF' or self.algorithm == 'equal-thetas-fast' or self.algorithm == 'equal-thetas-fast-mul':
            # Update controller in the -fast version
            if self.algorithm == 'equal-thetas-fast' or self.algorithm == 'equal-thetas-fast-mul':
                dt = self.sim.now - self.lastDecision
                if dt > 1: dt = 1
                for i in range(0, len(self.backends)):
                    # Gain
                    gamma = self.equal_thetas_fast_gain * dt

                    # Calculate the negative deviation from the average
                    e = self.lastThetas[i] - avg(self.lastThetas)
                    # Integrate the negative deviation from the average
                    self.queueOffsets[
                        i] += gamma * e  # + Kp * (e - self.lastThetaErrors[i])
                    self.lastThetaErrors[i] = e
                self.lastDecision = self.sim.now

            # To prevent starvation, choose a random empty server..
            empty_servers = [i for i in range(0, len(self.queueLengths)) \
             if self.queueLengths[i] == 0]

            if empty_servers:
                chosenBackendIndex = self.random.choice(empty_servers)
            else:
                if self.algorithm == 'equal-thetas-fast-mul':
                    # ...or choose replica with shortest (queue * 2 ** queueOffset)
                    chosenBackendIndex = \
                     min(range(0, len(self.queueLengths)), \
                     key = lambda i: self.queueLengths[i] * (2 ** (-self.queueOffsets[i])))
                else:
                    # ...or choose replica with shortest (queue + queueOffset)
                    chosenBackendIndex = \
                     min(range(0, len(self.queueLengths)), \
                     key = lambda i: self.queueLengths[i]-self.queueOffsets[i])

        elif self.algorithm == 'theta-diff-plus-SQF':
            # choose replica with shortest (queue + queueOffset)
            chosenBackendIndex = \
             min(range(0, len(self.queueLengths)), \
             key = lambda i: self.queueLengths[i]-self.queueOffsets[i])
            pass
        elif self.algorithm == 'random':
            # round robin
            chosenBackendIndex = \
             self.random.choice(range(0, len(self.backends)))
        elif self.algorithm == 'RR':
            # round robin
            chosenBackendIndex = \
             (self.numRequests % len(self.backends)) - 1
        elif self.algorithm == 'SQF':
            # choose replica with shortest queue
            chosenBackendIndex = \
             min(range(0, len(self.queueLengths)), \
             key = lambda i: self.queueLengths[i])
        elif self.algorithm == 'SQF-plus':
            # choose replica with shortest queue
            minIndices = [
                i for i, x in enumerate(self.queueLengths)
                if x == min(self.queueLengths)
            ]
            if len(minIndices) == 1:
                chosenBackendIndex = minIndices[0]
            else:
                dimmers = [self.lastThetas[i] for i in minIndices]
                maxDimmerIndex = dimmers.index(max(dimmers))
                chosenBackendIndex = minIndices[maxDimmerIndex]
        elif self.algorithm == '2RC':
            maxlat = [max(x) if x else 0 for x in self.lastLatencies]
            if len(self.backends) == 1:
                chosenBackendIndex = 0
            # randomly select two backends and send it to the one with lowest latency
            else:
                backends = set(range(0, len(self.backends)))
                randomlychosen = self.random.sample(backends, 2)
                if maxlat[randomlychosen[0]] > maxlat[randomlychosen[1]]:
                    chosenBackendIndex = randomlychosen[1]
                else:
                    chosenBackendIndex = randomlychosen[0]
        elif self.algorithm == 'FRF':
            # choose replica with minimum latency
            maxlat = [max(x) if x else 0 for x in self.lastLatencies]
            chosenBackendIndex = \
             maxlat.index(min(maxlat))
        elif self.algorithm == 'FRF-EWMA':
            # choose replica with minimum EWMA latency
            #self.sim.log(self, "EWMA RT {0}", self.ewmaResponseTime)
            chosenBackendIndex = \
             min(range(0, len(self.backends)), \
             key = lambda i: self.ewmaResponseTime[i])
        elif self.algorithm == 'predictive':
            maxlat = np.array([max(x) if x else 0 for x in self.lastLatencies])
            maxlatLast = np.array(
                [max(x) if x else 0 for x in self.lastLastLatencies])
            wlat = 0.2
            wqueue = 0.8
            points = wlat * (maxlat - maxlatLast) + wqueue * (
                np.array(self.queueLengths) - np.array(self.lastQueueLengths))
            # choose replica with shortest queue
            chosenBackendIndex = \
             min(range(0, len(points)), \
             key = lambda i: points[i])
        elif self.algorithm == "SRTF":
            # choose replica with shortest "time" queue
            #chosenBackendIndex = \
            #	min(range(0, len(self.backends)), \
            #	key = lambda i: sum([r.remainingTime if hasattr(r, 'remainingTime') else 0 for r in self.backends[i].activeRequests]))
            chosenBackendIndex = \
             min(range(0, len(self.queueLengths)), \
             key = lambda i: self.queueLengths[i] * (self.backends[i].serviceTimeY * self.lastThetas[i] + self.backends[i].serviceTimeN * (1 - self.lastThetas[i])))
        elif self.algorithm == 'theta-diff-plus-fast':
            dt = self.sim.now - self.lastDecision
            if dt > 1: dt = 1

            for i in range(0, len(self.backends)):
                # Gain
                Kp = 0.25
                Ti = 5.0
                gammaTr = .01

                # PI control law
                e = self.lastThetas[i] - self.lastLastThetas[i]
                self.queueOffsets[i] += (Kp * e +
                                         (Kp / Ti) * self.lastThetas[i]) * dt

                # Anti-windup
                self.queueOffsets[i] -= gammaTr * (self.queueOffsets[i] -
                                                   self.queueLengths[i]) * dt
                self.lastThetaErrors[i] = e

            self.lastDecision = self.sim.now

            # choose replica with shortest (queue + queueOffset)
            chosenBackendIndex = \
             min(range(0, len(self.queueLengths)), \
             key = lambda i: self.queueLengths[i]-self.queueOffsets[i])

        else:
            raise Exception("Unknown load-balancing algorithm " +
                            self.algorithm)

        request.chosenBackend = self.backends[chosenBackendIndex]
        newRequest = Request()
        newRequest.originalRequest = request
        newRequest.onCompleted = lambda: self.onCompleted(newRequest)
        #self.sim.log(self, "Directed request to {0}", chosenBackendIndex)
        self.queueLengths[chosenBackendIndex] += 1
        self.numRequestsPerReplica[chosenBackendIndex] += 1
        self.backends[chosenBackendIndex].request(newRequest)