forked from opensearch-project/opensearch-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature/extensions] Adding support for Extension Create Components […
…environment settings and add setting update consumers] (opensearch-project#138) * Added support for enviornment settings requests and unit tests Signed-off-by: Joshua Palis <[email protected]> * updated environment settings support for create components Signed-off-by: Joshua Palis <[email protected]> * Updating javadocs for environmentsetting request/ response handlers Signed-off-by: Joshua Palis <[email protected]> * Adding inital support for AddSettingsUpdateConsumer Signed-off-by: Joshua Palis <[email protected]> * Updated implementation in preparation for getSettings support integration Signed-off-by: Joshua Palis <[email protected]> * Updated sendAddSettingsUpdateConsumerRequest to pull settings list directly from setting update consumer registry Signed-off-by: Joshua Palis <[email protected]> * Integrated writeable settings support Signed-off-by: Joshua Palis <[email protected]> * Updating writeable settings integration for setting type Signed-off-by: Joshua Palis <[email protected]> * Replacing AddSettingsUpdateConsumerResponseHandler with ExtensionBooleanResponseHandler, moving handleUpdateSettings method to seperate class Signed-off-by: Joshua Palis <[email protected]> * refactoring sendAddSettingsUpdateConsumerRequest. Now each component will only need to provide a map of setting and consumer, and upon sending the addsettingupdateconsumer request, this map will be registered within the updateSettingsRequestHandler Signed-off-by: Joshua Palis <[email protected]> * simplifying settingUpdateConsumer registration Signed-off-by: Joshua Palis <[email protected]> * Fixing javadocs Signed-off-by: Joshua Palis <[email protected]> * fixing javadocs Signed-off-by: Joshua Palis <[email protected]> * adding missing javadocs Signed-off-by: Joshua Palis <[email protected]> * fixing javadocs Signed-off-by: Joshua Palis <[email protected]> Signed-off-by: Joshua Palis <[email protected]>
- Loading branch information
Showing
4 changed files
with
275 additions
and
1 deletion.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
src/main/java/org/opensearch/sdk/handlers/EnvironmentSettingsResponseHandler.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,46 @@ | ||
/* | ||
* 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.sdk.handlers; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.env.EnvironmentSettingsResponse; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.sdk.ExtensionsRunner; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.opensearch.transport.TransportException; | ||
import org.opensearch.transport.TransportResponseHandler; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This class handles the response from OpenSearch to a {@link ExtensionsRunner#sendEnvironmentSettingsRequest} call. | ||
*/ | ||
public class EnvironmentSettingsResponseHandler implements TransportResponseHandler<EnvironmentSettingsResponse> { | ||
private static final Logger logger = LogManager.getLogger(EnvironmentSettingsResponseHandler.class); | ||
|
||
@Override | ||
public void handleResponse(EnvironmentSettingsResponse response) { | ||
logger.info("received {}", response); | ||
} | ||
|
||
@Override | ||
public void handleException(TransportException exp) { | ||
logger.info("EnvironmentSettingsRequest failed", exp); | ||
} | ||
|
||
@Override | ||
public String executor() { | ||
return ThreadPool.Names.GENERIC; | ||
} | ||
|
||
@Override | ||
public EnvironmentSettingsResponse read(StreamInput in) throws IOException { | ||
return new EnvironmentSettingsResponse(in); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/org/opensearch/sdk/handlers/UpdateSettingsRequestHandler.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,110 @@ | ||
/* | ||
* 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.sdk.handlers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.Version; | ||
import org.opensearch.common.settings.Setting; | ||
import org.opensearch.common.settings.WriteableSetting; | ||
import org.opensearch.common.unit.ByteSizeValue; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.extensions.ExtensionBooleanResponse; | ||
import org.opensearch.extensions.UpdateSettingsRequest; | ||
|
||
/** | ||
* Handles requests to update settings | ||
*/ | ||
public class UpdateSettingsRequestHandler { | ||
|
||
private static final Logger logger = LogManager.getLogger(UpdateSettingsRequestHandler.class); | ||
|
||
private Map<Setting<?>, Consumer<?>> settingUpdateConsumers; | ||
|
||
/** | ||
* Instantiates a new Update Setting Request Handler | ||
*/ | ||
public UpdateSettingsRequestHandler() { | ||
this.settingUpdateConsumers = new HashMap<>(); | ||
} | ||
|
||
/** | ||
* Registers the component {@link Setting} and the corresponding consumer to the settingsUpdateConsumer map. | ||
* This map is used only when handling {@link UpdateSettingsRequest} | ||
* | ||
* @param settingUpdateConsumers The settings and their corresponding update consumers to register | ||
*/ | ||
public void registerSettingUpdateConsumer(Map<Setting<?>, Consumer<?>> settingUpdateConsumers) { | ||
this.settingUpdateConsumers.putAll(settingUpdateConsumers); | ||
} | ||
|
||
/** | ||
* Handles a request to update a setting from OpenSearch. Extensions must register their setting keys and consumers within the settingUpdateConsumer map | ||
* | ||
* @param updateSettingsRequest The request to handle. | ||
* @return A response acknowledging the request. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public ExtensionBooleanResponse handleUpdateSettingsRequest(UpdateSettingsRequest updateSettingsRequest) { | ||
|
||
logger.info("Registering UpdateSettingsRequest received from OpenSearch"); | ||
|
||
boolean settingUpdateStatus = true; | ||
|
||
WriteableSetting.SettingType settingType = updateSettingsRequest.getSettingType(); | ||
Setting<?> componentSetting = updateSettingsRequest.getComponentSetting(); | ||
Object data = updateSettingsRequest.getData(); | ||
|
||
// Setting updater in OpenSearch performs setting change validation, only need to cast the consumer to the corresponding type and | ||
// invoke the consumer | ||
try { | ||
switch (settingType) { | ||
case Boolean: | ||
Consumer<Boolean> boolConsumer = (Consumer<Boolean>) this.settingUpdateConsumers.get(componentSetting); | ||
boolConsumer.accept(Boolean.parseBoolean(data.toString())); | ||
case Integer: | ||
Consumer<Integer> intConsumer = (Consumer<Integer>) this.settingUpdateConsumers.get(componentSetting); | ||
intConsumer.accept(Integer.parseInt(data.toString())); | ||
case Long: | ||
Consumer<Long> longConsumer = (Consumer<Long>) this.settingUpdateConsumers.get(componentSetting); | ||
longConsumer.accept(Long.parseLong(data.toString())); | ||
case Float: | ||
Consumer<Float> floatConsumer = (Consumer<Float>) this.settingUpdateConsumers.get(componentSetting); | ||
floatConsumer.accept(Float.parseFloat(data.toString())); | ||
case Double: | ||
Consumer<Double> doubleConsumer = (Consumer<Double>) this.settingUpdateConsumers.get(componentSetting); | ||
doubleConsumer.accept(Double.parseDouble(data.toString())); | ||
case String: | ||
Consumer<String> stringConsumer = (Consumer<String>) this.settingUpdateConsumers.get(componentSetting); | ||
stringConsumer.accept(data.toString()); | ||
case TimeValue: | ||
Consumer<TimeValue> timeValueConsumer = (Consumer<TimeValue>) this.settingUpdateConsumers.get(componentSetting); | ||
timeValueConsumer.accept(TimeValue.parseTimeValue(data.toString(), componentSetting.getKey())); | ||
case ByteSizeValue: | ||
Consumer<ByteSizeValue> byteSizeValueConsumer = (Consumer<ByteSizeValue>) this.settingUpdateConsumers.get( | ||
componentSetting | ||
); | ||
byteSizeValueConsumer.accept(ByteSizeValue.parseBytesSizeValue(data.toString(), componentSetting.getKey())); | ||
case Version: | ||
Consumer<Version> versionConsumer = (Consumer<Version>) this.settingUpdateConsumers.get(componentSetting); | ||
versionConsumer.accept((Version) data); | ||
default: | ||
throw new UnsupportedOperationException("Setting Update Consumer type does not exist and is not handled here"); | ||
} | ||
} catch (Exception e) { | ||
logger.info(e.getMessage()); | ||
settingUpdateStatus = false; | ||
} | ||
|
||
return new ExtensionBooleanResponse(settingUpdateStatus); | ||
} | ||
} |
Oops, something went wrong.