20
20
package org .elasticsearch .cluster .metadata ;
21
21
22
22
import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
23
- import org .apache .logging .log4j .Logger ;
24
23
import org .apache .logging .log4j .LogManager ;
24
+ import org .apache .logging .log4j .Logger ;
25
25
import org .apache .logging .log4j .message .ParameterizedMessage ;
26
26
import org .elasticsearch .ElasticsearchException ;
27
27
import org .elasticsearch .ResourceAlreadyExistsException ;
@@ -438,6 +438,13 @@ public ClusterState execute(ClusterState currentState) throws Exception {
438
438
indexScopedSettings );
439
439
}
440
440
final Settings actualIndexSettings = indexSettingsBuilder .build ();
441
+
442
+ /*
443
+ * We can not check the shard limit until we have applied templates, otherwise we do not know the actual number of shards
444
+ * that will be used to create this index.
445
+ */
446
+ checkShardLimit (actualIndexSettings , currentState );
447
+
441
448
tmpImdBuilder .settings (actualIndexSettings );
442
449
443
450
if (recoverFromIndex != null ) {
@@ -593,7 +600,7 @@ static int getNumberOfShards(final Settings.Builder indexSettingsBuilder) {
593
600
assert Version .CURRENT .major == 7 ;
594
601
final int numberOfShards ;
595
602
final Version indexVersionCreated =
596
- Version .fromId (Integer .parseInt (indexSettingsBuilder .get (IndexMetaData .SETTING_INDEX_VERSION_CREATED .getKey ())));
603
+ Version .fromId (Integer .parseInt (indexSettingsBuilder .get (IndexMetaData .SETTING_INDEX_VERSION_CREATED .getKey ())));
597
604
if (indexVersionCreated .before (Version .V_7_0_0 )) {
598
605
numberOfShards = 5 ;
599
606
} else {
@@ -602,6 +609,10 @@ static int getNumberOfShards(final Settings.Builder indexSettingsBuilder) {
602
609
return numberOfShards ;
603
610
}
604
611
612
+ protected void checkShardLimit (final Settings settings , final ClusterState clusterState ) {
613
+ MetaDataCreateIndexService .checkShardLimit (settings , clusterState );
614
+ }
615
+
605
616
@ Override
606
617
public void onFailure (String source , Exception e ) {
607
618
if (e instanceof ResourceAlreadyExistsException ) {
@@ -622,9 +633,6 @@ public void validateIndexSettings(String indexName, final Settings settings, fin
622
633
final boolean forbidPrivateIndexSettings ) throws IndexCreationException {
623
634
List <String > validationErrors = getIndexSettingsValidationErrors (settings , forbidPrivateIndexSettings );
624
635
625
- Optional <String > shardAllocation = checkShardLimit (settings , clusterState );
626
- shardAllocation .ifPresent (validationErrors ::add );
627
-
628
636
if (validationErrors .isEmpty () == false ) {
629
637
ValidationException validationException = new ValidationException ();
630
638
validationException .addValidationErrors (validationErrors );
@@ -635,15 +643,21 @@ public void validateIndexSettings(String indexName, final Settings settings, fin
635
643
/**
636
644
* Checks whether an index can be created without going over the cluster shard limit.
637
645
*
638
- * @param settings The settings of the index to be created.
639
- * @param clusterState The current cluster state.
640
- * @return If present, an error message to be used to reject index creation. If empty, a signal that this operation may be carried out.
646
+ * @param settings the settings of the index to be created
647
+ * @param clusterState the current cluster state
648
+ * @throws ValidationException if creating this index would put the cluster over the cluster shard limit
641
649
*/
642
- static Optional <String > checkShardLimit (Settings settings , ClusterState clusterState ) {
643
- int shardsToCreate = IndexMetaData .INDEX_NUMBER_OF_SHARDS_SETTING .get (settings )
644
- * (1 + IndexMetaData .INDEX_NUMBER_OF_REPLICAS_SETTING .get (settings ));
650
+ public static void checkShardLimit (final Settings settings , final ClusterState clusterState ) {
651
+ final int numberOfShards = IndexMetaData .INDEX_NUMBER_OF_SHARDS_SETTING .get (settings );
652
+ final int numberOfReplicas = IndexMetaData .INDEX_NUMBER_OF_REPLICAS_SETTING .get (settings );
653
+ final int shardsToCreate = numberOfShards * (1 + numberOfReplicas );
645
654
646
- return IndicesService .checkShardLimit (shardsToCreate , clusterState );
655
+ final Optional <String > shardLimit = IndicesService .checkShardLimit (shardsToCreate , clusterState );
656
+ if (shardLimit .isPresent ()) {
657
+ final ValidationException e = new ValidationException ();
658
+ e .addValidationError (shardLimit .get ());
659
+ throw e ;
660
+ }
647
661
}
648
662
649
663
List <String > getIndexSettingsValidationErrors (final Settings settings , final boolean forbidPrivateIndexSettings ) {
0 commit comments