Skip to content

Commit

Permalink
Remove unused IndicesOptions#fromByte method (#50683)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jaymode committed Jan 6, 2020
1 parent 7fd84a0 commit e5191e7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,83 +119,6 @@ private IndicesOptions(Collection<Option> options, Collection<WildcardStates> ex
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)
*/
Expand Down Expand Up @@ -265,24 +188,12 @@ public void writeIndicesOptions(StreamOutput out) throws IOException {
options = EnumSet.copyOf(options);
options.remove(Option.IGNORE_THROTTLED);
}
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeEnumSet(options);
out.writeEnumSet(expandWildcards);
} else {
out.write(IndicesOptions.toByte(this));
}
out.writeEnumSet(options);
out.writeEnumSet(expandWildcards);
}

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));
} else {
byte id = in.readByte();
if (id >= OLD_VALUES.length) {
throw new IllegalArgumentException("No valid missing index type id: " + id);
}
return OLD_VALUES[id];
}
return new IndicesOptions(in.readEnumSet(Option.class), in.readEnumSet(WildcardStates.class));
}

public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,14 @@

package org.elasticsearch.action.admin.indices.settings.get;

import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.util.Base64;

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()
.includeDefaults(true)
.humanReadable(true)
Expand All @@ -49,20 +42,4 @@ public void testSerdeRoundTrip() throws IOException {
GetSettingsRequest deserialized = new GetSettingsRequest(si);
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;

import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -78,8 +79,12 @@ public void testSerialization() throws Exception {

public void testSerializationPre70() throws Exception {
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++) {
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(),
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());

Expand All @@ -100,12 +105,7 @@ public void testSerializationPre70() throws Exception {
assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices()));

assertEquals(indicesOptions2.ignoreAliases(), indicesOptions.ignoreAliases());
if (output.getVersion().onOrAfter(Version.V_6_6_0)) {
assertEquals(indicesOptions2.ignoreThrottled(), indicesOptions.ignoreThrottled());
} else {
assertFalse(indicesOptions2.ignoreThrottled()); // make sure we never write this option to pre 6.6
}

assertEquals(indicesOptions2.ignoreThrottled(), indicesOptions.ignoreThrottled());
}
}

Expand Down Expand Up @@ -193,31 +193,6 @@ public void testFromParameters() {
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() {
IndicesOptions indicesOptions = IndicesOptions.fromOptions(
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),
Expand Down

0 comments on commit e5191e7

Please sign in to comment.