def set_time(process_id, clock): processId = int(process_id) process = sharedData.GetProcessByID(processId) if process != None: msg = DSMessage(DSMessageType.SetTime) msg.Argument = clock result = process.DSSocket.SendMessage(msg) click.echo(result) else: click.echo("ProcessId is not valid, the process wasn't found.")
def handleOperationAndAcknowledgeTheCoordinator(self): messageType = None if self.Operation.Type == DSMessageType.SetNewValue: self.temporarilySetNewValueHandler() messageType = DSMessageType.VoteCommit elif self.Operation.Type == DSMessageType.RollbackValues: self.temporarilyRollbackValuesHandler() messageType = DSMessageType.VoteCommit if self.flipParticipantAcknowledge == True: if messageType == DSMessageType.VoteCommit: messageType = DSMessageType.VoteAbort else: messageType = DSMessageType.VoteCommit if messageType == DSMessageType.VoteCommit: self.State = DSParticipantOperationStatus.READY else: self.Abort() coordinatorProcess = dsProcessManager.GetCoordinator() coordinatorProcess.DSSocket.SendMessage( DSMessage(messageType, self.CoordinatorTransactionId)) if messageType == DSMessageType.VoteCommit: #if we are sending VoteCommit, means that the participant in the ready state # and should wait specific amount of time for the response from coordinatory self.dsTimeout.Run(self.onReadyTimeout)
def freeze(process_id): processId = int(process_id) process = sharedData.GetProcessByID(processId) if process != None: result = process.DSSocket.SendMessage(DSMessage(DSMessageType.Freeze)) click.echo(result) else: click.echo("Process is not found")
def getProcessData(pid): process = dsProcessManager.GetProcessByID(pid) if process == None: return 'the process id is not valid' val = process.DSSocket.SendMessage(DSMessage(DSMessageType.GetData)) if val == 'timeout': val = 'timeout: the process is not responding' return val
def getProcessesData(): result = '' for p in dsProcessManager.DSProcesses: result += p.Id if p.IsCoordinator: result += ' (coordinator)' result += ' : ' + p.DSSocket.SendMessage( DSMessage(DSMessageType.GetData)) + '\n' return result
def ping(process_id): """This is an additional command to check the status of a specific process.""" processId = int(process_id) process = sharedData.GetProcessByID(processId) if process != None: result = process.DSSocket.SendMessage(DSMessage(DSMessageType.Ping)) click.echo(result) else: click.echo("ProcessId is not valid, the process wasn't found.")
def notifyProcessesAboutANewOperation(self): processes = dsProcessManager.GetParticipants() val = ('', '') for p in processes: res = p.DSSocket.SendMessage(DSMessage(DSMessageType.InitRequest, self.Id)) # by passing the Id of the transactin we can follow up the transaction if needed if res == 'timeout': val = ('timeout', p.Id) break return val
def StartElectionFromFirstProcess(sharedData): processes = list( filter(lambda x: not x.isSuspended(), sharedData.BullyProcesses)) processes = BullyProcess.GetSortProcessList(processes) if len(processes) == 0: return firstProc = processes[0] firstProc.DSSocket.SendMessage(DSMessage(DSMessageType.StartElection)) return firstProc.Id
def addProcess(pid): coordinator = dsProcessManager.GetCoordinator() if coordinator == None: return 'coordinator is not available' newProcess = dsProcessManager.GetProcessByID(pid) if newProcess != None: return 'the process id is duplicate' newProcess = DSProcess(pid, False) val = coordinator.DSSocket.SendMessage( DSMessage(DSMessageType.SyncNewProcess, newProcess)) if val == 'timeout': return 'timeout: coordinator is not responding' newProcess.Run() dsProcessManager.AddProcesses([newProcess]) dataStr = newProcess.DSSocket.SendMessage(DSMessage(DSMessageType.GetData)) return 'the process with the id \'' + pid + '\' is added and its data is [' + dataStr + ']'
def setNewValue(value): coordinator = dsProcessManager.GetCoordinator() if coordinator == None: return 'coordinator is not available' val = coordinator.DSSocket.SendMessage( DSMessage(DSMessageType.SetNewValue, value)) if val == 'timeout': val = 'timeout: coordinator is not responding' return val
def rollbackFromPosition(position): coordinator = dsProcessManager.GetCoordinator() if coordinator == None: return 'coordinator is not available' val = coordinator.DSSocket.SendMessage( DSMessage(DSMessageType.RollbackValues, position)) if val == 'timeout': val = 'timeout: coordinator is not responding' return val
def sendVoteRequest(self): processes = dsProcessManager.GetParticipants() self.ProcessesVoteRequestIsSentTo = processes val = ('', '') for p in processes: res = p.DSSocket.SendMessage(DSMessage(DSMessageType.VoteRequest, self.Operation, self.Id)) if res == 'timeout': val = ('timeout', p.Id) break return val
def applyArbitraryFailure(pid, timeout): process = dsProcessManager.GetProcessByID(pid) if process == None: return 'the process id is not valid' val = process.DSSocket.SendMessage( DSMessage(DSMessageType.ArbitraryFailure, int(timeout))) if val == 'timeout': val = 'timeout: the process is not responding' return val
def kill(process_id): processId = int(process_id) process = sharedData.GetProcessByID(processId) if process != None: result = process.DSSocket.SendMessage(DSMessage(DSMessageType.Kill)) if result == None: click.echo("The process is killed now.") else: click.echo("The process is killed now - the process with ID '" + str(result) + "' started election.") else: click.echo("Process is not found")
def startElection(self): nextProcessId = self.getNextProcessID() if nextProcessId == -1: self.notifyProcessesAboutNewCoordinator() else: processes = list( filter(lambda x: x.Id == nextProcessId, self.sharedData.BullyProcesses)) processes = BullyProcess.GetSortProcessList(processes) nextProc = processes[0] msg = DSMessage(DSMessageType.StartElection) nextProc.DSSocket.SendMessage(msg)
def ClockCommandHandler(self, dsMessage): self.updateClock() desc = self.Name + "_" + str( self.getParticipationCounterAsString()) + ", " + self.Clock + "\n" processes = list( filter(lambda x: x.Id > self.Id, self.sharedData.BullyProcesses)) processesLength = len(processes) if processesLength != 0: processes = BullyProcess.GetSortProcessList(processes) nextProc = processes[0] msg = DSMessage(DSMessageType.Clock) result = nextProc.DSSocket.SendMessage(msg) desc += result return desc
def ListCommandHandler(self, dsMessage): desc = str(self.Id) + ", " + self.Name + "_" + str( self.getParticipationCounterAsString()) if self.Id == self.CoordinatorProcessId: desc += " (Coordinator)" desc += "\n" processes = list( filter(lambda x: x.Id > self.Id, self.sharedData.BullyProcesses)) processesLength = len(processes) if processesLength != 0: processes = BullyProcess.GetSortProcessList(processes) nextProc = processes[0] msg = DSMessage(DSMessageType.List) result = nextProc.DSSocket.SendMessage(msg) desc += result return desc
def onReadyTimeout(self): # in the case of timeout when the participant is in the ready state, we should contact # other participants to find their state. we are deciding as follow based on the other prticipant's state: # if other participant's State is INIT => this participant will be ABORT # if other participant's State is ABORT => this participant will be ABORT # if other participant's State is PRECOMMIT => this participant will be COMMIT # if other participant's State is COMMIT => this participant will be COMMIT # if other participant's State is READY => we will reach another participant, if all others are in ready state, # we need to wait for the coordinator to be recovered if self.State == DSParticipantOperationStatus.READY: processes = dsProcessManager.GetParticipants() for p in processes: if p.Id != self.DSProcess.Id: state = p.DSSocket.SendMessage(DSMessage(DSMessageType.GetParticipantState, self.CoordinatorTransactionId)) if state == DSParticipantOperationStatus.INIT or state == DSParticipantOperationStatus.ABORT: self.Abort() elif state == DSParticipantOperationStatus.COMMIT or state == DSParticipantOperationStatus.PRECOMMIT: self.Commit()
def getNextProcessID(self): NextProcessId = -1 processes = list( filter(lambda x: x.Id > self.Id and not x.isSuspended(), self.sharedData.BullyProcesses)) processesLength = len(processes) if processesLength != 0: processIds = [] for index, process in enumerate(processes): msg = DSMessage(DSMessageType.Nudge) result = process.DSSocket.SendMessage(msg) if result: processId = int(result) if processId: processIds.append(processId) if len(processIds) != 0: processIds.sort() NextProcessId = processIds[0] return NextProcessId
def list(): processes = BullyProcess.GetSortProcessList(sharedData.BullyProcesses) firstProc = processes[0] msg = DSMessage(DSMessageType.List) result = firstProc.DSSocket.SendMessage(msg) click.echo(result)
def PreCommit(self): self.State = DSParticipantOperationStatus.PRECOMMIT coordinatorProcess = dsProcessManager.GetCoordinator() coordinatorProcess.DSSocket.SendMessage(DSMessage(DSMessageType.PreCommitAcknowledge, self.CoordinatorTransactionId))
def getCoordinatorClock(self): process = BullyProcess.GetCoordinator(self.sharedData) return process.DSSocket.SendMessage(DSMessage(DSMessageType.GetClock))
def notifyProcessesAboutNewCoordinator(self): self.CoordinatorProcessId = self.Id for index, process in enumerate(self.sharedData.BullyProcesses): if process.Id != self.Id: msg = DSMessage(DSMessageType.NewCoordinator, self.Id) process.DSSocket.SendMessage(msg)
def resetClocks(self): for process in self.sharedData.BullyProcesses: process.DSSocket.SendMessage(DSMessage(DSMessageType.ResetClock))
def sendGlobalAbort(self): processes = dsProcessManager.GetParticipants() for p in processes: p.DSSocket.SendMessage(DSMessage(DSMessageType.GlobalAbort, self.Id))
def sendPreCommit(self): processes = dsProcessManager.GetParticipants() for p in processes: p.DSSocket.SendMessage(DSMessage(DSMessageType.PreCommit, self.Id))