Skip to content

Commit 8798cfc

Browse files
committed
feedback
1 parent 03ec728 commit 8798cfc

File tree

6 files changed

+28
-66
lines changed

6 files changed

+28
-66
lines changed

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/transport/TransportVersionGenerationFuncTest.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,12 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
345345
file("myserver/build.gradle") << """
346346
tasks.named('validateTransportVersionResources') {
347347
shouldValidateDensity = false
348+
shouldValidatePrimaryIdNotPatch = false
348349
}
349350
"""
350351

351352
when:
352-
def result = runGenerateAndValidateTask("--backport-branches=9.2", "--increment=100").build()
353+
def result = runGenerateAndValidateTask("--increment=100").build()
353354

354355
then:
355356
assertGenerateAndValidateSuccess(result)

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/transport/TransportVersionValidationFuncTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TransportVersionValidationFuncTest extends AbstractTransportVersionFuncTes
2626
def "definitions must be referenced"() {
2727
given:
2828
javaSource("myplugin", "org.elasticsearch.plugin", "MyPlugin",
29-
"import org.elasticsearch.TransportVersion;", """
29+
"import org.elasticsearch.TransportVersion;", """
3030
static final TransportVersion dne = TransportVersion.fromName("dne");
3131
""")
3232
when:

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public abstract class GenerateTransportVersionDefinitionTask extends DefaultTask
5151
@Option(option = "name", description = "The name of the Transport Version definition, e.g. --name=my_new_tv")
5252
public abstract Property<String> getTransportVersionName();
5353

54-
@Optional
5554
@Input
55+
@Optional
5656
@Option(
5757
option = "backport-branches",
5858
description = "The branches this definition will be backported to, e.g. --backport-branches=9.1,8.19"
@@ -85,9 +85,9 @@ public void run() throws IOException {
8585
checkReleaseBranches(mainUpperBounds, releaseBranches);
8686

8787
if (name.isEmpty()) {
88-
resetAllLatestFiles(resources);
88+
resetAllUpperBounds(resources);
8989
} else {
90-
List<TransportVersionId> ids = updateUpperBoundsFiles(resources, mainUpperBounds, releaseBranches, name);
90+
List<TransportVersionId> ids = updateUpperBounds(resources, mainUpperBounds, releaseBranches, name);
9191
// (Re)write the definition file.
9292
resources.writeReferableDefinition(new TransportVersionDefinition(name, ids));
9393
}
@@ -111,7 +111,7 @@ private void checkReleaseBranches(List<TransportVersionUpperBound> mainUpperBoun
111111
}
112112
}
113113

114-
private List<TransportVersionId> updateUpperBoundsFiles(
114+
private List<TransportVersionId> updateUpperBounds(
115115
TransportVersionResourcesService resources,
116116
List<TransportVersionUpperBound> existingUpperBounds,
117117
Set<String> targetReleaseBranches,
@@ -126,19 +126,11 @@ private List<TransportVersionId> updateUpperBoundsFiles(
126126

127127
TransportVersionDefinition existingDefinition = resources.getReferableDefinitionFromMain(name);
128128
for (TransportVersionUpperBound existingUpperBound : existingUpperBounds) {
129-
130129
String releaseBranch = existingUpperBound.releaseBranch();
131-
boolean targetThisBranch = targetReleaseBranches.contains(existingUpperBound.releaseBranch());
132130

133-
// Case: Targeting this upper bound file and it already points at the name we're generating
134-
if (targetThisBranch && name.equals(existingUpperBound.name())) {
135-
ids.add(existingUpperBound.id());
136-
continue;
137-
}
138-
139-
if (targetThisBranch) {
131+
if (targetReleaseBranches.contains(releaseBranch)) {
140132
// Case: targeting this branch, find an existing id for this branch if it exists
141-
TransportVersionId targetId = maybeGetExistingId(existingUpperBound, existingDefinition);
133+
TransportVersionId targetId = maybeGetExistingId(existingUpperBound, existingDefinition, name);
142134
if (targetId == null) {
143135
// Case: an id doesn't yet exist for this branch, so create one
144136
int increment = releaseBranch.equals(mainReleaseBranch) ? primaryIncrement : 1;
@@ -167,7 +159,7 @@ private Set<String> getTargetReleaseBranches() {
167159
return releaseBranches;
168160
}
169161

170-
private void resetAllLatestFiles(TransportVersionResourcesService resources) throws IOException {
162+
private void resetAllUpperBounds(TransportVersionResourcesService resources) throws IOException {
171163
// TODO: this should also _delete_ extraneous files from latest?
172164
for (TransportVersionUpperBound upperBound : resources.getUpperBoundsFromMain()) {
173165
resources.writeUpperBound(upperBound);
@@ -216,13 +208,20 @@ private String findAddedTransportVersionName(
216208
}
217209
}
218210

219-
private TransportVersionId maybeGetExistingId(TransportVersionUpperBound upperBound, TransportVersionDefinition existingDefinition) {
211+
private TransportVersionId maybeGetExistingId(TransportVersionUpperBound upperBound, TransportVersionDefinition existingDefinition, String name) {
220212
if (existingDefinition == null) {
213+
// the name doesn't yet exist, so there is no id to return
221214
return null;
222215
}
216+
if (upperBound.name().equals(name)) {
217+
// the name exists and this upper bound already points at it
218+
return upperBound.id();
219+
}
223220
if (upperBound.releaseBranch().equals(getMainReleaseBranch().get())) {
224-
return existingDefinition.ids().get(0); // main is always the primary id
221+
// the upper bound is for main, so return the primary id
222+
return existingDefinition.ids().get(0);
225223
}
224+
// the upper bound is for a non-main branch, so find the id with the same base
226225
for (TransportVersionId id : existingDefinition.ids()) {
227226
if (id.base() == upperBound.id().base()) {
228227
return id;

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

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,19 @@
99

1010
package org.elasticsearch.gradle.internal.transport;
1111

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-
}
12+
record TransportVersionId(int complete, int base, int patch) implements Comparable<TransportVersionId> {
3813

3914
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;
15+
int patch = complete % 1000;
16+
int base = complete - patch;
4417

45-
return new TransportVersionId(complete, major, server, subsidiary, patch);
18+
return new TransportVersionId(complete, base, patch);
4619
}
4720

4821
static TransportVersionId fromString(String s) {
4922
return fromInt(Integer.parseInt(s));
5023
}
5124

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-
6325
@Override
6426
public int compareTo(TransportVersionId o) {
6527
// note: this is descending order so the arguments are reversed
@@ -70,8 +32,4 @@ public int compareTo(TransportVersionId o) {
7032
public String toString() {
7133
return Integer.toString(complete);
7234
}
73-
74-
public int base() {
75-
return (complete / 1000) * 1000;
76-
}
7735
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void apply(Project project) {
5757
t.setDescription("Validates that all transport version resources are internally consistent with each other");
5858
t.getReferencesFiles().setFrom(tvReferencesConfig);
5959
t.getShouldValidateDensity().convention(true);
60+
t.getShouldValidatePrimaryIdNotPatch().convention(true);
6061
});
6162
project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(t -> t.dependsOn(validateTask));
6263

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public Path getResourcesDir() {
5757
@Input
5858
public abstract Property<Boolean> getShouldValidateDensity();
5959

60+
@Input
61+
public abstract Property<Boolean> getShouldValidatePrimaryIdNotPatch();
62+
6063
private record IdAndDefinition(TransportVersionId id, TransportVersionDefinition definition) {}
6164

6265
private static final Pattern NAME_FORMAT = Pattern.compile("[a-z0-9_]+");
@@ -153,7 +156,7 @@ private void validateNamedDefinition(TransportVersionDefinition definition, Set<
153156
TransportVersionId id = definition.ids().get(ndx);
154157

155158
if (ndx == 0) {
156-
if (id.patch() != 0) {
159+
if (getShouldValidatePrimaryIdNotPatch().get() && id.patch() != 0) {
157160
throwDefinitionFailure(definition, "has patch version " + id.complete() + " as primary id");
158161
}
159162
} else {

0 commit comments

Comments
 (0)