Skip to content

Commit

Permalink
Update API: Update through an alias with routing configured on it fai…
Browse files Browse the repository at this point in the history
…l to use the routing, closes elastic#2155.
  • Loading branch information
kimchy committed Aug 9, 2012
1 parent f8e60dd commit 48a923a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Expand Up @@ -85,6 +85,15 @@ protected void doExecute(Request request, ActionListener<Response> listener) {

protected abstract ClusterBlockException checkRequestBlock(ClusterState state, Request request);

/**
* Resolves the request, by default, simply setting the concrete index (if its aliased one). If the resolve
* means a different execution, then return false here to indicate not to continue and execute this request.
*/
protected boolean resolveRequest(ClusterState state, Request request, ActionListener<Response> listener) {
request.index(state.metaData().concreteIndex(request.index()));
return true;
}

protected boolean retryOnFailure(Throwable e) {
return false;
}
Expand Down Expand Up @@ -132,7 +141,10 @@ public boolean start(final boolean fromClusterEvent) throws ElasticSearchExcepti
throw blockException;
}
}
request.index(clusterState.metaData().concreteIndex(request.index()));
// check if we need to execute, and if not, return
if (!resolveRequest(clusterState, request, listener)) {
return true;
}
blockException = checkRequestBlock(clusterState, request);
if (blockException != null) {
if (blockException.retryable()) {
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.routing.PlainShardIterator;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.routing.ShardRouting;
Expand Down Expand Up @@ -127,6 +128,15 @@ protected boolean retryOnFailure(Throwable e) {
return false;
}

@Override
protected boolean resolveRequest(ClusterState state, UpdateRequest request, ActionListener<UpdateResponse> listener) {
MetaData metaData = clusterService.state().metaData();
String aliasOrIndex = request.index();
request.routing((metaData.resolveIndexRouting(request.routing(), aliasOrIndex)));
request.index(metaData.concreteIndex(request.index()));
return true;
}

@Override
protected ShardIterator shards(ClusterState clusterState, UpdateRequest request) throws ElasticSearchException {
if (request.shardId() != -1) {
Expand Down
Expand Up @@ -88,6 +88,17 @@ public void testAliasCrudRouting() throws Exception {
assertThat(client.prepareGet("alias0", "type1", "1").execute().actionGet().exists(), equalTo(true));
}

logger.info("--> updating with id [1] and routing through alias");
client.prepareUpdate("alias0", "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
.setScript("ctx._source.field = 'value2'")
.execute().actionGet();
for (int i = 0; i < 5; i++) {
assertThat(client.prepareGet("alias0", "type1", "1").execute().actionGet().exists(), equalTo(true));
assertThat(client.prepareGet("alias0", "type1", "1").execute().actionGet().sourceAsMap().get("field").toString(), equalTo("value2"));
}


logger.info("--> deleting with no routing, should not delete anything");
client.prepareDelete("test", "type1", "1").setRefresh(true).execute().actionGet();
for (int i = 0; i < 5; i++) {
Expand Down

0 comments on commit 48a923a

Please sign in to comment.