Skip to content

Commit d45ef00

Browse files
committed
Revert "Revert "Add deprecation warning for default shards (#30587)""
This reverts commit 93e3e08.
1 parent 47fc038 commit d45ef00

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.test.rest;
21+
22+
import org.elasticsearch.client.Request;
23+
import org.elasticsearch.client.Response;
24+
25+
import java.io.IOException;
26+
import java.util.regex.Matcher;
27+
28+
import static org.elasticsearch.common.logging.DeprecationLogger.WARNING_HEADER_PATTERN;
29+
import static org.hamcrest.Matchers.equalTo;
30+
31+
public class DefaultShardsIT extends ESRestTestCase {
32+
33+
public void testDefaultShards() throws IOException {
34+
final Response response = client().performRequest(new Request("PUT", "/index"));
35+
final String warning = response.getHeader("Warning");
36+
final Matcher matcher = WARNING_HEADER_PATTERN.matcher(warning);
37+
assertTrue(matcher.matches());
38+
final String message = matcher.group(1);
39+
assertThat(message, equalTo("the default number of shards will change from [5] to [1] in 7.0.0; "
40+
+ "if you wish to continue using the default of [5] shards, "
41+
+ "you must manage this on the create index request or with an index template"));
42+
}
43+
44+
public void testNonDefaultShards() throws IOException {
45+
final Request request = new Request("PUT", "/index");
46+
request.setJsonEntity("{\"settings\":{\"index.number_of_shards\":1}}");
47+
final Response response = client().performRequest(request);
48+
assertNull(response.getHeader("Warning"));
49+
}
50+
51+
}

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
package org.elasticsearch.cluster.metadata;
2121

22-
import com.carrotsearch.hppc.cursors.ObjectCursor;
2322
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2423
import org.apache.logging.log4j.Logger;
2524
import org.apache.logging.log4j.message.ParameterizedMessage;
26-
import org.apache.lucene.util.CollectionUtil;
2725
import org.elasticsearch.ElasticsearchException;
2826
import org.elasticsearch.ResourceAlreadyExistsException;
2927
import org.elasticsearch.Version;
@@ -59,7 +57,6 @@
5957
import org.elasticsearch.common.io.PathUtils;
6058
import org.elasticsearch.common.logging.DeprecationLogger;
6159
import org.elasticsearch.common.logging.Loggers;
62-
import org.elasticsearch.common.regex.Regex;
6360
import org.elasticsearch.common.settings.IndexScopedSettings;
6461
import org.elasticsearch.common.settings.Setting;
6562
import org.elasticsearch.common.settings.Settings;
@@ -81,12 +78,10 @@
8178
import org.joda.time.DateTime;
8279
import org.joda.time.DateTimeZone;
8380

84-
import java.io.IOException;
8581
import java.io.UnsupportedEncodingException;
8682
import java.nio.file.Path;
8783
import java.util.ArrayList;
8884
import java.util.Collections;
89-
import java.util.Comparator;
9085
import java.util.HashMap;
9186
import java.util.List;
9287
import java.util.Locale;
@@ -376,6 +371,9 @@ public ClusterState execute(ClusterState currentState) throws Exception {
376371
// now, put the request settings, so they override templates
377372
indexSettingsBuilder.put(request.settings());
378373
if (indexSettingsBuilder.get(SETTING_NUMBER_OF_SHARDS) == null) {
374+
deprecationLogger.deprecated("the default number of shards will change from [5] to [1] in 7.0.0; "
375+
+ "if you wish to continue using the default of [5] shards, "
376+
+ "you must manage this on the create index request or with an index template");
379377
indexSettingsBuilder.put(SETTING_NUMBER_OF_SHARDS, settings.getAsInt(SETTING_NUMBER_OF_SHARDS, 5));
380378
}
381379
if (indexSettingsBuilder.get(SETTING_NUMBER_OF_REPLICAS) == null) {

server/src/test/java/org/elasticsearch/cluster/metadata/IndexCreationTaskTests.java

+36
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public void testMatchTemplates() throws Exception {
120120

121121
final ClusterState result = executeTask();
122122

123+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
124+
+ "if you wish to continue using the default of [5] shards, "
125+
+ "you must manage this on the create index request or with an index template");
126+
123127
assertThat(result.metaData().index("test").getAliases(), hasAllKeys("alias_from_template_1", "alias_from_template_2"));
124128
assertThat(result.metaData().index("test").getAliases(), not(hasKey("alias_from_template_3")));
125129
}
@@ -134,6 +138,10 @@ public void testApplyDataFromTemplate() throws Exception {
134138

135139
final ClusterState result = executeTask();
136140

141+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
142+
+ "if you wish to continue using the default of [5] shards, "
143+
+ "you must manage this on the create index request or with an index template");
144+
137145
assertThat(result.metaData().index("test").getAliases(), hasKey("alias1"));
138146
assertThat(result.metaData().index("test").getCustoms(), hasKey("custom1"));
139147
assertThat(result.metaData().index("test").getSettings().get("key1"), equalTo("value1"));
@@ -148,6 +156,10 @@ public void testApplyDataFromRequest() throws Exception {
148156

149157
final ClusterState result = executeTask();
150158

159+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
160+
+ "if you wish to continue using the default of [5] shards, "
161+
+ "you must manage this on the create index request or with an index template");
162+
151163
assertThat(result.metaData().index("test").getAliases(), hasKey("alias1"));
152164
assertThat(result.metaData().index("test").getCustoms(), hasKey("custom1"));
153165
assertThat(result.metaData().index("test").getSettings().get("key1"), equalTo("value1"));
@@ -177,6 +189,10 @@ public void testRequestDataHavePriorityOverTemplateData() throws Exception {
177189

178190
final ClusterState result = executeTask();
179191

192+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
193+
+ "if you wish to continue using the default of [5] shards, "
194+
+ "you must manage this on the create index request or with an index template");
195+
180196
assertThat(result.metaData().index("test").getCustoms().get("custom1"), equalTo(mergedCustom));
181197
assertThat(result.metaData().index("test").getAliases().get("alias1").getSearchRouting(), equalTo("fromReq"));
182198
assertThat(result.metaData().index("test").getSettings().get("key1"), equalTo("reqValue"));
@@ -186,6 +202,10 @@ public void testRequestDataHavePriorityOverTemplateData() throws Exception {
186202
public void testDefaultSettings() throws Exception {
187203
final ClusterState result = executeTask();
188204

205+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
206+
+ "if you wish to continue using the default of [5] shards, "
207+
+ "you must manage this on the create index request or with an index template");
208+
189209
assertThat(result.getMetaData().index("test").getSettings().get(SETTING_NUMBER_OF_SHARDS), equalTo("5"));
190210
}
191211

@@ -194,6 +214,10 @@ public void testSettingsFromClusterState() throws Exception {
194214

195215
final ClusterState result = executeTask();
196216

217+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
218+
+ "if you wish to continue using the default of [5] shards, "
219+
+ "you must manage this on the create index request or with an index template");
220+
197221
assertThat(result.getMetaData().index("test").getSettings().get(SETTING_NUMBER_OF_SHARDS), equalTo("15"));
198222
}
199223

@@ -246,6 +270,10 @@ public void testRequestStateOpen() throws Exception {
246270

247271
executeTask();
248272

273+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
274+
+ "if you wish to continue using the default of [5] shards, "
275+
+ "you must manage this on the create index request or with an index template");
276+
249277
verify(allocationService, times(1)).reroute(anyObject(), anyObject());
250278
}
251279

@@ -255,6 +283,10 @@ public void testIndexRemovalOnFailure() throws Exception {
255283

256284
expectThrows(RuntimeException.class, this::executeTask);
257285

286+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
287+
+ "if you wish to continue using the default of [5] shards, "
288+
+ "you must manage this on the create index request or with an index template");
289+
258290
verify(indicesService, times(1)).removeIndex(anyObject(), anyObject(), anyObject());
259291
}
260292

@@ -290,6 +322,10 @@ public void testValidateWaitForActiveShardsFailure() throws Exception {
290322

291323
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, this::executeTask);
292324

325+
assertWarnings("the default number of shards will change from [5] to [1] in 7.0.0; "
326+
+ "if you wish to continue using the default of [5] shards, "
327+
+ "you must manage this on the create index request or with an index template");
328+
293329
assertThat(e.getMessage(), containsString("invalid wait_for_active_shards"));
294330
}
295331

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/DoSection.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,16 @@ void checkWarningHeaders(final List<String> warningHeaders) {
271271
final boolean matches = matcher.matches();
272272
if (matches) {
273273
final String message = matcher.group(1);
274-
if (expected.remove(message) == false) {
274+
// noinspection StatementWithEmptyBody
275+
if (message.equals("the default number of shards will change from [5] to [1] in 7.0.0; "
276+
+ "if you wish to continue using the default of [5] shards, "
277+
+ "you must manage this on the create index request or with an index template")) {
278+
/*
279+
* This warning header will come back in the vast majority of our tests that create an index. Rather than rewrite our
280+
* tests to assert this warning header, we assume that it is expected.
281+
*/
282+
}
283+
else if (expected.remove(message) == false) {
275284
unexpected.add(header);
276285
}
277286
} else {

0 commit comments

Comments
 (0)