-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathgeneric.py
More file actions
858 lines (748 loc) · 37 KB
/
generic.py
File metadata and controls
858 lines (748 loc) · 37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# deal with unicode literals: http://www.python.org/dev/peps/pep-0263/
# vim: set fileencoding=utf-8 :
"""
----------------
IMAP SYNC ENGINE
----------------
Okay, here's the deal.
The IMAP sync engine runs per-folder on each account.
Only one initial sync can be running per-account at a time, to avoid
hammering the IMAP backend too hard (Gmail shards per-user, so parallelizing
folder download won't actually increase our throughput anyway).
Any time we reconnect, we have to make sure the folder's uidvalidity hasn't
changed, and if it has, we need to update the UIDs for any messages we've
already downloaded. A folder's uidvalidity cannot change during a session
(SELECT during an IMAP session starts a session on a folder) (see
http://tools.ietf.org/html/rfc3501#section-2.3.1.1).
Note that despite a session giving you a HIGHESTMODSEQ at the start of a
SELECT, that session will still always give you the latest message list
including adds, deletes, and flag changes that have happened since that
highestmodseq. (In Gmail, there is a small delay between changes happening on
the web client and those changes registering on a connected IMAP session,
though bizarrely the HIGHESTMODSEQ is updated immediately.) So we have to keep
in mind that the data may be changing behind our backs as we're syncing.
Fetching info about UIDs that no longer exist is not an error but gives us
empty data.
Folder sync state is stored in the ImapFolderSyncStatus table to allow for
restarts.
Here's the state machine:
-----
| ---------------- ----------------------
∨ | initial sync | <-----> | initial uidinvalid |
---------- ---------------- ----------------------
| finish | | ^
---------- | |_________________________
^ ∨ |
| ---------------- ----------------------
|---| poll | <-----> | poll uidinvalid |
---------------- ----------------------
| ∧
----
We encapsulate sync engine instances in greenlets for cooperative coroutine
scheduling around network I/O.
--------------
SESSION SCOPES
--------------
Database sessions are held for as short a duration as possible---just to
query for needed information or update the local state. Long-held database
sessions reduce scalability.
"""
from __future__ import division
from datetime import datetime, timedelta
from gevent import Greenlet
import gevent
import imaplib
from sqlalchemy import func
from sqlalchemy.orm import load_only
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from inbox.basicauth import ValidationError
from inbox.util.concurrency import retry_with_logging
from inbox.util.debug import bind_context
from inbox.util.itert import chunk
from inbox.util.misc import or_none
from inbox.util.threading import fetch_corresponding_thread, MAX_THREAD_LENGTH
from inbox.util.stats import statsd_client
from nylas.logging import get_logger
log = get_logger()
from inbox.crispin import connection_pool, retry_crispin, FolderMissingError
from inbox.models import Folder, Account, Message
from inbox.models.backends.imap import (ImapFolderSyncStatus, ImapThread,
ImapUid, ImapFolderInfo)
from inbox.models.session import session_scope
from inbox.mailsync.backends.imap import common
from inbox.mailsync.backends.base import (MailsyncDone, MailsyncError,
THROTTLE_COUNT, THROTTLE_WAIT)
from inbox.heartbeat.store import HeartbeatStatusProxy
from inbox.events.ical import import_attached_events
# Idle doesn't necessarily pick up flag changes, so we don't want to
# idle for very long, or we won't detect things like messages being
# marked as read.
IDLE_WAIT = 30
DEFAULT_POLL_FREQUENCY = 30
# Poll on the Inbox folder more often.
INBOX_POLL_FREQUENCY = 10
FAST_FLAGS_REFRESH_LIMIT = 100
SLOW_FLAGS_REFRESH_LIMIT = 2000
SLOW_REFRESH_INTERVAL = timedelta(seconds=3600)
FAST_REFRESH_INTERVAL = timedelta(seconds=30)
# Maximum number of uidinvalidity errors in a row.
MAX_UIDINVALID_RESYNCS = 5
CONDSTORE_FLAGS_REFRESH_BATCH_SIZE = 200
class FolderSyncEngine(Greenlet):
"""Base class for a per-folder IMAP sync engine."""
def __init__(self, account_id, namespace_id, folder_name,
email_address, provider_name, syncmanager_lock):
with session_scope(namespace_id) as db_session:
try:
folder = db_session.query(Folder). \
filter(Folder.name == folder_name,
Folder.account_id == account_id).one()
except NoResultFound:
raise MailsyncError(u"Missing Folder '{}' on account {}"
.format(folder_name, account_id))
self.folder_id = folder.id
self.folder_role = folder.canonical_name
# Metric flags for sync performance
self.is_initial_sync = folder.initial_sync_end is None
self.is_first_sync = folder.initial_sync_start is None
self.is_first_message = self.is_first_sync
bind_context(self, 'foldersyncengine', account_id, self.folder_id)
self.account_id = account_id
self.namespace_id = namespace_id
self.folder_name = folder_name
self.email_address = email_address
if self.folder_name.lower() == 'inbox':
self.poll_frequency = INBOX_POLL_FREQUENCY
else:
self.poll_frequency = DEFAULT_POLL_FREQUENCY
self.syncmanager_lock = syncmanager_lock
self.state = None
self.provider_name = provider_name
self.last_fast_refresh = None
self.flags_fetch_results = {}
self.conn_pool = connection_pool(self.account_id)
self.state_handlers = {
'initial': self.initial_sync,
'initial uidinvalid': self.resync_uids,
'poll': self.poll,
'poll uidinvalid': self.resync_uids,
}
self.setup_heartbeats()
Greenlet.__init__(self)
# Some generic IMAP servers are throwing UIDVALIDITY
# errors forever. Instead of resyncing those servers
# ad vitam, we keep track of the number of consecutive
# times we got such an error and bail out if it's higher than
# MAX_UIDINVALID_RESYNCS.
self.uidinvalid_count = 0
def setup_heartbeats(self):
self.heartbeat_status = HeartbeatStatusProxy(self.account_id,
self.folder_id,
self.folder_name,
self.email_address,
self.provider_name)
def _run(self):
# Bind greenlet-local logging context.
self.log = log.new(account_id=self.account_id, folder=self.folder_name,
provider=self.provider_name)
# eagerly signal the sync status
self.heartbeat_status.publish()
try:
self.update_folder_sync_status(lambda s: s.start_sync())
except IntegrityError:
# The state insert failed because the folder ID ForeignKey
# was no longer valid, ie. the folder for this engine was deleted
# while we were starting up.
# Exit the sync and let the monitor sort things out.
log.info("Folder state loading failed due to IntegrityError",
folder_id=self.folder_id, account_id=self.account_id)
raise MailsyncDone()
# NOTE: The parent ImapSyncMonitor handler could kill us at any
# time if it receives a shutdown command. The shutdown command is
# equivalent to ctrl-c.
while True:
retry_with_logging(self._run_impl, account_id=self.account_id,
provider=self.provider_name, logger=log)
def _run_impl(self):
old_state = self.state
try:
self.state = self.state_handlers[old_state]()
self.heartbeat_status.publish(state=self.state)
except UidInvalid:
self.state = self.state + ' uidinvalid'
self.uidinvalid_count += 1
self.heartbeat_status.publish(state=self.state)
# Check that we're not stuck in an endless uidinvalidity resync loop.
if self.uidinvalid_count > MAX_UIDINVALID_RESYNCS:
log.error('Resynced more than MAX_UIDINVALID_RESYNCS in a'
' row. Stopping sync.')
with session_scope(self.namespace_id) as db_session:
account = db_session.query(Account).get(self.account_id)
account.disable_sync('Detected endless uidvalidity '
'resync loop')
account.sync_state = 'stopped'
db_session.commit()
raise MailsyncDone()
except FolderMissingError:
# Folder was deleted by monitor while its sync was running.
# TODO: Monitor should handle shutting down the folder engine.
log.info('Folder disappeared. Stopping sync.',
account_id=self.account_id, folder_id=self.folder_id)
raise MailsyncDone()
except ValidationError as exc:
log.error('Error authenticating; stopping sync', exc_info=True,
account_id=self.account_id, folder_id=self.folder_id,
logstash_tag='mark_invalid')
with session_scope(self.namespace_id) as db_session:
account = db_session.query(Account).get(self.account_id)
account.mark_invalid()
account.update_sync_error(exc)
raise MailsyncDone()
# State handlers are idempotent, so it's okay if we're
# killed between the end of the handler and the commit.
if self.state != old_state:
def update(status):
status.state = self.state
self.update_folder_sync_status(update)
if self.state == old_state and self.state in ['initial', 'poll']:
# We've been through a normal state transition without raising any
# error. It's safe to reset the uidvalidity counter.
self.uidinvalid_count = 0
def update_folder_sync_status(self, cb):
# Loads the folder sync status and invokes the provided callback to
# modify it. Commits any changes and updates `self.state` to ensure
# they are never out of sync.
with session_scope(self.namespace_id) as db_session:
try:
state = ImapFolderSyncStatus.state
saved_folder_status = db_session.query(ImapFolderSyncStatus)\
.filter_by(account_id=self.account_id, folder_id=self.folder_id)\
.options(load_only(state)).one()
except NoResultFound:
saved_folder_status = ImapFolderSyncStatus(
account_id=self.account_id, folder_id=self.folder_id)
db_session.add(saved_folder_status)
cb(saved_folder_status)
db_session.commit()
self.state = saved_folder_status.state
def set_stopped(self, db_session):
self.update_folder_sync_status(lambda s: s.stop_sync())
def _report_initial_sync_start(self):
with session_scope(self.namespace_id) as db_session:
q = db_session.query(Folder).get(self.folder_id)
q.initial_sync_start = datetime.utcnow()
def _report_initial_sync_end(self):
with session_scope(self.namespace_id) as db_session:
q = db_session.query(Folder).get(self.folder_id)
q.initial_sync_end = datetime.utcnow()
@retry_crispin
def initial_sync(self):
log.bind(state='initial')
log.info('starting initial sync')
if self.is_first_sync:
self._report_initial_sync_start()
self.is_first_sync = False
with self.conn_pool.get() as crispin_client:
crispin_client.select_folder(self.folder_name, uidvalidity_cb)
# Ensure we have an ImapFolderInfo row created prior to sync start.
with session_scope(self.namespace_id) as db_session:
try:
db_session.query(ImapFolderInfo). \
filter(ImapFolderInfo.account_id == self.account_id,
ImapFolderInfo.folder_id == self.folder_id). \
one()
except NoResultFound:
imapfolderinfo = ImapFolderInfo(
account_id=self.account_id, folder_id=self.folder_id,
uidvalidity=crispin_client.selected_uidvalidity,
uidnext=crispin_client.selected_uidnext)
db_session.add(imapfolderinfo)
db_session.commit()
self.initial_sync_impl(crispin_client)
if self.is_initial_sync:
self._report_initial_sync_end()
self.is_initial_sync = False
return 'poll'
@retry_crispin
def poll(self):
log.bind(state='poll')
log.debug('polling')
self.poll_impl()
return 'poll'
@retry_crispin
def resync_uids(self):
log.bind(state=self.state)
log.warning('UIDVALIDITY changed; initiating resync')
self.resync_uids_impl()
return 'initial'
def initial_sync_impl(self, crispin_client):
# We wrap the block in a try/finally because the change_poller greenlet
# needs to be killed when this greenlet is interrupted
change_poller = None
try:
assert crispin_client.selected_folder_name == self.folder_name
remote_uids = crispin_client.all_uids()
with self.syncmanager_lock:
with session_scope(self.namespace_id) as db_session:
local_uids = common.local_uids(self.account_id, db_session,
self.folder_id)
common.remove_deleted_uids(
self.account_id, self.folder_id,
set(local_uids).difference(remote_uids))
new_uids = set(remote_uids).difference(local_uids)
with session_scope(self.namespace_id) as db_session:
account = db_session.query(Account).get(self.account_id)
throttled = account.throttled
self.update_uid_counts(
db_session,
remote_uid_count=len(remote_uids),
# This is the initial size of our download_queue
download_uid_count=len(new_uids))
change_poller = gevent.spawn(self.poll_for_changes)
bind_context(change_poller, 'changepoller', self.account_id,
self.folder_id)
uids = sorted(new_uids, reverse=True)
count = 0
for uid in uids:
# The speedup from batching appears to be less clear for
# non-Gmail accounts, so for now just download one-at-a-time.
self.download_and_commit_uids(crispin_client, [uid])
self.heartbeat_status.publish()
count += 1
if throttled and count >= THROTTLE_COUNT:
# Throttled accounts' folders sync at a rate of
# 1 message/ minute, after the first approx. THROTTLE_COUNT
# messages per folder are synced.
# Note this is an approx. limit since we use the #(uids),
# not the #(messages).
gevent.sleep(THROTTLE_WAIT)
finally:
if change_poller is not None:
# schedule change_poller to die
gevent.kill(change_poller)
def should_idle(self, crispin_client):
if not hasattr(self, '_should_idle'):
self._should_idle = (
crispin_client.idle_supported() and self.folder_name in
crispin_client.folder_names()['inbox']
)
return self._should_idle
def poll_impl(self):
with self.conn_pool.get() as crispin_client:
self.check_uid_changes(crispin_client)
if self.should_idle(crispin_client):
crispin_client.select_folder(self.folder_name,
self.uidvalidity_cb)
idling = True
try:
crispin_client.idle(IDLE_WAIT)
except Exception as exc:
# With some servers we get e.g.
# 'Unexpected IDLE response: * FLAGS (...)'
if isinstance(exc, imaplib.IMAP4.error) and \
exc.message.startswith('Unexpected IDLE response'):
log.info('Error initiating IDLE, not idling',
error=exc)
try:
# Still have to take the connection out of IDLE
# mode to reuse it though.
crispin_client.conn.idle_done()
except AttributeError:
pass
idling = False
else:
raise
else:
idling = False
# Close IMAP connection before sleeping
if not idling:
gevent.sleep(self.poll_frequency)
def resync_uids_impl(self):
# First, let's check if the UIVDALIDITY change was spurious, if
# it is, just discard it and go on.
with self.conn_pool.get() as crispin_client:
crispin_client.select_folder(self.folder_name, lambda *args: True)
remote_uidvalidity = crispin_client.selected_uidvalidity
remote_uidnext = crispin_client.selected_uidnext
if remote_uidvalidity <= self.uidvalidity:
log.debug('UIDVALIDITY unchanged')
return
# Otherwise, if the UIDVALIDITY really has changed, discard all saved
# UIDs for the folder, mark associated messages for garbage-collection,
# and return to the 'initial' state to resync.
# This will cause message and threads to be deleted and recreated, but
# uidinvalidity is sufficiently rare that this tradeoff is acceptable.
with session_scope(self.namespace_id) as db_session:
invalid_uids = {
uid for uid, in db_session.query(ImapUid.msg_uid).
filter_by(account_id=self.account_id,
folder_id=self.folder_id)
}
common.remove_deleted_uids(self.account_id, self.folder_id,
invalid_uids)
self.uidvalidity = remote_uidvalidity
self.highestmodseq = None
self.uidnext = remote_uidnext
@retry_crispin
def poll_for_changes(self):
log.new(account_id=self.account_id, folder=self.folder_name)
while True:
log.debug('polling for changes')
self.poll_impl()
def create_message(self, db_session, acct, folder, msg):
assert acct is not None and acct.namespace is not None
# Check if we somehow already saved the imapuid (shouldn't happen, but
# possible due to race condition). If so, don't commit changes.
existing_imapuid = db_session.query(ImapUid).filter(
ImapUid.account_id == acct.id, ImapUid.folder_id == folder.id,
ImapUid.msg_uid == msg.uid).first()
if existing_imapuid is not None:
log.error('Expected to create imapuid, but existing row found',
remote_msg_uid=msg.uid,
existing_imapuid=existing_imapuid.id)
return None
# Check if the message is valid.
# https://sentry.nylas.com/sentry/sync-prod/group/3387/
if msg.body is None:
log.warning('Server returned a message with an empty body.')
return None
new_uid = common.create_imap_message(db_session, acct, folder, msg)
self.add_message_to_thread(db_session, new_uid.message, msg)
db_session.flush()
# We're calling import_attached_events here instead of some more
# obvious place (like Message.create_from_synced) because the function
# requires new_uid.message to have been flushed.
# This is necessary because the import_attached_events does db lookups.
if new_uid.message.has_attached_events:
with db_session.no_autoflush:
import_attached_events(db_session, acct, new_uid.message)
# If we're in the polling state, then we want to report the metric
# for latency when the message was received vs created
if self.state == 'poll':
latency_millis = (
datetime.utcnow() - new_uid.message.received_date) \
.total_seconds() * 1000
metrics = [
'.'.join(['accounts', 'overall', 'message_latency']),
'.'.join(['providers', self.provider_name, 'message_latency']),
]
for metric in metrics:
statsd_client.timing(metric, latency_millis)
return new_uid
def _count_thread_messages(self, thread_id, db_session):
count, = db_session.query(func.count(Message.id)). \
filter(Message.thread_id == thread_id).one()
return count
def add_message_to_thread(self, db_session, message_obj, raw_message):
"""Associate message_obj to the right Thread object, creating a new
thread if necessary."""
with db_session.no_autoflush:
# Disable autoflush so we don't try to flush a message with null
# thread_id.
parent_thread = fetch_corresponding_thread(
db_session, self.namespace_id, message_obj)
construct_new_thread = True
if parent_thread:
# If there's a parent thread that isn't too long already,
# add to it. Otherwise create a new thread.
parent_message_count = self._count_thread_messages(
parent_thread.id, db_session)
if parent_message_count < MAX_THREAD_LENGTH:
construct_new_thread = False
if construct_new_thread:
message_obj.thread = ImapThread.from_imap_message(
db_session, self.namespace_id, message_obj)
else:
parent_thread.messages.append(message_obj)
def download_and_commit_uids(self, crispin_client, uids):
start = datetime.utcnow()
raw_messages = crispin_client.uids(uids)
if not raw_messages:
return 0
new_uids = set()
with self.syncmanager_lock:
with session_scope(self.namespace_id) as db_session:
account = Account.get(self.account_id, db_session)
folder = Folder.get(self.folder_id, db_session)
for msg in raw_messages:
uid = self.create_message(db_session, account,
folder, msg)
if uid is not None:
db_session.add(uid)
db_session.flush()
new_uids.add(uid)
db_session.commit()
log.info('Committed new UIDs',
new_committed_message_count=len(new_uids))
# If we downloaded uids, record message velocity (#uid / latency)
if self.state == 'initial' and len(new_uids):
self._report_message_velocity(datetime.utcnow() - start,
len(new_uids))
if self.is_first_message:
self._report_first_message()
self.is_first_message = False
return len(new_uids)
def _report_first_message(self):
now = datetime.utcnow()
with session_scope(self.namespace_id) as db_session:
account = db_session.query(Account).get(self.account_id)
account_created = account.created_at
latency = (now - account_created).total_seconds() * 1000
metrics = [
'.'.join(['providers', self.provider_name, 'first_message']),
'.'.join(['providers', 'overall', 'first_message'])
]
for metric in metrics:
statsd_client.timing(metric, latency)
def _report_message_velocity(self, timedelta, num_uids):
latency = (timedelta).total_seconds() * 1000
latency_per_uid = float(latency) / num_uids
metrics = [
'.'.join(['providers', self.provider_name,
'message_velocity']),
'.'.join(['providers', 'overall', 'message_velocity'])
]
for metric in metrics:
statsd_client.timing(metric, latency_per_uid)
def update_uid_counts(self, db_session, **kwargs):
saved_status = db_session.query(ImapFolderSyncStatus).join(Folder). \
filter(ImapFolderSyncStatus.account_id == self.account_id,
Folder.name == self.folder_name).one()
# We're not updating the current_remote_count metric
# so don't update uid_checked_timestamp.
if kwargs.get('remote_uid_count') is None:
saved_status.update_metrics(kwargs)
else:
metrics = dict(uid_checked_timestamp=datetime.utcnow())
metrics.update(kwargs)
saved_status.update_metrics(metrics)
def get_new_uids(self, crispin_client):
try:
remote_uidnext = crispin_client.conn.folder_status(
self.folder_name, ['UIDNEXT']).get('UIDNEXT')
except ValueError:
# Work around issue where ValueError is raised on parsing STATUS
# response.
log.warning('Error getting UIDNEXT', exc_info=True)
remote_uidnext = None
except imaplib.IMAP4.error as e:
if '[NONEXISTENT]' in e.message:
raise FolderMissingError()
else:
raise e
if remote_uidnext is not None and remote_uidnext == self.uidnext:
return
log.info('UIDNEXT changed, checking for new UIDs',
remote_uidnext=remote_uidnext, saved_uidnext=self.uidnext)
crispin_client.select_folder(self.folder_name, self.uidvalidity_cb)
with session_scope(self.namespace_id) as db_session:
lastseenuid = common.lastseenuid(self.account_id, db_session,
self.folder_id)
latest_uids = crispin_client.conn.fetch('{}:*'.format(lastseenuid + 1),
['UID']).keys()
new_uids = set(latest_uids) - {lastseenuid}
if new_uids:
for uid in sorted(new_uids):
self.download_and_commit_uids(crispin_client, [uid])
self.uidnext = remote_uidnext
def condstore_refresh_flags(self, crispin_client):
new_highestmodseq = crispin_client.conn.folder_status(
self.folder_name, ['HIGHESTMODSEQ'])['HIGHESTMODSEQ']
# Ensure that we have an initial highestmodseq value stored before we
# begin polling for changes.
if self.highestmodseq is None:
self.highestmodseq = new_highestmodseq
if new_highestmodseq == self.highestmodseq:
# Don't need to do anything if the highestmodseq hasn't
# changed.
return
elif new_highestmodseq < self.highestmodseq:
# This should really never happen, but if it does, handle it.
log.warning('got server highestmodseq less than saved '
'highestmodseq',
new_highestmodseq=new_highestmodseq,
saved_highestmodseq=self.highestmodseq)
return
log.info('HIGHESTMODSEQ has changed, getting changed UIDs',
new_highestmodseq=new_highestmodseq,
saved_highestmodseq=self.highestmodseq)
crispin_client.select_folder(self.folder_name, self.uidvalidity_cb)
changed_flags = crispin_client.condstore_changed_flags(
self.highestmodseq)
remote_uids = crispin_client.all_uids()
# In order to be able to sync changes to tens of thousands of flags at
# once, we commit updates in batches. We do this in ascending order by
# modseq and periodically "checkpoint" our saved highestmodseq. (It's
# safe to checkpoint *because* we go in ascending order by modseq.)
# That way if the process gets restarted halfway through this refresh,
# we don't have to completely start over. It's also slow to load many
# objects into the SQLAlchemy session and then issue lots of commits;
# we avoid that by batching.
iterable = sorted(changed_flags.items(), key=lambda t: t[1].modseq)
flag_batches = chunk(iterable, CONDSTORE_FLAGS_REFRESH_BATCH_SIZE)
for flag_batch in flag_batches:
with session_scope(self.namespace_id) as db_session:
common.update_metadata(self.account_id, self.folder_id,
self.folder_role, dict(flag_batch),
db_session)
if len(flag_batch) == CONDSTORE_FLAGS_REFRESH_BATCH_SIZE:
interim_highestmodseq = max(v.modseq for k, v in flag_batch)
self.highestmodseq = interim_highestmodseq
with session_scope(self.namespace_id) as db_session:
local_uids = common.local_uids(self.account_id, db_session,
self.folder_id)
expunged_uids = set(local_uids).difference(remote_uids)
if expunged_uids:
# If new UIDs have appeared since we last checked in
# get_new_uids, save them first. We want to always have the
# latest UIDs before expunging anything, in order to properly
# capture draft revisions.
with session_scope(self.namespace_id) as db_session:
lastseenuid = common.lastseenuid(self.account_id, db_session,
self.folder_id)
if remote_uids and lastseenuid < max(remote_uids):
log.info('Downloading new UIDs before expunging')
self.get_new_uids(crispin_client)
common.remove_deleted_uids(self.account_id, self.folder_id,
expunged_uids)
self.highestmodseq = new_highestmodseq
def generic_refresh_flags(self, crispin_client):
now = datetime.utcnow()
slow_refresh_due = (
self.last_slow_refresh is None or
now > self.last_slow_refresh + SLOW_REFRESH_INTERVAL
)
fast_refresh_due = (
self.last_fast_refresh is None or
now > self.last_fast_refresh + FAST_REFRESH_INTERVAL
)
if slow_refresh_due:
self.refresh_flags_impl(crispin_client, SLOW_FLAGS_REFRESH_LIMIT)
self.last_slow_refresh = datetime.utcnow()
elif fast_refresh_due:
self.refresh_flags_impl(crispin_client, FAST_FLAGS_REFRESH_LIMIT)
self.last_fast_refresh = datetime.utcnow()
def refresh_flags_impl(self, crispin_client, max_uids):
crispin_client.select_folder(self.folder_name, self.uidvalidity_cb)
with session_scope(self.namespace_id) as db_session:
local_uids = common.local_uids(account_id=self.account_id,
session=db_session,
folder_id=self.folder_id,
limit=max_uids)
flags = crispin_client.flags(local_uids)
if (max_uids in self.flags_fetch_results and
self.flags_fetch_results[max_uids] == (local_uids, flags)):
# If the flags fetch response is exactly the same as the last one
# we got, then we don't need to persist any changes.
log.debug('Unchanged flags refresh response, '
'not persisting changes', max_uids=max_uids)
return
log.debug('Changed flags refresh response, persisting changes',
max_uids=max_uids)
expunged_uids = set(local_uids).difference(flags.keys())
common.remove_deleted_uids(self.account_id, self.folder_id,
expunged_uids)
with session_scope(self.namespace_id) as db_session:
common.update_metadata(self.account_id, self.folder_id,
self.folder_role, flags, db_session)
self.flags_fetch_results[max_uids] = (local_uids, flags)
def check_uid_changes(self, crispin_client):
self.get_new_uids(crispin_client)
if crispin_client.condstore_supported():
self.condstore_refresh_flags(crispin_client)
else:
self.generic_refresh_flags(crispin_client)
@property
def uidvalidity(self):
if not hasattr(self, '_uidvalidity'):
self._uidvalidity = self._load_imap_folder_info().uidvalidity
return self._uidvalidity
@uidvalidity.setter
def uidvalidity(self, value):
self._update_imap_folder_info('uidvalidity', value)
self._uidvalidity = value
@property
def uidnext(self):
if not hasattr(self, '_uidnext'):
self._uidnext = self._load_imap_folder_info().uidnext
return self._uidnext
@uidnext.setter
def uidnext(self, value):
self._update_imap_folder_info('uidnext', value)
self._uidnext = value
@property
def last_slow_refresh(self):
# We persist the last_slow_refresh timestamp so that we don't end up
# doing a (potentially expensive) full flags refresh for every account
# on every process restart.
if not hasattr(self, '_last_slow_refresh'):
self._last_slow_refresh = self._load_imap_folder_info(). \
last_slow_refresh
return self._last_slow_refresh
@last_slow_refresh.setter
def last_slow_refresh(self, value):
self._update_imap_folder_info('last_slow_refresh', value)
self._last_slow_refresh = value
@property
def highestmodseq(self):
if not hasattr(self, '_highestmodseq'):
self._highestmodseq = self._load_imap_folder_info().highestmodseq
return self._highestmodseq
@highestmodseq.setter
def highestmodseq(self, value):
self._highestmodseq = value
self._update_imap_folder_info('highestmodseq', value)
def _load_imap_folder_info(self):
with session_scope(self.namespace_id) as db_session:
imapfolderinfo = db_session.query(ImapFolderInfo). \
filter(ImapFolderInfo.account_id == self.account_id,
ImapFolderInfo.folder_id == self.folder_id). \
one()
db_session.expunge(imapfolderinfo)
return imapfolderinfo
def _update_imap_folder_info(self, attrname, value):
with session_scope(self.namespace_id) as db_session:
imapfolderinfo = db_session.query(ImapFolderInfo). \
filter(ImapFolderInfo.account_id == self.account_id,
ImapFolderInfo.folder_id == self.folder_id). \
one()
setattr(imapfolderinfo, attrname, value)
db_session.commit()
def uidvalidity_cb(self, account_id, folder_name, select_info):
assert folder_name == self.folder_name
assert account_id == self.account_id
selected_uidvalidity = select_info['UIDVALIDITY']
is_valid = (self.uidvalidity is None or
selected_uidvalidity <= self.uidvalidity)
if not is_valid:
raise UidInvalid(
'folder: {}, remote uidvalidity: {}, '
'cached uidvalidity: {}'.format(folder_name.encode('utf-8'),
selected_uidvalidity,
self.uidvalidity))
return select_info
class UidInvalid(Exception):
"""Raised when a folder's UIDVALIDITY changes, requiring a resync."""
pass
# This version is elsewhere in the codebase, so keep it for now
# TODO(emfree): clean this up.
def uidvalidity_cb(account_id, folder_name, select_info):
assert folder_name is not None and select_info is not None, \
"must start IMAP session before verifying UIDVALIDITY"
with session_scope(account_id) as db_session:
saved_folder_info = common.get_folder_info(account_id, db_session,
folder_name)
saved_uidvalidity = or_none(saved_folder_info, lambda i:
i.uidvalidity)
selected_uidvalidity = select_info['UIDVALIDITY']
if saved_folder_info:
is_valid = (saved_uidvalidity is None or
selected_uidvalidity <= saved_uidvalidity)
if not is_valid:
raise UidInvalid(
'folder: {}, remote uidvalidity: {}, '
'cached uidvalidity: {}'.format(folder_name.encode('utf-8'),
selected_uidvalidity,
saved_uidvalidity))
return select_info