예제 #1
0
파일: sqdHelper.py 프로젝트: ajZiiiN/sqd
    def checkWorker(self, ip):
        # assumes that msgClient at Leader has already started
        # --
        # createMsg(id, type, sName, rName, time, opName, args):
        #68926bc9cb24244f919aa03a090aa535::R::sqdC/Fname::sqdL::2015-12-02 00:02:30::addClient:<Args>
        print "Checking Worker..."
        ret = False
        msgId = u.genNewId()
        type = "R"
        sName = "sqdL/checkWorker"
        rName = "sqdW"
        now = datetime.now()
        opName = "iamAlive"
        args = tuple()

        msg = u.createMsg(msgId, type, sName, rName, now, opName, args)

        # send a message to Worker through msgClient
        if ip not in self.workers.keys():
            print "worker not available..."
        else:
            id, M = u.resolveMsg(msg)
            self.workers[ip].outbox[id] = M

            self.workers[ip].send(msg)
            print "checkWorker: " + msg

            for i in range(1,30):
                if id  in self.workers[ip].inbox.keys():
                    ret = self.workers[ip].inbox[id]
                    self.workers[ip].inbox.pop(id,0)
                    self.workers[ip].outbox.pop(id,0)
                    return ret
                else:
                    time.sleep(2)

        return ret


        # if send is success, its good
        # wait on its inbox to get an ack

        # Return True on Success and False on failure
        # its a blocking call

        pass
예제 #2
0
파일: sqdHelper.py 프로젝트: ajZiiiN/sqd
    def addClient(self, clientIp):
        # adding client to cluster
        # ---
        print "Adding client: " + clientIp
        existingClientsInConfig = self.leaderConfig["leader"]["clients"]

        if clientIp not in existingClientsInConfig:
            self.leaderConfig["leader"]["clients"].append(clientIp)

            #update config
            utils.writeJSON(os.path.join(self.configDir, self.configFile), self.leaderConfig)

        existingClientsInServer = self.clients.keys()

        if clientIp not in existingClientsInServer:
            self.clients[clientIp] = msgClient(clientIp)
            self.clients[clientIp].run()

        # Check for client
        print "Checking Client..."
        ret = None
        msgId = u.genNewId()
        type = "R"
        sName = "sqdL/addClient"
        rName = "sqdC"
        now = datetime.now()
        opName = "iamClient"

        # [TODO] for now always the last worker ip is send for testing purposes.
        # it should be same worker on which addClient is envoked after getting infor from client
        workerIp =  self.leaderConfig["leader"]["workers"][-1]
        args = (self.leaderConfig["leader"]["host"], self.leaderConfig["leader"]["cluster"], workerIp)

        msg = u.createMsg(msgId, type, sName, rName, now, opName, args)


        if clientIp not in self.clients.keys():
            print "Client (%s) not available..." % (clientIp,)
        else:
            id, M = u.resolveMsg(msg)
            self.clients[clientIp].outbox[id] = M

            self.clients[clientIp].send(msg)
            print "checkClient: " + msg

            for i in range(1,40):
                if id  in self.clients[clientIp].inbox.keys():
                    ret = self.clients[clientIp].inbox[id]
                    self.clients[clientIp].inbox.pop(id,0)
                    self.clients[clientIp].outbox.pop(id,0)
                    print "Info from Client..", str(ret["args"])
                    break
                else:
                    time.sleep(2)

            # if got the necessary info from client
            # pass it on to worker
            if ret != None and len(ret["args"]) == 3:
                argsToWorker = ret["args"]
                ret = None

                idW = u.genNewId()
                type = "R"
                sName = "sqdL/addClient"
                rName = "sqdW"
                now = datetime.now()
                opName = "addClient"


                msg = u.createMsg(idW, type, sName, rName, now, opName, argsToWorker)

                print "keeping the message content in outbox..."
                curId, M = u.resolveMsg(msg)
                self.workers[workerIp].outbox[id] = M

                self.workers[workerIp].send(msg)
                print "Sending back to worker : ", msg

                for i in range(1,30):
                    if id  in self.workers[workerIp].inbox.keys():
                        ret = self.workers[workerIp].inbox[id]
                        self.workers[workerIp].inbox.pop(id,0)
                        self.workers[workerIp].outbox.pop(id,0)
                        print "Worker sent: ", ret
                        if ret == 1:
                            self.leaderConfig["leader"]["relation"][clientIp] = workerIp
                            #update config
                            utils.writeJSON(os.path.join(self.configDir, self.configFile), self.leaderConfig)

                        return ret
                    else:
                        time.sleep(2)
            else:
                print "Something went wrong while sending to worker..."

        return ret

        # Response handler for new worker coming in
        # If workers name
        pass