Skip to content

Commit 8f32467

Browse files
committed
Index Aliases, closes #88.
1 parent 4851ddd commit 8f32467

File tree

53 files changed

+1128
-130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1128
-130
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/Actions.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,11 @@
1919

2020
package org.elasticsearch.action;
2121

22-
import org.elasticsearch.cluster.ClusterState;
23-
2422
/**
2523
* @author kimchy (shay.banon)
2624
*/
2725
public class Actions {
2826

29-
public static String[] processIndices(ClusterState state, String[] indices) {
30-
if (indices == null || indices.length == 0) {
31-
return state.routingTable().indicesRouting().keySet().toArray(new String[state.routingTable().indicesRouting().keySet().size()]);
32-
}
33-
if (indices.length == 1) {
34-
if (indices[0].length() == 0) {
35-
return state.routingTable().indicesRouting().keySet().toArray(new String[state.routingTable().indicesRouting().keySet().size()]);
36-
}
37-
if (indices[0].equals("_all")) {
38-
return state.routingTable().indicesRouting().keySet().toArray(new String[state.routingTable().indicesRouting().keySet().size()]);
39-
}
40-
}
41-
return indices;
42-
}
43-
4427
public static ActionRequestValidationException addValidationError(String error, ActionRequestValidationException validationException) {
4528
if (validationException == null) {
4629
validationException = new ActionRequestValidationException();

modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.action.admin.cluster.ping.replication.TransportShardReplicationPingAction;
3030
import org.elasticsearch.action.admin.cluster.ping.single.TransportSinglePingAction;
3131
import org.elasticsearch.action.admin.cluster.state.TransportClusterStateAction;
32+
import org.elasticsearch.action.admin.indices.alias.TransportIndicesAliasesAction;
3233
import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction;
3334
import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction;
3435
import org.elasticsearch.action.admin.indices.flush.TransportFlushAction;
@@ -74,6 +75,7 @@ public class TransportActionModule extends AbstractModule {
7475
bind(TransportCreateIndexAction.class).asEagerSingleton();
7576
bind(TransportPutMappingAction.class).asEagerSingleton();
7677
bind(TransportDeleteIndexAction.class).asEagerSingleton();
78+
bind(TransportIndicesAliasesAction.class).asEagerSingleton();
7779

7880
bind(TransportShardGatewaySnapshotAction.class).asEagerSingleton();
7981
bind(TransportIndexGatewaySnapshotAction.class).asEagerSingleton();

modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static class Indices {
5151
public static final String REFRESH = "indices/refresh";
5252
public static final String OPTIMIZE = "indices/optimize";
5353
public static final String STATUS = "indices/status";
54+
public static final String ALIASES = "indices/aliases";
5455

5556
public static class Gateway {
5657
public static final String SNAPSHOT = "indices/gateway/snapshot";

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public String[] indices() {
5555
return indices;
5656
}
5757

58+
void indices(String[] indices) {
59+
this.indices = indices;
60+
}
61+
5862
public TimeValue timeout() {
5963
return timeout;
6064
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/health/TransportClusterHealthAction.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.elasticsearch.transport.TransportService;
3737
import org.elasticsearch.util.settings.Settings;
3838

39-
import static org.elasticsearch.action.Actions.*;
40-
4139
/**
4240
* @author kimchy (shay.banon)
4341
*/
@@ -110,8 +108,9 @@ private ClusterHealthResponse clusterHealth(ClusterHealthRequest request) {
110108
RoutingTableValidation validation = clusterState.routingTable().validate(clusterState.metaData());
111109
ClusterHealthResponse response = new ClusterHealthResponse(clusterName.value(), validation.failures());
112110

113-
String[] indices = processIndices(clusterState, request.indices());
114-
for (String index : indices) {
111+
request.indices(clusterState.metaData().concreteIndices(request.indices()));
112+
113+
for (String index : request.indices()) {
115114
IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(index);
116115
IndexMetaData indexMetaData = clusterState.metaData().index(index);
117116
if (indexRoutingTable == null) {

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/ping/broadcast/TransportBroadcastPingAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.concurrent.atomic.AtomicReferenceArray;
4040

4141
import static com.google.common.collect.Lists.*;
42-
import static org.elasticsearch.action.Actions.*;
4342

4443
/**
4544
* @author kimchy (Shay Banon)
@@ -63,7 +62,7 @@ public class TransportBroadcastPingAction extends TransportBroadcastOperationAct
6362
}
6463

6564
@Override protected GroupShardsIterator shards(BroadcastPingRequest request, ClusterState clusterState) {
66-
return indicesService.searchShards(clusterState, processIndices(clusterState, request.indices()), request.queryHint());
65+
return indicesService.searchShards(clusterState, request.indices(), request.queryHint());
6766
}
6867

6968
@Override protected BroadcastPingResponse newResponse(BroadcastPingRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) {

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/ping/replication/TransportShardReplicationPingAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.inject.Inject;
2323
import org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction;
2424
import org.elasticsearch.cluster.ClusterService;
25+
import org.elasticsearch.cluster.ClusterState;
2526
import org.elasticsearch.cluster.action.shard.ShardStateAction;
2627
import org.elasticsearch.cluster.routing.ShardsIterator;
2728
import org.elasticsearch.indices.IndicesService;
@@ -59,7 +60,7 @@ public class TransportShardReplicationPingAction extends TransportShardReplicati
5960
@Override protected void shardOperationOnBackup(ShardOperationRequest shardRequest) {
6061
}
6162

62-
@Override protected ShardsIterator shards(ShardReplicationPingRequest request) {
63+
@Override protected ShardsIterator shards(ClusterState clusterState, ShardReplicationPingRequest request) {
6364
return clusterService.state().routingTable().index(request.index()).shard(request.shardId()).shardsIt();
6465
}
6566
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.indices.alias;
21+
22+
import com.google.common.collect.Lists;
23+
import org.elasticsearch.action.ActionRequestValidationException;
24+
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
25+
import org.elasticsearch.cluster.metadata.AliasAction;
26+
import org.elasticsearch.util.io.stream.StreamInput;
27+
import org.elasticsearch.util.io.stream.StreamOutput;
28+
29+
import java.io.IOException;
30+
import java.util.List;
31+
32+
import static org.elasticsearch.action.Actions.*;
33+
import static org.elasticsearch.cluster.metadata.AliasAction.*;
34+
35+
/**
36+
* A request to add/remove aliases for one or more indices.
37+
*
38+
* @author kimchy (shay.banon)
39+
*/
40+
public class IndicesAliasesRequest extends MasterNodeOperationRequest {
41+
42+
private List<AliasAction> aliasActions = Lists.newArrayList();
43+
44+
public IndicesAliasesRequest() {
45+
46+
}
47+
48+
/**
49+
* Adds an alias to the index.
50+
*
51+
* @param index The index
52+
* @param alias The alias
53+
*/
54+
public IndicesAliasesRequest addAlias(String index, String alias) {
55+
aliasActions.add(new AliasAction(AliasAction.Type.ADD, index, alias));
56+
return this;
57+
}
58+
59+
/**
60+
* Adds an alias to the index.
61+
*
62+
* @param index The index
63+
* @param alias The alias
64+
*/
65+
public IndicesAliasesRequest removeAlias(String index, String alias) {
66+
aliasActions.add(new AliasAction(AliasAction.Type.REMOVE, index, alias));
67+
return this;
68+
}
69+
70+
List<AliasAction> aliasActions() {
71+
return this.aliasActions;
72+
}
73+
74+
@Override public ActionRequestValidationException validate() {
75+
ActionRequestValidationException validationException = null;
76+
if (aliasActions.isEmpty()) {
77+
validationException = addValidationError("Must specify at least one alias action", validationException);
78+
}
79+
return validationException;
80+
}
81+
82+
@Override public void readFrom(StreamInput in) throws IOException {
83+
super.readFrom(in);
84+
int size = in.readVInt();
85+
for (int i = 0; i < size; i++) {
86+
aliasActions.add(readAliasAction(in));
87+
}
88+
}
89+
90+
@Override public void writeTo(StreamOutput out) throws IOException {
91+
super.writeTo(out);
92+
out.writeVInt(aliasActions.size());
93+
for (AliasAction aliasAction : aliasActions) {
94+
aliasAction.writeTo(out);
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.indices.alias;
21+
22+
import org.elasticsearch.action.ActionResponse;
23+
import org.elasticsearch.util.io.stream.StreamInput;
24+
import org.elasticsearch.util.io.stream.StreamOutput;
25+
import org.elasticsearch.util.io.stream.Streamable;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* A response for a create index action.
31+
*
32+
* @author kimchy (shay.banon)
33+
*/
34+
public class IndicesAliasesResponse implements ActionResponse, Streamable {
35+
36+
IndicesAliasesResponse() {
37+
}
38+
39+
@Override public void readFrom(StreamInput in) throws IOException {
40+
}
41+
42+
@Override public void writeTo(StreamOutput out) throws IOException {
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.indices.alias;
21+
22+
import com.google.inject.Inject;
23+
import org.elasticsearch.ElasticSearchException;
24+
import org.elasticsearch.action.TransportActions;
25+
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
26+
import org.elasticsearch.cluster.ClusterService;
27+
import org.elasticsearch.cluster.metadata.MetaDataService;
28+
import org.elasticsearch.threadpool.ThreadPool;
29+
import org.elasticsearch.transport.TransportService;
30+
import org.elasticsearch.util.settings.Settings;
31+
32+
/**
33+
* @author kimchy (shay.banon)
34+
*/
35+
public class TransportIndicesAliasesAction extends TransportMasterNodeOperationAction<IndicesAliasesRequest, IndicesAliasesResponse> {
36+
37+
private final MetaDataService metaDataService;
38+
39+
@Inject public TransportIndicesAliasesAction(Settings settings, TransportService transportService, ClusterService clusterService,
40+
ThreadPool threadPool, MetaDataService metaDataService) {
41+
super(settings, transportService, clusterService, threadPool);
42+
this.metaDataService = metaDataService;
43+
}
44+
45+
@Override protected String transportAction() {
46+
return TransportActions.Admin.Indices.ALIASES;
47+
}
48+
49+
@Override protected IndicesAliasesRequest newRequest() {
50+
return new IndicesAliasesRequest();
51+
}
52+
53+
@Override protected IndicesAliasesResponse newResponse() {
54+
return new IndicesAliasesResponse();
55+
}
56+
57+
@Override protected IndicesAliasesResponse masterOperation(IndicesAliasesRequest request) throws ElasticSearchException {
58+
MetaDataService.IndicesAliasesResult indicesAliasesResult = metaDataService.indicesAliases(request.aliasActions());
59+
return new IndicesAliasesResponse();
60+
}
61+
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.action.admin.indices.create;
2121

22-
import com.google.common.collect.Maps;
2322
import org.elasticsearch.ElasticSearchIllegalArgumentException;
2423
import org.elasticsearch.action.ActionRequestValidationException;
2524
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
@@ -33,6 +32,7 @@
3332
import java.util.Map;
3433
import java.util.concurrent.TimeUnit;
3534

35+
import static com.google.common.collect.Maps.*;
3636
import static org.elasticsearch.action.Actions.*;
3737
import static org.elasticsearch.util.TimeValue.*;
3838
import static org.elasticsearch.util.settings.ImmutableSettings.Builder.*;
@@ -54,7 +54,7 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
5454

5555
private Settings settings = EMPTY_SETTINGS;
5656

57-
private Map<String, String> mappings = Maps.newHashMap();
57+
private Map<String, String> mappings = newHashMap();
5858

5959
private TimeValue timeout = new TimeValue(10, TimeUnit.SECONDS);
6060

@@ -106,6 +106,14 @@ public CreateIndexRequest settings(Settings settings) {
106106
return this;
107107
}
108108

109+
/**
110+
* The settings to created the index with.
111+
*/
112+
public CreateIndexRequest settings(Settings.Builder settings) {
113+
this.settings = settings.build();
114+
return this;
115+
}
116+
109117
/**
110118
* Adds mapping that will be added when the index gets created.
111119
*

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/gateway/snapshot/TransportShardGatewaySnapshotAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.inject.Inject;
2323
import org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction;
2424
import org.elasticsearch.cluster.ClusterService;
25+
import org.elasticsearch.cluster.ClusterState;
2526
import org.elasticsearch.cluster.action.shard.ShardStateAction;
2627
import org.elasticsearch.cluster.routing.ShardsIterator;
2728
import org.elasticsearch.index.gateway.IndexShardGatewayService;
@@ -64,7 +65,7 @@ public class TransportShardGatewaySnapshotAction extends TransportShardReplicati
6465
// silently ignore, we disable it with #ignoreBackups anyhow
6566
}
6667

67-
@Override protected ShardsIterator shards(ShardGatewaySnapshotRequest request) {
68+
@Override protected ShardsIterator shards(ClusterState clusterState, ShardGatewaySnapshotRequest request) {
6869
return clusterService.state().routingTable().index(request.index()).shard(request.shardId()).shardsIt();
6970
}
7071

0 commit comments

Comments
 (0)