-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WriteableSetting wrapper class and RegisterSettingsRequest
Signed-off-by: Daniel Widdis <widdis@gmail.com>
- Loading branch information
Showing
2 changed files
with
302 additions
and
5 deletions.
There are no files selected for viewing
291 changes: 291 additions & 0 deletions
291
server/src/main/java/org/opensearch/common/settings/WriteableSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,291 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.settings; | ||
|
||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.io.stream.BytesStreamInput; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.io.stream.Writeable; | ||
import org.opensearch.common.settings.Setting.Property; | ||
import org.opensearch.common.unit.ByteSizeValue; | ||
import org.opensearch.common.unit.TimeValue; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Wrapper for {@link Setting} with {@link #writeTo(StreamOutput)} implementation dependent on the setting type. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class WriteableSetting implements Writeable { | ||
|
||
private enum WriteableSettingGenericType { | ||
Boolean, | ||
Integer, | ||
Long, | ||
Float, | ||
Double, | ||
String, | ||
TimeValue, // long + TimeUnit | ||
ByteSizeValue // long + ByteSizeUnit | ||
} | ||
|
||
private Setting<?> setting; | ||
private WriteableSettingGenericType type; | ||
|
||
/** | ||
* Wrap a {@link Setting}. If the default value is not null, the generic type is determined. | ||
* | ||
* @param setting | ||
* The setting to wrap. | ||
*/ | ||
public WriteableSetting(Setting<?> setting) { | ||
this(setting, getGenericTypeFromDefault(setting)); | ||
} | ||
|
||
/** | ||
* Wrap a {@link Setting} with a specified generic type. | ||
* | ||
* @param setting | ||
* The setting to wrap. | ||
* @param type | ||
* The Generic type of the setting. | ||
*/ | ||
public WriteableSetting(Setting<?> setting, WriteableSettingGenericType type) { | ||
this.setting = setting; | ||
this.type = type; | ||
} | ||
|
||
/** | ||
* Wrap a {@link Setting} read from a stream. | ||
* | ||
* @param in Input to read the value from. | ||
* @throws IOException if there is an error reading the values | ||
*/ | ||
public WriteableSetting(StreamInput in) throws IOException { | ||
// Read the type | ||
this.type = WriteableSettingGenericType.valueOf(in.readString()); | ||
// Read the key | ||
String key = in.readString(); | ||
// Read the default value | ||
Object defaultValue = readDefaultValue(in, type); | ||
// Read a boolean specifying whether the fallback settings is null | ||
WriteableSetting fallback = null; | ||
boolean hasFallback = in.readBoolean(); | ||
if (hasFallback) { | ||
fallback = new WriteableSetting(in); | ||
} | ||
// We are using known types so don't need the parser | ||
// We are not using validator | ||
// Read properties | ||
int size = in.readVInt(); | ||
Property[] propArray = new Property[size]; | ||
for (int i = 0; i < size; i++) { | ||
propArray[i] = Property.valueOf(in.readString()); | ||
} | ||
// Put it all in a setting object | ||
this.setting = createSetting(type, key, defaultValue, fallback, propArray); | ||
} | ||
|
||
private static WriteableSettingGenericType getGenericTypeFromDefault(Setting<?> setting) { | ||
Object defaultValue = setting.getDefault(Settings.EMPTY); | ||
if (defaultValue == null) { | ||
throw new IllegalArgumentException("Unable to determine the generic type of this setting with a null default value."); | ||
} | ||
String typeStr = defaultValue.getClass().getSimpleName(); | ||
try { | ||
return WriteableSettingGenericType.valueOf(typeStr); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException("Invalid WriteableSettingGenericType: " + typeStr); | ||
} | ||
} | ||
|
||
public Setting<?> getSetting() { | ||
return this.setting; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Setting<?> createSetting( | ||
WriteableSettingGenericType readType, | ||
String key, | ||
Object defaultValue, | ||
WriteableSetting fallback, | ||
Property[] propArray | ||
) { | ||
switch (readType) { | ||
case Boolean: | ||
return fallback == null | ||
? Setting.boolSetting(key, (boolean) defaultValue, propArray) | ||
: Setting.boolSetting(key, (Setting<Boolean>) fallback.getSetting(), propArray); | ||
case Integer: | ||
return fallback == null | ||
? Setting.intSetting(key, (int) defaultValue, propArray) | ||
: Setting.intSetting(key, (Setting<Integer>) fallback.getSetting(), propArray); | ||
case Long: | ||
return fallback == null | ||
? Setting.longSetting(key, (long) defaultValue, propArray) | ||
: Setting.longSetting(key, (Setting<Long>) fallback.getSetting(), propArray); | ||
case Float: | ||
return fallback == null | ||
? Setting.floatSetting(key, (float) defaultValue, propArray) | ||
: Setting.floatSetting(key, (Setting<Float>) fallback.getSetting(), propArray); | ||
case Double: | ||
return fallback == null | ||
? Setting.doubleSetting(key, (double) defaultValue, propArray) | ||
: Setting.doubleSetting(key, (Setting<Double>) fallback.getSetting(), propArray); | ||
case String: | ||
return fallback == null | ||
? Setting.simpleString(key, (String) defaultValue, propArray) | ||
: Setting.simpleString(key, (Setting<String>) fallback.getSetting(), propArray); | ||
case TimeValue: | ||
return fallback == null | ||
? Setting.timeSetting(key, (TimeValue) defaultValue, propArray) | ||
: Setting.timeSetting(key, (Setting<TimeValue>) fallback.getSetting(), propArray); | ||
case ByteSizeValue: | ||
return fallback == null | ||
? Setting.byteSizeSetting(key, (ByteSizeValue) defaultValue, propArray) | ||
: Setting.byteSizeSetting(key, (Setting<ByteSizeValue>) fallback.getSetting(), propArray); | ||
default: | ||
// This Should Never Happen (TM) | ||
throw new UnsupportedOperationException("A WriteableSettingGenericType has been added to the enum and not handled here."); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
// Write the type | ||
out.writeString(type.name()); | ||
// Write the key | ||
out.writeString(setting.getKey()); | ||
// Write the default value | ||
writeDefaultValue(out, setting.getDefault(Settings.EMPTY), type); | ||
// Write a boolean specifying whether the fallback settings is null | ||
boolean hasFallback = setting.fallbackSetting != null; | ||
out.writeBoolean(hasFallback); | ||
if (hasFallback) { | ||
new WriteableSetting(setting.fallbackSetting, type).writeTo(out); | ||
} | ||
// We are using known types so don't need the parser | ||
// We are not using validator | ||
// Write properties | ||
out.writeVInt(setting.getProperties().size()); | ||
for (Property prop : setting.getProperties()) { | ||
out.writeString(prop.name()); | ||
} | ||
} | ||
|
||
private void writeDefaultValue(StreamOutput out, Object defaultValue, WriteableSettingGenericType type) throws IOException { | ||
switch (this.type) { | ||
case Boolean: | ||
out.writeBoolean((boolean) defaultValue); | ||
break; | ||
case Integer: | ||
out.writeInt((int) defaultValue); | ||
break; | ||
case Long: | ||
out.writeLong((long) defaultValue); | ||
break; | ||
case Float: | ||
out.writeFloat((float) defaultValue); | ||
break; | ||
case Double: | ||
out.writeDouble((double) defaultValue); | ||
break; | ||
case String: | ||
out.writeString((String) defaultValue); | ||
break; | ||
case TimeValue: | ||
TimeValue tv = (TimeValue) defaultValue; | ||
out.writeLong(tv.duration()); | ||
out.writeString(tv.timeUnit().name()); | ||
break; | ||
case ByteSizeValue: | ||
((ByteSizeValue) defaultValue).writeTo(out); | ||
break; | ||
default: | ||
// This Should Never Happen (TM) | ||
throw new UnsupportedOperationException("A WriteableSettingGenericType has been added to the enum and not handled here."); | ||
} | ||
} | ||
|
||
private Object readDefaultValue(StreamInput in, WriteableSettingGenericType readType) throws IOException { | ||
switch (readType) { | ||
case Boolean: | ||
return in.readBoolean(); | ||
case Integer: | ||
return in.readInt(); | ||
case Long: | ||
return in.readLong(); | ||
case Float: | ||
return in.readFloat(); | ||
case Double: | ||
return in.readDouble(); | ||
case String: | ||
return in.readString(); | ||
case TimeValue: | ||
long duration = in.readLong(); | ||
TimeUnit unit = TimeUnit.valueOf(in.readString()); | ||
return new TimeValue(duration, unit); | ||
case ByteSizeValue: | ||
return new ByteSizeValue(in); | ||
default: | ||
// This Should Never Happen (TM) | ||
throw new UnsupportedOperationException("A WriteableSettingGenericType has been added to the enum and not handled here."); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WriteableSettings{type=Setting<" + type + ">, setting=" + setting + "}"; | ||
} | ||
|
||
public static void main(String... args) throws IOException { | ||
// TEMPORARY, this will be moved to a test class | ||
|
||
// LegacyOpenDistroAnomalyDetectorSettings.MAX_RETRY_FOR_END_RUN_EXCEPTION | ||
// Note this provides default value of 6 | ||
Setting<Integer> fallbackSetting = Setting.intSetting( | ||
"opendistro.anomaly_detection.max_retry_for_end_run_exception", | ||
6, | ||
0, | ||
Setting.Property.NodeScope, | ||
Setting.Property.Dynamic // , | ||
// Fails if we try to use it | ||
// Setting.Property.Deprecated | ||
); | ||
|
||
System.out.println("Original with fallback"); | ||
// AnomalyDetectorSetting.MAX_RETRY_FOR_END_RUN_EXCEPTION | ||
// Note this falls back to the other setting | ||
// This populates the default value since we have no settings, although it won't be used | ||
Setting<Integer> intSetting = Setting.intSetting( | ||
"plugins.anomaly_detection.max_retry_for_end_run_exception", | ||
fallbackSetting, | ||
0, | ||
Setting.Property.NodeScope, | ||
Setting.Property.Dynamic | ||
); | ||
System.out.println("Original"); | ||
System.out.println(intSetting.toString()); | ||
|
||
WriteableSetting wsOut = new WriteableSetting(intSetting); | ||
try (BytesStreamOutput out = new BytesStreamOutput()) { | ||
wsOut.writeTo(out); | ||
out.flush(); | ||
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) { | ||
WriteableSetting wsIn = new WriteableSetting(in); | ||
System.out.println("After transport across a (byte)stream"); | ||
// We get a 6!!!! | ||
System.out.println(wsIn.toString()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters