Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

New SQL cluster settings endpoint #400

Merged
merged 9 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/user/admin/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_opendistro/_sql/settings -d '{
"transient" : {
"opendistro.sql.enabled" : "false"
}
Expand Down Expand Up @@ -99,7 +99,7 @@ You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_opendistro/_sql/settings -d '{
"transient" : {
"opendistro.sql.query.slowlog" : "10"
}
Expand Down Expand Up @@ -141,7 +141,7 @@ You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_opendistro/_sql/settings -d '{
"transient" : {
"opendistro.sql.query.analysis.enabled" : "false"
}
Expand Down Expand Up @@ -185,7 +185,7 @@ You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_opendistro/_sql/settings -d '{
"transient" : {
"opendistro.sql.query.analysis.semantic.suggestion" : "true"
}
Expand Down Expand Up @@ -253,7 +253,7 @@ You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_opendistro/_sql/settings -d '{
"transient" : {
"opendistro.sql.query.analysis.semantic.threshold" : "50"
}
Expand Down Expand Up @@ -299,7 +299,7 @@ You can update the setting with a new value like this.

SQL query::

>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{
>> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_opendistro/_sql/settings -d '{
"transient" : {
"opendistro.sql.query.response.format" : "json"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazon.opendistroforelasticsearch.sql.plugin;

import com.amazon.opendistroforelasticsearch.sql.executor.format.ErrorMessageFactory;

import com.amazon.opendistroforelasticsearch.sql.utils.LogUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.elasticsearch.rest.RestStatus.INTERNAL_SERVER_ERROR;

/**
* Interface to manage opendistro.sql.* cluster settings
* All non-sql settings are ignored.
* Any non-transient and non-persistent settings are ignored.
*/
public class RestSqlSettingsAction extends BaseRestHandler {
private static final Logger LOG = LogManager.getLogger(RestSqlSettingsAction.class);

private static final String PERSISTENT = "persistent";
private static final String TRANSIENT = "transient";
private static final String SQL_SETTINGS_PREFIX = "opendistro.sql.";

/**
* API endpoint path
*/
public static final String SETTINGS_API_ENDPOINT = "/_opendistro/_sql/settings";

public RestSqlSettingsAction(Settings settings, RestController restController) {
super();
restController.registerHandler(RestRequest.Method.PUT, SETTINGS_API_ENDPOINT, this);
}

@Override
public String getName() {
return "sql_settings_action";
}

/**
* @see org.elasticsearch.rest.action.admin.cluster.RestClusterUpdateSettingsAction
*/
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
LogUtils.addRequestId();
final ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = Requests.clusterUpdateSettingsRequest();
clusterUpdateSettingsRequest.timeout(request.paramAsTime("timeout", clusterUpdateSettingsRequest.timeout()));
clusterUpdateSettingsRequest.masterNodeTimeout(
request.paramAsTime("master_timeout", clusterUpdateSettingsRequest.masterNodeTimeout()));
Map<String, Object> source;
try (XContentParser parser = request.contentParser()) {
source = parser.map();
}

try {
if (source.containsKey(TRANSIENT)) {
clusterUpdateSettingsRequest.transientSettings(getAndFilterSettings((Map) source.get(TRANSIENT)));
}
if (source.containsKey(PERSISTENT)) {
clusterUpdateSettingsRequest.persistentSettings(getAndFilterSettings((Map) source.get(PERSISTENT)));
}

return channel -> client.admin().cluster().updateSettings(
abbashus marked this conversation as resolved.
Show resolved Hide resolved
clusterUpdateSettingsRequest, new RestToXContentListener<>(channel));
} catch (Exception e) {
LOG.error("Error changing OpenDistro SQL plugin cluster settings", e);
return channel -> channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR,
ErrorMessageFactory.createErrorMessage(e, INTERNAL_SERVER_ERROR.getStatus()).toString()));
}
}

@Override
protected Set<String> responseParams() {
Set<String> responseParams = new HashSet<>(super.responseParams());
responseParams.addAll(Arrays.asList("sql", "flat", "separator", "_score", "_type", "_id", "newLine", "format"));
return responseParams;
}

private Settings getAndFilterSettings(Map<String, ?> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(source);
Settings.Builder settingsBuilder = Settings.builder().
loadFromSource(Strings.toString(builder), builder.contentType());
settingsBuilder.keys().removeIf(key -> !key.startsWith(SQL_SETTINGS_PREFIX));
return settingsBuilder.build();
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC
Metrics.getInstance().registerDefaultMetrics();
abbashus marked this conversation as resolved.
Show resolved Hide resolved
return Arrays.asList(
new RestSqlAction(settings, restController),
new RestSqlStatsAction(settings, restController));
new RestSqlStatsAction(settings, restController),
new RestSqlSettingsAction(settings, restController));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ default Requests put(String name, Object value) {
StringUtils.format("\"%s\": {\"%s\": \"%s\"}", "transient", name, value);
return new Requests(
restClient(),
new SqlRequest("PUT", "/_cluster/settings", new Body(setting).toString()),
new SqlRequest("PUT", "/_opendistro/_sql/settings", new Body(setting).toString()),
null
);
}
Expand Down
Loading