You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 1 | """ |
| 2 | This file contains the event scheduler class for CHOTestMonkey |
| 3 | Author: you@onlab.us |
| 4 | """ |
| 5 | from threading import Lock, Condition |
| 6 | from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event |
| 7 | from tests.CHOTestMonkey.dependencies.events.TestEvent import * |
| 8 | from tests.CHOTestMonkey.dependencies.events.CheckEvent import * |
| 9 | from tests.CHOTestMonkey.dependencies.events.NetworkEvent import * |
| 10 | from tests.CHOTestMonkey.dependencies.events.AppEvent import * |
| 11 | from tests.CHOTestMonkey.dependencies.events.ONOSEvent import * |
| 12 | |
| 13 | class EventScheduleMethod: |
| 14 | def __init__( self ): |
| 15 | self.map = {} |
| 16 | self.RUN_NON_BLOCK = 1 |
| 17 | self.map[ 1 ] = 'RUN_NON_BLOCK' |
| 18 | self.RUN_BLOCK = -1 |
| 19 | self.map[ -1 ] = 'RUN_BLOCK' |
| 20 | |
| 21 | class EventTuple: |
| 22 | def __init__( self, id, className, typeString, typeIndex, scheduleMethod, args, rerunInterval, maxRerunNum ): |
| 23 | self.default = '' |
| 24 | self.id = 0 |
| 25 | self.className = className |
| 26 | self.typeString = typeString |
| 27 | self.typeIndex = typeIndex |
| 28 | self.scheduleMethod = scheduleMethod |
| 29 | self.args = args |
| 30 | self.rerunInterval = rerunInterval |
| 31 | self.maxRerunNum = maxRerunNum |
| 32 | |
| 33 | def startEvent( self ): |
| 34 | assert self.className in globals().keys() |
| 35 | event = globals()[ self.className ] |
| 36 | return event().startEvent( self.args ) |
| 37 | |
| 38 | class EventScheduler: |
| 39 | def __init__( self ): |
| 40 | self.default = '' |
| 41 | self.pendingEvents = [] |
| 42 | self.pendingEventsCondition = Condition() |
| 43 | self.runningEvents = [] |
| 44 | self.runningEventsCondition = Condition() |
| 45 | self.isRunning = True |
| 46 | self.idleCondition = Condition() |
| 47 | self.pendingEventsCapacity = int( main.params[ 'SCHEDULER' ][ 'pendingEventsCapacity' ] ) |
| 48 | self.runningEventsCapacity = int( main.params[ 'SCHEDULER' ][ 'runningEventsCapacity' ] ) |
| 49 | self.scheduleLoopSleep = float( main.params[ 'SCHEDULER' ][ 'scheduleLoopSleep' ] ) |
| 50 | |
| 51 | def scheduleEvent( self, typeIndex, scheduleMethod, args=None, index=-1 ): |
| 52 | """ |
| 53 | Insert an event to pendingEvents |
| 54 | param: |
| 55 | index: the position to insert into pendingEvents, default value -1 implies the tail of pendingEvents |
| 56 | """ |
| 57 | if not typeIndex in main.enabledEvents.keys(): |
| 58 | main.log.warn( "Event Scheduler - event type %s not enabled" % ( typeIndex ) ) |
| 59 | return |
| 60 | if main.enabledEvents[ typeIndex ] in main.params[ 'EVENT' ].keys(): |
| 61 | if 'rerunInterval' in main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ].keys(): |
| 62 | rerunInterval = int( main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ][ 'rerunInterval' ] ) |
| 63 | maxRerunNum = int( main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ][ 'maxRerunNum' ] ) |
| 64 | else: |
| 65 | rerunInterval = int( main.params[ 'EVENT' ][ 'Event' ][ 'rerunInterval' ] ) |
| 66 | maxRerunNum = int( main.params[ 'EVENT' ][ 'Event' ][ 'maxRerunNum' ] ) |
| 67 | eventTuple = EventTuple( main.eventID, main.enabledEvents[ typeIndex ], EventType().map[ typeIndex ], typeIndex, scheduleMethod, args, rerunInterval, maxRerunNum ) |
| 68 | with main.variableLock: |
| 69 | main.eventID += 1 |
| 70 | main.log.debug( "Event Scheduler - Event added: %s, %s, %s" % ( typeIndex, |
| 71 | scheduleMethod, |
| 72 | args ) ) |
| 73 | with self.pendingEventsCondition: |
| 74 | if index == -1: |
| 75 | self.pendingEvents.append( eventTuple ) |
| 76 | elif index > -1 and index <= len( self.pendingEvents ): |
| 77 | self.pendingEvents.insert( index, eventTuple ) |
| 78 | else: |
| 79 | main.log.warn( "Event Scheduler - invalid index when isnerting event: %s" % ( index ) ) |
| 80 | self.pendingEventsCondition.notify() |
You Wang | 3ce74fd | 2016-07-14 09:34:30 -0700 | [diff] [blame] | 81 | #self.printEvents() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 82 | |
| 83 | def startScheduler( self ): |
| 84 | """ |
| 85 | Start the loop which schedules the events in pendingEvents |
| 86 | """ |
| 87 | import time |
| 88 | |
| 89 | while 1: |
| 90 | with self.pendingEventsCondition: |
| 91 | while len( self.pendingEvents ) == 0: |
| 92 | self.pendingEventsCondition.wait() |
| 93 | eventTuple = self.pendingEvents[ 0 ] |
| 94 | main.log.debug( "Event Scheduler - Scheduling event: %s, %s, %s" % ( eventTuple.typeIndex, |
| 95 | eventTuple.scheduleMethod, |
| 96 | eventTuple.args ) ) |
| 97 | if eventTuple.scheduleMethod == EventScheduleMethod().RUN_NON_BLOCK: |
| 98 | # Run NON_BLOCK events using threads |
| 99 | with self.pendingEventsCondition: |
| 100 | self.pendingEvents.remove( eventTuple ) |
| 101 | t = main.Thread( target=self.startEvent, |
| 102 | threadID=main.threadID, |
| 103 | name="startEvent", |
| 104 | args=[ eventTuple ]) |
| 105 | t.start() |
| 106 | with main.variableLock: |
| 107 | main.threadID += 1 |
| 108 | elif eventTuple.scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 109 | # Wait for all other events before start |
| 110 | with self.runningEventsCondition: |
| 111 | while not len( self.runningEvents ) == 0: |
| 112 | self.runningEventsCondition.wait() |
| 113 | # BLOCK events will temporarily block the following events until finish running |
| 114 | with self.pendingEventsCondition: |
| 115 | self.pendingEvents.remove( eventTuple ) |
| 116 | self.startEvent( eventTuple ) |
| 117 | else: |
| 118 | with self.pendingEventsCondition: |
| 119 | self.pendingEvents.remove( eventTuple ) |
| 120 | time.sleep( self.scheduleLoopSleep ) |
| 121 | |
| 122 | def startEvent( self, eventTuple ): |
| 123 | """ |
| 124 | Start a network/ONOS/application event |
| 125 | """ |
| 126 | import time |
| 127 | |
| 128 | with self.runningEventsCondition: |
| 129 | self.runningEvents.append( eventTuple ) |
You Wang | 3ce74fd | 2016-07-14 09:34:30 -0700 | [diff] [blame] | 130 | #self.printEvents() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 131 | rerunNum = 0 |
| 132 | result = eventTuple.startEvent() |
| 133 | while result == EventStates().FAIL and rerunNum < eventTuple.maxRerunNum: |
| 134 | time.sleep( eventTuple.rerunInterval ) |
| 135 | rerunNum += 1 |
| 136 | main.log.debug( eventTuple.typeString + ": retry number " + str( rerunNum ) ) |
| 137 | result = eventTuple.startEvent() |
| 138 | if result == EventStates().FAIL: |
| 139 | main.log.error( eventTuple.typeString + " failed" ) |
| 140 | main.caseResult = main.FALSE |
| 141 | if main.params[ 'TEST' ][ 'pauseTest' ] == 'on': |
| 142 | #self.isRunning = False |
| 143 | #main.log.error( "Event Scheduler - Test paused. To resume test, run \'resume-test\' command in CLI debugging mode" ) |
| 144 | main.stop() |
| 145 | with self.runningEventsCondition: |
| 146 | self.runningEvents.remove( eventTuple ) |
| 147 | if len( self.runningEvents ) == 0: |
| 148 | self.runningEventsCondition.notify() |
| 149 | with self.pendingEventsCondition: |
| 150 | if len( self.pendingEvents ) == 0: |
| 151 | with self.idleCondition: |
| 152 | self.idleCondition.notify() |
You Wang | 3ce74fd | 2016-07-14 09:34:30 -0700 | [diff] [blame] | 153 | #self.printEvents() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 154 | |
| 155 | def printEvents( self ): |
| 156 | """ |
| 157 | Print all the events in pendingEvents and runningEvents |
| 158 | """ |
| 159 | events = " [" |
| 160 | with self.runningEventsCondition: |
| 161 | for index in range( 0, len( self.runningEvents ) - 1 ): |
| 162 | events += str( self.runningEvents[ index ].typeIndex ) |
| 163 | events += ", " |
| 164 | if len( self.runningEvents ) > 0: |
| 165 | events += str( self.runningEvents[ -1 ].typeIndex ) |
| 166 | events += "]" |
| 167 | events += " [" |
| 168 | with self.pendingEventsCondition: |
| 169 | for index in range( 0, len( self.pendingEvents ) - 1 ): |
| 170 | events += str( self.pendingEvents[ index ].typeIndex ) |
| 171 | events += ", " |
| 172 | if len( self.pendingEvents ) > 0: |
| 173 | events += str( self.pendingEvents[ -1 ].typeIndex ) |
| 174 | events += "]" |
You Wang | 3ce74fd | 2016-07-14 09:34:30 -0700 | [diff] [blame] | 175 | main.log.debug( "Event Scheduler - Events: " + events ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 176 | |
| 177 | def isAvailable( self ): |
You Wang | 3ce74fd | 2016-07-14 09:34:30 -0700 | [diff] [blame] | 178 | with self.runningEventsCondition: |
| 179 | with self.pendingEventsCondition: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 180 | return len( self.pendingEvents ) < self.pendingEventsCapacity and\ |
| 181 | len( self.runningEvents ) < self.runningEventsCapacity and\ |
| 182 | self.isRunning |
| 183 | |
| 184 | def isIdle( self ): |
You Wang | 3ce74fd | 2016-07-14 09:34:30 -0700 | [diff] [blame] | 185 | with self.runningEventsCondition: |
| 186 | with self.pendingEventsCondition: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 187 | return len( self.pendingEvents ) == 0 and\ |
| 188 | len( self.runningEvents ) == 0 and\ |
| 189 | self.isRunning |
| 190 | |
| 191 | def setPendingEventsCapacity( self, capacity ): |
| 192 | self.pendingEventsCapacity = capacity |
| 193 | |
| 194 | def setRunningState( self, state ): |
| 195 | assert state == True or state == False |
| 196 | self.isRunning = state |
| 197 | |