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