Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions lib/elastomer/client/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ def health( params = {} )
response.body
end

# Comprehensive state information of the whole cluster.
# Comprehensive state information of the whole cluster. For 1.x metric
# and index filtering, use the :metrics and :indices parameter keys.
#
# See http://www.elasticsearch.org/guide/reference/api/admin-cluster-state/
#
# params - Parameters Hash
#
# Returns the response as a Hash
def state( params = {} )
response = client.get '/_cluster/state', params.merge(:action => 'cluster.state')
response = client.get '/_cluster/state{/metrics}{/indices}', params.merge(:action => 'cluster.state')
response.body
end

Expand Down Expand Up @@ -168,11 +170,17 @@ def update_aliases( actions, params = {} )
#
# Returns the template definitions as a Hash
def templates
h = state(
:filter_blocks => true,
:filter_nodes => true,
:filter_routing_table => true
)
# ES 1.x supports state filtering via a path segment called metrics.
# ES 0.90 uses query parameters instead.
if client.semantic_version >= '1.0.0'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be inclined to do if client.path_segment_state_filtering? or something to tie that logic up in the client.

h = state(:metrics => 'metadata')
else
h = state(
:filter_blocks => true,
:filter_nodes => true,
:filter_routing_table => true,
)
end
h['metadata']['templates']
end

Expand All @@ -181,11 +189,17 @@ def templates
#
# Returns the indices definitions as a Hash
def indices
h = state(
:filter_blocks => true,
:filter_nodes => true,
:filter_routing_table => true
)
# ES 1.x supports state filtering via a path segment called metrics.
# ES 0.90 uses query parameters instead.
if client.semantic_version >= '1.0.0'
h = state(:metrics => 'metadata')
else
h = state(
:filter_blocks => true,
:filter_nodes => true,
:filter_routing_table => true,
)
end
h['metadata']['indices']
end

Expand All @@ -195,11 +209,17 @@ def indices
#
# Returns the nodes definitions as a Hash
def nodes
h = state(
:filter_blocks => true,
:filter_metadata => true,
:filter_routing_table => true
)
# ES 1.x supports state filtering via a path segment called metrics.
# ES 0.90 uses query parameters instead.
if client.semantic_version >= '1.0.0'
h = state(:metrics => 'nodes')
else
h = state(
:filter_blocks => true,
:filter_metadata => true,
:filter_routing_table => true,
)
end
h['nodes']
end

Expand Down
5 changes: 3 additions & 2 deletions lib/elastomer/client/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ def info( params = {} )
response.body
end

# Retrieve one or more (or all) of the cluster nodes statistics.
# Retrieve one or more (or all) of the cluster nodes statistics. For 1.x
# stats filtering, use the :stats parameter key.
# See http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-stats/
#
# params - Parameters Hash
#
# Returns the response as a Hash
def stats( params = {} )
response = client.get '/_nodes{/node_id}/stats', update_params(params, :action => 'nodes.stats')
response = client.get '/_nodes{/node_id}/stats{/stats}', update_params(params, :action => 'nodes.stats')
response.body
end

Expand Down
22 changes: 22 additions & 0 deletions test/client/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@
assert h.key?('cluster_name'), 'the cluster name is returned'
assert h.key?('master_node'), 'the master node is returned'
assert_instance_of Hash, h['nodes'], 'the node list is returned'
assert_instance_of Hash, h['metadata'], 'the metadata are returned'
end

if es_version_1_x?
it 'filters cluster state by metrics' do
h = @cluster.state(:metrics => 'nodes')
refute h.key('metadata'), 'expected only nodes state'
h = @cluster.state(:metrics => 'metadata')
refute h.key('nodes'), 'expected only metadata state'
end

it 'filters cluster state by indices' do
@index.create({}) unless @index.exists?
h = @cluster.state(:metrics => 'metadata', :indices => @name)
assert [@name], h['metadata']['indices'].keys
end
end

it 'gets the cluster settings' do
Expand Down Expand Up @@ -55,6 +71,12 @@
assert_equal "60", value
end

it 'returns the list of indices in the cluster' do
@index.create({}) unless @index.exists?
indices = @cluster.indices
assert !indices.empty?, 'expected to see an index'
end

it 'returns the list of nodes in the cluster' do
nodes = @cluster.nodes
assert !nodes.empty?, 'we have to have some nodes'
Expand Down
1 change: 1 addition & 0 deletions test/client/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
}
}
)
wait_for_index(@name)

tokens = @index.analyze 'Just a few words to analyze.', :analyzer => 'english_standard'
tokens = tokens['tokens'].map { |h| h['token'] }
Expand Down
13 changes: 11 additions & 2 deletions test/client/nodes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Elastomer::Client::Nodes do

it 'gets info for the ndoe(s)' do
it 'gets info for the node(s)' do
h = $client.nodes.info
assert h.key?('cluster_name'), 'the cluster name is returned'
assert_instance_of Hash, h['nodes'], 'the node list is returned'
Expand All @@ -16,6 +16,15 @@
assert node.key?('indices'), 'indices stats are returned'
end

if es_version_1_x?
it 'filters node stats' do
h = $client.nodes.stats(:stats => 'http')
node = h['nodes'].values.first
assert node.key?('http'), 'expected http stats to be present'
assert !node.key?('indices'), 'expected indices stats to be absent'
end
end

it 'gets the hot threads for the node(s)' do
str = $client.nodes.hot_threads :read_timeout => 2
assert_instance_of String, str
Expand All @@ -27,7 +36,7 @@
assert_empty h['nodes']
end

it 'can be scoped to a multiple nodes' do
it 'can be scoped to multiple nodes' do
h = $client.nodes(%w[node1 node2 node3]).info
assert_empty h['nodes']
end
Expand Down
6 changes: 6 additions & 0 deletions test/client/template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
@template.delete if @template.exists?
end

it 'lists templates in the cluster' do
@template.create({:template => 'test-elastomer*'})
templates = $client.cluster.templates
assert !templates.empty?, "expected to see a template"
end

it 'creates a template' do
assert !@template.exists?, 'the template should not exist'

Expand Down
3 changes: 3 additions & 0 deletions test/middleware/opaque_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
]
}

stub.get('/') { |env|
[ 200, {}, {'version' => {'number' => '1.0.0'}} ]
}
stub.get('/_cluster/state') { |env|
[ 200, {'X-Opaque-Id' => "00000000-0000-0000-0000-000000000000"}, %q[{"foo":"bar"}] ]
}
Expand Down