-
Notifications
You must be signed in to change notification settings - Fork 356
Make agent index generation stable irrespective of filesystem iteration order #11889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fc7d48c
5d8ef78
51b20c7
794f5f2
2f40c3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| package datadog.trace.bootstrap; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.DataInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.jar.JarFile; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipOutputStream; | ||
|
|
@@ -103,6 +108,25 @@ private static void createFile(Path root, String relativePath) throws IOExceptio | |
| Files.createFile(file); | ||
| } | ||
|
|
||
| private static Path writeIndex(Path resourcesDir, Path indexFile) throws IOException { | ||
| AgentJarIndex.IndexGenerator generator = new AgentJarIndex.IndexGenerator(resourcesDir); | ||
| generator.buildIndex(); | ||
| generator.writeIndex(indexFile); | ||
| return indexFile; | ||
| } | ||
|
|
||
| private static List<String> readPrefixes(Path indexFile) throws IOException { | ||
| try (DataInputStream in = | ||
| new DataInputStream(new BufferedInputStream(Files.newInputStream(indexFile)))) { | ||
| int prefixCount = in.readInt(); | ||
| String[] prefixes = new String[prefixCount]; | ||
| for (int i = 0; i < prefixCount; i++) { | ||
| prefixes[i] = in.readUTF(); | ||
| } | ||
| return Arrays.asList(prefixes); | ||
| } | ||
| } | ||
|
Comment on lines
+111
to
+128
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 suggestion: What about reusing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I introduced |
||
|
|
||
| @Test | ||
| void classEntryNameResolvesBootstrapClassToTopLevel(@TempDir Path tempDir) throws Exception { | ||
| Path resources = tempDir.resolve("resources"); | ||
|
|
@@ -199,6 +223,40 @@ void multiplePrefixesAreIndexedIndependently(@TempDir Path tempDir) throws Excep | |
| index.classEntryName("com.datadoghq.stats.StatsClient")); | ||
| } | ||
|
|
||
| @Test | ||
| void buildIndexWritesPrefixesInSortedOrder(@TempDir Path tempDir) throws Exception { | ||
| Path resources = tempDir.resolve("resources"); | ||
| createFile(resources, "metrics/com/datadoghq/stats/StatsClient.classdata"); | ||
| createFile(resources, "inst/datadog/trace/instrumentation/servlet/ServletAdvice.classdata"); | ||
| createFile(resources, "appsec/com/datadog/appsec/Event.classdata"); | ||
|
|
||
| Path indexFile = writeIndex(resources, tempDir.resolve("dd-java-agent.index")); | ||
|
|
||
| assertEquals(Arrays.asList("appsec/", "inst/", "metrics/"), readPrefixes(indexFile)); | ||
| } | ||
|
|
||
| @Test | ||
| void buildIndexIsIndependentOfDirectoryCreationOrder(@TempDir Path tempDir) throws Exception { | ||
| Path buildResources = tempDir.resolve("build-resources"); | ||
| createFile(buildResources, "appsec/com/datadog/appsec/Event.classdata"); | ||
| createFile(buildResources, "ci-visibility/com/datadog/ci/Visibility.classdata"); | ||
| createFile(buildResources, "cws-tls/com/datadog/cws/Tls.classdata"); | ||
| createFile( | ||
| buildResources, "inst/datadog/trace/instrumentation/servlet/ServletAdvice.classdata"); | ||
|
|
||
| Path deployResources = tempDir.resolve("deploy-resources"); | ||
| createFile(deployResources, "cws-tls/com/datadog/cws/Tls.classdata"); | ||
| createFile(deployResources, "ci-visibility/com/datadog/ci/Visibility.classdata"); | ||
| createFile(deployResources, "appsec/com/datadog/appsec/Event.classdata"); | ||
| createFile( | ||
| deployResources, "inst/datadog/trace/instrumentation/servlet/ServletAdvice.classdata"); | ||
|
|
||
| Path buildIndex = writeIndex(buildResources, tempDir.resolve("build-dd-java-agent.index")); | ||
| Path deployIndex = writeIndex(deployResources, tempDir.resolve("deploy-dd-java-agent.index")); | ||
|
|
||
| assertArrayEquals(Files.readAllBytes(buildIndex), Files.readAllBytes(deployIndex)); | ||
| } | ||
|
|
||
| @Test | ||
| void buildIndexWithEmptyResourcesDirProducesWorkingIndex(@TempDir Path tempDir) throws Exception { | ||
| Path resources = tempDir.resolve("resources"); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.