python/ray/serve/deployment_state.py
def _stop_wrong_version_replicas(self) -> bool:
        """Stops replicas with outdated versions to implement rolling updates.

        This includes both explicit code version updates and changes to the
        user_config.

        Returns whether any replicas were stopped.
        """
        # Short circuit if target replicas is 0 (the deployment is being
        # deleted) because this will be handled in the main loop.
        if self._target_replicas == 0:
            return False

        # We include STARTING and UPDATING replicas here
        # because if there are replicas still pending startup, we may as well
        # terminate them and start new version replicas instead.
        old_running_replicas = self._replicas.count(
            exclude_version=self._target_version,
            states=[ReplicaState.STARTING, ReplicaState.UPDATING, ReplicaState.RUNNING],
        )
        old_stopping_replicas = self._replicas.count(
            exclude_version=self._target_version, states=[ReplicaState.STOPPING]
        )
        new_running_replicas = self._replicas.count(
            version=self._target_version, states=[ReplicaState.RUNNING]
        )

        # If the deployment is currently scaling down, let the scale down
        # complete before doing a rolling update.
        if self._target_replicas < old_running_replicas + old_stopping_replicas:
            return 0

        # The number of replicas that are currently in transition between
        # an old version and the new version. Note that we cannot directly
        # count the number of stopping replicas because once replicas finish
        # stopping, they are removed from the data structure.
        pending_replicas = (
            self._target_replicas - new_running_replicas - old_running_replicas
        )

        # Maximum number of replicas that can be updating at any given time.
        # There should never be more than rollout_size old replicas stopping
        # or rollout_size new replicas starting.
        rollout_size = max(int(0.2 * self._target_replicas), 1)
        max_to_stop = max(rollout_size - pending_replicas, 0)

        replicas_to_update = self._replicas.pop(
            exclude_version=self._target_version,
            states=[ReplicaState.STARTING, ReplicaState.RUNNING],
            max_replicas=max_to_stop,
            ranking_function=rank_replicas_for_stopping,
        )

        replicas_stopped = False
        code_version_changes = 0
        user_config_changes = 0
        for replica in replicas_to_update:
            # If the code version is a mismatch, we stop the replica. A new one
            # with the correct version will be started later as part of the
            # normal scale-up process.
            if replica.version.code_version != self._target_version.code_version:
                code_version_changes += 1
                replica.stop()
                self._replicas.add(ReplicaState.STOPPING, replica)
                replicas_stopped = True
            # If only the user_config is a mismatch, we update it dynamically
            # without restarting the replica.
            elif (
                replica.version.user_config_hash
                != self._target_version.user_config_hash
            ):
                user_config_changes += 1
                replica.update_user_config(self._target_version.user_config)
                self._replicas.add(ReplicaState.UPDATING, replica)
                logger.debug(
                    "Adding UPDATING to replica_tag: "
                    f"{replica.replica_tag}, deployment_name: {self._name}"
                )
            else:
                assert False, "Update must be code version or user config."

        if code_version_changes > 0:
            logger.info(
                f"Stopping {code_version_changes} replicas of "
                f"deployment '{self._name}' with outdated versions."
            )

        if user_config_changes > 0:
            logger.info(
                f"Updating {user_config_changes} replicas of "
                f"deployment '{self._name}' with outdated "
                f"user_configs."
            )

        return replicas_stopped
Stops replicas with outdated versions to implement rolling updates.

        This includes both explicit code version updates and changes to the
        user_config.

        Returns whether any replicas were stopped.
        
Stops replicas with outdated versions to implement rolling updates.
This includes both explicit code version updates and changes to the
user_config.

Returns whether any replicas were stopped.
Stops replicas with outdated versions to implement rolling updates . This includes both explicit code version updates and changes to the user_config . Returns whether any replicas were stopped .
def _stop_wrong_version_replicas(self) -> bool:
        if self._target_replicas == 0:
            return False
        old_running_replicas = self._replicas.count(
            exclude_version=self._target_version,
            states=[ReplicaState.STARTING, ReplicaState.UPDATING, ReplicaState.RUNNING],
        )
        old_stopping_replicas = self._replicas.count(
            exclude_version=self._target_version, states=[ReplicaState.STOPPING]
        )
        new_running_replicas = self._replicas.count(
            version=self._target_version, states=[ReplicaState.RUNNING]
        )
        if self._target_replicas < old_running_replicas + old_stopping_replicas:
            return 0
        pending_replicas = (
            self._target_replicas - new_running_replicas - old_running_replicas
        )
        rollout_size = max(int(0.2 * self._target_replicas), 1)
        max_to_stop = max(rollout_size - pending_replicas, 0)
        replicas_to_update = self._replicas.pop(
            exclude_version=self._target_version,
            states=[ReplicaState.STARTING, ReplicaState.RUNNING],
            max_replicas=max_to_stop,
            ranking_function=rank_replicas_for_stopping,
        )
        replicas_stopped = False
        code_version_changes = 0
        user_config_changes = 0
        for replica in replicas_to_update:
            if replica.version.code_version != self._target_version.code_version:
                code_version_changes += 1
                replica.stop()
                self._replicas.add(ReplicaState.STOPPING, replica)
                replicas_stopped = True
            elif (
                replica.version.user_config_hash
                != self._target_version.user_config_hash
            ):
                user_config_changes += 1
                replica.update_user_config(self._target_version.user_config)
                self._replicas.add(ReplicaState.UPDATING, replica)
                logger.debug(
                    "Adding UPDATING to replica_tag: "
                    f"{replica.replica_tag}, deployment_name: {self._name}"
                )
            else:
                assert False, "Update must be code version or user config."
        if code_version_changes > 0:
            logger.info(
                f"Stopping {code_version_changes} replicas of "
                f"deployment '{self._name}' with outdated versions."
            )
        if user_config_changes > 0:
            logger.info(
                f"Updating {user_config_changes} replicas of "
                f"deployment '{self._name}' with outdated "
                f"user_configs."
            )
        return replicas_stopped
def _stop_wrong_version_replicas ( self ) -> bool : if self . _target_replicas == 0 : return False old_running_replicas = self . _replicas . count ( exclude_version = self . _target_version , states = [ ReplicaState . STARTING , ReplicaState . UPDATING , ReplicaState . RUNNING ] , ) old_stopping_replicas = self . _replicas . count ( exclude_version = self . _target_version , states = [ ReplicaState . STOPPING ] ) new_running_replicas = self . _replicas . count ( version = self . _target_version , states = [ ReplicaState . RUNNING ] ) if self . _target_replicas < old_running_replicas + old_stopping_replicas : return 0 pending_replicas = ( self . _target_replicas - new_running_replicas - old_running_replicas ) rollout_size = max ( int ( 0.2 * self . _target_replicas ) , 1 ) max_to_stop = max ( rollout_size - pending_replicas , 0 ) replicas_to_update = self . _replicas . pop ( exclude_version = self . _target_version , states = [ ReplicaState . STARTING , ReplicaState . RUNNING ] , max_replicas = max_to_stop , ranking_function = rank_replicas_for_stopping , ) replicas_stopped = False code_version_changes = 0 user_config_changes = 0 for replica in replicas_to_update : if replica . version . code_version != self . _target_version . code_version : code_version_changes += 1 replica . stop ( ) self . _replicas . add ( ReplicaState . STOPPING , replica ) replicas_stopped = True elif ( replica . version . user_config_hash != self . _target_version . user_config_hash ) : user_config_changes += 1 replica . update_user_config ( self . _target_version . user_config ) self . _replicas . add ( ReplicaState . UPDATING , replica ) logger . debug ( "Adding UPDATING to replica_tag: " f"{replica.replica_tag}, deployment_name: {self._name}" ) else : assert False , "Update must be code version or user config." if code_version_changes > 0 : logger . info ( f"Stopping {code_version_changes} replicas of " f"deployment '{self._name}' with outdated versions." ) if user_config_changes > 0 : logger . info ( f"Updating {user_config_changes} replicas of " f"deployment '{self._name}' with outdated " f"user_configs." ) return replicas_stopped
Stops replicas with outdated versions to implement rolling updates.
Stops replicas with outdated versions to implement rolling updates .
"""Stops replicas with outdated versions to implement rolling updates.

        This includes both explicit code version updates and changes to the
        user_config.

        Returns whether any replicas were stopped.
        """ # Short circuit if target replicas is 0 (the deployment is being # deleted) because this will be handled in the main loop. # We include STARTING and UPDATING replicas here # because if there are replicas still pending startup, we may as well # terminate them and start new version replicas instead. # If the deployment is currently scaling down, let the scale down # complete before doing a rolling update. # The number of replicas that are currently in transition between # an old version and the new version. Note that we cannot directly # count the number of stopping replicas because once replicas finish # stopping, they are removed from the data structure. # Maximum number of replicas that can be updating at any given time. # There should never be more than rollout_size old replicas stopping # or rollout_size new replicas starting. # If the code version is a mismatch, we stop the replica. A new one # with the correct version will be started later as part of the # normal scale-up process. # If only the user_config is a mismatch, we update it dynamically # without restarting the replica.
self