Remove unused IndicesOptions#fromByte method (#50683)

This change removes a no longer used method, `fromByte`, in
IndicesOptions. This method was necessary for backwards compatibility
with versions prior to 6.4.0 and was used when talking to those
versions. However, the minimum wire compatibility version has changed
and we no longer use this code.

Backport of #50665
This commit is contained in:
Jay Modi 2020-01-06 14:57:10 -07:00 committed by GitHub
parent 7fd84a03a0
commit e5191e77e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 147 deletions

View File

@ -119,83 +119,6 @@ public class IndicesOptions implements ToXContentFragment {
expandWildcards.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(expandWildcards)); expandWildcards.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(expandWildcards));
} }
// Package visible for testing
static IndicesOptions fromByte(final byte id) {
// IGNORE_UNAVAILABLE = 1;
// ALLOW_NO_INDICES = 2;
// EXPAND_WILDCARDS_OPEN = 4;
// EXPAND_WILDCARDS_CLOSED = 8;
// FORBID_ALIASES_TO_MULTIPLE_INDICES = 16;
// FORBID_CLOSED_INDICES = 32;
// IGNORE_ALIASES = 64;
Set<Option> opts = new HashSet<>();
Set<WildcardStates> wildcards = new HashSet<>();
if ((id & 1) != 0) {
opts.add(Option.IGNORE_UNAVAILABLE);
}
if ((id & 2) != 0) {
opts.add(Option.ALLOW_NO_INDICES);
}
if ((id & 4) != 0) {
wildcards.add(WildcardStates.OPEN);
}
if ((id & 8) != 0) {
wildcards.add(WildcardStates.CLOSED);
}
if ((id & 16) != 0) {
opts.add(Option.FORBID_ALIASES_TO_MULTIPLE_INDICES);
}
if ((id & 32) != 0) {
opts.add(Option.FORBID_CLOSED_INDICES);
}
if ((id & 64) != 0) {
opts.add(Option.IGNORE_ALIASES);
}
return new IndicesOptions(opts, wildcards);
}
/**
* See: {@link #fromByte(byte)}
*/
private static byte toByte(IndicesOptions options) {
byte id = 0;
if (options.ignoreUnavailable()) {
id |= 1;
}
if (options.allowNoIndices()) {
id |= 2;
}
if (options.expandWildcardsOpen()) {
id |= 4;
}
if (options.expandWildcardsClosed()) {
id |= 8;
}
// true is default here, for bw comp we keep the first 16 values
// in the array same as before + the default value for the new flag
if (options.allowAliasesToMultipleIndices() == false) {
id |= 16;
}
if (options.forbidClosedIndices()) {
id |= 32;
}
if (options.ignoreAliases()) {
id |= 64;
}
return id;
}
private static final IndicesOptions[] OLD_VALUES;
static {
short max = 1 << 7;
OLD_VALUES = new IndicesOptions[max];
for (short id = 0; id < max; id++) {
OLD_VALUES[id] = IndicesOptions.fromByte((byte)id);
}
}
/** /**
* @return Whether specified concrete indices should be ignored when unavailable (missing or closed) * @return Whether specified concrete indices should be ignored when unavailable (missing or closed)
*/ */
@ -265,24 +188,12 @@ public class IndicesOptions implements ToXContentFragment {
options = EnumSet.copyOf(options); options = EnumSet.copyOf(options);
options.remove(Option.IGNORE_THROTTLED); options.remove(Option.IGNORE_THROTTLED);
} }
if (out.getVersion().onOrAfter(Version.V_6_4_0)) { out.writeEnumSet(options);
out.writeEnumSet(options); out.writeEnumSet(expandWildcards);
out.writeEnumSet(expandWildcards);
} else {
out.write(IndicesOptions.toByte(this));
}
} }
public static IndicesOptions readIndicesOptions(StreamInput in) throws IOException { public static IndicesOptions readIndicesOptions(StreamInput in) throws IOException {
if (in.getVersion().onOrAfter(Version.V_6_4_0)) { return new IndicesOptions(in.readEnumSet(Option.class), in.readEnumSet(WildcardStates.class));
return new IndicesOptions(in.readEnumSet(Option.class), in.readEnumSet(WildcardStates.class));
} else {
byte id = in.readByte();
if (id >= OLD_VALUES.length) {
throw new IllegalArgumentException("No valid missing index type id: " + id);
}
return OLD_VALUES[id];
}
} }
public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices,

View File

@ -19,21 +19,14 @@
package org.elasticsearch.action.admin.indices.settings.get; package org.elasticsearch.action.admin.indices.settings.get;
import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.Base64;
public class GetSettingsRequestTests extends ESTestCase { public class GetSettingsRequestTests extends ESTestCase {
private static final String TEST_622_REQUEST_BYTES = "ADwDAAEKdGVzdF9pbmRleA4BEHRlc3Rfc2V0dGluZ19rZXkB";
private static final GetSettingsRequest TEST_622_REQUEST = new GetSettingsRequest()
.indices("test_index")
.names("test_setting_key")
.humanReadable(true);
private static final GetSettingsRequest TEST_700_REQUEST = new GetSettingsRequest() private static final GetSettingsRequest TEST_700_REQUEST = new GetSettingsRequest()
.includeDefaults(true) .includeDefaults(true)
.humanReadable(true) .humanReadable(true)
@ -49,20 +42,4 @@ public class GetSettingsRequestTests extends ESTestCase {
GetSettingsRequest deserialized = new GetSettingsRequest(si); GetSettingsRequest deserialized = new GetSettingsRequest(si);
assertEquals(TEST_700_REQUEST, deserialized); assertEquals(TEST_700_REQUEST, deserialized);
} }
public void testSerializeBackwardsCompatibility() throws IOException {
BytesStreamOutput bso = new BytesStreamOutput();
bso.setVersion(Version.V_6_2_2);
TEST_700_REQUEST.writeTo(bso);
byte[] responseBytes = BytesReference.toBytes(bso.bytes());
assertEquals(TEST_622_REQUEST_BYTES, Base64.getEncoder().encodeToString(responseBytes));
}
public void testDeserializeBackwardsCompatibility() throws IOException {
StreamInput si = StreamInput.wrap(Base64.getDecoder().decode(TEST_622_REQUEST_BYTES));
si.setVersion(Version.V_6_2_2);
GetSettingsRequest deserialized = new GetSettingsRequest(si);
assertEquals(TEST_622_REQUEST, deserialized);
}
} }

View File

@ -42,6 +42,7 @@ import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.OptionalInt;
import static org.elasticsearch.test.VersionUtils.randomVersionBetween; import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
@ -78,8 +79,12 @@ public class IndicesOptionsTests extends ESTestCase {
public void testSerializationPre70() throws Exception { public void testSerializationPre70() throws Exception {
int iterations = randomIntBetween(5, 20); int iterations = randomIntBetween(5, 20);
List<Version> declaredVersions = Version.getDeclaredVersions(Version.class);
OptionalInt maxV6Id = declaredVersions.stream().filter(v -> v.major == 6).mapToInt(v -> v.id).max();
assertTrue(maxV6Id.isPresent());
final Version maxVersion = Version.fromId(maxV6Id.getAsInt());
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
Version version = randomVersionBetween(random(), null, Version.V_6_6_0); Version version = randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), maxVersion);
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
@ -100,12 +105,7 @@ public class IndicesOptionsTests extends ESTestCase {
assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices())); assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices()));
assertEquals(indicesOptions2.ignoreAliases(), indicesOptions.ignoreAliases()); assertEquals(indicesOptions2.ignoreAliases(), indicesOptions.ignoreAliases());
if (output.getVersion().onOrAfter(Version.V_6_6_0)) { assertEquals(indicesOptions2.ignoreThrottled(), indicesOptions.ignoreThrottled());
assertEquals(indicesOptions2.ignoreThrottled(), indicesOptions.ignoreThrottled());
} else {
assertFalse(indicesOptions2.ignoreThrottled()); // make sure we never write this option to pre 6.6
}
} }
} }
@ -193,31 +193,6 @@ public class IndicesOptionsTests extends ESTestCase {
assertEquals(defaultOptions.ignoreAliases(), updatedOptions.ignoreAliases()); assertEquals(defaultOptions.ignoreAliases(), updatedOptions.ignoreAliases());
} }
public void testSimpleByteBWC() {
Map<Byte, IndicesOptions> old = new HashMap<>();
// These correspond to each individual option (bit) in the old byte-based IndicesOptions
old.put((byte) 0, IndicesOptions.fromOptions(false, false, false, false, true, false, false, false));
old.put((byte) 1, IndicesOptions.fromOptions(true, false, false, false, true, false, false, false));
old.put((byte) 2, IndicesOptions.fromOptions(false, true, false, false, true, false, false, false));
old.put((byte) 4, IndicesOptions.fromOptions(false, false, true, false, true, false, false, false));
old.put((byte) 8, IndicesOptions.fromOptions(false, false, false, true, true, false, false, false));
old.put((byte) 16, IndicesOptions.fromOptions(false, false, false, false, false, false, false, false));
old.put((byte) 32, IndicesOptions.fromOptions(false, false, false, false, true, true, false, false));
old.put((byte) 64, IndicesOptions.fromOptions(false, false, false, false, true, false, true, false));
// Test a few multi-selected options
old.put((byte) 13, IndicesOptions.fromOptions(true, false, true, true, true, false, false, false));
old.put((byte) 19, IndicesOptions.fromOptions(true, true, false, false, false, false, false, false));
old.put((byte) 24, IndicesOptions.fromOptions(false, false, false, true, false, false, false, false));
old.put((byte) 123, IndicesOptions.fromOptions(true, true, false, true, false, true, true, false));
for (Map.Entry<Byte, IndicesOptions> entry : old.entrySet()) {
IndicesOptions indicesOptions2 = IndicesOptions.fromByte(entry.getKey());
logger.info("--> 1 {}", entry.getValue().toString());
logger.info("--> 2 {}", indicesOptions2.toString());
assertThat("IndicesOptions for byte " + entry.getKey() + " differ for conversion",indicesOptions2, equalTo(entry.getValue()));
}
}
public void testEqualityAndHashCode() { public void testEqualityAndHashCode() {
IndicesOptions indicesOptions = IndicesOptions.fromOptions( IndicesOptions indicesOptions = IndicesOptions.fromOptions(
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),