Beispiel #1
0
    def __acquireLockInternal(self, blockingLock, timeout):
        waitPeriod = 3
        if not blockingLock:
            waitPeriod = min(3, timeout)
        utilities.runCommandSilently('sleep ' + str(waitPeriod))
        elapsedTime = waitPeriod
        glogger.debug(self.clientId + " trying again to acquire lock")
        start = time.time()
        if not self.__isLockFree():
            if blockingLock:
                self.__waitForLockInfinitely()
            else:
                end = time.time()
                delay = int(end-start)
                remainingTime = timeout - elapsedTime - delay
                if remainingTime <= 0:
                    return False
                timeExpired,elapsedTimeInWait = self.__timedWaitForLock(remainingTime)
                if timeExpired:
                    return False
                elapsedTime += elapsedTimeInWait
                start = time.time()

        if self.__createLockFile() != 0:
            end = time.time()
            delay = int(end-start)
            remainingTime = timeout - elapsedTime - delay
            if not blockingLock and remainingTime <= 0:
                return False
            return self.__acquireLockInternal(blockingLock, remainingTime)
        return True
    def __acquireLockInternal(self, blockingLock, timeout):
        waitPeriod = 3
        if not blockingLock:
            waitPeriod = min(3, timeout)
        utilities.runCommandSilently('sleep ' + str(waitPeriod))
        elapsedTime = waitPeriod
        glogger.debug(self.clientId + " trying again to acquire lock")
        start = time.time()
        if not self.__isLockFree():
            if blockingLock:
                self.__waitForLockInfinitely()
            else:
                end = time.time()
                delay = int(end - start)
                remainingTime = timeout - elapsedTime - delay
                if remainingTime <= 0:
                    return False
                timeExpired, elapsedTimeInWait = self.__timedWaitForLock(
                    remainingTime)
                if timeExpired:
                    return False
                elapsedTime += elapsedTimeInWait
                start = time.time()

        if self.__createLockFile() != 0:
            end = time.time()
            delay = int(end - start)
            remainingTime = timeout - elapsedTime - delay
            if not blockingLock and remainingTime <= 0:
                return False
            return self.__acquireLockInternal(blockingLock, remainingTime)
        return True
Beispiel #3
0
 def __createLockFile(self):
     tmpFolder = self.tmpDir + "/" + self.clientId
     command = 'mkdir -p ' + tmpFolder
     status = utilities.runCommandSilently(command)
     if status != 0:
         glogger.warn(self.clientId + " could not create directory " + tmpFolder)
         return -1
     tmpLockFile = tmpFolder + "/" + self.lockname
     command = 'touch ' + tmpLockFile + ' | echo "' + self.clientId + '" > ' + tmpLockFile
     status = utilities.runCommandSilently(command)
     if status != 0:
         glogger.warn(self.clientId + " could not create file " + tmpLockFile)
         command = "rm -rf " + tmpFolder
         utilities.runCommandSilently(command)
         return -1
     try:
         status = hdfsCommandExecutor.execute('put', tmpLockFile, self.lockBaseDir)
         if status == 0:
             glogger.debug(self.clientId + " has created lock file successfully")
     except HdfsDataNodeDownException as ex:
         status = -1
         glogger.warn(self.clientId + " could not create lock file\n" + str(ex) + "\n")
         try:
             glogger.debug(self.clientId + " removing partial lock file")
             HdfsLock.releaseForcefully(self.lockname)
             glogger.debug(self.clientId + " removed partial lock file")
         except HdfsException as e:
             glogger.warn(self.clientId + " could not remove partial lock file\n" + str(e) + "\n")           
     except HdfsException as e:
         status = -1
         glogger.warn(self.clientId + " could not create lock file\n" + str(e) + "\n")
     command = "rm -rf " + tmpFolder
     utilities.runCommandSilently(command)
     return status
 def __createLockFile(self):
     tmpFolder = self.tmpDir + "/" + self.clientId
     command = 'mkdir -p ' + tmpFolder
     status = utilities.runCommandSilently(command)
     if status != 0:
         glogger.warn(self.clientId + " could not create directory " +
                      tmpFolder)
         return -1
     tmpLockFile = tmpFolder + "/" + self.lockname
     command = 'touch ' + tmpLockFile + ' | echo "' + self.clientId + '" > ' + tmpLockFile
     status = utilities.runCommandSilently(command)
     if status != 0:
         glogger.warn(self.clientId + " could not create file " +
                      tmpLockFile)
         command = "rm -rf " + tmpFolder
         utilities.runCommandSilently(command)
         return -1
     try:
         status = hdfsCommandExecutor.execute('put', tmpLockFile,
                                              self.lockBaseDir)
         if status == 0:
             glogger.debug(self.clientId +
                           " has created lock file successfully")
     except HdfsDataNodeDownException as ex:
         status = -1
         glogger.warn(self.clientId + " could not create lock file\n" +
                      str(ex) + "\n")
         try:
             glogger.debug(self.clientId + " removing partial lock file")
             HdfsLock.releaseForcefully(self.lockname)
             glogger.debug(self.clientId + " removed partial lock file")
         except HdfsException as e:
             glogger.warn(self.clientId +
                          " could not remove partial lock file\n" + str(e) +
                          "\n")
     except HdfsException as e:
         status = -1
         glogger.warn(self.clientId + " could not create lock file\n" +
                      str(e) + "\n")
     command = "rm -rf " + tmpFolder
     utilities.runCommandSilently(command)
     return status