forked from learningequality/kolibri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
441 lines (376 loc) · 15.4 KB
/
sync.py
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
import json
import logging
import math
from contextlib import contextmanager
from django.core.management import call_command
from django.core.management.base import CommandError
from morango.models import Filter
from morango.models import InstanceIDModel
from morango.models import ScopeDefinition
from morango.sync.controller import MorangoProfileController
from ..utils import create_superuser_and_provision_device
from ..utils import get_baseurl
from ..utils import get_client_and_server_certs
from ..utils import get_dataset_id
from kolibri.core.auth.constants.morango_sync import PROFILE_FACILITY_DATA
from kolibri.core.auth.constants.morango_sync import ScopeDefinitions
from kolibri.core.auth.constants.morango_sync import State
from kolibri.core.auth.management.utils import get_facility
from kolibri.core.auth.management.utils import run_once
from kolibri.core.auth.models import dataset_cache
from kolibri.core.logger.utils.data import bytes_for_humans
from kolibri.core.tasks.exceptions import UserCancelledError
from kolibri.core.tasks.management.commands.base import AsyncCommand
from kolibri.core.utils.lock import db_lock_sqlite_only
from kolibri.utils import conf
DATA_PORTAL_SYNCING_BASE_URL = conf.OPTIONS["Urls"]["DATA_PORTAL_SYNCING_BASE_URL"]
TRANSFER_MESSAGE = "{records_transferred}/{records_total}, {transfer_total}"
logger = logging.getLogger(__name__)
class Command(AsyncCommand):
help = "Allow the syncing of facility data with Kolibri Data Portal or another Kolibri device."
def add_arguments(self, parser):
parser.add_argument(
"--facility", action="store", type=str, help="ID of facility to sync"
)
parser.add_argument(
"--baseurl", type=str, default=DATA_PORTAL_SYNCING_BASE_URL, dest="baseurl"
)
parser.add_argument("--noninteractive", action="store_true")
parser.add_argument(
"--chunk-size",
type=int,
default=500,
help="Chunk size of records to send/retrieve per request",
)
parser.add_argument(
"--no-push", action="store_true", help="Do not push data to the server"
)
parser.add_argument(
"--no-pull", action="store_true", help="Do not pull data from the server"
)
parser.add_argument(
"--username",
type=str,
help="username of superuser on server we are syncing with",
)
parser.add_argument(
"--password",
type=str,
help="password of superuser on server we are syncing with",
)
parser.add_argument(
"--no-provision",
action="store_true",
help="do not create a facility and temporary superuser",
)
# parser.add_argument("--scope-id", type=str, default=FULL_FACILITY)
def handle_async(self, *args, **options): # noqa C901
(
baseurl,
facility_id,
chunk_size,
username,
password,
no_push,
no_pull,
noninteractive,
no_provision,
) = (
options["baseurl"],
options["facility"],
options["chunk_size"],
options["username"],
options["password"],
options["no_push"],
options["no_pull"],
options["noninteractive"],
options["no_provision"],
)
PORTAL_SYNC = baseurl == DATA_PORTAL_SYNCING_BASE_URL
# validate url that is passed in
if not PORTAL_SYNC:
baseurl = get_baseurl(baseurl)
# call this in case user directly syncs without migrating database
if not ScopeDefinition.objects.filter():
call_command("loaddata", "scopedefinitions")
dataset_cache.clear()
dataset_cache.activate()
# try to connect to server
controller = MorangoProfileController(PROFILE_FACILITY_DATA)
network_connection = controller.create_network_connection(baseurl)
# if instance_ids are equal, this means device is trying to sync with itself, which we don't allow
if (
InstanceIDModel.get_or_create_current_instance()[0].id
== network_connection.server_info["instance_id"]
):
raise CommandError(
"Device can not sync with itself. Please recheck base URL and try again."
)
if PORTAL_SYNC: # do portal sync setup
facility = get_facility(
facility_id=facility_id, noninteractive=noninteractive
)
# check for the certs we own for the specific facility
client_cert = (
facility.dataset.get_owned_certificates()
.filter(scope_definition_id=ScopeDefinitions.FULL_FACILITY)
.first()
)
if not client_cert:
raise CommandError(
"This device does not own a certificate for Facility: {}".format(
facility.name
)
)
# get primary partition
scope_params = json.loads(client_cert.scope_params)
dataset_id = scope_params["dataset_id"]
# check if the server already has a cert for this facility
server_certs = network_connection.get_remote_certificates(
dataset_id, scope_def_id=ScopeDefinitions.FULL_FACILITY
)
# if necessary, push a cert up to the server
server_cert = (
server_certs[0]
if server_certs
else network_connection.push_signed_client_certificate_chain(
local_parent_cert=client_cert,
scope_definition_id=ScopeDefinitions.FULL_FACILITY,
scope_params=scope_params,
)
)
else: # do P2P setup
dataset_id = get_dataset_id(
baseurl, identifier=facility_id, noninteractive=noninteractive
)
client_cert, server_cert, username = get_client_and_server_certs(
username,
password,
dataset_id,
network_connection,
noninteractive=noninteractive,
)
logger.info("Syncing has been initiated (this may take a while)...")
sync_session_client = network_connection.create_sync_session(
client_cert, server_cert, chunk_size=chunk_size
)
try:
# pull from server
if not no_pull:
self._handle_pull(sync_session_client, noninteractive, dataset_id)
# and push our own data to server
if not no_push:
self._handle_push(sync_session_client, noninteractive, dataset_id)
if not no_provision:
with self._lock():
create_superuser_and_provision_device(
username, dataset_id, noninteractive=noninteractive
)
except UserCancelledError:
if self.job:
self.job.extra_metadata.update(sync_state=State.CANCELLED)
self.job.save_meta()
logger.info("Syncing has been cancelled.")
return
network_connection.close()
if self.job:
self.job.extra_metadata.update(sync_state=State.COMPLETED)
self.job.save_meta()
dataset_cache.deactivate()
logger.info("Syncing has been completed.")
@contextmanager
def _lock(self):
cancellable = False
# job can't be cancelled while locked
if self.job:
cancellable = self.job.cancellable
self.job.save_as_cancellable(cancellable=False)
# Morango v0.6.13+ uses read repeatable isolation for postgres, and handles database
# transactions in a special manner that would be problematic if we began a transaction here.
# If data has changed during morango's transactions, postgres may throw a serialization
# rollback error, which will cause morango to retry it. So it's safe to avoid locking here
# when using postgres
with db_lock_sqlite_only():
yield
if self.job:
self.job.save_as_cancellable(cancellable=cancellable)
def _raise_cancel(self, *args, **kwargs):
if self.is_cancelled() and (not self.job or self.job.cancellable):
raise UserCancelledError()
def _handle_pull(self, sync_session_client, noninteractive, dataset_id):
"""
:type sync_session_client: morango.sync.syncsession.SyncSessionClient
:type noninteractive: bool
:type dataset_id: str
"""
sync_client = sync_session_client.get_pull_client()
sync_client.signals.queuing.connect(self._raise_cancel)
sync_client.signals.transferring.connect(self._raise_cancel)
self._queueing_tracker_adapter(
sync_client.signals.queuing,
"Remotely preparing data",
State.REMOTE_QUEUING,
noninteractive,
)
self._transfer_tracker_adapter(
sync_client.signals.transferring,
"Receiving data ({})".format(TRANSFER_MESSAGE),
State.PULLING,
noninteractive,
)
self._queueing_tracker_adapter(
sync_client.signals.dequeuing,
"Locally integrating received data",
State.LOCAL_DEQUEUING,
noninteractive,
)
self._session_tracker_adapter(
sync_client.signals.session,
"Creating pull transfer session",
"Completed pull transfer session",
)
sync_client.initialize(Filter(dataset_id))
sync_client.run()
with self._lock():
sync_client.finalize()
def _handle_push(self, sync_session_client, noninteractive, dataset_id):
"""
:type sync_session_client: morango.sync.syncsession.SyncSessionClient
:type noninteractive: bool
:type dataset_id: str
"""
sync_client = sync_session_client.get_push_client()
sync_client.signals.transferring.connect(self._raise_cancel)
self._queueing_tracker_adapter(
sync_client.signals.queuing,
"Locally preparing data to send",
State.LOCAL_QUEUING,
noninteractive,
)
self._transfer_tracker_adapter(
sync_client.signals.transferring,
"Sending data ({})".format(TRANSFER_MESSAGE),
State.PUSHING,
noninteractive,
)
self._queueing_tracker_adapter(
sync_client.signals.dequeuing,
"Remotely integrating data",
State.REMOTE_DEQUEUING,
noninteractive,
)
self._session_tracker_adapter(
sync_client.signals.session,
"Creating push transfer session",
"Completed push transfer session",
)
with self._lock():
sync_client.initialize(Filter(dataset_id))
sync_client.run()
# we can't cancel remotely integrating data
if self.job:
self.job.save_as_cancellable(cancellable=False)
sync_client.finalize()
def _update_all_progress(self, progress_fraction, progress):
"""
Override parent progress update callback to report from the progress tracker we're sent
"""
if self.job:
self.job.update_progress(progress_fraction, 1.0)
self.job.extra_metadata.update(progress.extra_data)
self.job.save_meta()
def _session_tracker_adapter(self, signal_group, started_msg, completed_msg):
"""
Attaches a signal handler to session creation signals
:type signal_group: morango.sync.syncsession.SyncSignalGroup
:type started_msg: str
:type completed_msg: str
"""
@run_once
def session_creation(transfer_session):
"""
A session is created individually for pushing and pulling
"""
logger.info(started_msg)
if self.job:
self.job.extra_metadata.update(sync_state=State.SESSION_CREATION)
@run_once
def session_destruction(transfer_session):
if transfer_session.records_total == 0:
logger.info("There are no records to transfer")
logger.info(completed_msg)
signal_group.started.connect(session_creation)
signal_group.completed.connect(session_destruction)
def _transfer_tracker_adapter(
self, signal_group, message, sync_state, noninteractive
):
"""
Attaches a signal handler to pushing/pulling signals
:type signal_group: morango.sync.syncsession.SyncSignalGroup
:type message: str
:type sync_state: str
:type noninteractive: bool
"""
tracker = self.start_progress(total=100)
def stats_msg(transfer_session):
transfer_total = (
transfer_session.bytes_sent + transfer_session.bytes_received
)
return message.format(
records_transferred=transfer_session.records_transferred,
records_total=transfer_session.records_total,
transfer_total=bytes_for_humans(transfer_total),
)
def stats(transfer_session):
logger.info(stats_msg(transfer_session))
def handler(transfer_session):
"""
:type transfer_session: morango.models.core.TransferSession
"""
try:
progress = (
100
* transfer_session.records_transferred
/ float(transfer_session.records_total)
)
except ZeroDivisionError:
progress = 100
tracker.update_progress(
increment=math.ceil(progress - tracker.progress),
message=stats_msg(transfer_session),
extra_data=dict(
bytes_sent=transfer_session.bytes_sent,
bytes_received=transfer_session.bytes_received,
sync_state=sync_state,
),
)
if noninteractive or tracker.progressbar is None:
signal_group.started.connect(stats)
signal_group.in_progress.connect(stats)
signal_group.connect(handler)
# log one more time at end to capture in logging output
signal_group.completed.connect(stats)
def _queueing_tracker_adapter(
self, signal_group, message, sync_state, noninteractive
):
"""
Attaches a signal handler to queuing/dequeuing signals
:type signal_group: morango.sync.syncsession.SyncSignalGroup
:type message: str
:type sync_state: str
:type noninteractive: bool
"""
tracker = self.start_progress(total=2)
def started(transfer_session):
dataset_cache.clear()
if noninteractive or tracker.progressbar is None:
logger.info(message)
def handler(transfer_session):
tracker.update_progress(
message=message, extra_data=dict(sync_state=sync_state)
)
if noninteractive or tracker.progressbar is None:
signal_group.started.connect(started)
signal_group.started.connect(started)
signal_group.started.connect(handler)
signal_group.completed.connect(handler)