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