• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tomdesair / tus-java-server / 43579910339

14 Nov 2026 09:18AM UTC coverage: 89.558% (-0.3%) from 89.87%
43579910339

Pull #152

github

web-flow
Merge 0171156dc into bf81f66f9
Pull Request #152: Fix distributed cloud performance, security, and robustness concerns

3456 of 4120 branches covered (83.88%)

Branch coverage included in aggregate %.

519 of 563 new or added lines in 39 files covered. (92.18%)

7 existing lines in 3 files now uncovered.

7033 of 7592 relevant lines covered (92.64%)

5.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.66
/src/main/java/me/desair/tus/server/upload/cache/ThreadLocalCachedStorageAndLockingService.java
1
package me.desair.tus.server.upload.cache;
2

3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.OutputStream;
6
import java.lang.ref.WeakReference;
7
import java.time.Duration;
8
import java.util.Objects;
9
import me.desair.tus.server.checksum.ChecksumAlgorithm;
10
import me.desair.tus.server.checksum.ChecksumKey;
11
import me.desair.tus.server.exception.TusException;
12
import me.desair.tus.server.exception.UploadNotFoundException;
13
import me.desair.tus.server.upload.UploadId;
14
import me.desair.tus.server.upload.UploadIdFactory;
15
import me.desair.tus.server.upload.UploadInfo;
16
import me.desair.tus.server.upload.UploadLock;
17
import me.desair.tus.server.upload.UploadLockingService;
18
import me.desair.tus.server.upload.UploadStorageService;
19
import me.desair.tus.server.upload.concatenation.UploadConcatenationService;
20

21
/**
22
 * Combined implementation of {@link UploadStorageService} and {@link UploadLockingService}. Uses
23
 * both of them as delegates but allowing to reduce disk operations during a request processing by
24
 * caching UploadInfo in the memory. UploadLockingService service is used as a delegate to cleanup
25
 * cached data on releasing a lock.
26
 */
27
public class ThreadLocalCachedStorageAndLockingService
28
    implements UploadLockingService, UploadStorageService {
29

30
  private final ThreadLocal<WeakReference<UploadInfo>> uploadInfoCache = new ThreadLocal<>();
10 ✔
31
  private final UploadLockingService lockingServiceDelegate;
32
  private final UploadStorageService storageServiceDelegate;
33
  private UploadIdFactory idFactory;
34

35
  /** Constructor of ThreadLocalCachedStorageAndLockingService. */
36
  public ThreadLocalCachedStorageAndLockingService(
37
      UploadStorageService storageServiceDelegate, UploadLockingService lockingServiceDelegate) {
4 ✔
38
    if (storageServiceDelegate.getClass() == ThreadLocalCachedStorageAndLockingService.class) {
8 ✔
39
      this.storageServiceDelegate =
6 ✔
40
          ((ThreadLocalCachedStorageAndLockingService) storageServiceDelegate)
41
              .storageServiceDelegate;
42
    } else {
43
      this.storageServiceDelegate = storageServiceDelegate;
6 ✔
44
    }
45
    if (lockingServiceDelegate.getClass() == ThreadLocalCachedStorageAndLockingService.class) {
8 ✔
46
      this.lockingServiceDelegate =
6 ✔
47
          ((ThreadLocalCachedStorageAndLockingService) lockingServiceDelegate)
48
              .lockingServiceDelegate;
49
    } else {
50
      this.lockingServiceDelegate = lockingServiceDelegate;
6 ✔
51
    }
52
  }
2 ✔
53

54
  /**
55
   * Returns the underlying wrapped {@link UploadStorageService} delegate.
56
   *
57
   * @return the storage service delegate
58
   */
59
  public UploadStorageService getStorageServiceDelegate() {
60
    return storageServiceDelegate;
6 ✔
61
  }
62

63
  /**
64
   * Returns the underlying wrapped {@link UploadLockingService} delegate.
65
   *
66
   * @return the locking service delegate
67
   */
68
  public UploadLockingService getLockingServiceDelegate() {
69
    return lockingServiceDelegate;
6 ✔
70
  }
71

72
  @Override
73
  public UploadInfo getUploadInfo(UploadId id) throws IOException {
74
    if (id == null) {
4 ✔
75
      return null;
4 ✔
76
    }
77
    UploadInfo uploadInfo;
78
    WeakReference<UploadInfo> ref = uploadInfoCache.get();
10 ✔
79
    if (ref == null || (uploadInfo = ref.get()) == null || !id.equals(uploadInfo.getId())) {
26 ✔
80
      uploadInfo = storageServiceDelegate.getUploadInfo(id);
10 ✔
81
      uploadInfoCache.set(new WeakReference<>(uploadInfo));
14 ✔
82
    }
83
    return uploadInfo;
4 ✔
84
  }
85

86
  @Override
87
  public UploadInfo getUploadInfo(String uploadUrl, String ownerKey) throws IOException {
88
    // Skip cache lookup when idFactory has not been wired yet (e.g., an external cache wrapper
89
    // passed into VirtualConcatenationService before setIdFactory runs).
90
    UploadInfo uploadInfo =
91
        idFactory != null ? getUploadInfo(idFactory.readUploadId(uploadUrl)) : null;
23 ✔
92
    if (uploadInfo == null || !Objects.equals(uploadInfo.getOwnerKey(), ownerKey)) {
14 ✔
93
      uploadInfo = storageServiceDelegate.getUploadInfo(uploadUrl, ownerKey);
12 ✔
94
      uploadInfoCache.set(new WeakReference<>(uploadInfo));
14 ✔
95
    }
96
    return uploadInfo;
4 ✔
97
  }
98

99
  @Override
100
  public void update(UploadInfo uploadInfo) throws IOException, UploadNotFoundException {
101
    try {
102
      storageServiceDelegate.update(uploadInfo);
8 ✔
103
      uploadInfoCache.set(new WeakReference<>(uploadInfo));
14 ✔
104
    } catch (IOException | UploadNotFoundException | RuntimeException e) {
1 ✔
105
      // Clear cached state so failure recovery reads the true persisted metadata from storage.
106
      cleanupCache();
2 ✔
107
      throw e;
2 ✔
108
    }
2 ✔
109
  }
2 ✔
110

111
  @Override
112
  public void setIdFactory(UploadIdFactory idFactory) {
113
    this.idFactory = idFactory;
6 ✔
114
    this.storageServiceDelegate.setIdFactory(idFactory);
8 ✔
115
    this.lockingServiceDelegate.setIdFactory(idFactory);
8 ✔
116
  }
2 ✔
117

118
  @Override
119
  public String getUploadUri() {
120
    return storageServiceDelegate.getUploadUri();
8 ✔
121
  }
122

123
  @Override
124
  public UploadInfo append(UploadInfo upload, InputStream inputStream)
125
      throws IOException, TusException {
126
    try {
127
      UploadInfo info = storageServiceDelegate.append(upload, inputStream);
12 ✔
128
      uploadInfoCache.set(new WeakReference<>(info));
14 ✔
129
      return info;
4 ✔
130
    } catch (IOException | TusException | RuntimeException e) {
2 ✔
131
      // Evict stale pre-append UploadInfo so error handling calculates rolled-back bytes from
132
      // the actual persisted offset rather than the stale cached offset.
133
      cleanupCache();
4 ✔
134
      throw e;
4 ✔
135
    }
136
  }
137

138
  @Override
139
  public void setMaxUploadSize(Long maxUploadSize) {
140
    storageServiceDelegate.setMaxUploadSize(maxUploadSize);
8 ✔
141
  }
2 ✔
142

143
  @Override
144
  public long getMaxUploadSize() {
145
    return storageServiceDelegate.getMaxUploadSize();
8 ✔
146
  }
147

148
  @Override
149
  public void setMaxAppendSize(Long maxAppendSize) {
150
    storageServiceDelegate.setMaxAppendSize(maxAppendSize);
4 ✔
151
  }
1 ✔
152

153
  @Override
154
  public Long getMaxAppendSize() {
155
    return storageServiceDelegate.getMaxAppendSize();
8 ✔
156
  }
157

158
  @Override
159
  public void setMinAppendSize(Long minAppendSize) {
160
    storageServiceDelegate.setMinAppendSize(minAppendSize);
4 ✔
161
  }
1 ✔
162

163
  @Override
164
  public Long getMinAppendSize() {
165
    return storageServiceDelegate.getMinAppendSize();
8 ✔
166
  }
167

168
  @Override
169
  public void setMinSize(Long minSize) {
170
    storageServiceDelegate.setMinSize(minSize);
4 ✔
171
  }
1 ✔
172

173
  @Override
174
  public Long getMinSize() {
175
    return storageServiceDelegate.getMinSize();
8 ✔
176
  }
177

178
  @Override
179
  public UploadInfo create(UploadInfo info, String ownerKey) throws IOException {
180
    UploadInfo uploadInfo = storageServiceDelegate.create(info, ownerKey);
12 ✔
181
    uploadInfoCache.set(new WeakReference<>(uploadInfo));
14 ✔
182
    return uploadInfo;
4 ✔
183
  }
184

185
  @Override
186
  public InputStream getUploadedBytes(String uploadUri, String ownerKey)
187
      throws IOException, UploadNotFoundException {
188
    return storageServiceDelegate.getUploadedBytes(uploadUri, ownerKey);
12 ✔
189
  }
190

191
  @Override
192
  public InputStream getUploadedBytes(UploadId id) throws IOException, UploadNotFoundException {
193
    return storageServiceDelegate.getUploadedBytes(id);
10 ✔
194
  }
195

196
  @Override
197
  public void copyUploadTo(UploadInfo info, OutputStream outputStream)
198
      throws UploadNotFoundException, IOException {
199
    storageServiceDelegate.copyUploadTo(info, outputStream);
10 ✔
200
    uploadInfoCache.set(new WeakReference<>(info));
14 ✔
201
  }
2 ✔
202

203
  @Override
204
  public void cleanupExpiredUploads() throws IOException {
205
    storageServiceDelegate.cleanupExpiredUploads();
6 ✔
206
    // Since any cached uploads was potentially removed by the storage service
207
    // we clean the cache to prevent any stale state
208
    cleanupCache();
4 ✔
209
  }
2 ✔
210

211
  @Override
212
  @Deprecated
213
  public void cleanupExpiredUploads(UploadLockingService uploadLockingService) throws IOException {
214
    storageServiceDelegate.cleanupExpiredUploads(uploadLockingService);
4 ✔
215
    // Since any cached uploads was potentially removed by the storage service
216
    // we clean the cache to prevent any stale state
217
    cleanupCache();
2 ✔
218
  }
1 ✔
219

220
  @Override
221
  public void removeLastNumberOfBytes(UploadInfo uploadInfo, long byteCount)
222
      throws UploadNotFoundException, IOException {
223
    try {
224
      storageServiceDelegate.removeLastNumberOfBytes(uploadInfo, byteCount);
10 ✔
225
      uploadInfoCache.set(new WeakReference<>(uploadInfo));
14 ✔
226
    } catch (IOException | UploadNotFoundException | RuntimeException e) {
1 ✔
227
      cleanupCache();
2 ✔
228
      throw e;
2 ✔
229
    }
2 ✔
230
  }
2 ✔
231

232
  @Override
233
  public void terminateUpload(UploadInfo uploadInfo) throws UploadNotFoundException, IOException {
234
    storageServiceDelegate.terminateUpload(uploadInfo);
8 ✔
235
    // Since the upload is terminated and potentially removed by the storage service
236
    // we clean the cache to prevent any stale state
237
    cleanupCache();
4 ✔
238
  }
2 ✔
239

240
  @Override
241
  public Long getUploadExpirationPeriod() {
242
    return storageServiceDelegate.getUploadExpirationPeriod();
8 ✔
243
  }
244

245
  @Override
246
  public void setUploadExpirationPeriod(Long uploadExpirationPeriod) {
247
    storageServiceDelegate.setUploadExpirationPeriod(uploadExpirationPeriod);
8 ✔
248
  }
2 ✔
249

250
  @Override
251
  public void setUploadConcatenationService(UploadConcatenationService concatenationService) {
252
    storageServiceDelegate.setUploadConcatenationService(concatenationService);
8 ✔
253
  }
2 ✔
254

255
  @Override
256
  public UploadConcatenationService getUploadConcatenationService() {
257
    return storageServiceDelegate.getUploadConcatenationService();
8 ✔
258
  }
259

260
  @Override
261
  public void setUploadDeduplicationEnabled(boolean enabled) {
262
    storageServiceDelegate.setUploadDeduplicationEnabled(enabled);
8 ✔
263
  }
2 ✔
264

265
  @Override
266
  public boolean isUploadDeduplicationEnabled() {
267
    return storageServiceDelegate.isUploadDeduplicationEnabled();
4 ✔
268
  }
269

270
  @Override
271
  public void setJsonSerializationEnabled(boolean enabled) {
UNCOV
272
    storageServiceDelegate.setJsonSerializationEnabled(enabled);
×
UNCOV
273
  }
×
274

275
  @Override
276
  public boolean isJsonSerializationEnabled() {
UNCOV
277
    return storageServiceDelegate.isJsonSerializationEnabled();
×
278
  }
279

280
  @Override
281
  public UploadInfo getUploadInfoByChecksum(String checksum, ChecksumAlgorithm algorithm)
282
      throws IOException {
283
    return storageServiceDelegate.getUploadInfoByChecksum(checksum, algorithm);
12 ✔
284
  }
285

286
  @Override
287
  public UploadInfo deduplicate(UploadInfo uploadInfo, ChecksumKey checksumKey) throws IOException {
288
    try {
NEW
289
      UploadInfo result = storageServiceDelegate.deduplicate(uploadInfo, checksumKey);
×
NEW
290
      if (result != null) {
×
NEW
291
        uploadInfoCache.set(new WeakReference<>(result));
×
292
      }
NEW
293
      return result;
×
294
    } catch (IOException | RuntimeException e) {
1 ✔
295
      cleanupCache();
2 ✔
296
      throw e;
2 ✔
297
    }
298
  }
299

300
  @Override
301
  public void setUploadLockingService(UploadLockingService uploadLockingService) {
UNCOV
302
    storageServiceDelegate.setUploadLockingService(uploadLockingService);
×
UNCOV
303
  }
×
304

305
  @Override
306
  public UploadLockingService getUploadLockingService() {
307
    return storageServiceDelegate.getUploadLockingService();
4 ✔
308
  }
309

310
  @Override
311
  public boolean dataExists(UploadId id) throws IOException {
312
    return storageServiceDelegate.dataExists(id);
5 ✔
313
  }
314

315
  @Override
316
  public void deleteData(UploadId id) throws IOException {
317
    storageServiceDelegate.deleteData(id);
4 ✔
318
  }
1 ✔
319

320
  @Override
321
  public void setCloudUploadThreadPoolSize(int size) {
322
    storageServiceDelegate.setCloudUploadThreadPoolSize(size);
4 ✔
323
  }
1 ✔
324

325
  @Override
326
  public int getCloudUploadThreadPoolSize() {
327
    return storageServiceDelegate.getCloudUploadThreadPoolSize();
4 ✔
328
  }
329

330
  @Override
331
  public void setDrainTimeout(Duration drainTimeout) {
332
    storageServiceDelegate.setDrainTimeout(drainTimeout);
4 ✔
333
  }
1 ✔
334

335
  @Override
336
  public Duration getDrainTimeout() {
337
    return storageServiceDelegate.getDrainTimeout();
4 ✔
338
  }
339

340
  @Override
341
  public UploadLock lockUploadByUri(String requestUri) throws TusException, IOException {
342
    // Evict any unlocked pre-lock cached state (e.g. populated during getRawInterimResponse)
343
    // so the first read inside the exclusive lock always fetches fresh state from storage.
344
    cleanupCache();
4 ✔
345
    UploadLock uploadLock = lockingServiceDelegate.lockUploadByUri(requestUri);
10 ✔
346
    return new CachedLock(uploadLock);
12 ✔
347
  }
348

349
  @Override
350
  public UploadLock lockUpload(UploadId id) throws TusException, IOException {
351
    // Evict any unlocked pre-lock cached state before entering the exclusive lock scope.
352
    cleanupCache();
2 ✔
353
    UploadLock uploadLock = lockingServiceDelegate.lockUpload(id);
5 ✔
354
    return new CachedLock(uploadLock);
6 ✔
355
  }
356

357
  @Override
358
  public void cleanupStaleLocks() throws IOException {
359
    lockingServiceDelegate.cleanupStaleLocks();
6 ✔
360
    cleanupCache();
4 ✔
361
  }
2 ✔
362

363
  @Override
364
  public boolean isLocked(UploadId id) {
365
    return lockingServiceDelegate.isLocked(id);
10 ✔
366
  }
367

368
  @Override
369
  public void registerInputStream(String requestUri, InputStream inputStream) {
370
    lockingServiceDelegate.registerInputStream(requestUri, inputStream);
10 ✔
371
  }
2 ✔
372

373
  @Override
374
  public void unregisterInputStream(String requestUri) {
375
    lockingServiceDelegate.unregisterInputStream(requestUri);
8 ✔
376
  }
2 ✔
377

378
  @Override
379
  public void requestLockRelease(String requestUri) {
380
    lockingServiceDelegate.requestLockRelease(requestUri);
8 ✔
381
  }
2 ✔
382

383
  @Override
384
  public void cleanupLock(UploadId id) throws IOException {
385
    lockingServiceDelegate.cleanupLock(id);
4 ✔
386
    cleanupCache();
2 ✔
387
  }
1 ✔
388

389
  @Override
390
  public void close() throws IOException {
391
    try {
392
      lockingServiceDelegate.close();
6 ✔
393
    } finally {
394
      try {
395
        storageServiceDelegate.close();
6 ✔
396
      } finally {
397
        cleanupCache();
4 ✔
398
      }
399
    }
400
  }
2 ✔
401

402
  private void cleanupCache() {
403
    WeakReference<UploadInfo> ref = uploadInfoCache.get();
10 ✔
404
    if (ref != null) {
4 ✔
405
      uploadInfoCache.remove();
6 ✔
406
      ref.clear();
4 ✔
407
    }
408
  }
2 ✔
409

410
  class CachedLock implements UploadLock {
411

412
    private final UploadLock delegate;
413

414
    CachedLock(UploadLock delegate) {
10 ✔
415
      this.delegate = delegate;
6 ✔
416
    }
2 ✔
417

418
    @Override
419
    public String getUploadUri() {
420
      return delegate != null ? delegate.getUploadUri() : null;
8 !
421
    }
422

423
    @Override
424
    public void release() {
425
      try {
426
        if (delegate != null) {
3 !
427
          delegate.release();
3 ✔
428
        }
429
      } finally {
430
        // Clean up thread-local upload info cache when release() is called
431
        // to prevent ThreadLocal memory leaks across pooled servlet threads.
432
        cleanupCache();
3 ✔
433
      }
434
    }
1 ✔
435

436
    @Override
437
    public void close() throws IOException {
438
      try {
439
        if (delegate != null) {
6 ✔
440
          delegate.close();
6 ✔
441
        }
442
      } finally {
443
        cleanupCache();
6 ✔
444
      }
445
    }
2 ✔
446
  }
447
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc