SameerMesiah97 commented on code in PR #68922:
URL: https://github.com/apache/airflow/pull/68922#discussion_r3468430404


##########
providers/amazon/src/airflow/providers/amazon/aws/operators/redshift_cluster.py:
##########
@@ -872,7 +872,39 @@ def __init__(
         self.deferrable = deferrable
         self.max_attempts = max_attempts
 
+    def _resume_if_paused(self) -> None:
+        """
+        Resume the cluster first if it is paused, so it can be deleted.
+
+        A ``paused`` Redshift cluster cannot be deleted -- ``delete_cluster`` 
raises
+        ``InvalidClusterStateFault`` ("There is an operation running on the 
Cluster") and no
+        amount of retrying helps, because a paused cluster never leaves that 
state on its own.
+        Left unhandled, the cluster is silently leaked (it stays paused 
indefinitely until
+        external cleanup reaps it). Resume it and wait until it is 
``available`` before deleting.
+        """
+        try:
+            cluster_state = 
self.hook.cluster_status(cluster_identifier=self.cluster_identifier)
+        except self.hook.conn.exceptions.ClusterNotFoundFault:
+            return
+
+        if cluster_state != "paused":
+            return
+
+        self.log.info(
+            "Cluster %s is paused; resuming it before deletion (a paused 
cluster cannot be deleted).",
+            self.cluster_identifier,
+        )
+        
self.hook.conn.resume_cluster(ClusterIdentifier=self.cluster_identifier)
+        self.hook.conn.get_waiter("cluster_available").wait(
+            ClusterIdentifier=self.cluster_identifier,
+            WaiterConfig={"Delay": self.poll_interval, "MaxAttempts": 
self.max_attempts},
+        )
+
     def execute(self, context: Context):
+        # A paused cluster cannot be deleted; resume it first (otherwise the 
retry loop below
+        # would exhaust against InvalidClusterStateFault and the cluster would 
be leaked).
+        self._resume_if_paused()

Review Comment:
   Agree with the flag and that the default should be `False`. Of course, the 
downside here is that the `RedshiftDeleteClusterOperator` would remain inert 
for the paused clusters but that is a much better outcome than the one 
@o-nikolas outlined i.e. resumed clusters (which I believe cost significantly 
more) being orphaned. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to