|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client; |
| 21 | + |
| 22 | +import org.elasticsearch.ElasticsearchException; |
| 23 | +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; |
| 24 | +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; |
| 25 | +import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
| 28 | +import org.elasticsearch.common.xcontent.XContentType; |
| 29 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 30 | +import org.elasticsearch.indices.recovery.RecoverySettings; |
| 31 | +import org.elasticsearch.rest.RestStatus; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 38 | +import static org.hamcrest.Matchers.equalTo; |
| 39 | +import static org.hamcrest.Matchers.notNullValue; |
| 40 | +import static org.hamcrest.Matchers.nullValue; |
| 41 | + |
| 42 | +public class ClusterClientIT extends ESRestHighLevelClientTestCase { |
| 43 | + |
| 44 | + public void testClusterPutSettings() throws IOException { |
| 45 | + final String transientSettingKey = RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(); |
| 46 | + final int transientSettingValue = 10; |
| 47 | + |
| 48 | + final String persistentSettingKey = EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(); |
| 49 | + final String persistentSettingValue = EnableAllocationDecider.Allocation.NONE.name(); |
| 50 | + |
| 51 | + Settings transientSettings = Settings.builder().put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES).build(); |
| 52 | + Map<String, Object> map = new HashMap<>(); |
| 53 | + map.put(persistentSettingKey, persistentSettingValue); |
| 54 | + |
| 55 | + ClusterUpdateSettingsRequest setRequest = new ClusterUpdateSettingsRequest(); |
| 56 | + setRequest.transientSettings(transientSettings); |
| 57 | + setRequest.persistentSettings(map); |
| 58 | + |
| 59 | + ClusterUpdateSettingsResponse setResponse = execute(setRequest, highLevelClient().cluster()::putSettings, |
| 60 | + highLevelClient().cluster()::putSettingsAsync); |
| 61 | + |
| 62 | + assertAcked(setResponse); |
| 63 | + assertThat(setResponse.getTransientSettings().get(transientSettingKey), notNullValue()); |
| 64 | + assertThat(setResponse.getTransientSettings().get(persistentSettingKey), nullValue()); |
| 65 | + assertThat(setResponse.getTransientSettings().get(transientSettingKey), |
| 66 | + equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); |
| 67 | + assertThat(setResponse.getPersistentSettings().get(transientSettingKey), nullValue()); |
| 68 | + assertThat(setResponse.getPersistentSettings().get(persistentSettingKey), notNullValue()); |
| 69 | + assertThat(setResponse.getPersistentSettings().get(persistentSettingKey), equalTo(persistentSettingValue)); |
| 70 | + |
| 71 | + Map<String, Object> setMap = getAsMap("/_cluster/settings"); |
| 72 | + String transientSetValue = (String) XContentMapValues.extractValue("transient." + transientSettingKey, setMap); |
| 73 | + assertThat(transientSetValue, equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); |
| 74 | + String persistentSetValue = (String) XContentMapValues.extractValue("persistent." + persistentSettingKey, setMap); |
| 75 | + assertThat(persistentSetValue, equalTo(persistentSettingValue)); |
| 76 | + |
| 77 | + ClusterUpdateSettingsRequest resetRequest = new ClusterUpdateSettingsRequest(); |
| 78 | + resetRequest.transientSettings(Settings.builder().putNull(transientSettingKey)); |
| 79 | + resetRequest.persistentSettings("{\"" + persistentSettingKey + "\": null }", XContentType.JSON); |
| 80 | + |
| 81 | + ClusterUpdateSettingsResponse resetResponse = execute(resetRequest, highLevelClient().cluster()::putSettings, |
| 82 | + highLevelClient().cluster()::putSettingsAsync); |
| 83 | + |
| 84 | + assertThat(resetResponse.getTransientSettings().get(transientSettingKey), equalTo(null)); |
| 85 | + assertThat(resetResponse.getPersistentSettings().get(persistentSettingKey), equalTo(null)); |
| 86 | + assertThat(resetResponse.getTransientSettings(), equalTo(Settings.EMPTY)); |
| 87 | + assertThat(resetResponse.getPersistentSettings(), equalTo(Settings.EMPTY)); |
| 88 | + |
| 89 | + Map<String, Object> resetMap = getAsMap("/_cluster/settings"); |
| 90 | + String transientResetValue = (String) XContentMapValues.extractValue("transient." + transientSettingKey, resetMap); |
| 91 | + assertThat(transientResetValue, equalTo(null)); |
| 92 | + String persistentResetValue = (String) XContentMapValues.extractValue("persistent." + persistentSettingKey, resetMap); |
| 93 | + assertThat(persistentResetValue, equalTo(null)); |
| 94 | + } |
| 95 | + |
| 96 | + public void testClusterUpdateSettingNonExistent() { |
| 97 | + String setting = "no_idea_what_you_are_talking_about"; |
| 98 | + int value = 10; |
| 99 | + ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = new ClusterUpdateSettingsRequest(); |
| 100 | + clusterUpdateSettingsRequest.transientSettings(Settings.builder().put(setting, value).build()); |
| 101 | + |
| 102 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(clusterUpdateSettingsRequest, |
| 103 | + highLevelClient().cluster()::putSettings, highLevelClient().cluster()::putSettingsAsync)); |
| 104 | + assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST)); |
| 105 | + assertThat(exception.getMessage(), equalTo( |
| 106 | + "Elasticsearch exception [type=illegal_argument_exception, reason=transient setting [" + setting + "], not recognized]")); |
| 107 | + } |
| 108 | +} |
0 commit comments