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