Search code examples
ruby-on-railsruby-on-rails-3sphinxthinking-sphinx

Rails 3 ThinkingSphinx multitenancy


We're in the middle of a bit of a big upgrade:

  • Rails 2.3 to Rails 3.2
  • ThinkingSphinx 1.4.1 to ThinkingSphinx 3.0.6
  • Sphinx 0.9.9 to Sphinx 2.0.8

And now our ThinkingSphinx configuration is broken. We have a multi-tenant setup, so it's a bit complicated as to how we connect. Our method set_sphinx_connection is called every time the search action is fired off. Here's how we used to do it:

def set_sphinx_connection
  Thread.current[:thinking_sphinx_environment] = "tenant_#{Tenant::active.id}"
  ThinkingSphinx::Configuration.instance.reset
end

And here's how we're trying to do it now:

def set_sphinx_connection
  framework = ThinkingSphinx::Frameworks::Plain.new
  framework.environment = "tenant_#{Tenant::active.id}"
  ThinkingSphinx::Configuration.instance.framework = framework
end

Each tenant has its own sphinx config file, and each is searching on its own port. An excerpt from our thinking_sphinx.yml is below:

tenant_1:
  mysql41: 9312
  enable_star: true
  min_infix_len: 1
tenant_2:
  mysql41: 9313
  enable_star: true
  min_infix_len: 1
tenant_3:
  mysql41: 9314
  enable_star: true
  min_infix_len: 1

If you only ever search on one tenant, it works just fine. However, when you search on one and then another, one of two errors are occurring:

  • stale ID errors on the second tenant
  • the search returns in counts from the first tenant to the second tenant

Does anyone have a way of getting this working? Pat if you're out there can you help?

UPDATE: We think it has to do with Passenger. When the first instance starts up, it works for that tenant consistently. However, from then on that passenger instance cannot search any other tenant.


Solution

  • Putting this here in case someone else runs into this same issue. This was what ended up working.

    def set_sphinx_connection
      framework = ThinkingSphinx::Frameworks::Plain.new
      framework.environment = "tenant_#{Tenant::active.id}"
      ThinkingSphinx::Configuration.instance.framework = framework
      ThinkingSphinx::Connection.pool.clear
    end