blob: 9ec79cccb4f79b30d1e488a18be0242c32137cf5 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mack4fa85fb2013-09-25 14:39:57 -04003from datetime import datetime
Scott Baker76a840e2015-02-11 21:38:09 -08004from xos.config import Config
Andy Baviere7abb622013-10-18 15:11:56 -04005from util.logger import Logger, logging
Sapan Bhatiaeba08432014-04-28 23:58:36 -04006from observer.steps import *
Sapan Bhatiad9468eb2014-08-20 03:03:12 -04007from django.db.models import F, Q
Scott Baker8bbc77c2015-06-22 10:56:16 -07008from core.models import *
9from django.db import reset_queries
Sapan Bhatia47006112015-01-29 20:55:40 +000010import json
11import time
12import pdb
Andy Baviere7abb622013-10-18 15:11:56 -040013
Andy Bavier04111b72013-10-22 16:47:10 -040014logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040015
Sapan Bhatia2192fec2015-02-08 06:36:32 +000016def f7(seq):
17 seen = set()
18 seen_add = seen.add
19 return [ x for x in seq if not (x in seen or seen_add(x))]
20
21def elim_dups(backend_str):
22 strs = backend_str.split(' // ')
23 strs2 = f7(strs)
24 return ' // '.join(strs2)
Sapan Bhatiae6376de2015-05-13 15:51:03 +020025
Sapan Bhatia709bebd2015-02-08 06:35:36 +000026def deepgetattr(obj, attr):
27 return reduce(getattr, attr.split('.'), obj)
28
Sapan Bhatiae6376de2015-05-13 15:51:03 +020029
30class InnocuousException(Exception):
31 pass
32
Sapan Bhatia13c7f112013-09-02 14:19:35 -040033class FailedDependency(Exception):
Tony Mackce79de02013-09-24 10:12:33 -040034 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -040035
Tony Mackb469d242015-01-03 19:37:39 -050036class SyncStep(object):
Sapan Bhatiae6376de2015-05-13 15:51:03 +020037 """ An XOS Sync step.
Sapan Bhatia24836f12013-08-27 10:16:05 -040038
Tony Mackce79de02013-09-24 10:12:33 -040039 Attributes:
Sapan Bhatiae6376de2015-05-13 15:51:03 +020040 psmodel Model name the step synchronizes
Tony Mackce79de02013-09-24 10:12:33 -040041 dependencies list of names of models that must be synchronized first if the current model depends on them
Sapan Bhatiae6376de2015-05-13 15:51:03 +020042 """
Tony Mackce79de02013-09-24 10:12:33 -040043 slow=False
Tony Mackfbdae1b2015-02-24 14:16:43 -050044 def get_prop(self, prop):
Tony Mackce79de02013-09-24 10:12:33 -040045 try:
46 sync_config_dir = Config().sync_config_dir
47 except:
Scott Bakerb8059c92015-02-19 22:25:49 -080048 sync_config_dir = '/etc/xos/sync'
Tony Mackce79de02013-09-24 10:12:33 -040049 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
50 return open(prop_config_path).read().rstrip()
Sapan Bhatia24836f12013-08-27 10:16:05 -040051
Tony Mackce79de02013-09-24 10:12:33 -040052 def __init__(self, **args):
53 """Initialize a sync step
54 Keyword arguments:
55 name -- Name of the step
Scott Baker286a78f2015-02-18 16:13:48 -080056 provides -- XOS models sync'd by this step
Tony Mackce79de02013-09-24 10:12:33 -040057 """
58 dependencies = []
Tony Mack387a73f2013-09-18 07:59:14 -040059 self.driver = args.get('driver')
Sapan Bhatiaeba08432014-04-28 23:58:36 -040060 self.error_map = args.get('error_map')
61
Tony Mackce79de02013-09-24 10:12:33 -040062 try:
63 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
64 except:
65 self.soft_deadline = 5 # 5 seconds
Sapan Bhatia24836f12013-08-27 10:16:05 -040066
Tony Mackce79de02013-09-24 10:12:33 -040067 return
Sapan Bhatia24836f12013-08-27 10:16:05 -040068
Sapan Bhatiae17bc5b2014-04-30 00:53:06 -040069 def fetch_pending(self, deletion=False):
Sapan Bhatia21765662014-07-23 08:59:30 -040070 # This is the most common implementation of fetch_pending
71 # Steps should override it if they have their own logic
72 # for figuring out what objects are outstanding.
Sapan Bhatia99f49682015-01-29 20:58:25 +000073 main_obj = self.observes
Sapan Bhatiad9468eb2014-08-20 03:03:12 -040074 if (not deletion):
Sapan Bhatiad6e38842015-04-21 17:47:07 -040075 objs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False))
Sapan Bhatia21765662014-07-23 08:59:30 -040076 else:
77 objs = main_obj.deleted_objects.all()
78
79 return objs
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040080 #return Sliver.objects.filter(ip=None)
Sapan Bhatiae6376de2015-05-13 15:51:03 +020081
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040082 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -040083 for dep in self.dependencies:
Scott Baker105b6b72014-05-12 10:40:25 -070084 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
Sapan Bhatiae6376de2015-05-13 15:51:03 +020085
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -040086 try:
Sapan Bhatia709bebd2015-02-08 06:35:36 +000087 peer_object = deepgetattr(obj, peer_name)
Sapan Bhatiae6376de2015-05-13 15:51:03 +020088 try:
89 peer_objects = peer_object.all()
Sapan Bhatia709bebd2015-02-08 06:35:36 +000090 except AttributeError:
Sapan Bhatiae6376de2015-05-13 15:51:03 +020091 peer_objects = [peer_object]
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -040092 except:
Sapan Bhatia709bebd2015-02-08 06:35:36 +000093 peer_objects = []
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -040094
Sapan Bhatiae6376de2015-05-13 15:51:03 +020095 if (hasattr(obj,'controller')):
96 try:
97 peer_objects = filter(lambda o:o.controller==obj.controller, peer_objects)
98 except AttributeError:
99 pass
100
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000101 if (failed in peer_objects):
102 if (obj.backend_status!=failed.backend_status):
103 obj.backend_status = failed.backend_status
Sapan Bhatia7e482de2014-08-22 03:05:13 -0400104 obj.save(update_fields=['backend_status'])
Scott Baker4fd314e2015-03-04 21:31:14 -0800105 raise FailedDependency("Failed dependency for %s:%s peer %s:%s failed %s:%s" % (obj.__class__.__name__, str(getattr(obj,"pk","no_pk")), peer_object.__class__.__name__, str(getattr(peer_object,"pk","no_pk")), failed.__class__.__name__, str(getattr(failed,"pk","no_pk"))))
Sapan Bhatia24836f12013-08-27 10:16:05 -0400106
Sapan Bhatia60823362014-04-30 00:52:32 -0400107 def call(self, failed=[], deletion=False):
108 pending = self.fetch_pending(deletion)
Tony Mackce79de02013-09-24 10:12:33 -0400109 for o in pending:
Scott Baker8bbc77c2015-06-22 10:56:16 -0700110 # another spot to clean up debug state
111 try:
112 reset_queries()
113 except:
114 # this shouldn't happen, but in case it does, catch it...
115 logger.log_exc("exception in reset_queries")
116
Sapan Bhatia47006112015-01-29 20:55:40 +0000117 sync_failed = False
Tony Mack68e818d2013-09-25 13:34:17 -0400118 try:
Sapan Bhatia24a2a292015-02-10 17:21:33 -0500119 backoff_disabled = Config().observer_backoff_disabled
Sapan Bhatia9cd17be2015-02-10 17:16:07 -0500120 except:
121 backoff_disabled = 0
122
123 try:
Sapan Bhatia47006112015-01-29 20:55:40 +0000124 scratchpad = json.loads(o.backend_register)
125 if (scratchpad):
126 next_run = scratchpad['next_run']
Sapan Bhatia9cd17be2015-02-10 17:16:07 -0500127 if (not backoff_disabled and next_run>time.time()):
Sapan Bhatia47006112015-01-29 20:55:40 +0000128 sync_failed = True
Sapan Bhatia47006112015-01-29 20:55:40 +0000129 except:
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200130 logger.log_exc("Exception while loading scratchpad")
Sapan Bhatia47006112015-01-29 20:55:40 +0000131 pass
132
133 if (not sync_failed):
Sapan Bhatiaeba08432014-04-28 23:58:36 -0400134 try:
Sapan Bhatia47006112015-01-29 20:55:40 +0000135 for f in failed:
136 self.check_dependencies(o,f) # Raises exception if failed
137 if (deletion):
138 self.delete_record(o)
139 o.delete(purge=True)
140 else:
141 self.sync_record(o)
142 o.enacted = datetime.now() # Is this the same timezone? XXX
143 scratchpad = {'next_run':0, 'exponent':0}
144 o.backend_register = json.dumps(scratchpad)
145 o.backend_status = "1 - OK"
146 o.save(update_fields=['enacted','backend_status','backend_register'])
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200147 except (InnocuousException,Exception) as e:
Sapan Bhatia47006112015-01-29 20:55:40 +0000148 logger.log_exc("sync step failed!")
Sapan Bhatia2175c1d2015-02-08 06:31:42 +0000149 try:
150 if (o.backend_status.startswith('2 - ')):
151 str_e = '%s // %r'%(o.backend_status[4:],e)
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200152 str_e = elim_dups(str_e)
Sapan Bhatia2175c1d2015-02-08 06:31:42 +0000153 else:
154 str_e = '%r'%e
155 except:
156 str_e = '%r'%e
157
Sapan Bhatia9c308fc2014-08-22 03:07:59 -0400158 try:
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200159 error = self.error_map.map(str_e)
Sapan Bhatia9c308fc2014-08-22 03:07:59 -0400160 except:
Sapan Bhatiac368d4a2015-06-09 14:14:12 -0400161 error = '%s'%str_e
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200162
163 if isinstance(e, InnocuousException) and not force_error:
164 o.backend_status = '1 - %s'%error
165 else:
Sapan Bhatia5e2f87a2015-05-13 15:52:45 +0200166 o.backend_status = '2 - %s'%error
Sapan Bhatiaeba08432014-04-28 23:58:36 -0400167
Sapan Bhatia47006112015-01-29 20:55:40 +0000168 try:
169 scratchpad = json.loads(o.backend_register)
170 scratchpad['exponent']
171 except:
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200172 logger.log_exc("Exception while updating scratchpad")
Sapan Bhatia47006112015-01-29 20:55:40 +0000173 scratchpad = {'next_run':0, 'exponent':0}
174
175 # Second failure
176 if (scratchpad['exponent']):
177 delay = scratchpad['exponent'] * 600 # 10 minutes
178 if (delay<1440):
179 delay = 1440
180 scratchpad['next_run'] = time.time() + delay
181
182 scratchpad['exponent']+=1
183
184 o.backend_register = json.dumps(scratchpad)
185
186 # TOFIX:
187 # DatabaseError: value too long for type character varying(140)
188 if (o.pk):
189 try:
Sapan Bhatia2175c1d2015-02-08 06:31:42 +0000190 o.backend_status = o.backend_status[:1024]
Sapan Bhatia5e2f87a2015-05-13 15:52:45 +0200191 o.save(update_fields=['backend_status','backend_register','updated'])
Sapan Bhatia47006112015-01-29 20:55:40 +0000192 except:
193 print "Could not update backend status field!"
194 pass
195 sync_failed = True
196
197
198 if (sync_failed):
Tony Mack68e818d2013-09-25 13:34:17 -0400199 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -0400200
Tony Mackce79de02013-09-24 10:12:33 -0400201 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -0400202
Sapan Bhatia9028c9a2015-05-09 18:14:40 +0200203 def sync_record(self, o):
204 return
205
206 def delete_record(self, o):
207 return
208
Tony Mack16f04742013-09-25 08:53:28 -0400209 def __call__(self, **args):
210 return self.call(**args)