Skip to content

Test cluster Service Unavailable #181

Closed

Description

@khoan

Other than sleep, is there a way to wait until index is ready?

when testing Elasticsearch in rspec, cannot query index after it's created.

# spec/bam.rb
require 'rake'
require 'elasticsearch/extensions/test/cluster/tasks'
require 'elasticsearch/persistence/model'

class Bam
  include Elasticsearch::Persistence::Model
end

RSpec.describe 'Bam' do

  it 'cannot use elasticsearch endpoint after created' do
    Elasticsearch::Persistence.client = Elasticsearch::Client.new(host: 'localhost:9250', log: true)
    Elasticsearch::Extensions::Test::Cluster.start(nodes: 1)

    Bam.create_index!(force: true)

    # neither of the following work
    # Bam.refresh_index!
    # Elasticsearch::Persistence.client.indices.refresh

    # workaround 1 - wait for index to come online
    # sleep(0.1)

    # workaround 2 - index a document and wait for it to refresh
    # bam = Bam.create(id: 'bam')
    # Bam.refresh_index!
    # bam.delete

    Bam.count

    Elasticsearch::Extensions::Test::Cluster.stop
  end

end
$ bundle exec rspec spec/bam.rb
...
2015-06-17 10:34:46 +1000: PUT http://localhost:9250/bams [status:200, request:0.678s, query:n/a]
2015-06-17 10:34:46 +1000: > {"settings":{},"mappings":{"bam":{"properties":{"created_at":{"type":"date"},"updated_at":{"type":"date"}}}}}
2015-06-17 10:34:46 +1000: < {"acknowledged":true}
2015-06-17 10:34:46 +1000: GET http://localhost:9250/bams/bam/_search?search_type=count [status:503, request:0.016s, query:N/A]
2015-06-17 10:34:46 +1000: > {"query":{"match_all":{}}}
2015-06-17 10:34:46 +1000: < {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed]","status":503}
2015-06-17 10:34:46 +1000: [503] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed]","status":503}
...

Activity

  1. karmi commented on Jun 17, 2015

    @karmi
    Contributor

    Sure, just use refresh_index! (or client.indices.refresh of course)

  2. khoan commented on Jun 17, 2015

    @khoan
    Author

    @karmi thanks for the suggestion. Neither of Bam.refresh_index! nor Elasticsearch::Persistence.client.indices.refresh works. But sleeping for a bit seems to work.

  3. karmi commented on Jun 17, 2015

    @karmi
    Contributor

    On a really slow system, you might want to wait for cluster health, see eg. until client.cluster.health(level: 'indices')['indices']['bicycles.stackexchange.com']['status'] == 'green' in https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl#features-overview

  4. khoan commented on Jun 17, 2015

    @khoan
    Author

    that's a more reliable way of checking.

    I guess other way is to index a document, call refresh_index!, then delete document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions