Skip to content

Commit ca7a746

Browse files
committed
Indices API: Add open and close index, closes elastic#447.
1 parent a76824e commit ca7a746

32 files changed

+1623
-8
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
import org.elasticsearch.action.admin.cluster.state.TransportClusterStateAction;
3333
import org.elasticsearch.action.admin.indices.alias.TransportIndicesAliasesAction;
3434
import org.elasticsearch.action.admin.indices.cache.clear.TransportClearIndicesCacheAction;
35+
import org.elasticsearch.action.admin.indices.close.TransportCloseIndexAction;
3536
import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction;
3637
import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction;
3738
import org.elasticsearch.action.admin.indices.flush.TransportFlushAction;
3839
import org.elasticsearch.action.admin.indices.gateway.snapshot.TransportGatewaySnapshotAction;
3940
import org.elasticsearch.action.admin.indices.mapping.delete.TransportDeleteMappingAction;
4041
import org.elasticsearch.action.admin.indices.mapping.put.TransportPutMappingAction;
42+
import org.elasticsearch.action.admin.indices.open.TransportOpenIndexAction;
4143
import org.elasticsearch.action.admin.indices.optimize.TransportOptimizeAction;
4244
import org.elasticsearch.action.admin.indices.refresh.TransportRefreshAction;
4345
import org.elasticsearch.action.admin.indices.settings.TransportUpdateSettingsAction;
@@ -80,6 +82,8 @@ public class TransportActionModule extends AbstractModule {
8082
bind(TransportIndicesStatusAction.class).asEagerSingleton();
8183
bind(TransportCreateIndexAction.class).asEagerSingleton();
8284
bind(TransportDeleteIndexAction.class).asEagerSingleton();
85+
bind(TransportOpenIndexAction.class).asEagerSingleton();
86+
bind(TransportCloseIndexAction.class).asEagerSingleton();
8387
bind(TransportPutMappingAction.class).asEagerSingleton();
8488
bind(TransportDeleteMappingAction.class).asEagerSingleton();
8589
bind(TransportIndicesAliasesAction.class).asEagerSingleton();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public static class Admin {
4949
public static class Indices {
5050
public static final String CREATE = "indices/createIndex";
5151
public static final String DELETE = "indices/deleteIndex";
52+
public static final String OPEN = "indices/openIndex";
53+
public static final String CLOSE = "indices/closeIndex";
5254
public static final String FLUSH = "indices/flush";
5355
public static final String REFRESH = "indices/refresh";
5456
public static final String OPTIMIZE = "indices/optimize";
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.close;
21+
22+
import org.elasticsearch.action.ActionRequestValidationException;
23+
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
24+
import org.elasticsearch.common.io.stream.StreamInput;
25+
import org.elasticsearch.common.io.stream.StreamOutput;
26+
import org.elasticsearch.common.unit.TimeValue;
27+
28+
import java.io.IOException;
29+
30+
import static org.elasticsearch.action.Actions.*;
31+
import static org.elasticsearch.common.unit.TimeValue.*;
32+
33+
/**
34+
* A request to close an index.
35+
*
36+
* @author kimchy (shay.banon)
37+
*/
38+
public class CloseIndexRequest extends MasterNodeOperationRequest {
39+
40+
private String index;
41+
42+
private TimeValue timeout = timeValueSeconds(10);
43+
44+
CloseIndexRequest() {
45+
}
46+
47+
/**
48+
* Constructs a new delete index request for the specified index.
49+
*/
50+
public CloseIndexRequest(String index) {
51+
this.index = index;
52+
}
53+
54+
@Override public ActionRequestValidationException validate() {
55+
ActionRequestValidationException validationException = null;
56+
if (index == null) {
57+
validationException = addValidationError("index is missing", validationException);
58+
}
59+
return validationException;
60+
}
61+
62+
/**
63+
* The index to delete.
64+
*/
65+
String index() {
66+
return index;
67+
}
68+
69+
/**
70+
* Timeout to wait for the index deletion to be acknowledged by current cluster nodes. Defaults
71+
* to <tt>10s</tt>.
72+
*/
73+
TimeValue timeout() {
74+
return timeout;
75+
}
76+
77+
/**
78+
* Timeout to wait for the index deletion to be acknowledged by current cluster nodes. Defaults
79+
* to <tt>10s</tt>.
80+
*/
81+
public CloseIndexRequest timeout(TimeValue timeout) {
82+
this.timeout = timeout;
83+
return this;
84+
}
85+
86+
/**
87+
* Timeout to wait for the index deletion to be acknowledged by current cluster nodes. Defaults
88+
* to <tt>10s</tt>.
89+
*/
90+
public CloseIndexRequest timeout(String timeout) {
91+
return timeout(TimeValue.parseTimeValue(timeout, null));
92+
}
93+
94+
@Override public void readFrom(StreamInput in) throws IOException {
95+
super.readFrom(in);
96+
index = in.readUTF();
97+
timeout = readTimeValue(in);
98+
}
99+
100+
@Override public void writeTo(StreamOutput out) throws IOException {
101+
super.writeTo(out);
102+
out.writeUTF(index);
103+
timeout.writeTo(out);
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.close;
21+
22+
import org.elasticsearch.action.ActionResponse;
23+
import org.elasticsearch.common.io.stream.StreamInput;
24+
import org.elasticsearch.common.io.stream.StreamOutput;
25+
import org.elasticsearch.common.io.stream.Streamable;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* A response for a close index action.
31+
*
32+
* @author kimchy (shay.banon)
33+
*/
34+
public class CloseIndexResponse implements ActionResponse, Streamable {
35+
36+
private boolean acknowledged;
37+
38+
CloseIndexResponse() {
39+
}
40+
41+
CloseIndexResponse(boolean acknowledged) {
42+
this.acknowledged = acknowledged;
43+
}
44+
45+
public boolean acknowledged() {
46+
return acknowledged;
47+
}
48+
49+
public boolean getAcknowledged() {
50+
return acknowledged();
51+
}
52+
53+
@Override public void readFrom(StreamInput in) throws IOException {
54+
acknowledged = in.readBoolean();
55+
}
56+
57+
@Override public void writeTo(StreamOutput out) throws IOException {
58+
out.writeBoolean(acknowledged);
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.close;
21+
22+
import org.elasticsearch.ElasticSearchException;
23+
import org.elasticsearch.action.TransportActions;
24+
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
25+
import org.elasticsearch.cluster.ClusterService;
26+
import org.elasticsearch.cluster.ClusterState;
27+
import org.elasticsearch.cluster.block.ClusterBlockLevel;
28+
import org.elasticsearch.cluster.metadata.MetaDataStateIndexService;
29+
import org.elasticsearch.common.inject.Inject;
30+
import org.elasticsearch.common.settings.Settings;
31+
import org.elasticsearch.threadpool.ThreadPool;
32+
import org.elasticsearch.transport.TransportService;
33+
34+
import java.util.concurrent.CountDownLatch;
35+
import java.util.concurrent.atomic.AtomicReference;
36+
37+
/**
38+
* Delete index action.
39+
*
40+
* @author kimchy (shay.banon)
41+
*/
42+
public class TransportCloseIndexAction extends TransportMasterNodeOperationAction<CloseIndexRequest, CloseIndexResponse> {
43+
44+
private final MetaDataStateIndexService stateIndexService;
45+
46+
@Inject public TransportCloseIndexAction(Settings settings, TransportService transportService, ClusterService clusterService,
47+
ThreadPool threadPool, MetaDataStateIndexService stateIndexService) {
48+
super(settings, transportService, clusterService, threadPool);
49+
this.stateIndexService = stateIndexService;
50+
}
51+
52+
@Override protected String transportAction() {
53+
return TransportActions.Admin.Indices.CLOSE;
54+
}
55+
56+
@Override protected CloseIndexRequest newRequest() {
57+
return new CloseIndexRequest();
58+
}
59+
60+
@Override protected CloseIndexResponse newResponse() {
61+
return new CloseIndexResponse();
62+
}
63+
64+
@Override protected void checkBlock(CloseIndexRequest request, ClusterState state) {
65+
state.blocks().indexBlockedRaiseException(ClusterBlockLevel.METADATA, request.index());
66+
}
67+
68+
@Override protected CloseIndexResponse masterOperation(CloseIndexRequest request, ClusterState state) throws ElasticSearchException {
69+
final AtomicReference<CloseIndexResponse> responseRef = new AtomicReference<CloseIndexResponse>();
70+
final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
71+
final CountDownLatch latch = new CountDownLatch(1);
72+
stateIndexService.closeIndex(new MetaDataStateIndexService.Request(request.index()).timeout(request.timeout()), new MetaDataStateIndexService.Listener() {
73+
@Override public void onResponse(MetaDataStateIndexService.Response response) {
74+
responseRef.set(new CloseIndexResponse(response.acknowledged()));
75+
latch.countDown();
76+
}
77+
78+
@Override public void onFailure(Throwable t) {
79+
failureRef.set(t);
80+
latch.countDown();
81+
}
82+
});
83+
84+
try {
85+
latch.await();
86+
} catch (InterruptedException e) {
87+
failureRef.set(e);
88+
}
89+
90+
if (failureRef.get() != null) {
91+
if (failureRef.get() instanceof ElasticSearchException) {
92+
throw (ElasticSearchException) failureRef.get();
93+
} else {
94+
throw new ElasticSearchException(failureRef.get().getMessage(), failureRef.get());
95+
}
96+
}
97+
98+
return responseRef.get();
99+
}
100+
}

0 commit comments

Comments
 (0)