Skip to content

ActiveRecord 5.2 StatementCache Pins Queries to Shards #554

@bschmeck

Description

@bschmeck

A change was made in ActiveRecord 5.2 such that in addition to the generated AST for a query, the class used to execute that query is cached. The StatementCache grabs that class via relation.klass which would normally be an ActiveRecord model. However, Octopus wraps the relation in a RelationProxy and that proxy object is the return value of relation.klass.

The next time a query that has a cached statement (e.g. find_by(id:)) the query is executed using that RelationProxy object. This is problematic because that RelationProxy object stores the shard it queried the first time and will send all subsequent queries to that shard.

I have been able to get our test suite to pass by monkey patching StatementCache::create to pull the underlying AR model via relation.klass.klass.klass but I would prefer to find a solution that modifies Octopus instead of ActiveRecord.

We are very late in upgrading to 5.2, I had assumed Octopus was widely used with AR 5.2 and am surprised to have encountered this bug. Is this a known issue for Octopus and is there a recommended workaround?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions