Skip to content

Commit

Permalink
Refactored indices aliases api to make use of the new recently introd…
Browse files Browse the repository at this point in the history
…uced generic ack mechanism

Closes elastic#4114
  • Loading branch information
javanna committed Nov 14, 2013
1 parent ae27403 commit a0f3b09
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 354 deletions.
@@ -0,0 +1,49 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

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

import org.elasticsearch.cluster.ack.ClusterStateUpdateRequest;
import org.elasticsearch.cluster.metadata.AliasAction;

/**
* Cluster state update request that allows to add or remove aliases
*/
public class IndicesAliasesClusterStateUpdateRequest extends ClusterStateUpdateRequest<IndicesAliasesClusterStateUpdateRequest> {

AliasAction[] actions;

IndicesAliasesClusterStateUpdateRequest() {

}

/**
* Returns the alias actions to be performed
*/
public AliasAction[] actions() {
return actions;
}

/**
* Sets the alias actions to be executed
*/
public IndicesAliasesClusterStateUpdateRequest actions(AliasAction[] actions) {
this.actions = actions;
return this;
}
}
Expand Up @@ -22,12 +22,11 @@
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand All @@ -41,17 +40,14 @@

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.cluster.metadata.AliasAction.readAliasAction;
import static org.elasticsearch.common.unit.TimeValue.readTimeValue;

/**
* A request to add/remove aliases for one or more indices.
*/
public class IndicesAliasesRequest extends MasterNodeOperationRequest<IndicesAliasesRequest> {
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> {

private List<AliasAction> aliasActions = Lists.newArrayList();

private TimeValue timeout = TimeValue.timeValueSeconds(10);

public IndicesAliasesRequest() {

}
Expand Down Expand Up @@ -147,31 +143,6 @@ public List<AliasAction> getAliasActions() {
return aliasActions();
}

/**
* Timeout to wait till the put mapping gets acknowledged of all current cluster nodes. Defaults to
* <tt>10s</tt>.
*/
TimeValue timeout() {
return timeout;
}

/**
* Timeout to wait till the alias operations get acknowledged of all current cluster nodes. Defaults to
* <tt>10s</tt>.
*/
public IndicesAliasesRequest timeout(TimeValue timeout) {
this.timeout = timeout;
return this;
}

/**
* Timeout to wait till the alias operations get acknowledged of all current cluster nodes. Defaults to
* <tt>10s</tt>.
*/
public IndicesAliasesRequest timeout(String timeout) {
return timeout(TimeValue.parseTimeValue(timeout, TimeValue.timeValueSeconds(10)));
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
Expand All @@ -196,7 +167,7 @@ public void readFrom(StreamInput in) throws IOException {
for (int i = 0; i < size; i++) {
aliasActions.add(readAliasAction(in));
}
timeout = readTimeValue(in);
readTimeout(in, null);
}

@Override
Expand All @@ -206,6 +177,6 @@ public void writeTo(StreamOutput out) throws IOException {
for (AliasAction aliasAction : aliasActions) {
aliasAction.writeTo(out);
}
timeout.writeTo(out);
writeTimeout(out, null);
}
}
Expand Up @@ -20,19 +20,18 @@
package org.elasticsearch.action.admin.indices.alias;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.internal.InternalIndicesAdminClient;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.FilterBuilder;

import java.util.Map;

/**
*
*/
public class IndicesAliasesRequestBuilder extends MasterNodeOperationRequestBuilder<IndicesAliasesRequest, IndicesAliasesResponse, IndicesAliasesRequestBuilder> {
public class IndicesAliasesRequestBuilder extends AcknowledgedRequestBuilder<IndicesAliasesRequest, IndicesAliasesResponse, IndicesAliasesRequestBuilder> {

public IndicesAliasesRequestBuilder(IndicesAdminClient indicesClient) {
super((InternalIndicesAdminClient) indicesClient, new IndicesAliasesRequest());
Expand Down Expand Up @@ -106,16 +105,6 @@ public IndicesAliasesRequestBuilder removeAlias(String index, String alias) {
return this;
}

/**
* Sets operation timeout.
*
* @param timeout
*/
public IndicesAliasesRequestBuilder setTimeout(TimeValue timeout) {
request.timeout(timeout);
return this;
}

@Override
protected void doExecute(ActionListener<IndicesAliasesResponse> listener) {
((IndicesAdminClient) client).aliases(request, listener);
Expand Down
Expand Up @@ -19,7 +19,7 @@

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

import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

Expand All @@ -28,31 +28,25 @@
/**
* A response for a add/remove alias action.
*/
public class IndicesAliasesResponse extends ActionResponse {

private boolean acknowledged;
public class IndicesAliasesResponse extends AcknowledgedResponse {

IndicesAliasesResponse() {

}

IndicesAliasesResponse(boolean acknowledged) {
this.acknowledged = acknowledged;
}

public boolean isAcknowledged() {
return acknowledged;
super(acknowledged);
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
acknowledged = in.readBoolean();
readAcknowledged(in, null);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(acknowledged);
writeAcknowledged(out, null);
}
}
Expand Up @@ -25,6 +25,8 @@
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateListener;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.AliasAction;
Expand All @@ -37,7 +39,7 @@
import java.util.Set;

/**
*
* Add/remove aliases action
*/
public class TransportIndicesAliasesAction extends TransportMasterNodeOperationAction<IndicesAliasesRequest, IndicesAliasesResponse> {

Expand Down Expand Up @@ -82,10 +84,15 @@ protected ClusterBlockException checkBlock(IndicesAliasesRequest request, Cluste

@Override
protected void masterOperation(final IndicesAliasesRequest request, final ClusterState state, final ActionListener<IndicesAliasesResponse> listener) throws ElasticSearchException {
indexAliasesService.indicesAliases(new MetaDataIndexAliasesService.Request(request.aliasActions().toArray(new AliasAction[request.aliasActions().size()]), request.timeout()).masterTimeout(request.masterNodeTimeout()), new MetaDataIndexAliasesService.Listener() {

IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest()
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.actions(request.aliasActions().toArray(new AliasAction[request.aliasActions().size()]));

indexAliasesService.indicesAliases(updateRequest, new ClusterStateUpdateListener() {
@Override
public void onResponse(MetaDataIndexAliasesService.Response response) {
listener.onResponse(new IndicesAliasesResponse(response.acknowledged()));
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(new IndicesAliasesResponse(response.isAcknowledged()));
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/elasticsearch/cluster/ClusterModule.java
Expand Up @@ -76,7 +76,6 @@ protected void configure() {
bind(NodeMappingCreatedAction.class).asEagerSingleton();
bind(NodeMappingRefreshAction.class).asEagerSingleton();
bind(MappingUpdatedAction.class).asEagerSingleton();
bind(NodeAliasesUpdatedAction.class).asEagerSingleton();
bind(NodeIndicesStateUpdatedAction.class).asEagerSingleton();

bind(ClusterInfoService.class).to(InternalClusterInfoService.class).asEagerSingleton();
Expand Down

0 comments on commit a0f3b09

Please sign in to comment.