Skip to content

Commit 3c6f3ed

Browse files
committed
Improve the temp location manager api
1 parent 9f2cbf1 commit 3c6f3ed

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ private TempLocationManager() {
158158
String pid = PidHelper.getPid();
159159

160160
baseTempDir = configuredTempDir.resolve("ddprof");
161+
baseTempDir.toFile().deleteOnExit();
162+
161163
tempDir = baseTempDir.resolve("pid_" + pid);
162164
cleanupTask = CompletableFuture.runAsync(() -> cleanup(false));
163165

@@ -180,28 +182,42 @@ private TempLocationManager() {
180182
* @return the temporary directory for the current process
181183
*/
182184
public Path getTempDir() {
183-
return getTempDir(true);
185+
return getTempDir(null);
186+
}
187+
188+
/**
189+
* Get the temporary subdirectory for the current process. The directory will be removed at JVM
190+
* exit.
191+
*
192+
* @param subPath the relative subdirectory path, may be {@literal null}
193+
* @return the temporary subdirectory for the current process
194+
*/
195+
public Path getTempDir(Path subPath) {
196+
return getTempDir(subPath, true);
184197
}
185198

186199
/**
187-
* Get the temporary directory for the current process.
200+
* Get the temporary subdirectory for the current process.
188201
*
202+
* @param subPath the relative subdirectory path, may be {@literal null}
189203
* @param create if true, create the directory if it does not exist
190204
* @return the temporary directory for the current process
191205
* @throws IllegalStateException if the directory could not be created
192206
*/
193-
public Path getTempDir(boolean create) {
194-
if (create && !Files.exists(tempDir)) {
207+
public Path getTempDir(Path subPath, boolean create) {
208+
Path rslt =
209+
subPath != null && !subPath.toString().isEmpty() ? tempDir.resolve(subPath) : tempDir;
210+
if (create && !Files.exists(rslt)) {
195211
try {
196212
Files.createDirectories(
197-
tempDir,
213+
rslt,
198214
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
199215
} catch (Exception e) {
200216
log.warn("Failed to create temp directory: {}", tempDir, e);
201217
throw new IllegalStateException("Failed to create temp directory: " + tempDir, e);
202218
}
203219
}
204-
return tempDir;
220+
return rslt;
205221
}
206222

207223
void cleanup(boolean cleanSelf) {

dd-java-agent/agent-profiling/profiling-controller/src/test/java/com/datadog/profiling/controller/TempLocationManagerTest.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,37 @@
1212
import java.util.Properties;
1313
import java.util.UUID;
1414
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.params.ParameterizedTest;
16+
import org.junit.jupiter.params.provider.ValueSource;
1517

1618
public class TempLocationManagerTest {
17-
@Test
18-
void testDefault() throws Exception {
19+
@ParameterizedTest
20+
@ValueSource(strings = {"", "test1"})
21+
void testDefault(String subPath) throws Exception {
1922
TempLocationManager tempLocationManager = TempLocationManager.getInstance(true);
20-
Path tempDir = tempLocationManager.getTempDir();
23+
Path tempDir = tempLocationManager.getTempDir(Paths.get(subPath));
2124
assertNotNull(tempDir);
2225
assertTrue(Files.exists(tempDir));
2326
assertTrue(Files.isDirectory(tempDir));
2427
assertTrue(Files.isWritable(tempDir));
2528
assertTrue(Files.isReadable(tempDir));
2629
assertTrue(Files.isExecutable(tempDir));
27-
assertTrue(tempDir.endsWith("pid_" + PidHelper.getPid()));
30+
assertTrue(tempDir.toString().contains("pid_" + PidHelper.getPid()));
2831
}
2932

30-
@Test
31-
void testFromConfig() throws Exception {
33+
@ParameterizedTest
34+
@ValueSource(strings = {"", "test1"})
35+
void testFromConfig(String subPath) throws Exception {
3236
Path myDir =
3337
Files.createTempDirectory(
3438
"ddprof-test-",
3539
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
40+
myDir.toFile().deleteOnExit();
3641
Properties props = new Properties();
3742
props.put(ProfilingConfig.PROFILING_TEMP_DIR, myDir.toString());
3843
ConfigProvider configProvider = ConfigProvider.withPropertiesOverride(props);
3944
TempLocationManager tempLocationManager = new TempLocationManager(configProvider);
40-
Path tempDir = tempLocationManager.getTempDir();
45+
Path tempDir = tempLocationManager.getTempDir(Paths.get(subPath));
4146
assertNotNull(tempDir);
4247
assertTrue(tempDir.toString().startsWith(myDir.toString()));
4348
}
@@ -58,29 +63,42 @@ void testFromConfigNotWritable() throws Exception {
5863
Files.createTempDirectory(
5964
"ddprof-test-",
6065
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r-x------")));
66+
myDir.toFile().deleteOnExit();
6167
Properties props = new Properties();
6268
props.put(ProfilingConfig.PROFILING_TEMP_DIR, myDir.toString());
6369
ConfigProvider configProvider = ConfigProvider.withPropertiesOverride(props);
6470
TempLocationManager tempLocationManager = new TempLocationManager(configProvider);
6571
assertThrows(IllegalStateException.class, tempLocationManager::getTempDir);
6672
}
6773

68-
@Test
69-
void testCleanup() throws Exception {
74+
@ParameterizedTest
75+
@ValueSource(strings = {"", "test1"})
76+
void testCleanup(String subPath) throws Exception {
7077
Path myDir =
7178
Files.createTempDirectory(
7279
"ddprof-test-",
7380
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
81+
myDir.toFile().deleteOnExit();
7482
Properties props = new Properties();
7583
props.put(ProfilingConfig.PROFILING_TEMP_DIR, myDir.toString());
84+
props.put(
85+
ProfilingConfig.PROFILING_UPLOAD_PERIOD,
86+
"0"); // to force immediate cleanup; must be a string value!
7687
ConfigProvider configProvider = ConfigProvider.withPropertiesOverride(props);
7788
TempLocationManager tempLocationManager = new TempLocationManager(configProvider);
78-
Path tempDir = tempLocationManager.getTempDir();
89+
Path tempDir = tempLocationManager.getTempDir(Paths.get(subPath));
7990
assertNotNull(tempDir);
8091

8192
// fake temp location
82-
Path fakeTempDir = tempDir.getParent().resolve("pid_00000");
93+
Path fakeTempDir = tempDir.getParent();
94+
while (fakeTempDir != null && !fakeTempDir.endsWith("ddprof")) {
95+
fakeTempDir = fakeTempDir.getParent();
96+
}
97+
fakeTempDir = fakeTempDir.resolve("pid_0000");
8398
Files.createDirectories(fakeTempDir);
99+
Path tmpFile = Files.createFile(fakeTempDir.resolve("test.txt"));
100+
tmpFile.toFile().deleteOnExit(); // make sure this is deleted at exit
101+
fakeTempDir.toFile().deleteOnExit(); // also this one
84102
tempLocationManager.cleanup(false);
85103
// fake temp location should be deleted
86104
// real temp location should be kept

0 commit comments

Comments
 (0)