Skip to content

Add support for limit and offset in list_values method #5176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 12, 2021

Conversation

anirudhbagri
Copy link
Contributor

@anirudhbagri anirudhbagri commented Mar 3, 2021

Added ability to provide limit and offset for list_values().

Partially fixes #5097 and fixes #5171.

@pull-request-size pull-request-size bot added the size/S PR that changes 10-29 lines. Very easy to review. label Mar 3, 2021
@cognifloyd
Copy link
Member

We just reformatted the code with black. (Hooray!) And this PR got caught in the cross fire too. (Arrgh!)
Please merge master into your branch, resolve the conflicts (Ouch! Sorry!), and reformat with black (I recommend running pre-commit install after you've merged master so that black reformatting happens automatically on commit).

@arm4b
Copy link
Member

arm4b commented Mar 8, 2021

@cognifloyd Can you please also point @anirudhbagri in the right direction to finish this PR?

Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just reviewing this. My first question was, what defines the default limit? It looks like it's defined in config:

st2/conf/st2.conf.sample

Lines 43 to 44 in 56101b8

# Maximum limit (page size) argument which can be specified by the user in a query string.
max_page_size = 100

cfg.IntOpt(
"max_page_size",
default=100,
help="Maximum limit (page size) argument which can be "
"specified by the user in a query string.",
),

# Maximum value of limit which can be specified by user
@property
def max_limit(self):
return cfg.CONF.api.max_page_size

So, could we use the default from the config?

@cognifloyd
Copy link
Member

Let's see. I think we'll also need some tests for this feature. It looks like this is where the list_values() tests are:

def test_datastore_operations_list_values(self):

Oh, and I guess that's also the test that is failing: https://github.com/StackStorm/st2/pull/5176/checks?check_run_id=2063056481#step:14:2287

Plus, I think we'll need a test for the st2client bit.

@cognifloyd
Copy link
Member

Oh. And I often forget adding a changelog entry. Please do that too. Then I think this will be good to merge :)

@pull-request-size pull-request-size bot added size/M PR that changes 30-99 lines. Good size to review. and removed size/S PR that changes 10-29 lines. Very easy to review. labels Mar 9, 2021
@arm4b arm4b added this to the 3.5.0 milestone Mar 9, 2021
Copy link
Member

@arm4b arm4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @anirudhbagri for the contribution and @cognifloyd for review and making this PR complete 👍

@Kami would also appreciate your final 👀 and ✔️ as someone involved in previous pagination discussions around this specific issue.

@arm4b arm4b requested a review from Kami March 9, 2021 20:06
Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -195,6 +196,9 @@ def get_all(self, **kwargs):
if user:
params["user"] = user

if offset:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since if we use a truthy check, this means offset won't be included if set to 0, but yeah I think this should be fine since that's the current / existing default behavior if offset is not specified.

@Kami Kami merged commit 514586d into StackStorm:master Mar 12, 2021
@glafkosch
Copy link

glafkosch commented Apr 15, 2023

I have upgraded to latest Stackstorm 3.8 version and I am getting also this issue. Any fix?

st2.actions.python.WorkflowCleanupAction: DEBUG Creating new Client object.\n\"auth.api_url\" configuration option is not configured\nst2.actions.python.WorkflowCleanupAction: DEBUG Retrieving all the values from the datastore\nTraceback (most recent call last):\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/oslo_config/cfg.py\", line 2262, in _get\n return self.__cache[key]\nKeyError: ('api', 'max_page_size')\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/python_runner/python_action_wrapper.py\", line 395, in <module>\n obj.run()\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/python_runner/python_action_wrapper.py\", line 214, in run\n output = action.run(**self._parameters)\n File \"/opt/stackstorm/packs/tf/actions/workflow_cleanup.py\", line 14, in run\n items = self.action_service.list_values(local=False, prefix=key_prefix)\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/python_runner/python_action_wrapper.py\", line 136, in list_values\n return self.datastore_service.list_values(local=local, prefix=prefix)\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/st2common/services/datastore.py\", line 92, in list_values\n limit = limit or cfg.CONF.api.max_page_size\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/oslo_config/cfg.py\", line 2547, in __getattr__\n return self._conf._get(name, self._group)\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/oslo_config/cfg.py\", line 2264, in _get\n value = self._do_get(name, group, namespace)\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/oslo_config/cfg.py\", line 2282, in _do_get\n info = self._get_opt_info(name, group)\n File \"/opt/stackstorm/st2/lib/python3.8/site-packages/oslo_config/cfg.py\", line 2415, in _get_opt_info\n raise NoSuchOptError(opt_name, group)\noslo_config.cfg.NoSuchOptError: no such option in group api: max_page_size\n"

My workflow_clean.py

from st2common.runners.base_action import Action

class WorkflowCleanupAction(Action):
    def run(self, scan_id):
        key_prefix = f"tf.StoreWorkerResultAction:{scan_id}"
        items = self.action_service.list_values(local=False, prefix=key_prefix)
        for item in items:
            self.action_service.delete_value(item.name, local=False)

        return (True, None)

Temporary Fix

        #items = self.action_service.list_values(local=False, prefix=key_prefix)
        items = self.action_service.datastore_service.list_values(local=False, prefix=key_prefix, limit=100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement size/M PR that changes 30-99 lines. Good size to review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sensor_service.list_value() doesn't return all the keys macthing the prefix Sensor "list_values" method limited to 100 keystore values
5 participants