diff options
author | Jay Faulkner <jay@jvf.cc> | 2022-11-10 15:54:44 -0800 |
---|---|---|
committer | Jay Faulkner <jay@jvf.cc> | 2023-02-13 11:46:21 -0800 |
commit | 36ef217fdb3a63ad70a567a94f1229922409964b (patch) | |
tree | 5093c72b6b2a01fa20a0c35c9b857039da50d227 /ironic/db/sqlalchemy/api.py | |
parent | a66208f24b021e08efe8c56bbaa6b760e9a15804 (diff) | |
download | ironic-36ef217fdb3a63ad70a567a94f1229922409964b.tar.gz |
DB & Object layer for node.shard
DB and object implementations for new node.shard key.
Story: 2010768
Task: 46624
Change-Id: Ia7ef3cffc321c93501b1cc5185972a4ac1dcb212
Diffstat (limited to 'ironic/db/sqlalchemy/api.py')
-rw-r--r-- | ironic/db/sqlalchemy/api.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/ironic/db/sqlalchemy/api.py b/ironic/db/sqlalchemy/api.py index 7a6e6862e..7e9254b05 100644 --- a/ironic/db/sqlalchemy/api.py +++ b/ironic/db/sqlalchemy/api.py @@ -2588,3 +2588,31 @@ class Connection(api.Connection): return query.one() except NoResultFound: raise exception.NodeInventoryNotFound(node_id=node_id) + + def get_shard_list(self): + """Return a list of shards. + + :returns: A list of dicts containing the keys name and count. + """ + # Note(JayF): This should never be a large enough list to require + # pagination. Furthermore, it wouldn't really be a sensible + # thing to paginate as the data it's fetching can mutate. + # So we just aren't even going to try. + shard_list = [] + with _session_for_read() as session: + res = session.execute( + # Note(JayF): SQLAlchemy counts are notoriously slow because + # sometimes they will use a subquery. Be careful + # before changing this to use any magic. + sa.text( + "SELECT count(id), shard from nodes group by shard;" + )).fetchall() + + if res: + res.sort(key=lambda x: x[0], reverse=True) + for shard in res: + shard_list.append( + {"name": str(shard[1]), "count": shard[0]} + ) + + return shard_list |