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 ;
@@ -435,6 +435,13 @@ public ClusterState execute(ClusterState currentState) throws Exception {
435
435
indexScopedSettings );
436
436
}
437
437
final Settings actualIndexSettings = indexSettingsBuilder .build ();
438
+
439
+ /*
440
+ * We can not check the shard limit until we have applied templates, otherwise we do not know the actual number of shards
441
+ * that will be used to create this index.
442
+ */
443
+ checkShardLimit (actualIndexSettings , currentState );
444
+
438
445
tmpImdBuilder .settings (actualIndexSettings );
439
446
440
447
if (recoverFromIndex != null ) {
@@ -583,6 +590,10 @@ public ClusterState execute(ClusterState currentState) throws Exception {
583
590
}
584
591
}
585
592
593
+ protected void checkShardLimit (final Settings settings , final ClusterState clusterState ) {
594
+ MetaDataCreateIndexService .checkShardLimit (settings , clusterState , deprecationLogger );
595
+ }
596
+
586
597
@ Override
587
598
public void onFailure (String source , Exception e ) {
588
599
if (e instanceof ResourceAlreadyExistsException ) {
@@ -603,9 +614,6 @@ public void validateIndexSettings(String indexName, final Settings settings, fin
603
614
final boolean forbidPrivateIndexSettings ) throws IndexCreationException {
604
615
List <String > validationErrors = getIndexSettingsValidationErrors (settings , forbidPrivateIndexSettings );
605
616
606
- Optional <String > shardAllocation = checkShardLimit (settings , clusterState , deprecationLogger );
607
- shardAllocation .ifPresent (validationErrors ::add );
608
-
609
617
if (validationErrors .isEmpty () == false ) {
610
618
ValidationException validationException = new ValidationException ();
611
619
validationException .addValidationErrors (validationErrors );
@@ -616,16 +624,22 @@ public void validateIndexSettings(String indexName, final Settings settings, fin
616
624
/**
617
625
* Checks whether an index can be created without going over the cluster shard limit.
618
626
*
619
- * @param settings The settings of the index to be created.
620
- * @param clusterState The current cluster state.
621
- * @param deprecationLogger The logger to use to emit a deprecation warning, if appropriate.
622
- * @return If present, an error message to be used to reject index creation. If empty, a signal that this operation may be carried out.
627
+ * @param settings the settings of the index to be created
628
+ * @param clusterState the current cluster state
629
+ * @param deprecationLogger the logger to use to emit a deprecation warning, if appropriate
630
+ * @throws ValidationException if creating this index would put the cluster over the cluster shard limit
623
631
*/
624
- static Optional <String > checkShardLimit (Settings settings , ClusterState clusterState , DeprecationLogger deprecationLogger ) {
625
- int shardsToCreate = IndexMetaData .INDEX_NUMBER_OF_SHARDS_SETTING .get (settings )
626
- * (1 + IndexMetaData .INDEX_NUMBER_OF_REPLICAS_SETTING .get (settings ));
627
-
628
- return IndicesService .checkShardLimit (shardsToCreate , clusterState , deprecationLogger );
632
+ public static void checkShardLimit (Settings settings , ClusterState clusterState , DeprecationLogger deprecationLogger ) {
633
+ final int numberOfShards = IndexMetaData .INDEX_NUMBER_OF_SHARDS_SETTING .get (settings );
634
+ final int numberOfReplicas = IndexMetaData .INDEX_NUMBER_OF_REPLICAS_SETTING .get (settings );
635
+ final int shardsToCreate = numberOfShards * (1 + numberOfReplicas );
636
+
637
+ final Optional <String > shardLimit = IndicesService .checkShardLimit (shardsToCreate , clusterState , deprecationLogger );
638
+ if (shardLimit .isPresent ()) {
639
+ final ValidationException e = new ValidationException ();
640
+ e .addValidationError (shardLimit .get ());
641
+ throw e ;
642
+ }
629
643
}
630
644
631
645
List <String > getIndexSettingsValidationErrors (final Settings settings , final boolean forbidPrivateIndexSettings ) {
0 commit comments