@@ -583,10 +583,7 @@ func doStructuralNormalization(conf *normalizedSpanConfig) error {
583583 voterConstraints [rel .voterIndex ].numReplicas
584584 if neededVoterReplicas > 0 && remainingAll > 0 {
585585 // We can satisfy some voter replicas.
586- toAdd := remainingAll
587- if toAdd > neededVoterReplicas {
588- toAdd = neededVoterReplicas
589- }
586+ toAdd := min (remainingAll , neededVoterReplicas )
590587 voterConstraints [rel .voterIndex ].numReplicas += toAdd
591588 allReplicaConstraints [rel .allIndex ].remainingReplicas -= toAdd
592589 }
@@ -610,18 +607,14 @@ func doStructuralNormalization(conf *normalizedSpanConfig) error {
610607 // constraint will then be available for subsequent narrowing.
611608 if emptyVoterConstraintIndex > 0 && emptyConstraintIndex > 0 {
612609 neededReplicas := conf .voterConstraints [emptyVoterConstraintIndex ].numReplicas
613- actualReplicas := voterConstraints [emptyVoterConstraintIndex ].numReplicas
614- remaining := neededReplicas - actualReplicas
615- if remaining > 0 {
616- remainingSatisfiable := allReplicaConstraints [emptyConstraintIndex ].remainingReplicas
617- if remainingSatisfiable > 0 {
618- count := remainingSatisfiable
619- if count > remaining {
620- count = remaining
621- }
622- voterConstraints [emptyVoterConstraintIndex ].numReplicas += count
623- allReplicaConstraints [emptyConstraintIndex ].remainingReplicas -= count
624- }
610+ if voterConstraints [emptyVoterConstraintIndex ].numReplicas != 0 {
611+ panic ("programming error" )
612+ }
613+ remainingSatisfiable := allReplicaConstraints [emptyConstraintIndex ].remainingReplicas
614+ if neededReplicas > 0 && remainingSatisfiable > 0 {
615+ count := min (remainingSatisfiable , neededReplicas )
616+ voterConstraints [emptyVoterConstraintIndex ].numReplicas += count
617+ allReplicaConstraints [emptyConstraintIndex ].remainingReplicas -= count
625618 }
626619 }
627620
@@ -767,19 +760,17 @@ func doStructuralNormalization(conf *normalizedSpanConfig) error {
767760 // rel.voterIndex must be emptyVoterConstraintIndex.
768761 continue
769762 }
770- if conf .constraints [rel .allIndex ].numReplicas < conf .voterConstraints [rel .voterIndex ].numReplicas {
771- toAddCount := conf .voterConstraints [rel .voterIndex ].numReplicas -
772- conf .constraints [rel .allIndex ].numReplicas
773- availableCount := conf .constraints [emptyConstraintIndex ].numReplicas
774- if availableCount < toAddCount {
775- // TODO(wenyihu6): this should also create an error, since we will
776- // end up with two identical constraint conjunctions in replica
777- // constraints and voter constraints, with the former having a lower
778- // count than the latter.
779- toAddCount = availableCount
780- }
781- conf .constraints [emptyConstraintIndex ].numReplicas -= toAddCount
782- conf .constraints [rel .allIndex ].numReplicas += toAddCount
763+ toAddCount := conf .voterConstraints [rel .voterIndex ].numReplicas -
764+ conf .constraints [rel .allIndex ].numReplicas
765+ availableCount := conf .constraints [emptyConstraintIndex ].numReplicas
766+ if toAddCount > 0 && availableCount > 0 {
767+ // TODO(wenyihu6): this should also create an error, since we will
768+ // end up with two identical constraint conjunctions in replica
769+ // constraints and voter constraints, with the former having a lower
770+ // count than the latter.
771+ add := min (toAddCount , availableCount )
772+ conf .constraints [emptyConstraintIndex ].numReplicas -= add
773+ conf .constraints [rel .allIndex ].numReplicas += add
783774 }
784775 }
785776 // For conjStrictSubset, if the subset relationship is with
@@ -796,14 +787,12 @@ func doStructuralNormalization(conf *normalizedSpanConfig) error {
796787 continue
797788 }
798789 availableCount := conf .constraints [emptyConstraintIndex ].numReplicas
799- if availableCount > 0 {
800- toAddCount := conf .voterConstraints [rel .voterIndex ].numReplicas
801- if toAddCount > availableCount {
802- toAddCount = availableCount
803- }
804- conf .constraints [emptyConstraintIndex ].numReplicas -= toAddCount
790+ toAddCount := conf .voterConstraints [rel .voterIndex ].numReplicas
791+ if availableCount > 0 && toAddCount > 0 {
792+ add := min (availableCount , toAddCount )
793+ conf .constraints [emptyConstraintIndex ].numReplicas -= add
805794 conf .constraints = append (conf .constraints , internedConstraintsConjunction {
806- numReplicas : toAddCount ,
795+ numReplicas : add ,
807796 constraints : conf .voterConstraints [rel .voterIndex ].constraints ,
808797 })
809798 }
0 commit comments