簡體   English   中英

避免在 Elasticsearch 中超時,在 Java 中重新索引

[英]Avoid timeout in Elasticsearch re-indexing in Java

當記錄數較高時,以下代碼在客戶端(Elasticsearch 客戶端)中返回超時。

CompletableFuture<BulkByScrollResponse> future = new CompletableFuture<>();
client.reindexAsync(request, RequestOptions.DEFAULT, new ActionListener<BulkByScrollResponse>() {
@Override
public void onResponse(BulkByScrollResponse bulkByScrollResponse) {
    future.complete(bulkByScrollResponse);
}

@Override
public void onFailure(Exception e) {
    future.completeExceptionally(e);
}
});
BulkByScrollResponse response = future.get(10, TimeUnit.MINUTES); // client timeout occured before this timeout

下面是客戶端配置。

connectTimeout: 60000
socketTimeout: 600000
maxRetryTimeoutMillis: 600000

有沒有辦法無限期地等待重新索引完成?

將重建索引請求作為任務提交:

TaskSubmissionResponse task = esClient.submitReindexTask(reindex, RequestOptions.DEFAULT);

獲取任務id:

TaskId taskId = new TaskId(task.getTask());

然后定期檢查任務狀態:

        GetTaskRequest taskQuery = new GetTaskRequest(taskId.getNodeId(), taskId.getId());
        GetTaskResponse taskStatus;
        do {
            Thread.sleep(TimeUnit.MINUTES.toMillis(1));
            taskStatus = esClient.tasks()
                    .get(taskQuery, RequestOptions.DEFAULT)
                    .orElseThrow(() -> new IllegalStateException("Reindex task not found. id=" + taskId));
        } while (!taskStatus.isCompleted());

Elasticsearch java api 關於任務處理的文檔很爛。

參考

我認為無限期地等待完成重新索引過程並為超時提供非常高的值並不是一個更好的選擇,因為這不是一個正確的解決方法,而且弊大於利。

相反,您應該檢查響應,添加更多調試日志以找到根本原因並解決它們。 另請查看我的提示以提高重新索引速度,這應該可以解決您的一些潛在問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM