3030import com .google .common .collect .Iterables ;
3131import com .google .common .collect .Sets ;
3232
33+ import org .apache .cassandra .config .DatabaseDescriptor ;
3334import org .apache .cassandra .cql3 .QualifiedName ;
3435import org .apache .cassandra .db .TypeSizes ;
3536import org .apache .cassandra .exceptions .InvalidRequestException ;
@@ -379,10 +380,10 @@ public static IndexHints fromCQLNames(Set<QualifiedName> included,
379380 TableMetadata table ,
380381 IndexRegistry indexRegistry )
381382 {
382- if (included != null && included .size () > Short . MAX_VALUE )
383+ if (included != null && included .size () > maxIncludedOrExcludedIndexCount () )
383384 throw new InvalidRequestException (TOO_MANY_INDEXES_ERROR + included .size ());
384385
385- if (excluded != null && excluded .size () > Short . MAX_VALUE )
386+ if (excluded != null && excluded .size () > maxIncludedOrExcludedIndexCount () )
386387 throw new InvalidRequestException (TOO_MANY_INDEXES_ERROR + excluded .size ());
387388
388389 IndexHints hints = IndexHints .create (fetchIndexes (included , table , indexRegistry ),
@@ -408,6 +409,14 @@ public static IndexHints fromCQLNames(Set<QualifiedName> included,
408409 return hints ;
409410 }
410411
412+ private static int maxIncludedOrExcludedIndexCount ()
413+ {
414+ int guardrail = DatabaseDescriptor .getGuardrailsConfig ().getSecondaryIndexesPerTableFailThreshold ();
415+
416+ // If no guardrail is configured, use a value that safely fits in a single byte for serialization:
417+ return guardrail > 0 ? guardrail : 128 ;
418+ }
419+
411420 private static Set <IndexMetadata > fetchIndexes (Set <QualifiedName > indexNames , TableMetadata table , IndexRegistry indexRegistry )
412421 {
413422 if (indexNames == null || indexNames .isEmpty ())
@@ -583,9 +592,9 @@ private void serialize(Set<IndexMetadata> indexes, DataOutputPlus out, int versi
583592 return ;
584593
585594 int n = indexes .size ();
586- assert n < Short . MAX_VALUE : TOO_MANY_INDEXES_ERROR + n ;
595+ assert n < maxIncludedOrExcludedIndexCount () : TOO_MANY_INDEXES_ERROR + n ;
587596
588- out .writeVInt (n );
597+ out .writeVInt32 (n );
589598 for (IndexMetadata index : indexes )
590599 IndexMetadata .serializer .serialize (index , out , version );
591600 }
0 commit comments