Skip to content

Commit

Permalink
Type annotations fixed (#200)
Browse files Browse the repository at this point in the history
* Add type annotations

* fix line endings

* fix type annotations
  • Loading branch information
CCCodes authored Jan 10, 2021
1 parent 3b8f8c9 commit be3ea1d
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 111 deletions.
10 changes: 5 additions & 5 deletions jobManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class JobManager(object):

def __init__(self, queue):
def __init__(self, queue: JobQueue) -> None:
self.daemon = True
self.jobQueue = queue
self.preallocator = self.jobQueue.preallocator
Expand All @@ -37,19 +37,19 @@ def __init__(self, queue):
self.nextId = 10000
self.running = False

def start(self):
def start(self) -> None:
if self.running:
return
thread = threading.Thread(target=self.__manage)
thread.daemon = True
thread.start()

def run(self):
def run(self) -> None:
if self.running:
return
self.__manage()

def _getNextID(self):
def _getNextID(self) -> int:
""" _getNextID - returns next ID to be used for a job-associated
VM. Job-associated VM's have 5-digit ID numbers between 10000
and 99999.
Expand All @@ -60,7 +60,7 @@ def _getNextID(self):
self.nextId = 10000
return id

def __manage(self):
def __manage(self) -> None:
self.running = True
while True:
# Blocks until we get a next job
Expand Down
30 changes: 16 additions & 14 deletions jobQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
from builtins import object
from builtins import str
import threading, logging, time
from typing import Optional, Union

from datetime import datetime
from tangoObjects import TangoDictionary, TangoJob, TangoQueue
from preallocator import Preallocator
from config import Config

#
Expand All @@ -33,7 +35,7 @@

class JobQueue(object):

def __init__(self, preallocator):
def __init__(self, preallocator: Preallocator) -> None:
"""
Here we maintain several data structures used to keep track of the
jobs present for the autograder.
Expand Down Expand Up @@ -63,7 +65,7 @@ def __init__(self, preallocator):
self.log = logging.getLogger("JobQueue")
self.nextID = 1

def _getNextID(self):
def _getNextID(self) -> int:
"""_getNextID - updates and returns the next ID to be used for a job
Jobs have ID's between 1 and MAX_JOBID.
"""
Expand Down Expand Up @@ -94,7 +96,7 @@ def _getNextID(self):
self.log.debug("_getNextID|Released lock to job queue.")
return id

def remove(self, id):
def remove(self, id: str) -> int:
"""remove - Remove job from live queue
"""
status = -1
Expand All @@ -115,7 +117,7 @@ def remove(self, id):
self.log.error("Job %s not found in queue" % id)
return status

def add(self, job):
def add(self, job: TangoJob) -> Union[int, str]:
"""add - add job to live queue
This function assigns an ID number to a *new* job and then adds it
to the queue of live jobs.
Expand Down Expand Up @@ -168,7 +170,7 @@ def add(self, job):

return str(job.id)

def addDead(self, job):
def addDead(self, job: TangoJob) -> int:
""" addDead - add a job to the dead queue.
Called by validateJob when a job validation fails.
Returns -1 on failure and the job id on success
Expand Down Expand Up @@ -200,7 +202,7 @@ def addDead(self, job):

return job.id

def delJob(self, id, deadjob):
def delJob(self, id: int, deadjob) -> int:
""" delJob - Implements delJob() interface call
@param id - The id of the job to remove
@param deadjob - If 0, move the job from the live queue to the
Expand All @@ -225,7 +227,7 @@ def delJob(self, id, deadjob):
self.log.error("Job %s not found in dead queue" % id)
return status

def get(self, id):
def get(self, id: str) -> Optional[TangoJob]:
"""get - retrieve job from live queue
@param id - the id of the job to retrieve
"""
Expand All @@ -236,7 +238,7 @@ def get(self, id):
self.log.debug("get| Released lock to job queue.")
return job

def assignJob(self, jobId):
def assignJob(self, jobId: str) -> None:
""" assignJob - marks a job to be assigned
"""
self.queueLock.acquire()
Expand All @@ -255,7 +257,7 @@ def assignJob(self, jobId):
self.queueLock.release()
self.log.debug("assignJob| Released lock to job queue.")

def unassignJob(self, jobId):
def unassignJob(self, jobId: str) -> None:
""" unassignJob - marks a job to be unassigned
Note: We assume here that a job is to be rescheduled or
'retried' when you unassign it. This retry is done by
Expand Down Expand Up @@ -285,7 +287,7 @@ def unassignJob(self, jobId):
self.queueLock.release()
self.log.debug("unassignJob| Released lock to job queue.")

def makeDead(self, id, reason):
def makeDead(self, id: int, reason: str) -> int:
""" makeDead - move a job from live queue to dead queue
"""
self.log.info("makeDead| Making dead job ID: " + str(id))
Expand Down Expand Up @@ -313,7 +315,7 @@ def makeDead(self, id, reason):
self.log.debug("makeDead| Released lock to job queue.")
return status

def getInfo(self):
def getInfo(self) -> dict:

info = {}
info['size'] = len(self.liveJobs.keys())
Expand All @@ -322,7 +324,7 @@ def getInfo(self):

return info

def reset(self):
def reset(self) -> None:
""" reset - resets and clears all the internal dictionaries
and queues
"""
Expand All @@ -331,7 +333,7 @@ def reset(self):
self.unassignedJobs._clean()


def getNextPendingJob(self):
def getNextPendingJob(self) -> TangoJob:
"""Gets the next unassigned live job. Note that this is a
blocking function and we will block till there is an available
job.
Expand All @@ -353,7 +355,7 @@ def getNextPendingJob(self):
self.log.debug("getNextPendingJob| Released lock to job queue.")
return job

def reuseVM(self, job):
def reuseVM(self, job: TangoJob) -> Optional['vm']:
"""Helps a job reuse a vm. This is called if CONFIG.REUSE_VM is
set to true.
"""
Expand Down
32 changes: 16 additions & 16 deletions preallocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from builtins import object
from builtins import range
import threading, logging, time, copy
from typing import Optional, List, Dict, Any

from tangoObjects import TangoDictionary, TangoQueue, TangoIntValue
from config import Config
Expand All @@ -21,22 +22,22 @@

class Preallocator(object):

def __init__(self, vmms):
def __init__(self, vmms: List[str]) -> None:
self.machines = TangoDictionary("machines")
self.lock = threading.Lock()
self.nextID = TangoIntValue("nextID", 1000)
self.vmms = vmms
self.log = logging.getLogger("Preallocator")

def poolSize(self, vmName):
def poolSize(self, vmName: str) -> int:
""" poolSize - returns the size of the vmName pool, for external callers
"""
if vmName not in self.machines:
return 0
else:
return len(self.machines.get(vmName)[0])

def update(self, vm, num):
def update(self, vm, num: int) -> None:
""" update - Updates the number of machines of a certain type
to be preallocated.
Expand Down Expand Up @@ -68,7 +69,7 @@ def update(self, vm, num):

# If delta == 0 then we are the perfect number!

def allocVM(self, vmName):
def allocVM(self, vmName: str):
""" allocVM - Allocate a VM from the free list
"""
vm = None
Expand All @@ -83,10 +84,9 @@ def allocVM(self, vmName):
# If we're not reusing instances, then crank up a replacement
if vm and not Config.REUSE_VMS:
threading.Thread(target=self.__create(vm, 1)).start()

return vm

def freeVM(self, vm):
def freeVM(self, vm) -> None:
""" freeVM - Returns a VM instance to the free list
"""
# Sanity check: Return a VM to the free list only if it is
Expand All @@ -106,7 +106,7 @@ def freeVM(self, vm):
vmms = self.vmms[vm.vmms]
vmms.safeDestroyVM(vm)

def addVM(self, vm):
def addVM(self, vm) -> None:
""" addVM - add a particular VM instance to the pool
"""
self.lock.acquire()
Expand All @@ -115,7 +115,7 @@ def addVM(self, vm):
self.machines.set(vm.name, machine)
self.lock.release()

def removeVM(self, vm):
def removeVM(self, vm) -> None:
""" removeVM - remove a particular VM instance from the pool
"""
self.lock.acquire()
Expand All @@ -124,7 +124,7 @@ def removeVM(self, vm):
self.machines.set(vm.name, machine)
self.lock.release()

def _getNextID(self):
def _getNextID(self) -> int:
""" _getNextID - returns next ID to be used for a preallocated
VM. Preallocated VM's have 4-digit ID numbers between 1000
and 9999.
Expand All @@ -140,7 +140,7 @@ def _getNextID(self):
self.lock.release()
return id

def __create(self, vm, cnt):
def __create(self, vm, cnt: int) -> None:
""" __create - Creates count VMs and adds them to the pool
This function should always be called in a thread since it
Expand All @@ -161,7 +161,7 @@ def __create(self, vm, cnt):
self.log.debug("__create: Added vm %s to pool %s " %
(newVM.id, newVM.name))

def __destroy(self, vm):
def __destroy(self, vm) -> None:
""" __destroy - Removes a VM from the pool
If the user asks for fewer preallocated VMs, then we will
Expand All @@ -179,7 +179,7 @@ def __destroy(self, vm):
vmms = self.vmms[vm.vmms]
vmms.safeDestroyVM(dieVM)

def createVM(self, vm):
def createVM(self, vm) -> None:
""" createVM - Called in non-thread context to create a single
VM and add it to the pool
"""
Expand All @@ -197,7 +197,7 @@ def createVM(self, vm):
self.log.debug("createVM: Added vm %s to pool %s" %
(newVM.id, newVM.name))

def destroyVM(self, vmName, id):
def destroyVM(self, vmName: str, id: int) -> int:
""" destroyVM - Called by the delVM API function to remove and
destroy a particular VM instance from a pool. We only allow
this function when the system is queiscent (pool size == free
Expand Down Expand Up @@ -226,16 +226,16 @@ def destroyVM(self, vmName, id):
else:
return -1

def getAllPools(self):
def getAllPools(self) -> Dict[str, Dict[str, Any]]:
result = {}
for vmName in self.machines:
result[vmName] = self.getPool(vmName)
return result

def getPool(self, vmName):
def getPool(self, vmName: str) -> Dict[str, Any]:
""" getPool - returns the members of a pool and its free list
"""
result = {}
result = {} # Dict[str, List[Any]]
if vmName not in self.machines:
return result

Expand Down
Loading

0 comments on commit be3ea1d

Please sign in to comment.