uranusjr commented on code in PR #32709:
URL: https://github.com/apache/airflow/pull/32709#discussion_r1272919829
##########
airflow/models/pool.py:
##########
@@ -161,21 +163,26 @@ def slots_stats(
from airflow.models.taskinstance import TaskInstance # Avoid circular
import
pools: dict[str, PoolStats] = {}
+ pool_includes_deferred: dict[str, bool] = {}
- query = select(Pool.pool, Pool.slots)
+ query = select(Pool.pool, Pool.slots, Pool.include_deferred)
if lock_rows:
query = with_row_locks(query, session=session, **nowait(session))
pool_rows = session.execute(query)
- for (pool_name, total_slots) in pool_rows:
+ for (pool_name, total_slots, includes_deferred) in pool_rows:
Review Comment:
Please unify whether you want to use _include_ or _includes_.
##########
airflow/models/pool.py:
##########
@@ -224,15 +236,25 @@ def occupied_slots(self, session: Session = NEW_SESSION)
-> int:
"""
from airflow.models.taskinstance import TaskInstance # Avoid circular
import
+ occupied_states = Pool.get_occupied_states(self.include_deferred)
+
return int(
session.scalar(
select(func.sum(TaskInstance.pool_slots))
.filter(TaskInstance.pool == self.pool)
- .filter(TaskInstance.state.in_(EXECUTION_STATES))
+ .filter(TaskInstance.state.in_(occupied_states))
)
or 0
)
+ @staticmethod
+ def get_occupied_states(include_deferred):
+ if include_deferred:
+ return EXECUTION_STATES | {
+ TaskInstanceState.DEFERRED,
+ }
+ return EXECUTION_STATES
Review Comment:
Why is this not an instance method so we can simply do
`pool.get_occupied_states()` without arguments?
##########
airflow/migrations/versions/0128_2_7_0_add_include_deferred_column_to_pool.py:
##########
@@ -0,0 +1,52 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""add include_deferred column to pool
+
+Revision ID: 405de8318b3a
+Revises: 788397e78828
+Create Date: 2023-07-20 04:22:21.007342
+
+"""
+
+import sqlalchemy as sa
+from alembic import op
+
+
+# revision identifiers, used by Alembic.
+revision = "405de8318b3a"
+down_revision = "788397e78828"
+branch_labels = None
+depends_on = None
+airflow_version = "2.7.0"
+
+
+def upgrade():
+ """Apply add include_deferred column to pool"""
+ with op.batch_alter_table("slot_pool") as batch_op:
+ batch_op.add_column(
+ sa.Column(
+ "include_deferred", sa.Boolean, default=False, nullable=False,
server_default=sa.false()
Review Comment:
Do we still need `default`?
##########
airflow/models/pool.py:
##########
@@ -290,6 +312,25 @@ def scheduled_slots(self, session: Session = NEW_SESSION)
-> int:
or 0
)
+ @provide_session
+ def deferred_slots(self, session: Session = NEW_SESSION) -> int:
+ """
+ Get the number of slots deferred at the moment.
+
+ :param session: SQLAlchemy ORM Session
+ :return: the number of deferred slots
+ """
+ from airflow.models.taskinstance import TaskInstance # Avoid circular
import
+
+ return int(
+ session.scalar(
+ select(func.sum(TaskInstance.pool_slots))
+ .filter(TaskInstance.pool == self.pool)
+ .filter(TaskInstance.state == TaskInstanceState.DEFERRED)
Review Comment:
```suggestion
.where(TaskInstance.pool == self.pool, TaskInstance.state ==
TaskInstanceState.DEFERRED)
```
--
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]