invalidate-project-configs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. import click
  3. from sentry.runner import configure
  4. def validate_percentage(ctx, param, value):
  5. if 1 <= value <= 100:
  6. return value
  7. raise click.BadParameter("percentage must be integer between 1 and 100")
  8. @click.command()
  9. @click.option(
  10. "--percentage",
  11. callback=validate_percentage,
  12. default=100,
  13. help="Percentage of orgs to invalidate. Only useful for testing this command.",
  14. )
  15. def invalidate_project_configs(percentage):
  16. """Gradually evict existing project configs from redis and recompute them.
  17. This can be used to speed up incident recovery, for example when faulty project configs
  18. that cannot be read by Relay have been written to redis.
  19. NOTE: This will increase load on the relay_config_bulk Celery queue.
  20. """
  21. configure()
  22. from sentry.models.organization import Organization
  23. from sentry.tasks.relay import schedule_invalidate_project_config
  24. from sentry.utils.query import RangeQuerySetWrapperWithProgressBar
  25. # NOTE: Instead of iterating over all projects, we could use redis.scan_iter()
  26. # to evict only project configs that exist in redis.
  27. queryset = Organization.objects.all().values_list("id", flat=True)
  28. click.echo(f"Invalidating project configs for {percentage}% of orgs...")
  29. for org_id in RangeQuerySetWrapperWithProgressBar(queryset, result_value_getter=lambda x: x):
  30. if (org_id % 100) < percentage:
  31. schedule_invalidate_project_config(
  32. trigger="invalidate-all-orgs", organization_id=org_id, countdown=0
  33. )
  34. if __name__ == "__main__":
  35. invalidate_project_configs()