Skip to content

Commit eac9d76

Browse files
committed
Merge branch 'main' into transport/generate-definitions
2 parents 4ad2c99 + e7abaf6 commit eac9d76

18 files changed

+232
-201
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/CollectTransportVersionReferencesTask.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public abstract class CollectTransportVersionReferencesTask extends DefaultTask
6161

6262
@TaskAction
6363
public void checkTransportVersion() throws IOException {
64-
var results = new HashSet<TransportVersionUtils.TransportVersionReference>();
64+
var results = new HashSet<TransportVersionReference>();
6565

6666
for (var cpElement : getClassPath()) {
6767
Path file = cpElement.toPath();
@@ -74,8 +74,7 @@ public void checkTransportVersion() throws IOException {
7474
Files.writeString(outputFile, String.join("\n", results.stream().map(Object::toString).sorted().toList()));
7575
}
7676

77-
private void addNamesFromClassesDirectory(Set<TransportVersionUtils.TransportVersionReference> results, Path basePath)
78-
throws IOException {
77+
private void addNamesFromClassesDirectory(Set<TransportVersionReference> results, Path basePath) throws IOException {
7978
Files.walkFileTree(basePath, new SimpleFileVisitor<>() {
8079
@Override
8180
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
@@ -90,8 +89,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
9089
});
9190
}
9291

93-
private void addNamesFromClass(Set<TransportVersionUtils.TransportVersionReference> results, InputStream classBytes, String classname)
94-
throws IOException {
92+
private void addNamesFromClass(Set<TransportVersionReference> results, InputStream classBytes, String classname) throws IOException {
9593
ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM9) {
9694
@Override
9795
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
@@ -111,7 +109,7 @@ public void visitMethodInsn(int opcode, String owner, String name, String descri
111109
if (abstractInstruction instanceof LdcInsnNode ldcInsnNode
112110
&& ldcInsnNode.cst instanceof String tvName
113111
&& tvName.isEmpty() == false) {
114-
results.add(new TransportVersionUtils.TransportVersionReference(tvName, location));
112+
results.add(new TransportVersionReference(tvName, location));
115113
} else {
116114
// The instruction is not a LDC with a String constant (or an empty String), which is not allowed.
117115
throw new RuntimeException(

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/GenerateTransportVersionDefinitionTask.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
import org.elasticsearch.gradle.Version;
1313
import org.elasticsearch.gradle.VersionProperties;
14-
import org.elasticsearch.gradle.internal.transport.TransportVersionUtils.TransportVersionDefinition;
15-
import org.elasticsearch.gradle.internal.transport.TransportVersionUtils.TransportVersionLatest;
1614
import org.gradle.api.DefaultTask;
1715
import org.gradle.api.Project;
1816
import org.gradle.api.file.DirectoryProperty;
@@ -43,12 +41,11 @@
4341

4442
import javax.inject.Inject;
4543

46-
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.IdComponents.PATCH;
47-
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.IdComponents.SERVER;
48-
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.IdComponents.SUBSIDIARY;
44+
import static org.elasticsearch.gradle.internal.transport.TransportVersionId.Component.PATCH;
45+
import static org.elasticsearch.gradle.internal.transport.TransportVersionId.Component.SERVER;
46+
import static org.elasticsearch.gradle.internal.transport.TransportVersionId.Component.SUBSIDIARY;
4947
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.LATEST_DIR;
5048
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.SERVERLESS_BRANCH;
51-
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.TransportVersionId;
5249
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.latestFilePath;
5350
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.readLatestFile;
5451
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.writeDefinitionFile;
@@ -94,7 +91,7 @@ public abstract class GenerateTransportVersionDefinitionTask extends DefaultTask
9491

9592
// @Optional
9693
// @Input
97-
// public abstract Property<Function<String, IdComponents>> getIdIncrementSupplier();
94+
// public abstract Property<Function<String, Component>> getIdIncrementSupplier();
9895

9996
private final Path rootPath;
10097
private final ExecOperations execOperations;

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/GlobalTransportVersionManagementPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void apply(Project project) {
3333
Configuration tvReferencesConfig = project.getConfigurations().create("globalTvReferences");
3434
tvReferencesConfig.setCanBeConsumed(false);
3535
tvReferencesConfig.setCanBeResolved(true);
36-
tvReferencesConfig.attributes(TransportVersionUtils::addTransportVersionReferencesAttribute);
36+
tvReferencesConfig.attributes(TransportVersionReference::addArtifactAttribute);
3737

3838
// iterate through all projects, and if the management plugin is applied, add that project back as a dep to check
3939
for (Project subProject : project.getRootProject().getSubprojects()) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.transport;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
record TransportVersionDefinition(String name, List<TransportVersionId> ids) {
16+
public static TransportVersionDefinition fromString(String filename, String contents) {
17+
assert filename.endsWith(".csv");
18+
String name = filename.substring(0, filename.length() - 4);
19+
List<TransportVersionId> ids = new ArrayList<>();
20+
21+
if (contents.isEmpty() == false) {
22+
for (String rawId : contents.split(",")) {
23+
try {
24+
ids.add(TransportVersionId.fromString(rawId));
25+
} catch (NumberFormatException e) {
26+
throw new IllegalStateException("Failed to parse id " + rawId + " in " + filename, e);
27+
}
28+
}
29+
}
30+
31+
return new TransportVersionDefinition(name, ids);
32+
}
33+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.transport;
11+
12+
record TransportVersionId(int complete, int major, int server, int subsidiary, int patch) implements Comparable<TransportVersionId> {
13+
14+
/**
15+
* The TV format:
16+
* <p>
17+
* MM_NNN_S_PP
18+
* <p>
19+
* M - The major version of Elasticsearch
20+
* NNN - The server version part
21+
* S - The subsidiary version part. It should always be 0 here, it is only used in subsidiary repositories.
22+
* PP - The patch version part
23+
*/
24+
public enum Component {
25+
MAJOR(1_000_0_00, 2),
26+
SERVER(1_0_00, 3),
27+
SUBSIDIARY(1_00, 1),
28+
PATCH(1, 2);
29+
30+
private final int value;
31+
private final int max;
32+
33+
Component(int value, int numDigits) {
34+
this.value = value;
35+
this.max = (int) Math.pow(10, numDigits);
36+
}
37+
}
38+
39+
public static TransportVersionId fromInt(int complete) {
40+
int patch = complete % Component.PATCH.max;
41+
int subsidiary = (complete / Component.SUBSIDIARY.value) % Component.SUBSIDIARY.max;
42+
int server = (complete / Component.SERVER.value) % Component.SERVER.max;
43+
int major = complete / Component.MAJOR.value;
44+
45+
return new TransportVersionId(complete, major, server, subsidiary, patch);
46+
}
47+
48+
static TransportVersionId fromString(String s) {
49+
return fromInt(Integer.parseInt(s));
50+
}
51+
52+
public TransportVersionId bumpComponent(Component component) {
53+
int zeroesCleared = (complete / component.value) * component.value;
54+
int newId = zeroesCleared + component.value;
55+
if ((newId / component.value) % component.max == 0) {
56+
throw new IllegalStateException(
57+
"Insufficient" + component.name() + " version section in TransportVersion: " + complete + ", Cannot bump."
58+
);
59+
}
60+
return fromInt(newId);
61+
}
62+
63+
@Override
64+
public int compareTo(TransportVersionId o) {
65+
return Integer.compare(complete, o.complete);
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return Integer.toString(complete);
71+
}
72+
73+
public int base() {
74+
return (complete / 1000) * 1000;
75+
}
76+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.transport;
11+
12+
record TransportVersionLatest(String branch, String name, TransportVersionId id) {
13+
14+
public static TransportVersionLatest fromString(String filename, String contents) {
15+
assert filename.endsWith(".csv");
16+
String branch = filename.substring(0, filename.length() - 4);
17+
18+
String[] parts = contents.split(",");
19+
if (parts.length != 2) {
20+
throw new IllegalStateException("Invalid transport version latest file [" + filename + "]: " + contents);
21+
}
22+
23+
return new TransportVersionLatest(branch, parts[0], TransportVersionId.fromString(parts[1]));
24+
}
25+
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionManagementPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void apply(Project project) {
3838
Configuration tvReferencesConfig = project.getConfigurations().create("transportVersionReferences", c -> {
3939
c.setCanBeConsumed(true);
4040
c.setCanBeResolved(false);
41-
c.attributes(TransportVersionUtils::addTransportVersionReferencesAttribute);
41+
c.attributes(TransportVersionReference::addArtifactAttribute);
4242
});
4343
project.getArtifacts().add(tvReferencesConfig.getName(), collectTask);
4444

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.transport;
11+
12+
import org.gradle.api.attributes.Attribute;
13+
import org.gradle.api.attributes.AttributeContainer;
14+
15+
import java.io.IOException;
16+
import java.nio.charset.StandardCharsets;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import static org.gradle.api.artifacts.type.ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE;
23+
24+
record TransportVersionReference(String name, String location) {
25+
26+
private static final Attribute<Boolean> REFERENCES_ATTRIBUTE = Attribute.of("transport-version-references", Boolean.class);
27+
28+
static List<TransportVersionReference> listFromFile(Path file) throws IOException {
29+
assert file.endsWith(".csv");
30+
List<TransportVersionReference> results = new ArrayList<>();
31+
for (String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
32+
String[] parts = line.split(",", 2);
33+
if (parts.length != 2) {
34+
throw new IOException("Invalid transport version data file [" + file + "]: " + line);
35+
}
36+
results.add(new TransportVersionReference(parts[0], parts[1]));
37+
}
38+
return results;
39+
}
40+
41+
static void addArtifactAttribute(AttributeContainer attributes) {
42+
attributes.attribute(ARTIFACT_TYPE_ATTRIBUTE, "csv");
43+
attributes.attribute(REFERENCES_ATTRIBUTE, true);
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return name + "," + location;
49+
}
50+
}

0 commit comments

Comments
 (0)