Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cluster Service Unavailable #181

Closed
khoan opened this issue Jun 17, 2015 · 4 comments
Closed

Test cluster Service Unavailable #181

khoan opened this issue Jun 17, 2015 · 4 comments

Comments

@khoan
Copy link

khoan commented Jun 17, 2015

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}
...
@karmi
Copy link
Contributor

karmi commented Jun 17, 2015

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

@khoan
Copy link
Author

khoan commented Jun 17, 2015

@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.

@karmi
Copy link
Contributor

karmi commented Jun 17, 2015

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

@khoan
Copy link
Author

khoan commented Jun 17, 2015

that's a more reliable way of checking.

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

@khoan khoan closed this as completed Jun 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants