Skip to content

Update methods sent to read-only database with identity_cache gem  #542

@gaga5lala

Description

@gaga5lala

Hi, I've encountered a shard mismatch problem when using octopus with identity_cache gem.

Environment

Databases:

  • master * 1
  • replica * 1

Config:

  • replicated: true
  • fully_replicated: false (set replicated_model by ourself)

Gems:

  • rails 4.2.11
  • octopus: 0.10.2
  • identity_cache: 0.4.1

Scenario

Model

  • User (replicated_model)
    • has_one :setting
  • Setting (replicated_model)

When I want to update the setting record fetched from cache, the update query will be sent to the replica shard.

# in controller

# first request works, later requests all failed
User.fetch(1).setting.update(some_attribute)

By using binding.pry I found that first time .current_shard will return :master, record fetched by another request will get :replica then the update query sent to wrong replica database.

But if I using normal Active Record method (e.g. Member.find(1).setting.update(some_attribute)) all request are success.

Is there some class variables stored across requests but computing after ActiveRecord instance initialize, but record initialized by IdentityCache cause the problem?

Thanks

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