|
20 | 20 | import stroom.task.api.TaskContextFactory;
|
21 | 21 | import stroom.util.io.FileUtil;
|
22 | 22 | import stroom.util.io.TempDirProvider;
|
| 23 | +import stroom.util.logging.LambdaLogger; |
| 24 | +import stroom.util.logging.LambdaLoggerFactory; |
23 | 25 | import stroom.util.shared.ResourceKey;
|
24 | 26 |
|
25 | 27 | import jakarta.inject.Inject;
|
|
29 | 31 | import java.io.UncheckedIOException;
|
30 | 32 | import java.nio.file.Files;
|
31 | 33 | import java.nio.file.Path;
|
32 |
| -import java.nio.file.Paths; |
33 |
| -import java.util.HashSet; |
34 |
| -import java.util.Set; |
| 34 | +import java.time.Instant; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.UUID; |
| 37 | +import java.util.concurrent.ConcurrentHashMap; |
35 | 38 |
|
36 | 39 | /**
|
37 |
| - * Simple Store that gives you 1 hour to use your temp file and then it deletes |
38 |
| - * it. |
| 40 | + * Simple Store that gives you 1 hour to use your temp file before it deletes it. |
39 | 41 | */
|
40 | 42 | @Singleton
|
41 | 43 | public class ResourceStoreImpl implements ResourceStore {
|
42 | 44 |
|
| 45 | + private static final LambdaLogger LOGGER = LambdaLoggerFactory.getLogger(ResourceStoreImpl.class); |
| 46 | + |
43 | 47 | private final TempDirProvider tempDirProvider;
|
44 | 48 | private final TaskContextFactory taskContextFactory;
|
| 49 | + private final Map<String, ResourceItem> currentFiles = new ConcurrentHashMap<>(); |
45 | 50 |
|
46 |
| - private Set<ResourceKey> currentFiles = new HashSet<>(); |
47 |
| - private Set<ResourceKey> oldFiles = new HashSet<>(); |
48 |
| - private long sequence; |
| 51 | + private volatile Instant lastCleanupTime; |
49 | 52 |
|
50 | 53 | @Inject
|
51 | 54 | public ResourceStoreImpl(final TempDirProvider tempDirProvider,
|
@@ -73,47 +76,108 @@ void shutdown() {
|
73 | 76 | }
|
74 | 77 |
|
75 | 78 | @Override
|
76 |
| - public synchronized ResourceKey createTempFile(final String name) { |
77 |
| - final String fileName = FileUtil.getCanonicalPath(getTempDir().resolve((sequence++) + name)); |
78 |
| - final ResourceKey resourceKey = new ResourceKey(name, fileName); |
79 |
| - currentFiles.add(resourceKey); |
80 |
| - |
| 79 | + public ResourceKey createTempFile(final String name) { |
| 80 | + final String uuid = UUID.randomUUID().toString(); |
| 81 | + final Path path = getTempDir().resolve(uuid); |
| 82 | + final ResourceKey resourceKey = new ResourceKey(uuid, name); |
| 83 | + final ResourceItem resourceItem = new ResourceItem(resourceKey, path, Instant.now()); |
| 84 | + currentFiles.put(uuid, resourceItem); |
81 | 85 | return resourceKey;
|
82 | 86 | }
|
83 | 87 |
|
84 | 88 | @Override
|
85 |
| - public synchronized void deleteTempFile(final ResourceKey resourceKey) { |
86 |
| - currentFiles.remove(resourceKey); |
87 |
| - oldFiles.remove(resourceKey); |
88 |
| - final Path file = Paths.get(resourceKey.getKey()); |
89 |
| - try { |
90 |
| - Files.deleteIfExists(file); |
91 |
| - } catch (final IOException e) { |
92 |
| - throw new UncheckedIOException(e); |
| 89 | + public void deleteTempFile(final ResourceKey resourceKey) { |
| 90 | + final ResourceItem resourceItem = currentFiles.remove(resourceKey.getKey()); |
| 91 | + if (resourceItem != null) { |
| 92 | + final Path file = resourceItem.getPath(); |
| 93 | + try { |
| 94 | + Files.deleteIfExists(file); |
| 95 | + } catch (final IOException e) { |
| 96 | + throw new UncheckedIOException(e); |
| 97 | + } |
93 | 98 | }
|
94 | 99 | }
|
95 | 100 |
|
96 | 101 | @Override
|
97 |
| - public synchronized Path getTempFile(final ResourceKey resourceKey) { |
| 102 | + public Path getTempFile(final ResourceKey resourceKey) { |
98 | 103 | // File gone !
|
99 |
| - if (!currentFiles.contains(resourceKey) && !oldFiles.contains(resourceKey)) { |
| 104 | + final ResourceItem resourceItem = currentFiles.get(resourceKey.getKey()); |
| 105 | + if (resourceItem == null) { |
100 | 106 | return null;
|
101 | 107 | }
|
102 |
| - return Paths.get(resourceKey.getKey()); |
| 108 | + resourceItem.setLastAccessTime(Instant.now()); |
| 109 | + return resourceItem.getPath(); |
103 | 110 | }
|
104 | 111 |
|
105 | 112 | void execute() {
|
106 | 113 | taskContextFactory.current().info(() -> "Deleting temp files");
|
107 |
| - flipStore(); |
| 114 | + cleanup(); |
108 | 115 | }
|
109 | 116 |
|
110 | 117 | /**
|
111 |
| - * Move the current files to the old files deleting the old ones. |
| 118 | + * Delete files that haven't been accessed since the last cleanup. |
| 119 | + * This allows us to choose the cleanup frequency. |
112 | 120 | */
|
113 |
| - private synchronized void flipStore() { |
114 |
| - final Set<ResourceKey> clonedOldFiles = new HashSet<>(oldFiles); |
115 |
| - clonedOldFiles.forEach(this::deleteTempFile); |
116 |
| - oldFiles = currentFiles; |
117 |
| - currentFiles = new HashSet<>(); |
| 121 | + private synchronized void cleanup() { |
| 122 | + if (lastCleanupTime != null) { |
| 123 | + // Delete anything that hasn't been accessed since we were last asked to cleanup. |
| 124 | + currentFiles.values().forEach(resourceItem -> { |
| 125 | + try { |
| 126 | + if (resourceItem.getLastAccessTime().isBefore(lastCleanupTime)) { |
| 127 | + deleteTempFile(resourceItem.getResourceKey()); |
| 128 | + } |
| 129 | + } catch (final RuntimeException e) { |
| 130 | + LOGGER.error(e::getMessage, e); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + lastCleanupTime = Instant.now(); |
| 135 | + } |
| 136 | + |
| 137 | + private static class ResourceItem { |
| 138 | + |
| 139 | + private final ResourceKey resourceKey; |
| 140 | + private final Path path; |
| 141 | + private final Instant createTime; |
| 142 | + private volatile Instant lastAccessTime; |
| 143 | + |
| 144 | + public ResourceItem(final ResourceKey resourceKey, |
| 145 | + final Path path, |
| 146 | + final Instant createTime) { |
| 147 | + this.resourceKey = resourceKey; |
| 148 | + this.path = path; |
| 149 | + this.createTime = createTime; |
| 150 | + this.lastAccessTime = createTime; |
| 151 | + } |
| 152 | + |
| 153 | + public ResourceKey getResourceKey() { |
| 154 | + return resourceKey; |
| 155 | + } |
| 156 | + |
| 157 | + public Path getPath() { |
| 158 | + return path; |
| 159 | + } |
| 160 | + |
| 161 | + public Instant getCreateTime() { |
| 162 | + return createTime; |
| 163 | + } |
| 164 | + |
| 165 | + public Instant getLastAccessTime() { |
| 166 | + return lastAccessTime; |
| 167 | + } |
| 168 | + |
| 169 | + public void setLastAccessTime(final Instant lastAccessTime) { |
| 170 | + this.lastAccessTime = lastAccessTime; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public String toString() { |
| 175 | + return "ResourceItem{" + |
| 176 | + "resourceKey=" + resourceKey + |
| 177 | + ", path=" + path + |
| 178 | + ", createTime=" + createTime + |
| 179 | + ", lastAccessTime=" + lastAccessTime + |
| 180 | + '}'; |
| 181 | + } |
118 | 182 | }
|
119 | 183 | }
|
0 commit comments