add cause to index creation

This commit is contained in:
kimchy 2010-04-01 22:42:51 +03:00
parent 118aa89614
commit 4b2ff13833
5 changed files with 29 additions and 6 deletions

View File

@ -50,6 +50,8 @@ import static org.elasticsearch.util.settings.ImmutableSettings.*;
*/
public class CreateIndexRequest extends MasterNodeOperationRequest {
private String cause = "";
private String index;
private Settings settings = EMPTY_SETTINGS;
@ -98,6 +100,13 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
return settings;
}
/**
* The cause for this index creation.
*/
String cause() {
return cause;
}
/**
* The settings to created the index with.
*/
@ -125,6 +134,14 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
return this;
}
/**
* The cause for this index creation.
*/
public CreateIndexRequest cause(String cause) {
this.cause = cause;
return this;
}
/**
* Adds mapping that will be added when the index gets created.
*
@ -162,6 +179,7 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
}
@Override public void readFrom(StreamInput in) throws IOException {
cause = in.readUTF();
index = in.readUTF();
settings = readSettingsFromStream(in);
timeout = readTimeValue(in);
@ -172,6 +190,7 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(cause);
out.writeUTF(index);
writeSettingsToStream(settings, out);
timeout.writeTo(out);

View File

@ -57,7 +57,11 @@ public class TransportCreateIndexAction extends TransportMasterNodeOperationActi
}
@Override protected CreateIndexResponse masterOperation(CreateIndexRequest request) throws ElasticSearchException {
MetaDataService.CreateIndexResult createIndexResult = metaDataService.createIndex(request.index(), request.settings(), request.mappings(), request.timeout());
String cause = request.cause();
if (cause.length() == 0) {
cause = "api";
}
MetaDataService.CreateIndexResult createIndexResult = metaDataService.createIndex(cause, request.index(), request.settings(), request.mappings(), request.timeout());
return new CreateIndexResponse(createIndexResult.acknowledged());
}
}

View File

@ -82,7 +82,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi
}
}
if (autoCreateIndex && !clusterService.state().metaData().hasConcreteIndex(indexRequest.index())) {
createIndexAction.execute(new CreateIndexRequest(indexRequest.index()), new ActionListener<CreateIndexResponse>() {
createIndexAction.execute(new CreateIndexRequest(indexRequest.index()).cause("auto(index api)"), new ActionListener<CreateIndexResponse>() {
@Override public void onResponse(CreateIndexResponse result) {
TransportIndexAction.super.doExecute(indexRequest, listener);
}

View File

@ -138,7 +138,7 @@ public class MetaDataService extends AbstractComponent {
return new IndicesAliasesResult();
}
public synchronized CreateIndexResult createIndex(final String index, final Settings indexSettings, Map<String, String> mappings, TimeValue timeout) throws IndexAlreadyExistsException {
public synchronized CreateIndexResult createIndex(final String cause, final String index, final Settings indexSettings, Map<String, String> mappings, TimeValue timeout) throws IndexAlreadyExistsException {
ClusterState clusterState = clusterService.state();
if (clusterState.routingTable().hasIndex(index)) {
@ -202,7 +202,7 @@ public class MetaDataService extends AbstractComponent {
}
};
nodeIndexCreatedAction.add(nodeCreatedListener);
clusterService.submitStateUpdateTask("create-index [" + index + "]", new ClusterStateUpdateTask() {
clusterService.submitStateUpdateTask("create-index [" + index + "], cause [" + cause + "]", new ClusterStateUpdateTask() {
@Override public ClusterState execute(ClusterState currentState) {
RoutingTable.Builder routingTableBuilder = new RoutingTable.Builder();
for (IndexRoutingTable indexRoutingTable : currentState.routingTable().indicesRouting().values()) {
@ -230,7 +230,7 @@ public class MetaDataService extends AbstractComponent {
.initializeEmpty(newMetaData.index(index));
routingTableBuilder.add(indexRoutingBuilder);
logger.info("Creating Index [{}], shards [{}]/[{}], mappings {}", new Object[]{index, indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas(), fMappings.keySet()});
logger.info("Creating Index [{}], cause [{}], shards [{}]/[{}], mappings {}", new Object[]{index, cause, indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas(), fMappings.keySet()});
RoutingTable newRoutingTable = shardsRoutingStrategy.reroute(newClusterStateBuilder().state(currentState).routingTable(routingTableBuilder).metaData(newMetaData).build());
return newClusterStateBuilder().state(currentState).routingTable(newRoutingTable).metaData(newMetaData).build();
}

View File

@ -140,7 +140,7 @@ public class GatewayService extends AbstractLifecycleComponent<GatewayService> i
threadPool.execute(new Runnable() {
@Override public void run() {
try {
metaDataService.createIndex(indexMetaData.index(), indexMetaData.settings(), indexMetaData.mappings(), timeValueMillis(10));
metaDataService.createIndex("gateway", indexMetaData.index(), indexMetaData.settings(), indexMetaData.mappings(), timeValueMillis(10));
} catch (Exception e) {
logger.error("Failed to create index [" + indexMetaData.index() + "]", e);
}