2020package org .elasticsearch .cluster .metadata ;
2121
2222import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
23- import org .apache .logging .log4j .Logger ;
2423import org .apache .logging .log4j .LogManager ;
24+ import org .apache .logging .log4j .Logger ;
2525import org .apache .logging .log4j .message .ParameterizedMessage ;
2626import org .elasticsearch .ElasticsearchException ;
2727import org .elasticsearch .ResourceAlreadyExistsException ;
@@ -435,6 +435,13 @@ public ClusterState execute(ClusterState currentState) throws Exception {
435435 indexScopedSettings );
436436 }
437437 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+
438445 tmpImdBuilder .settings (actualIndexSettings );
439446
440447 if (recoverFromIndex != null ) {
@@ -583,6 +590,10 @@ public ClusterState execute(ClusterState currentState) throws Exception {
583590 }
584591 }
585592
593+ protected void checkShardLimit (final Settings settings , final ClusterState clusterState ) {
594+ MetaDataCreateIndexService .checkShardLimit (settings , clusterState , deprecationLogger );
595+ }
596+
586597 @ Override
587598 public void onFailure (String source , Exception e ) {
588599 if (e instanceof ResourceAlreadyExistsException ) {
@@ -603,9 +614,6 @@ public void validateIndexSettings(String indexName, final Settings settings, fin
603614 final boolean forbidPrivateIndexSettings ) throws IndexCreationException {
604615 List <String > validationErrors = getIndexSettingsValidationErrors (settings , forbidPrivateIndexSettings );
605616
606- Optional <String > shardAllocation = checkShardLimit (settings , clusterState , deprecationLogger );
607- shardAllocation .ifPresent (validationErrors ::add );
608-
609617 if (validationErrors .isEmpty () == false ) {
610618 ValidationException validationException = new ValidationException ();
611619 validationException .addValidationErrors (validationErrors );
@@ -616,16 +624,22 @@ public void validateIndexSettings(String indexName, final Settings settings, fin
616624 /**
617625 * Checks whether an index can be created without going over the cluster shard limit.
618626 *
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
623631 */
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+ }
629643 }
630644
631645 List <String > getIndexSettingsValidationErrors (final Settings settings , final boolean forbidPrivateIndexSettings ) {
0 commit comments