-
Notifications
You must be signed in to change notification settings - Fork 38
/
folder.py
570 lines (487 loc) · 21.6 KB
/
folder.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
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
import attr
import pendulum
from parsec.core.fs.base import BaseEntry, BaseNotLoadedEntry, FSInvalidPath, FSError
from parsec.core.fs.merge_folder import merge_folder_manifest
from parsec.core.backend_storage import BackendConcurrencyError
from huepy import good, bad, run, info, que
async def _recursive_need_sync(entry):
if isinstance(entry, BaseNotLoadedEntry):
# TODO: Do we really need to reload the entire fs tree to retrieve
# what need to be sync ? couldn't we just interrogate local_storage ?
entry = await entry.load()
if isinstance(entry, BaseFolderEntry):
if entry.need_sync:
return True
for child_entry in entry._children.values():
if await _recursive_need_sync(child_entry):
return True
elif entry.need_sync:
return True
return False
def _recursive_need_flush(entry):
if isinstance(entry, BaseNotLoadedEntry):
return
if entry.need_flush:
return True
if isinstance(entry, BaseFolderEntry):
for child_entry in entry._children.values():
if _recursive_need_flush(child_entry):
return True
return False
class BaseFolderEntry(BaseEntry):
_user_id = attr.ib()
_device_name = attr.ib()
_created = attr.ib()
_updated = attr.ib()
_children = attr.ib()
_need_flush = attr.ib()
_need_sync = attr.ib()
_base_version = attr.ib()
def __init__(
self,
access,
user_id,
device_name,
need_flush=True,
need_sync=True,
created=None,
updated=None,
base_version=0,
name="",
parent=None,
children_accesses=None,
):
super().__init__(access, name, parent)
self._user_id = user_id
self._device_name = device_name
self._need_flush = need_flush
self._need_sync = need_sync
self._created = created or pendulum.utcnow()
self._updated = updated or self.created
self._base_version = base_version
self._children = {}
if children_accesses:
for name, access in children_accesses.items():
self._children[name] = self._fs._not_loaded_entry_cls(
access=access, name=name, parent=self
)
@property
def user_id(self):
return self._user_id
@property
def device_name(self):
return self._device_name
@property
def created(self):
return self._created
@property
def updated(self):
return self._updated
@property
def base_version(self):
return self._base_version
@property
def need_flush(self):
return self._need_flush
@property
def need_sync(self):
return self._need_sync
def keys(self):
return self._children.keys()
def __getitem__(self, name):
return self._children[name]
def __contains__(self, name):
return name in self._children
def _modified(self):
self._need_flush = True
self._need_sync = True
self._updated = pendulum.utcnow()
async def flush_no_lock(self, recursive=False):
if recursive:
# TODO: recursive lock maybe too violent here (e.g.
# `flush('/', recursive=True)` means we cannot modify / when
# recursively flush children, which maybe numerous...)
for child in self._children.values():
await child.flush(recursive=True)
if not _recursive_need_flush(self):
return
# Serialize as local folder manifest
manifest = {
"format": 1,
"type": "local_folder_manifest",
"user_id": self._user_id,
"device_name": self._device_name,
"need_sync": self._need_sync,
"base_version": self._base_version,
"created": self._created,
"updated": self._updated,
"children": {k: v._access.dump(with_type=True) for k, v in self._children.items()},
}
# Save the local folder manifest
access = self._access
print(good("flush %s %s" % (self.path, manifest)))
await self._fs.manifests_manager.flush_on_local(access.id, access.key, manifest)
self._need_flush = False
async def flush(self, recursive=False):
if not self._need_flush and not recursive:
return
async with self.acquire_write():
await self.flush_no_lock(recursive)
async def minimal_sync_if_placeholder(self):
if not self.is_placeholder:
return
# Don't actually synchronize the data to save to, otherwise
# consider version 1 is the newly created pristine object
async with self.acquire_write():
manifest = {
"format": 1,
"type": "folder_manifest",
"user_id": self._user_id,
"device_name": self._device_name,
"version": 1,
"created": self._created,
"updated": self._created,
"children": {},
}
key = self._access.key
print(run("min sync %s %s" % (self.path, manifest)))
id, rts, wts = await self._fs.manifests_manager.sync_new_entry_with_backend(
key, manifest
)
self._base_version = 1
self._access = self._fs._vlob_access_cls(id, rts, wts, key)
self._need_flush = True
await self.flush_no_lock()
async def sync(self, recursive=False, ignore_placeholders=False):
# TODO: if file is a placeholder but contains data we sync it two
# times...
await self.minimal_sync_if_placeholder()
async with self.acquire_write():
if not await _recursive_need_sync(self):
# This folder (and it children) hasn't been modified locally,
# just download last version from the backend if any.
manifest = await self._fs.manifests_manager.fetch_from_backend(
self._access.id, self._access.rts, self._access.key
)
if manifest["version"] != self.base_version:
self._user_id = manifest["user_id"]
self._device_name = manifest["device_name"]
self._created = manifest["created"]
self._updated = manifest["updated"]
self._base_version = manifest["version"]
self._children = {
k: self._fs._not_loaded_entry_cls(
name=k, parent=self, access=self._fs._vlob_access_cls(**v)
)
for k, v in manifest["children"].items()
}
return
# TODO: useful ?
# await self.flush_no_lock()
# Make a snapshot of ourself to avoid concurrency
# Serialize as local folder manifest
manifest = {
"format": 1,
"type": "folder_manifest",
"user_id": self._user_id,
"device_name": self._device_name,
"version": self._base_version + 1,
"created": self._created,
"updated": self._updated,
"children": {},
}
children = self._children.copy()
access = self._access
# TODO: should release the lock here
# Convert placeholder children into proper synchronized children
for name, entry in children.items():
if ignore_placeholders and entry.is_placeholder:
continue
else:
if recursive:
await entry.sync(recursive=True)
else:
await entry.minimal_sync_if_placeholder()
# TODO: Synchronize with up-to-date data and flush to avoid
# having to re-synchronize placeholders
manifest["children"][name] = entry._access.dump(with_type=False)
# Upload the file manifest as new vlob version
while True:
try:
await self._fs.manifests_manager.sync_with_backend(
access.id, access.wts, access.key, manifest
)
print(que("sync %s %s" % (self.path, manifest)))
break
except BackendConcurrencyError:
print(bad("concurrency error sync %s" % self.path))
print(info("manifest %s" % manifest))
base = await self._fs.manifests_manager.fetch_from_backend(
access.id, access.rts, access.key, version=manifest["version"] - 1
)
print(info("base %s" % base))
# Fetch last version from the backend and merge with it
# before retrying the synchronization
target = await self._fs.manifests_manager.fetch_from_backend(
access.id, access.rts, access.key
)
manifest["version"] = target["version"] + 1
print(info("target %s" % target))
# 3-ways merge between base, modified and target versions
manifest, _ = merge_folder_manifest(base, manifest, target)
print(info("merged %s" % manifest))
# TODO: should re-acquire the lock here and fix the following
# Else update base_version
self._base_version = manifest["version"]
# base = await self._fs.manifests_manager.fetch_from_backend(
# access.id, access.rts, access.key, self._base_version-1)
# target = manifest
# # target = await self._fs.manifests_manager.fetch_from_backend(
# # access.id, access.rts, access.key)
# diverged = self
# _, modified = merge_children(base, diverged, target, inplace=self)
# for k, v in self._children.items():
# # TODO: merge_children should take care of this !
# if isinstance(v, dict):
# self._children[k] = self._fs._not_loaded_entry_cls(
# self._vlob_access_cls(**v)
# )
# self._need_sync = modified
self._children = {
k: self._fs._not_loaded_entry_cls(
name=k, parent=self, access=self._fs._vlob_access_cls(**v)
)
for k, v in manifest["children"].items()
}
self._need_sync = False
self._need_flush = True
await self.flush_no_lock()
def _get_child(self, name):
try:
return self._children[name]
except KeyError:
raise FSInvalidPath("Path `%s/%s` doesn't exists" % (self.path, name))
async def fetch_child(self, name):
async with self.acquire_read():
# Check entry exists and return it if already loaded
entry = self._get_child(name)
# If entry hasn't been loaded yet, we must do it now
if not entry.is_loaded:
not_loaded_entry = entry
entry = await not_loaded_entry.load()
# Finally, update the parent to take into account this entry
async with self.acquire_write():
# As usual we must make sure the entry hasn't been tempered
# in the meantime (e.g. entry deleted or moved)
if self._children.get(name) is not_loaded_entry:
self._children[name] = entry
return entry
async def delete_child(self, name):
async with self.acquire_write():
return await self.delete_child_no_lock(name)
async def delete_child_no_lock(self, name):
entry = self._children.pop(name, None)
if not entry:
raise FSInvalidPath("Path `%s/%s` doesn't exists" % (self.path, name))
self._modified()
# Disconnect entry to be able to re-insert it somewhere else
entry._parent = None
return entry
async def insert_child_from_access(self, name, access):
async with self.acquire_write():
return await self.insert_child_from_access_no_lock(name, access)
async def insert_child_from_access_no_lock(self, name, access):
if name in self._children:
raise FSInvalidPath("Path `%s/%s` already exists" % (self.path, name))
child = self._fs._not_loaded_entry_cls(access=access, name=name, parent=self)
self._children[name] = child
self._modified()
return child
async def insert_child(self, name, child):
async with self.acquire_write():
await self.insert_child_no_lock(name, child)
async def insert_child_no_lock(self, name, child):
if child._parent:
raise FSError("Cannot insert %r in %r given it already has a parent" % (child, self))
if name in self._children:
raise FSInvalidPath("Path `%s/%s` already exists" % (self.path, name))
child._parent = self
self._children[name] = child
self._modified()
async def create_folder(self, name):
async with self.acquire_write():
return await self.create_folder_no_lock(name)
async def create_folder_no_lock(self, name):
if name in self._children:
raise FSInvalidPath("Path `%s/%s` already exists" % (self.path, name))
entry = self._fs._folder_entry_cls(
access=self._fs._placeholder_access_cls(),
user_id=self._fs.manifests_manager.device.user_id,
device_name=self._fs.manifests_manager.device.device_name,
name=name,
parent=self,
need_sync=True,
need_flush=True,
)
self._children[name] = entry
self._modified()
return entry
async def create_file(self, name):
async with self.acquire_write():
return await self.create_file_no_lock(name)
async def create_file_no_lock(self, name):
if name in self._children:
raise FSInvalidPath("Path `%s/%s` already exists" % (self.path, name))
entry = self._fs._file_entry_cls(
access=self._fs._placeholder_access_cls(),
user_id=self._fs.manifests_manager.device.user_id,
device_name=self._fs.manifests_manager.device.device_name,
name=name,
parent=self,
need_sync=True,
need_flush=True,
)
self._children[name] = entry
self._modified()
return entry
class BaseRootEntry(BaseFolderEntry):
_last_processed_message = attr.ib()
def __init__(self, *args, last_processed_message=0, **kwargs):
super().__init__(*args, **kwargs)
self._last_processed_message = last_processed_message
async def flush_no_lock(self, recursive=False):
if recursive:
# TODO: recursive lock maybe too violent here (e.g.
# `flush('/', recursive=True)` means we cannot modify / when
# recursively flush children, which maybe numerous...)
for child in self._children.values():
await child.flush(recursive=True)
if not _recursive_need_flush(self):
return
# Serialize as local folder manifest
manifest = {
"format": 1,
"type": "local_user_manifest",
"user_id": self._user_id,
"device_name": self._device_name,
"last_processed_message": self._last_processed_message,
"need_sync": self._need_sync,
"base_version": self._base_version,
"created": self._created,
"updated": self._updated,
"children": {k: v._access.dump(with_type=True) for k, v in self._children.items()},
}
# Save the local folder manifest
print(good("flush %s %s" % (self.path, manifest)))
await self._fs.manifests_manager.flush_user_manifest_on_local(manifest)
self._need_flush = False
async def sync(self, recursive=False, ignore_placeholders=False):
async with self.acquire_write():
if not await _recursive_need_sync(self):
# This folder (and it children) hasn't been modified locally,
# just download last version from the backend if any.
manifest = await self._fs.manifests_manager.fetch_user_manifest_from_backend()
if manifest and manifest["version"] != self.base_version:
self._user_id = manifest["user_id"]
self._device_name = manifest["device_name"]
self._last_processed_message = manifest["last_processed_message"]
self._created = manifest["created"]
self._updated = manifest["updated"]
self._base_version = manifest["version"]
self._children = {
k: self._fs._not_loaded_entry_cls(
name=k, parent=self, access=self._fs._vlob_access_cls(**v)
)
for k, v in manifest["children"].items()
}
return
# TODO: seems not useful...
# await self.flush_no_lock()
# Make a snapshot of ourself to avoid concurrency
# Serialize as local folder manifest
manifest = {
"format": 1,
"type": "user_manifest",
"user_id": self._user_id,
"device_name": self._device_name,
"last_processed_message": self._last_processed_message,
"version": self._base_version + 1,
"created": self._created,
"updated": self._updated,
"children": {},
}
children = self._children.copy()
# TODO: should release the lock here
# Convert placeholder children into proper synchronized children
for name, entry in children.items():
if ignore_placeholders and entry.is_placeholder:
continue
else:
if recursive:
await entry.sync(recursive=True)
else:
await entry.minimal_sync_if_placeholder()
# TODO: Synchronize with up-to-date data and flush to avoid
# having to re-synchronize placeholders
manifest["children"][name] = entry._access.dump(with_type=False)
# Upload the file manifest as new vlob version
while True:
try:
await self._fs.manifests_manager.sync_user_manifest_with_backend(manifest)
print(que("sync %s %s" % (self.path, manifest)))
break
except BackendConcurrencyError:
print(bad("concurrency error sync in root"))
print(info("manifest %s" % manifest))
base = await self._fs.manifests_manager.fetch_user_manifest_from_backend(
version=manifest["version"] - 1
)
if not base:
# base is version 0, which is never stored
base = {"children": {}}
print(info("base %s" % base))
# Fetch last version from the backend and merge with it
# before retrying the synchronization
target = await self._fs.manifests_manager.fetch_user_manifest_from_backend()
print(info("target %s" % target))
manifest["version"] = target["version"] + 1
# 3-ways merge between base, modified and target versions
manifest, _ = merge_folder_manifest(base, manifest, target)
print(info("merged %s" % manifest))
# TODO: should re-acquire the lock here and fix the following
# TODO: If conflict, do a 3-ways merge between base, modified and target versions ?
# Else update base_version
self._base_version = manifest["version"]
# target = manifest
# base = await self._fs.manifests_manager.fetch_user_manifest_from_backend(self._base_version-1)
# if not base:
# # Fake v0 version
# base = {
# 'format': 1,
# 'type': 'user_manifest',
# 'version': 0,
# 'created': target['created'],
# 'updated': target['updated'],
# 'children': {}
# }
# # target = await self._fs.manifests_manager.fetch_user_manifest_from_backend()
# diverged = self
# _, modified = merge_children(base, diverged, target, inplace=self)
# for k, v in self._children.items():
# # TODO: merge_children should take care of this !
# if isinstance(v, dict):
# self._children[k] = self._fs._not_loaded_entry_cls(
# self._fs._vlob_access_cls(**v)
# )
# self._need_sync = modified
self._children = {
k: self._fs._not_loaded_entry_cls(
name=k, parent=self, access=self._fs._vlob_access_cls(**v)
)
for k, v in manifest["children"].items()
}
self._need_sync = False
self._need_flush = True
await self.flush_no_lock()
async def minimal_sync_if_placeholder(self):
raise RuntimeError("Don't do that on root !")