Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import time |
| 2 | import traceback |
| 3 | import commands |
| 4 | import threading |
| 5 | import json |
| 6 | |
| 7 | from datetime import datetime |
| 8 | from collections import defaultdict |
| 9 | from core.models import * |
| 10 | from django.db.models import F, Q |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 11 | #from openstack.manager import OpenStackManager |
| 12 | from openstack.driver import OpenStackDriver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 13 | from util.logger import Logger, logging, logger |
| 14 | #from timeout import timeout |
Sapan Bhatia | 757e0b6 | 2013-09-02 16:55:00 -0400 | [diff] [blame] | 15 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 16 | from observer.steps import * |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 17 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 18 | debug_mode = False |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 19 | |
| 20 | logger = Logger(logfile='observer.log', level=logging.INFO) |
| 21 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 22 | class StepNotReady(Exception): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 23 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 24 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 25 | def toposort(g, steps): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 26 | reverse = {} |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 27 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 28 | for k,v in g.items(): |
| 29 | for rk in v: |
| 30 | try: |
| 31 | reverse[rk].append(k) |
| 32 | except: |
| 33 | reverse[rk]=k |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 34 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 35 | sources = [] |
| 36 | for k,v in g.items(): |
| 37 | if not reverse.has_key(k): |
| 38 | sources.append(k) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 39 | |
| 40 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 41 | for k,v in reverse.iteritems(): |
| 42 | if (not v): |
| 43 | sources.append(k) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 44 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 45 | order = [] |
| 46 | marked = [] |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 47 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 48 | while sources: |
| 49 | n = sources.pop() |
| 50 | try: |
| 51 | for m in g[n]: |
| 52 | if m not in marked: |
| 53 | sources.append(m) |
| 54 | marked.append(m) |
| 55 | except KeyError: |
| 56 | pass |
| 57 | order.append(n) |
Sapan Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame^] | 58 | |
| 59 | order.extend(set(steps)-set(order)) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 60 | return order |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 61 | |
| 62 | class PlanetStackObserver: |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 63 | sync_steps = [SyncNetworks,SyncNetworkSlivers,SyncSites,SyncSitePrivileges,SyncSlices,SyncSliceMemberships,SyncSlivers,SyncSliverIps,SyncExternalRoutes,SyncUsers,GarbageCollector] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 64 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 65 | def __init__(self): |
| 66 | # The Condition object that gets signalled by Feefie events |
Tony Mack | 3bf77b0 | 2013-09-25 00:49:50 -0400 | [diff] [blame] | 67 | self.step_lookup = {} |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 68 | self.load_sync_steps() |
| 69 | self.event_cond = threading.Condition() |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 70 | self.driver = OpenStackDriver() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 71 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 72 | def wait_for_event(self, timeout): |
| 73 | self.event_cond.acquire() |
| 74 | self.event_cond.wait(timeout) |
| 75 | self.event_cond.release() |
| 76 | |
| 77 | def wake_up(self): |
| 78 | logger.info('Wake up routine called. Event cond %r'%self.event_cond) |
| 79 | self.event_cond.acquire() |
| 80 | self.event_cond.notify() |
| 81 | self.event_cond.release() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 82 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 83 | def load_sync_steps(self): |
| 84 | dep_path = Config().observer_backend_dependency_graph |
| 85 | try: |
| 86 | # This contains dependencies between records, not sync steps |
| 87 | self.model_dependency_graph = json.loads(open(dep_path).read()) |
| 88 | except Exception,e: |
| 89 | raise e |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 90 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 91 | try: |
Tony Mack | c12d5ca | 2013-09-24 10:30:39 -0400 | [diff] [blame] | 92 | backend_path = Config().observer_pl_dependency_graph |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 93 | # This contains dependencies between backend records |
| 94 | self.backend_dependency_graph = json.loads(open(backend_path).read()) |
| 95 | except Exception,e: |
| 96 | # We can work without a backend graph |
| 97 | self.backend_dependency_graph = {} |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 98 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 99 | provides_dict = {} |
| 100 | for s in self.sync_steps: |
Sapan Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame^] | 101 | self.step_lookup[s.__name__] = s |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 102 | for m in s.provides: |
| 103 | try: |
| 104 | provides_dict[m.__name__].append(s.__name__) |
| 105 | except KeyError: |
| 106 | provides_dict[m.__name__]=[s.__name__] |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 107 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 108 | |
| 109 | step_graph = {} |
| 110 | for k,v in self.model_dependency_graph.iteritems(): |
| 111 | try: |
| 112 | for source in provides_dict[k]: |
| 113 | for m in v: |
| 114 | try: |
| 115 | for dest in provides_dict[m]: |
| 116 | # no deps, pass |
| 117 | try: |
| 118 | step_graph[source].append(dest) |
| 119 | except: |
| 120 | step_graph[source]=[dest] |
| 121 | except KeyError: |
| 122 | pass |
| 123 | |
| 124 | except KeyError: |
| 125 | pass |
| 126 | # no dependencies, pass |
| 127 | |
| 128 | #import pdb |
| 129 | #pdb.set_trace() |
| 130 | if (self.backend_dependency_graph): |
| 131 | backend_dict = {} |
| 132 | for s in self.sync_steps: |
| 133 | for m in s.serves: |
| 134 | backend_dict[m]=s.__name__ |
| 135 | |
| 136 | for k,v in backend_dependency_graph.iteritems(): |
| 137 | try: |
| 138 | source = backend_dict[k] |
| 139 | for m in v: |
| 140 | try: |
| 141 | dest = backend_dict[m] |
| 142 | except KeyError: |
| 143 | # no deps, pass |
| 144 | pass |
| 145 | step_graph[source]=dest |
| 146 | |
| 147 | except KeyError: |
| 148 | pass |
| 149 | # no dependencies, pass |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 150 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 151 | dependency_graph = step_graph |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 152 | |
Sapan Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame^] | 153 | self.ordered_steps = toposort(dependency_graph, map(lambda s:s.__name__,self.sync_steps)) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 154 | print "Order of steps=",self.ordered_steps |
| 155 | self.load_run_times() |
| 156 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 157 | |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 158 | def check_duration(self, step, duration): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 159 | try: |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 160 | if (duration > step.deadline): |
| 161 | logger.info('Sync step %s missed deadline, took %.2f seconds'%(step.name,duration)) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 162 | except AttributeError: |
| 163 | # S doesn't have a deadline |
| 164 | pass |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 165 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 166 | def update_run_time(self, step): |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 167 | self.last_run_times[step.__name__]=time.time() |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 168 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 169 | def check_schedule(self, step): |
Tony Mack | ea41f56 | 2013-09-25 08:10:40 -0400 | [diff] [blame] | 170 | time_since_last_run = time.time() - self.last_run_times[step.__name__] |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 171 | try: |
| 172 | if (time_since_last_run < step.requested_interval): |
| 173 | raise StepNotReady |
| 174 | except AttributeError: |
Tony Mack | ea41f56 | 2013-09-25 08:10:40 -0400 | [diff] [blame] | 175 | logger.info('Step %s does not have requested_interval set'%step.__name__) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 176 | raise StepNotReady |
| 177 | |
| 178 | def load_run_times(self): |
| 179 | try: |
| 180 | jrun_times = open('/tmp/observer_run_times').read() |
| 181 | self.last_run_times = json.loads(jrun_times) |
| 182 | except: |
| 183 | self.last_run_times={} |
| 184 | for e in self.ordered_steps: |
Tony Mack | c12d5ca | 2013-09-24 10:30:39 -0400 | [diff] [blame] | 185 | self.last_run_times[e]=0 |
Sapan Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 186 | |
| 187 | |
| 188 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 189 | def save_run_times(self): |
| 190 | run_times = json.dumps(self.last_run_times) |
| 191 | open('/tmp/observer_run_times','w').write(run_times) |
Sapan Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 192 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 193 | def check_class_dependency(self, step, failed_steps): |
| 194 | for failed_step in failed_steps: |
Tony Mack | 4fa85fb | 2013-09-25 14:39:57 -0400 | [diff] [blame] | 195 | step.dependencies = self.model_dependency_graph.get(step.provides[0].__name__, []) |
| 196 | if (failed_step in step.dependencies): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 197 | raise StepNotReady |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 198 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 199 | def run(self): |
| 200 | if not self.driver.enabled or not self.driver.has_openstack: |
| 201 | return |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 202 | while True: |
| 203 | try: |
| 204 | logger.info('Waiting for event') |
| 205 | tBeforeWait = time.time() |
Sapan Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame^] | 206 | self.wait_for_event(timeout=30) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 207 | logger.info('Observer woke up') |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 208 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 209 | # Set of whole steps that failed |
| 210 | failed_steps = [] |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 211 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 212 | # Set of individual objects within steps that failed |
| 213 | failed_step_objects = [] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 214 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 215 | for S in self.ordered_steps: |
Tony Mack | 3bf77b0 | 2013-09-25 00:49:50 -0400 | [diff] [blame] | 216 | step = self.step_lookup[S] |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 217 | start_time=time.time() |
| 218 | |
Tony Mack | 3bf77b0 | 2013-09-25 00:49:50 -0400 | [diff] [blame] | 219 | sync_step = step(driver=self.driver) |
Sapan Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame^] | 220 | sync_step.__name__ = step.__name__ |
Tony Mack | 3bf77b0 | 2013-09-25 00:49:50 -0400 | [diff] [blame] | 221 | #sync_step.dependencies = self.dependencies[sync_step.name] |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 222 | sync_step.debug_mode = debug_mode |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 223 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 224 | should_run = False |
| 225 | try: |
| 226 | # Various checks that decide whether |
| 227 | # this step runs or not |
| 228 | self.check_class_dependency(sync_step, failed_steps) # dont run Slices if Sites failed |
| 229 | self.check_schedule(sync_step) # dont run sync_network_routes if time since last run < 1 hour |
| 230 | should_run = True |
| 231 | except StepNotReady: |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 232 | logging.info('Step not ready: %s'%sync_step.__name__) |
Tony Mack | 3bf77b0 | 2013-09-25 00:49:50 -0400 | [diff] [blame] | 233 | failed_steps.append(sync_step) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 234 | except: |
Tony Mack | 3bf77b0 | 2013-09-25 00:49:50 -0400 | [diff] [blame] | 235 | failed_steps.append(sync_step) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 236 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 237 | if (should_run): |
| 238 | try: |
| 239 | duration=time.time() - start_time |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 240 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 241 | # ********* This is the actual sync step |
| 242 | failed_objects = sync_step(failed=failed_step_objects) |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 243 | |
| 244 | |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 245 | self.check_duration(sync_step, duration) |
| 246 | if failed_objects: |
| 247 | failed_step_objects.extend(failed_objects) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 248 | self.update_run_time(sync_step) |
| 249 | except: |
Sapan Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame^] | 250 | raise |
Tony Mack | ea41f56 | 2013-09-25 08:10:40 -0400 | [diff] [blame] | 251 | failed_steps.append(S) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 252 | self.save_run_times() |
| 253 | except: |
| 254 | logger.log_exc("Exception in observer run loop") |
| 255 | traceback.print_exc() |