33use std:: collections:: BTreeMap ;
44
55use snafu:: { ResultExt , Snafu , ensure} ;
6- use stackable_operator:: memory:: MemoryQuantity ;
6+ use stackable_operator:: {
7+ memory:: MemoryQuantity ,
8+ role_utils:: { ZeroReplicasCounting , fixed_replica_count} ,
9+ } ;
710
811use super :: {
912 ConfigFileName , env_reference, file_reference, format_properties,
@@ -21,7 +24,7 @@ use crate::{
2124 } ,
2225 } ,
2326 } ,
24- crd:: { storage:: NifiRepository , v1alpha1} ,
27+ crd:: { NifiRole , storage:: NifiRepository , v1alpha1} ,
2528 security:: {
2629 authentication:: {
2730 NifiAuthenticationConfig , STACKABLE_SERVER_TLS_DIR , STACKABLE_TLS_STORE_PASSWORD ,
@@ -526,9 +529,26 @@ pub fn build(
526529 "nifi.cluster.node.protocol.port" . to_string ( ) ,
527530 PROTOCOL_PORT . to_string ( ) ,
528531 ) ;
532+
533+ // In case the number of NiFi nodes is hard-coded to a fixed number (no auto-scaling), we can
534+ // tell this NiFi, so that startup is much quicker.
535+ let fixed_replica_count = fixed_replica_count (
536+ cluster
537+ . role_group_configs
538+ . get ( & NifiRole :: Node )
539+ . iter ( )
540+ . flat_map ( |nodes| nodes. values ( ) )
541+ . map ( |rg| rg. replicas ) ,
542+ // We rather treat explicit 0 values as [`None`], so that we don't end up with too few nodes
543+ ZeroReplicasCounting :: TreatAsZero ,
544+ ) ;
545+ let max_election_candidates = fixed_replica_count
546+ . map ( |count| count. to_string ( ) )
547+ // In case we don't know the replica count, we set it to "" as that's what we always did
548+ . unwrap_or_default ( ) ;
529549 properties. insert (
530550 "nifi.cluster.flow.election.max.candidates" . to_string ( ) ,
531- "" . to_string ( ) ,
551+ max_election_candidates ,
532552 ) ;
533553
534554 match cluster. cluster_config . clustering_backend {
0 commit comments