-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
LockHandler.ts
452 lines (374 loc) · 14.7 KB
/
LockHandler.ts
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
import { Dirnames } from './utils/types';
import shim from '../../shim';
import JoplinError from '../../JoplinError';
import time from '../../time';
import { FileApi } from '../../file-api';
import { AppType } from '../../models/Setting';
const { fileExtension, filename } = require('../../path-utils');
export enum LockType {
None = 0,
Sync = 1,
Exclusive = 2,
}
export enum LockClientType {
Desktop = 1,
Mobile = 2,
Cli = 3,
}
export interface Lock {
id?: string;
type: LockType;
clientType: LockClientType;
clientId: string;
updatedTime?: number;
}
function lockIsActive(lock: Lock, currentDate: Date, lockTtl: number): boolean {
return currentDate.getTime() - lock.updatedTime < lockTtl;
}
export function lockNameToObject(name: string, updatedTime: number = null): Lock {
const p = name.split('_');
const lock: Lock = {
id: null,
type: Number(p[0]) as LockType,
clientType: Number(p[1]) as LockClientType,
clientId: p[2],
updatedTime,
};
if (isNaN(lock.clientType)) throw new Error(`Invalid lock client type: ${name}`);
if (isNaN(lock.type)) throw new Error(`Invalid lock type: ${name}`);
return lock;
}
export function appTypeToLockType(appType: AppType): LockClientType {
if (appType === AppType.Desktop) return LockClientType.Desktop;
if (appType === AppType.Mobile) return LockClientType.Mobile;
if (appType === AppType.Cli) return LockClientType.Cli;
throw new Error(`Invalid app type: ${appType}`);
}
export function hasActiveLock(locks: Lock[], currentDate: Date, lockTtl: number, lockType: LockType, clientType: LockClientType = null, clientId: string = null) {
const lock = activeLock(locks, currentDate, lockTtl, lockType, clientType, clientId);
return !!lock;
}
// Finds if there's an active lock for this clientType and clientId and returns it.
// If clientType and clientId are not specified, returns the first active lock
// of that type instead.
export function activeLock(locks: Lock[], currentDate: Date, lockTtl: number, lockType: LockType, clientType: LockClientType = null, clientId: string = null) {
if (lockType === LockType.Exclusive) {
const activeLocks = locks
.slice()
.filter((lock: Lock) => lockIsActive(lock, currentDate, lockTtl) && lock.type === lockType)
.sort((a: Lock, b: Lock) => {
if (a.updatedTime === b.updatedTime) {
return a.clientId < b.clientId ? -1 : +1;
}
return a.updatedTime < b.updatedTime ? -1 : +1;
});
if (!activeLocks.length) return null;
const lock = activeLocks[0];
if (clientType && clientType !== lock.clientType) return null;
if (clientId && clientId !== lock.clientId) return null;
return lock;
} else if (lockType === LockType.Sync) {
for (const lock of locks) {
if (lock.type !== lockType) continue;
if (clientType && lock.clientType !== clientType) continue;
if (clientId && lock.clientId !== clientId) continue;
if (lockIsActive(lock, currentDate, lockTtl)) return lock;
}
return null;
}
throw new Error(`Unsupported lock type: ${lockType}`);
}
export interface AcquireLockOptions {
// In theory, a client that tries to acquire an exclusive lock shouldn't
// also have a sync lock. It can however happen when the app is closed
// before the end of the sync process, and then the user tries to upgrade
// the sync target.
//
// So maybe we could always automatically clear the sync locks (that belongs
// to the current client) when acquiring an exclusive lock, but to be safe
// we make the behaviour explicit via this option. It is used for example
// when migrating a sync target.
//
// https://discourse.joplinapp.org/t/error-upgrading-to-2-3-3/19549/4?u=laurent
clearExistingSyncLocksFromTheSameClient?: boolean;
timeoutMs?: number;
}
function defaultAcquireLockOptions(): AcquireLockOptions {
return {
clearExistingSyncLocksFromTheSameClient: false,
timeoutMs: 0,
};
}
interface RefreshTimer {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
id: any;
inProgress: boolean;
}
interface RefreshTimers {
[key: string]: RefreshTimer;
}
export interface LockHandlerOptions {
autoRefreshInterval?: number;
lockTtl?: number;
}
export const defaultLockTtl = 1000 * 60 * 3;
export default class LockHandler {
private api_: FileApi = null;
private refreshTimers_: RefreshTimers = {};
private autoRefreshInterval_: number = 1000 * 60;
private lockTtl_: number = defaultLockTtl;
public constructor(api: FileApi, options: LockHandlerOptions = null) {
if (!options) options = {};
this.api_ = api;
if ('lockTtl' in options) this.lockTtl_ = options.lockTtl;
if ('autoRefreshInterval' in options) this.autoRefreshInterval_ = options.autoRefreshInterval;
}
public get lockTtl(): number {
return this.lockTtl_;
}
// Should only be done for testing purposes since all clients should
// use the same lock max age.
public set lockTtl(v: number) {
this.lockTtl_ = v;
}
public get useBuiltInLocks() {
return this.api_.supportsLocks;
}
private lockFilename(lock: Lock) {
return `${[lock.type, lock.clientType, lock.clientId].join('_')}.json`;
}
private lockTypeFromFilename(name: string): LockType {
const ext = fileExtension(name);
if (ext !== 'json') return LockType.None;
if (name.indexOf(LockType.Sync.toString()) === 0) return LockType.Sync;
if (name.indexOf(LockType.Exclusive.toString()) === 0) return LockType.Exclusive;
return LockType.None;
}
private lockFilePath(lock: Lock) {
return `${Dirnames.Locks}/${this.lockFilename(lock)}`;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private lockFileToObject(file: any): Lock {
return lockNameToObject(filename(file.path), file.updated_time);
}
public async locks(lockType: LockType = null): Promise<Lock[]> {
if (this.useBuiltInLocks) {
const locks = (await this.api_.listLocks()).items;
return locks;
}
const result = await this.api_.list(Dirnames.Locks);
if (result.hasMore) throw new Error('hasMore not handled'); // Shouldn't happen anyway
const output = [];
for (const file of result.items) {
const type = this.lockTypeFromFilename(file.path);
if (type === LockType.None) continue;
if (lockType && type !== lockType) continue;
const lock = this.lockFileToObject(file);
output.push(lock);
}
return output;
}
private async saveLock(lock: Lock) {
await this.api_.put(this.lockFilePath(lock), JSON.stringify(lock));
}
// This is for testing only
public async saveLock_(lock: Lock) {
return this.saveLock(lock);
}
private async acquireSyncLock(clientType: LockClientType, clientId: string): Promise<Lock> {
if (this.useBuiltInLocks) return this.api_.acquireLock(LockType.Sync, clientType, clientId);
try {
let isFirstPass = true;
while (true) {
const locks = await this.locks();
const currentDate = await this.currentDate();
const [exclusiveLock, syncLock] = await Promise.all([
activeLock(locks, currentDate, this.lockTtl, LockType.Exclusive),
activeLock(locks, currentDate, this.lockTtl, LockType.Sync, clientType, clientId),
]);
if (exclusiveLock) {
throw new JoplinError(`Cannot acquire sync lock because the following client has an exclusive lock on the sync target: ${this.lockToClientString(exclusiveLock)}`, 'hasExclusiveLock');
}
if (syncLock) {
// Normally the second pass should happen immediately afterwards, but if for some reason
// (slow network, etc.) it took more than 10 seconds then refresh the lock.
if (isFirstPass || Date.now() - syncLock.updatedTime > 1000 * 10) {
await this.saveLock(syncLock);
}
return syncLock;
}
// Something wrong happened, which means we saved a lock but we didn't read
// it back. Could be application error or server issue.
if (!isFirstPass) throw new Error('Cannot acquire sync lock: either the lock could be written but not read back. Or it was expired before it was read again.');
await this.saveLock({
type: LockType.Sync,
clientType: clientType,
clientId: clientId,
});
isFirstPass = false;
}
} catch (error) {
await this.releaseLock(LockType.Sync, clientType, clientId);
throw error;
}
}
private lockToClientString(lock: Lock): string {
return `(${lock.clientType} #${lock.clientId})`;
}
private async acquireExclusiveLock(clientType: LockClientType, clientId: string, options: AcquireLockOptions = null): Promise<Lock> {
if (this.useBuiltInLocks) return this.api_.acquireLock(LockType.Exclusive, clientType, clientId);
// The logic to acquire an exclusive lock, while avoiding race conditions is as follow:
//
// - Check if there is a lock file present
//
// - If there is a lock file, see if I'm the one owning it by checking that its content has my identifier.
// - If that's the case, just write to the data file then delete the lock file.
// - If that's not the case, just wait a second or a small random length of time and try the whole cycle again-.
//
// -If there is no lock file, create one with my identifier and try the whole cycle again to avoid race condition (re-check that the lock file is really mine)-.
options = {
...defaultAcquireLockOptions(),
...options,
};
const startTime = Date.now();
async function waitForTimeout() {
if (!options.timeoutMs) return false;
const elapsed = Date.now() - startTime;
if (options.timeoutMs && elapsed < options.timeoutMs) {
await time.sleep(2);
return true;
}
return false;
}
try {
while (true) {
const locks = await this.locks();
const currentDate = await this.currentDate();
const [activeSyncLock, activeExclusiveLock] = await Promise.all([
activeLock(locks, currentDate, this.lockTtl, LockType.Sync),
activeLock(locks, currentDate, this.lockTtl, LockType.Exclusive),
]);
if (activeSyncLock) {
if (options.clearExistingSyncLocksFromTheSameClient && activeSyncLock.clientId === clientId && activeSyncLock.clientType === clientType) {
await this.releaseLock(LockType.Sync, clientType, clientId);
} else {
if (await waitForTimeout()) continue;
throw new JoplinError(`Cannot acquire exclusive lock because the following clients have a sync lock on the target: ${this.lockToClientString(activeSyncLock)}`, 'hasSyncLock');
}
}
if (activeExclusiveLock) {
if (activeExclusiveLock.clientId === clientId) {
// Save it again to refresh the timestamp
await this.saveLock(activeExclusiveLock);
return activeExclusiveLock;
} else {
// If there's already an exclusive lock, wait for it to be released
if (await waitForTimeout()) continue;
throw new JoplinError(`Cannot acquire exclusive lock because the following client has an exclusive lock on the sync target: ${this.lockToClientString(activeExclusiveLock)}`, 'hasExclusiveLock');
}
} else {
// If there's not already an exclusive lock, acquire one
// then loop again to check that we really got the lock
// (to prevent race conditions)
await this.saveLock({
type: LockType.Exclusive,
clientType: clientType,
clientId: clientId,
});
await time.msleep(100);
}
}
} catch (error) {
await this.releaseLock(LockType.Exclusive, clientType, clientId);
throw error;
}
}
private autoLockRefreshHandle(lock: Lock) {
return [lock.type, lock.clientType, lock.clientId].join('_');
}
public async currentDate() {
return this.api_.remoteDate();
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public startAutoLockRefresh(lock: Lock, errorHandler: Function): string {
const handle = this.autoLockRefreshHandle(lock);
if (this.refreshTimers_[handle]) {
throw new Error(`There is already a timer refreshing this lock: ${handle}`);
}
this.refreshTimers_[handle] = {
id: null,
inProgress: false,
};
this.refreshTimers_[handle].id = shim.setInterval(async () => {
if (this.refreshTimers_[handle].inProgress) return;
const defer = () => {
if (!this.refreshTimers_[handle]) return;
this.refreshTimers_[handle].inProgress = false;
};
this.refreshTimers_[handle].inProgress = true;
let error = null;
if (!this.refreshTimers_[handle]) return defer(); // Timeout has been cleared
const locks = await this.locks(lock.type);
if (!hasActiveLock(locks, await this.currentDate(), this.lockTtl, lock.type, lock.clientType, lock.clientId)) {
// If the previous lock has expired, we shouldn't try to acquire a new one. This is because other clients might have performed
// in the meantime operations that invalidates the current operation. For example, another client might have upgraded the
// sync target in the meantime, so any active operation should be cancelled here. Or if the current client was upgraded
// the sync target, another client might have synced since then, making any cached data invalid.
// In some cases it should be safe to re-acquire a lock but adding support for this would make the algorithm more complex
// without much benefits.
error = new JoplinError('Lock has expired', 'lockExpired');
} else {
try {
await this.acquireLock(lock.type, lock.clientType, lock.clientId);
if (!this.refreshTimers_[handle]) return defer(); // Timeout has been cleared
} catch (e) {
error = e;
}
}
if (error) {
if (this.refreshTimers_[handle]) {
shim.clearInterval(this.refreshTimers_[handle].id);
delete this.refreshTimers_[handle];
}
errorHandler(error);
}
defer();
}, this.autoRefreshInterval_);
return handle;
}
public stopAutoLockRefresh(lock: Lock) {
const handle = this.autoLockRefreshHandle(lock);
if (!this.refreshTimers_[handle]) {
// Should not throw an error because lock may have been cleared in startAutoLockRefresh
// if there was an error.
// throw new Error(`There is no such lock being auto-refreshed: ${this.lockToString(lock)}`);
return;
}
shim.clearInterval(this.refreshTimers_[handle].id);
delete this.refreshTimers_[handle];
}
public async acquireLock(lockType: LockType, clientType: LockClientType, clientId: string, options: AcquireLockOptions = null): Promise<Lock> {
options = {
...defaultAcquireLockOptions(),
...options,
};
if (lockType === LockType.Sync) {
return this.acquireSyncLock(clientType, clientId);
} else if (lockType === LockType.Exclusive) {
return this.acquireExclusiveLock(clientType, clientId, options);
} else {
throw new Error(`Invalid lock type: ${lockType}`);
}
}
public async releaseLock(lockType: LockType, clientType: LockClientType, clientId: string) {
if (this.useBuiltInLocks) {
await this.api_.releaseLock(lockType, clientType, clientId);
return;
}
await this.api_.delete(this.lockFilePath({
type: lockType,
clientType: clientType,
clientId: clientId,
}));
}
}