Skip to content

Commit ffd7c87

Browse files
authored
test(version_utils): fix uneven distribution when selecting a random version (#133214) (#133317)
1 parent f56179d commit ffd7c87

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

test/framework/src/main/java/org/elasticsearch/test/TransportVersionUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static TransportVersion getFirstVersion() {
4040

4141
/** Returns a random {@link TransportVersion} from all available versions. */
4242
public static TransportVersion randomVersion() {
43-
return VersionUtils.randomFrom(random(), allReleasedVersions(), TransportVersion::fromId);
43+
return VersionUtils.randomFrom(random(), allReleasedVersions());
4444
}
4545

4646
/** Returns a random {@link TransportVersion} from all available versions without the ignore set */
@@ -50,7 +50,7 @@ public static TransportVersion randomVersion(Set<TransportVersion> ignore) {
5050

5151
/** Returns a random {@link TransportVersion} from all available versions. */
5252
public static TransportVersion randomVersion(Random random) {
53-
return VersionUtils.randomFrom(random, allReleasedVersions(), TransportVersion::fromId);
53+
return VersionUtils.randomFrom(random, allReleasedVersions());
5454
}
5555

5656
/** Returns a random {@link TransportVersion} between <code>minVersion</code> and <code>maxVersion</code> (inclusive). */
@@ -77,7 +77,7 @@ public static TransportVersion randomVersionBetween(
7777
versions = versions.headSet(maxVersion, true);
7878
}
7979

80-
return VersionUtils.randomFrom(random, versions, TransportVersion::fromId);
80+
return VersionUtils.randomFrom(random, versions);
8181
}
8282

8383
public static TransportVersion getPreviousVersion() {

test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
package org.elasticsearch.test;
1111

12-
import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
12+
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
1313

1414
import org.elasticsearch.Build;
1515
import org.elasticsearch.Version;
@@ -21,7 +21,6 @@
2121
import java.util.NavigableSet;
2222
import java.util.Random;
2323
import java.util.TreeSet;
24-
import java.util.function.IntFunction;
2524

2625
/** Utilities for selecting versions in tests */
2726
public class VersionUtils {
@@ -77,7 +76,7 @@ public static Version getFirstVersion() {
7776

7877
/** Returns a random {@link Version} from all available versions. */
7978
public static Version randomVersion(Random random) {
80-
return randomFrom(random, ALL_VERSIONS, Version::fromId);
79+
return randomFrom(random, ALL_VERSIONS);
8180
}
8281

8382
/** Returns a random {@link Version} from all available versions, that is compatible with the given version. */
@@ -106,24 +105,15 @@ public static Version randomVersionBetween(Random random, @Nullable Version minV
106105
versions = versions.headSet(maxVersion, true);
107106
}
108107

109-
return randomFrom(random, versions, Version::fromId);
108+
return randomFrom(random, versions);
110109
}
111110

112111
/** Returns the maximum {@link Version} that is compatible with the given version. */
113112
public static Version maxCompatibleVersion(Version version) {
114113
return ALL_VERSIONS.tailSet(version, true).descendingSet().stream().filter(version::isCompatible).findFirst().orElseThrow();
115114
}
116115

117-
public static <T extends VersionId<T>> T randomFrom(Random random, NavigableSet<T> set, IntFunction<T> ctor) {
118-
// get the first and last id, pick a random id in the middle, then find that id in the set in O(nlogn) time
119-
// this assumes the id numbers are reasonably evenly distributed in the set
120-
assert set.isEmpty() == false;
121-
int lowest = set.getFirst().id();
122-
int highest = set.getLast().id();
123-
124-
T randomId = ctor.apply(RandomNumbers.randomIntBetween(random, lowest, highest));
125-
// try to find the id below, then the id above. We're just looking for *some* item in the set that is close to randomId
126-
T found = set.floor(randomId);
127-
return found != null ? found : set.ceiling(randomId);
116+
public static <T extends VersionId<T>> T randomFrom(Random random, NavigableSet<T> set) {
117+
return RandomPicks.randomFrom(random, set);
128118
}
129119
}

test/framework/src/main/java/org/elasticsearch/test/index/IndexVersionUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public static IndexVersion getLowestWriteCompatibleVersion() {
4545

4646
/** Returns a random {@link IndexVersion} from all available versions. */
4747
public static IndexVersion randomVersion() {
48-
return VersionUtils.randomFrom(random(), ALL_VERSIONS, IndexVersion::fromId);
48+
return VersionUtils.randomFrom(random(), ALL_VERSIONS);
4949
}
5050

5151
/** Returns a random {@link IndexVersion} from all versions that can be written to. */
5252
public static IndexVersion randomWriteVersion() {
53-
return VersionUtils.randomFrom(random(), ALL_WRITE_VERSIONS, IndexVersion::fromId);
53+
return VersionUtils.randomFrom(random(), ALL_WRITE_VERSIONS);
5454
}
5555

5656
/** Returns a random {@link IndexVersion} from all available versions without the ignore set */
@@ -78,7 +78,7 @@ public static IndexVersion randomVersionBetween(Random random, @Nullable IndexVe
7878
versions = versions.headSet(maxVersion, true);
7979
}
8080

81-
return VersionUtils.randomFrom(random, versions, IndexVersion::fromId);
81+
return VersionUtils.randomFrom(random, versions);
8282
}
8383

8484
public static IndexVersion getPreviousVersion() {

0 commit comments

Comments
 (0)