~5s delay when switching organisations via API

api

#1

We’re trying to automate creating/updating all the data (users, orgs, dashboards etc) via APIs. Every time we change the freshly created user’s active organisation, the action seems to actually take effect about 5 seconds after issuing the command. Seems there’s a cache somewhere that isn’t getting updated fast enough. Any ideas on how to circumvent this?

I’ve created a quick and dirty test case (requires java & docker): GrafanaTest.java.

It runs grafana via docker, creates two organisations and starts switching the admin user’s active organisation between them. Then it waits until /api/org returns the correct id.
The user in question is admin for simplicity, could be any user.

Output:

It took 4834 ms to change the active organization to 2
It took 5016 ms to change the active organization to 3
It took 5000 ms to change the active organization to 2
It took 5013 ms to change the active organization to 3
It took 5012 ms to change the active organization to 2
It took 5014 ms to change the active organization to 3
It took 5014 ms to change the active organization to 2
It took 5005 ms to change the active organization to 3
It took 5019 ms to change the active organization to 2
It took 5019 ms to change the active organization to 3
Took 4994 ms on average

Edit: this seems like a regression in 5.4.2. Tested with 5.3.4 and the results are quite different. Created a new issue: https://github.com/grafana/grafana/issues/14826