Skip to content

Commit

Permalink
Change behaviour of indices segments api to allow no indices
Browse files Browse the repository at this point in the history
Using '_cat/segments' or the indices segments api without matching any index
now returns empty result instead of throwing IndexMissingException.

Closes #9219
  • Loading branch information
Christoph Büscher committed Feb 12, 2015
1 parent de41981 commit 41befaf
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 7 deletions.
39 changes: 36 additions & 3 deletions rest-api-spec/test/cat.segments/10_basic.yaml
Expand Up @@ -24,6 +24,14 @@
---
"Test cat segments output":

- do:
cat.segments:
v: false

- match:
$body: |
/^$/
- do:
indices.create:
index: index1
Expand All @@ -39,7 +47,7 @@
refresh: true
- do:
cluster.health:
wait_for_status: yellow
wait_for_status: green
- do:
cat.segments:
v: false
Expand All @@ -57,7 +65,7 @@
number_of_replicas: "0"
- do:
cluster.health:
wait_for_status: yellow
wait_for_status: green
wait_for_relocating_shards: 0

- do:
Expand All @@ -68,7 +76,7 @@
refresh: true
- do:
cluster.health:
wait_for_status: yellow
wait_for_status: green


- do:
Expand All @@ -85,3 +93,28 @@
- match:
$body: |
/^(index2 .+ \n?)$/
---
"Test cat segments on closed index behaviour":

- do:
indices.create:
index: index1
body:
settings:
number_of_shards: "1"
number_of_replicas: "0"

- do:
cluster.health:
wait_for_status: green

- do:
indices.close:
index: index1

- do:
catch: forbidden
cat.segments:
index: index1
v: false
72 changes: 71 additions & 1 deletion rest-api-spec/test/indices.segments/10_basic.yaml
@@ -1,5 +1,75 @@
---
"segments test":
"no segments test":
- do:
indices.segments:
allow_no_indices: true

- match: { _shards.total: 0}
- match: { indices: {}}

- do:
catch: missing
indices.segments:
allow_no_indices: false

---
"basic segments test":

- do:
indices.create:
index: index1
body:
settings:
number_of_shards: "1"
number_of_replicas: "0"
- do:
index:
index: index1
type: type
body: { foo: bar }
refresh: true

- do:
cluster.health:
wait_for_status: green

- do:
indices.segments:
index: index1

- match: { _shards.total: 1}
- match: { indices.index1.shards.0.0.routing.primary: true}
- match: { indices.index1.shards.0.0.segments._0.num_docs: 1}

---
"closed segments test":

- do:
indices.create:
index: index1
body:
settings:
number_of_shards: "1"
number_of_replicas: "0"
- do:
index:
index: index1
type: type
body: { foo: bar }
refresh: true

- do:
indices.close:
index: index1

- do:
catch: forbidden
indices.segments:
index: index1

- do:
indices.segments:
index: index1
ignore_unavailable: true

- match: { _shards.total: 0}
Expand Up @@ -37,7 +37,6 @@ public IndicesSegmentsRequest() {

public IndicesSegmentsRequest(String... indices) {
super(indices);
indicesOptions(IndicesOptions.fromOptions(false, false, true, false));
}

/**
Expand Down
Expand Up @@ -19,11 +19,15 @@

package org.elasticsearch.action.admin.indices.segments;

import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.engine.Segment;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

Expand Down Expand Up @@ -56,4 +60,33 @@ public void testVerbose() {
List<Segment> segments = rsp.getIndices().get("test").iterator().next().getShards()[0].getSegments();
assertNotNull(segments.get(0).ramTree);
}

/**
* with the default IndicesOptions inherited from BroadcastOperationRequest this will raise an exception
*/
@Test(expected=org.elasticsearch.indices.IndexClosedException.class)
public void testRequestOnClosedIndex() {
client().admin().indices().prepareClose("test").get();
client().admin().indices().prepareSegments("test").get();
}

/**
* setting the "ignoreUnavailable" option prevents IndexClosedException
*/
public void testRequestOnClosedIndexIgnoreUnavailable() {
client().admin().indices().prepareClose("test").get();
IndicesOptions defaultOptions = new IndicesSegmentsRequest().indicesOptions();
IndicesOptions testOptions = IndicesOptions.fromOptions(true, true, true, false, defaultOptions);
IndicesSegmentResponse rsp = client().admin().indices().prepareSegments("test").setIndicesOptions(testOptions).get();
assertEquals(0, rsp.getIndices().size());
}

/**
* by default IndicesOptions setting IndicesSegmentsRequest should not throw exception when no index present
*/
public void testAllowNoIndex() {
client().admin().indices().prepareDelete("test").get();
IndicesSegmentResponse rsp = client().admin().indices().prepareSegments().get();
assertEquals(0, rsp.getIndices().size());
}
}
Expand Up @@ -362,7 +362,7 @@ public void testWildcardBehaviour() throws Exception {
verify(count(indices), false);
verify(clearCache(indices), false);
verify(_flush(indices),false);
verify(segments(indices), true);
verify(segments(indices), false);
verify(stats(indices), false);
verify(optimize(indices), false);
verify(refresh(indices), false);
Expand Down Expand Up @@ -437,7 +437,7 @@ public void testWildcardBehaviour() throws Exception {
verify(count(indices), false, 1);
verify(clearCache(indices), false);
verify(_flush(indices),false);
verify(segments(indices), true);
verify(segments(indices), false);
verify(stats(indices), false);
verify(optimize(indices), false);
verify(refresh(indices), false);
Expand Down

0 comments on commit 41befaf

Please sign in to comment.