org.elasticsearch.action.WriteConsistencyLevel Java Examples

The following examples show how to use org.elasticsearch.action.WriteConsistencyLevel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportReplicationAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
protected TransportReplicationAction(Settings settings, String actionName, TransportService transportService,
                                     ClusterService clusterService, IndicesService indicesService,
                                     ThreadPool threadPool, ShardStateAction shardStateAction,
                                     MappingUpdatedAction mappingUpdatedAction, ActionFilters actionFilters,
                                     IndexNameExpressionResolver indexNameExpressionResolver, Class<Request> request,
                                     Class<ReplicaRequest> replicaRequest, String executor) {
    super(settings, actionName, threadPool, actionFilters, indexNameExpressionResolver, transportService.getTaskManager());
    this.transportService = transportService;
    this.clusterService = clusterService;
    this.indicesService = indicesService;
    this.shardStateAction = shardStateAction;
    this.mappingUpdatedAction = mappingUpdatedAction;

    this.transportPrimaryAction = actionName + "[p]";
    this.transportReplicaAction = actionName + "[r]";
    this.executor = executor;
    this.checkWriteConsistency = checkWriteConsistency();
    transportService.registerRequestHandler(actionName, request, ThreadPool.Names.SAME, new OperationTransportHandler());
    transportService.registerRequestHandler(transportPrimaryAction, request, executor, new PrimaryOperationTransportHandler());
    // we must never reject on because of thread pool capacity on replicas
    transportService.registerRequestHandler(transportReplicaAction, replicaRequest, executor, true, new ReplicaOperationTransportHandler());

    this.transportOptions = transportOptions();

    this.defaultWriteConsistencyLevel = WriteConsistencyLevel.fromString(settings.get("action.write_consistency", "quorum"));
}
 
Example #2
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを登録する.
 * @param index インデックス名
 * @param type タイプ名
 * @param id ドキュメントのid
 * @param routingId routingId
 * @param data データ
 * @param opType 操作タイプ
 * @param version version番号
 * @return 非同期応答
 */
public ActionFuture<IndexResponse> asyncIndex(String index,
        String type,
        String id,
        String routingId,
        Map<String, Object> data,
        OpType opType,
        long version) {
    IndexRequestBuilder req = esTransportClient.prepareIndex(index, type, id).setSource(data).setOpType(opType)
            .setConsistencyLevel(WriteConsistencyLevel.DEFAULT).setRefresh(true);
    if (routingFlag) {
        req = req.setRouting(routingId);
    }
    if (version > -1) {
        req.setVersion(version);
    }

    ActionFuture<IndexResponse> ret = req.execute();
    EsRequestLogInfo logInfo = new EsRequestLogInfo(index, type, id, routingId, data, opType.toString(),
            version);
    this.fireEvent(Event.afterCreate, logInfo);

    return ret;
}
 
Example #3
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを登録する.
 * @param index インデックス名
 * @param type タイプ名
 * @param id ドキュメントのid
 * @param routingId routingId
 * @param data データ
 * @param opType 操作タイプ
 * @param version version番号
 * @return 非同期応答
 */
public ActionFuture<IndexResponse> asyncIndex(String index,
        String type,
        String id,
        String routingId,
        Map<String, Object> data,
        OpType opType,
        long version) {
    IndexRequestBuilder req = esTransportClient.prepareIndex(index, type, id).setSource(data).setOpType(opType)
            .setConsistencyLevel(WriteConsistencyLevel.DEFAULT).setRefresh(true);
    if (routingFlag) {
        req = req.setRouting(routingId);
    }
    if (version > -1) {
        req.setVersion(version);
    }

    ActionFuture<IndexResponse> ret = req.execute();
    EsRequestLogInfo logInfo = new EsRequestLogInfo(index, type, id, routingId, data, opType.toString(),
            version);
    this.fireEvent(Event.afterCreate, logInfo);

    return ret;
}
 
Example #4
Source File: RestDeleteAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    DeleteRequest deleteRequest = new DeleteRequest(request.param("index"), request.param("type"), request.param("id"));
    deleteRequest.routing(request.param("routing"));
    deleteRequest.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing
    deleteRequest.timeout(request.paramAsTime("timeout", DeleteRequest.DEFAULT_TIMEOUT));
    deleteRequest.refresh(request.paramAsBoolean("refresh", deleteRequest.refresh()));
    deleteRequest.version(RestActions.parseVersion(request));
    deleteRequest.versionType(VersionType.fromString(request.param("version_type"), deleteRequest.versionType()));

    String consistencyLevel = request.param("consistency");
    if (consistencyLevel != null) {
        deleteRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel));
    }

    client.delete(deleteRequest, new RestBuilderListener<DeleteResponse>(channel) {
        @Override
        public RestResponse buildResponse(DeleteResponse result, XContentBuilder builder) throws Exception {
            ActionWriteResponse.ShardInfo shardInfo = result.getShardInfo();
            builder.startObject().field(Fields.FOUND, result.isFound())
                    .field(Fields._INDEX, result.getIndex())
                    .field(Fields._TYPE, result.getType())
                    .field(Fields._ID, result.getId())
                    .field(Fields._VERSION, result.getVersion())
                    .value(shardInfo)
                    .endObject();
            RestStatus status = shardInfo.status();
            if (!result.isFound()) {
                status = NOT_FOUND;
            }
            return new BytesRestResponse(status, builder);
        }
    });
}
 
Example #5
Source File: UpdateRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    consistencyLevel = WriteConsistencyLevel.fromId(in.readByte());
    type = in.readString();
    id = in.readString();
    routing = in.readOptionalString();
    parent = in.readOptionalString();
    if (in.readBoolean()) {
        script = Script.readScript(in);
    }
    retryOnConflict = in.readVInt();
    refresh = in.readBoolean();
    if (in.readBoolean()) {
        doc = new IndexRequest();
        doc.readFrom(in);
    }
    int size = in.readInt();
    if (size >= 0) {
        fields = new String[size];
        for (int i = 0; i < size; i++) {
            fields[i] = in.readString();
        }
    }
    if (in.readBoolean()) {
        upsertRequest = new IndexRequest();
        upsertRequest.readFrom(in);
    }
    docAsUpsert = in.readBoolean();
    version = in.readLong();
    versionType = VersionType.fromValue(in.readByte());
    detectNoop = in.readBoolean();
    scriptedUpsert = in.readBoolean();
}
 
Example #6
Source File: ReplicationRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    if (in.readBoolean()) {
        shardId = ShardId.readShardId(in);
    } else {
        shardId = null;
    }
    consistencyLevel = WriteConsistencyLevel.fromId(in.readByte());
    timeout = TimeValue.readTimeValue(in);
    index = in.readString();
    canHaveDuplicates = in.readBoolean();
    // no need to serialize threaded* parameters, since they only matter locally
}
 
Example #7
Source File: RestUpdateAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    UpdateRequest updateRequest = new UpdateRequest(request.param("index"), request.param("type"), request.param("id"));
    updateRequest.routing(request.param("routing"));
    updateRequest.parent(request.param("parent"));
    updateRequest.timeout(request.paramAsTime("timeout", updateRequest.timeout()));
    updateRequest.refresh(request.paramAsBoolean("refresh", updateRequest.refresh()));
    String consistencyLevel = request.param("consistency");
    if (consistencyLevel != null) {
        updateRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel));
    }
    updateRequest.docAsUpsert(request.paramAsBoolean("doc_as_upsert", updateRequest.docAsUpsert()));
    ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
    scriptParameterParser.parseParams(request);
    ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
    if (scriptValue != null) {
        Map<String, Object> scriptParams = new HashMap<>();
        for (Map.Entry<String, String> entry : request.params().entrySet()) {
            if (entry.getKey().startsWith("sp_")) {
                scriptParams.put(entry.getKey().substring(3), entry.getValue());
            }
        }
        updateRequest.script(new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), scriptParams));
    }
    String sField = request.param("fields");
    if (sField != null) {
        String[] sFields = Strings.splitStringByCommaToArray(sField);
        if (sFields != null) {
            updateRequest.fields(sFields);
        }
    }
    updateRequest.retryOnConflict(request.paramAsInt("retry_on_conflict", updateRequest.retryOnConflict()));
    updateRequest.version(RestActions.parseVersion(request));
    updateRequest.versionType(VersionType.fromString(request.param("version_type"), updateRequest.versionType()));


    // see if we have it in the body
    if (request.hasContent()) {
        updateRequest.source(request.content());
        IndexRequest upsertRequest = updateRequest.upsertRequest();
        if (upsertRequest != null) {
            upsertRequest.routing(request.param("routing"));
            upsertRequest.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing
            upsertRequest.timestamp(request.param("timestamp"));
            if (request.hasParam("ttl")) {
                upsertRequest.ttl(request.param("ttl"));
            }
            upsertRequest.version(RestActions.parseVersion(request));
            upsertRequest.versionType(VersionType.fromString(request.param("version_type"), upsertRequest.versionType()));
        }
        IndexRequest doc = updateRequest.doc();
        if (doc != null) {
            doc.routing(request.param("routing"));
            doc.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing
            doc.timestamp(request.param("timestamp"));
            if (request.hasParam("ttl")) {
                doc.ttl(request.param("ttl"));
            }
            doc.version(RestActions.parseVersion(request));
            doc.versionType(VersionType.fromString(request.param("version_type"), doc.versionType()));
        }
    }

    client.update(updateRequest, new RestBuilderListener<UpdateResponse>(channel) {
        @Override
        public RestResponse buildResponse(UpdateResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            ActionWriteResponse.ShardInfo shardInfo = response.getShardInfo();
            builder.field(Fields._INDEX, response.getIndex())
                    .field(Fields._TYPE, response.getType())
                    .field(Fields._ID, response.getId())
                    .field(Fields._VERSION, response.getVersion());

            shardInfo.toXContent(builder, request);
            if (response.getGetResult() != null) {
                builder.startObject(Fields.GET);
                response.getGetResult().toXContentEmbedded(builder, request);
                builder.endObject();
            }

            builder.endObject();
            RestStatus status = shardInfo.status();
            if (response.isCreated()) {
                status = CREATED;
            }
            return new BytesRestResponse(status, builder);
        }
    });
}
 
Example #8
Source File: RestBulkAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    BulkRequest bulkRequest = Requests.bulkRequest();
    String defaultIndex = request.param("index");
    String defaultType = request.param("type");
    String defaultRouting = request.param("routing");
    String fieldsParam = request.param("fields");
    String[] defaultFields = fieldsParam != null ? Strings.commaDelimitedListToStringArray(fieldsParam) : null;

    String consistencyLevel = request.param("consistency");
    if (consistencyLevel != null) {
        bulkRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel));
    }
    bulkRequest.timeout(request.paramAsTime("timeout", BulkShardRequest.DEFAULT_TIMEOUT));
    bulkRequest.refresh(request.paramAsBoolean("refresh", bulkRequest.refresh()));
    bulkRequest.add(request.content(), defaultIndex, defaultType, defaultRouting, defaultFields, null, allowExplicitIndex);

    client.bulk(bulkRequest, new RestBuilderListener<BulkResponse>(channel) {
        @Override
        public RestResponse buildResponse(BulkResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field(Fields.TOOK, response.getTookInMillis());
            builder.field(Fields.ERRORS, response.hasFailures());
            builder.startArray(Fields.ITEMS);
            for (BulkItemResponse itemResponse : response) {
                builder.startObject();
                builder.startObject(itemResponse.getOpType());
                builder.field(Fields._INDEX, itemResponse.getIndex());
                builder.field(Fields._TYPE, itemResponse.getType());
                builder.field(Fields._ID, itemResponse.getId());
                long version = itemResponse.getVersion();
                if (version != -1) {
                    builder.field(Fields._VERSION, itemResponse.getVersion());
                }
                if (itemResponse.isFailed()) {
                    builder.field(Fields.STATUS, itemResponse.getFailure().getStatus().getStatus());
                    builder.startObject(Fields.ERROR);
                    ElasticsearchException.toXContent(builder, request, itemResponse.getFailure().getCause());
                    builder.endObject();
                } else {
                    ActionWriteResponse.ShardInfo shardInfo = itemResponse.getResponse().getShardInfo();
                    shardInfo.toXContent(builder, request);
                    if (itemResponse.getResponse() instanceof DeleteResponse) {
                        DeleteResponse deleteResponse = itemResponse.getResponse();
                        if (deleteResponse.isFound()) {
                            builder.field(Fields.STATUS, shardInfo.status().getStatus());
                        } else {
                            builder.field(Fields.STATUS, RestStatus.NOT_FOUND.getStatus());
                        }
                        builder.field(Fields.FOUND, deleteResponse.isFound());
                    } else if (itemResponse.getResponse() instanceof IndexResponse) {
                        IndexResponse indexResponse = itemResponse.getResponse();
                        if (indexResponse.isCreated()) {
                            builder.field(Fields.STATUS, RestStatus.CREATED.getStatus());
                        } else {
                            builder.field(Fields.STATUS, shardInfo.status().getStatus());
                        }
                    } else if (itemResponse.getResponse() instanceof UpdateResponse) {
                        UpdateResponse updateResponse = itemResponse.getResponse();
                        if (updateResponse.isCreated()) {
                            builder.field(Fields.STATUS, RestStatus.CREATED.getStatus());
                        } else {
                            builder.field(Fields.STATUS, shardInfo.status().getStatus());
                        }
                        if (updateResponse.getGetResult() != null) {
                            builder.startObject(Fields.GET);
                            updateResponse.getGetResult().toXContentEmbedded(builder, request);
                            builder.endObject();
                        }
                    }
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endArray();

            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #9
Source File: UpdateRequestBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the consistency level of write. Defaults to {@link org.elasticsearch.action.WriteConsistencyLevel#DEFAULT}
 */
public UpdateRequestBuilder setConsistencyLevel(WriteConsistencyLevel consistencyLevel) {
    request.consistencyLevel(consistencyLevel);
    return this;
}
 
Example #10
Source File: UpdateRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public WriteConsistencyLevel consistencyLevel() {
    return this.consistencyLevel;
}
 
Example #11
Source File: ReplicationRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public WriteConsistencyLevel consistencyLevel() {
    return this.consistencyLevel;
}
 
Example #12
Source File: ReplicationRequestBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the consistency level of write. Defaults to {@link org.elasticsearch.action.WriteConsistencyLevel#DEFAULT}
 */
@SuppressWarnings("unchecked")
public RequestBuilder setConsistencyLevel(WriteConsistencyLevel consistencyLevel) {
    request.consistencyLevel(consistencyLevel);
    return (RequestBuilder) this;
}
 
Example #13
Source File: TransportReplicationAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * checks whether we can perform a write based on the write consistency setting
 * returns **null* if OK to proceed, or a string describing the reason to stop
 */
String checkWriteConsistency(ShardId shardId) {
    if (checkWriteConsistency == false) {
        return null;
    }

    final WriteConsistencyLevel consistencyLevel;
    if (request.consistencyLevel() != WriteConsistencyLevel.DEFAULT) {
        consistencyLevel = request.consistencyLevel();
    } else {
        consistencyLevel = defaultWriteConsistencyLevel;
    }
    final int sizeActive;
    final int requiredNumber;
    IndexRoutingTable indexRoutingTable = state.getRoutingTable().index(shardId.getIndex());
    if (indexRoutingTable != null) {
        IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId.getId());
        if (shardRoutingTable != null) {
            sizeActive = shardRoutingTable.activeShards().size();
            if (consistencyLevel == WriteConsistencyLevel.QUORUM && shardRoutingTable.getSize() > 2) {
                // only for more than 2 in the number of shardIt it makes sense, otherwise its 1 shard with 1 replica, quorum is 1 (which is what it is initialized to)
                requiredNumber = (shardRoutingTable.getSize() / 2) + 1;
            } else if (consistencyLevel == WriteConsistencyLevel.ALL) {
                requiredNumber = shardRoutingTable.getSize();
            } else {
                requiredNumber = 1;
            }
        } else {
            sizeActive = 0;
            requiredNumber = 1;
        }
    } else {
        sizeActive = 0;
        requiredNumber = 1;
    }

    if (sizeActive < requiredNumber) {
        logger.trace("not enough active copies of shard [{}] to meet write consistency of [{}] (have {}, needed {}), scheduling a retry. action [{}], request [{}]",
            shardId, consistencyLevel, sizeActive, requiredNumber, transportPrimaryAction, request);
        return "Not enough active copies to meet write consistency of [" + consistencyLevel + "] (have " + sizeActive + ", needed " + requiredNumber + ").";
    } else {
        return null;
    }
}
 
Example #14
Source File: BulkRequestBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the consistency level. Defaults to {@link org.elasticsearch.action.WriteConsistencyLevel#DEFAULT}.
 */
public BulkRequestBuilder setConsistencyLevel(WriteConsistencyLevel consistencyLevel) {
    request.consistencyLevel(consistencyLevel);
    return this;
}
 
Example #15
Source File: BulkRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public WriteConsistencyLevel consistencyLevel() {
    return this.consistencyLevel;
}
 
Example #16
Source File: EsIndexProducerImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private BulkRequestBuilder initRequest() {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    bulkRequest.setConsistencyLevel( WriteConsistencyLevel.fromString( config.getWriteConsistencyLevel() ) );
    bulkRequest.setRefresh( config.isForcedRefresh() );
    return bulkRequest;
}