Skip to content

Commit 4e19254

Browse files
committed
iter
1 parent 7130d8b commit 4e19254

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

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

12-
import com.google.common.collect.Streams;
13-
import org.elasticsearch.gradle.VersionProperties;
1412
import org.gradle.api.DefaultTask;
1513
import org.gradle.api.file.RegularFileProperty;
1614
import org.gradle.api.provider.ListProperty;
@@ -22,14 +20,17 @@
2220

2321
import java.io.IOException;
2422
import java.nio.file.Path;
23+
import java.util.ArrayList;
24+
import java.util.Comparator;
25+
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.Objects;
2728
import java.util.function.Function;
28-
import java.util.stream.Stream;
2929

3030
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.IdIncrement;
3131
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getDefinedFile;
32-
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getLatestFile;
32+
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getLatestId;
33+
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getPriorLatestId;
3334
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.updateLatestFile;
3435
import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.writeDefinitionFile;
3536

@@ -78,37 +79,54 @@ public abstract class GenerateTransportVersionDataTask extends DefaultTask {
7879
@Input
7980
public abstract Property<Function<String, IdIncrement>> getIdIncrementSupplier();
8081

81-
8282
@TaskAction
8383
public void generateTransportVersionData() throws IOException {
8484
final Path tvDataDir = Objects.requireNonNull(getDataFileDirectory().getAsFile().get()).toPath();
8585
final var tvName = Objects.requireNonNull(getTVName().get());
8686
final var forMinorVersions = Objects.requireNonNull(getMinorVersionsForTV().get());
87+
final var idIncrementSupplier = Objects.requireNonNull(getIdIncrementSupplier().get());
8788

8889
// TODO
89-
// - do we need to validate that the minorVersions don't contain duplicates?
90-
// - is there an order we need to apply? ( I don't think so)
91-
90+
// - do we need to also validate that the minorVersions don't contain duplicates here? How do we enforce idempotency if we don't?
91+
// - is there an order we need to apply? ( I don't think so)
92+
// - Do we need to run this iteratively for backport construction, rather than accepting a list like this? (I don't think so)
93+
// - also parse args if run alone
94+
// - check that duplicate tags don't come in too
95+
96+
// Load the tvSetData for the specified name, if it exists
97+
final var tvDefinition = getDefinedFile(tvDataDir, tvName);
98+
boolean tvDefinitionExists = tvDefinition != null;
99+
final List<Integer> definitionIds = tvDefinitionExists ? tvDefinition.ids() : List.of();
100+
101+
var seenIds = new HashSet<Integer>();
102+
var ids = new ArrayList<>(definitionIds);
92103
for (var forMinorVersion : forMinorVersions) {
93104
// Get the latest transport version data for the specified minor version.
94-
final var latestTV = getLatestFile(tvDataDir, forMinorVersion);
105+
final int latestTV = getLatestId(tvDataDir, forMinorVersion);
95106

96107
// Create the new version
108+
final int newVersion = idIncrementSupplier.apply(forMinorVersion).bumpVersionNumber(latestTV);
97109

98-
// TODO
99-
100-
// Load the tvSetData for the specified name, if it exists
101-
final var tvSetDataFromFile = getDefinedFile(tvDataDir, tvName);
102-
final var tvSetFileExists = tvSetDataFromFile != null;
103-
104-
// Write the definition file.
105-
final var ids = tvSetFileExists
106-
? Streams.concat(tvSetDataFromFile.ids().stream(), Stream.of(newVersion)).sorted().toList().reversed()
107-
: List.of(newVersion);
108-
writeDefinitionFile(tvDataDir, tvName, ids);
110+
// Check that we don't already have an ID for this minor version
111+
var priorLatestID = getPriorLatestId(tvDataDir, forMinorVersion);
112+
if (containsValueInRange(priorLatestID, newVersion, ids)) {
113+
// TODO: Should we log something here?
114+
continue;
115+
}
109116

110117
// Update the LATEST file.
111118
updateLatestFile(tvDataDir, forMinorVersion, tvName, newVersion);
112119
}
120+
121+
writeDefinitionFile(tvDataDir, tvName, ids.stream().sorted(Comparator.reverseOrder()).toList());
122+
}
123+
124+
private boolean containsValueInRange(int lowerExclusive, int upperInclusive, List<Integer> ids) {
125+
for (var id : ids) {
126+
if (lowerExclusive < id && id <= upperInclusive) {
127+
return true;
128+
}
129+
}
130+
return false;
113131
}
114132
}

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/*
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-
*/
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+
*/
99

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

1212
import com.google.common.collect.Comparators;
13+
1314
import org.gradle.api.GradleException;
1415
import org.gradle.api.Project;
1516
import org.gradle.api.attributes.Attribute;
@@ -23,8 +24,10 @@
2324
import java.util.ArrayList;
2425
import java.util.Comparator;
2526
import java.util.List;
27+
import java.util.Objects;
2628
import java.util.regex.Pattern;
2729
import java.util.stream.Collectors;
30+
import java.util.stream.Stream;
2831

2932
import static org.gradle.api.artifacts.type.ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE;
3033

@@ -43,6 +46,10 @@ public String toString() {
4346
}
4447
}
4548

49+
static int getLatestId(Path dataDir, String majorMinor) throws IOException {
50+
return getLatestFile(dataDir, majorMinor).ids().getFirst();
51+
}
52+
4653
static TransportVersionDefinition getLatestFile(Path dataDir, String majorMinor) throws IOException {
4754
return readDefinitionFile(dataDir.resolve(LATEST_DIR).resolve(majorMinor + ".csv"), true);
4855
}
@@ -158,4 +165,26 @@ public int bumpVersionNumber(int tvIDToBump) {
158165
return newId;
159166
}
160167
}
168+
169+
public static int getPriorLatestId(Path dataDir, String majorMinor) throws IOException {
170+
var pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.csv$");
171+
172+
var matcher = pattern.matcher(majorMinor);
173+
var major = Integer.parseInt(matcher.group(1));
174+
var minor = Integer.parseInt(matcher.group(2));
175+
if (minor > 0) {
176+
return getLatestId(dataDir, major + "." + (minor - 1));
177+
}
178+
179+
try (var pathStream = Files.list(Objects.requireNonNull(dataDir.resolve(LATEST_DIR)))) {
180+
var highestMinorOfPrevMajor = pathStream.flatMap(path -> {
181+
var m = pattern.matcher(path.getFileName().toString());
182+
var fileMajor = Integer.parseInt(m.group(1));
183+
var fileMinor = Integer.parseInt(m.group(2));
184+
return fileMajor == major - 1 ? Stream.of(fileMinor) : Stream.empty();
185+
}).sorted().toList().getLast();
186+
return getLatestId(dataDir, (major - 1) + "." + highestMinorOfPrevMajor);
187+
}
188+
}
189+
161190
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.gradle.internal.transport;
1111

1212
import com.google.common.collect.Comparators;
13+
1314
import org.elasticsearch.gradle.internal.transport.TransportVersionUtils.TransportVersionReference;
1415
import org.gradle.api.DefaultTask;
1516
import org.gradle.api.file.ConfigurableFileCollection;
@@ -51,35 +52,30 @@ public void validateTransportVersions() throws IOException {
5152
}
5253

5354
// TODO validate that all files:
54-
// - have only have a single ID per release version
55-
// - have TVs in order
56-
// - have the correct name
57-
// - have the correct data format
58-
// - Don't have duplicate IDs across any files
59-
// - no duplicate names? Should be impossible due to filename conflicts
60-
55+
// - have only have a single ID per release version
56+
// - have TVs in order
57+
// - have the correct name
58+
// - have the correct data format
59+
// - Don't have duplicate IDs across any files
60+
// - no duplicate names? Should be impossible due to filename conflicts
6161

6262
try (var definitionsStream = Files.list(definitionsDir)) {
6363
for (var definitionFile : definitionsStream.toList()) {
6464
// Validate that all definitions are referenced in the code.
6565
var tv = readDefinitionFile(definitionFile, false);
6666
if (allTvNames.contains(tv.name()) == false) {
67-
throw new IllegalStateException("Transport version definition " + tv.name()
68-
+ " in file " + definitionFile + "is not referenced in the code.");
67+
throw new IllegalStateException(
68+
"Transport version definition " + tv.name() + " in file " + definitionFile + "is not referenced in the code."
69+
);
6970
}
7071

7172
// Validate that all Ids are in decending order:
7273
if (Comparators.isInOrder(tv.ids(), Comparator.reverseOrder()) == false) {
73-
throw new IllegalStateException("Transport version definition file " + definitionFile
74-
+ " does not have ordered ids");
74+
throw new IllegalStateException("Transport version definition file " + definitionFile + " does not have ordered ids");
7575
}
7676

77-
78-
79-
8077
}
8178
}
8279
}
8380

84-
8581
}

0 commit comments

Comments
 (0)