-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REST high-level client: add clear cache API #28866
Changes from 1 commit
271da22
c0dd793
22cb53e
cddc2e0
6814616
8acef57
f848991
1611f65
0ee3c95
ee5d389
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; | ||
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; | ||
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; | ||
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; | ||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
|
@@ -234,6 +235,17 @@ static Request flush(FlushRequest flushRequest) { | |
return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), null); | ||
} | ||
|
||
static Request clearCache(ClearIndicesCacheRequest clearIndicesCacheRequest) { | ||
String endpoint = endpoint(clearIndicesCacheRequest.indices(), "_cache", "clear"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this result a NPE if there are no indices specified ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great catch, this is true for every broadcast response There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this |
||
Params parameters = Params.builder(); | ||
parameters.withIndicesOptions(clearIndicesCacheRequest.indicesOptions()); | ||
parameters.putParam("query", Boolean.toString(clearIndicesCacheRequest.queryCache())); | ||
parameters.putParam("fielddata", Boolean.toString(clearIndicesCacheRequest.fieldDataCache())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit : isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I only went with the code ( without going into details ). However, I just called
The deprecation seems to be wrapped in the ParseField#match
Good catch! Maybe it would be better to have a separate PR for the ref docs ( so that we can track when the preferred name has been changed and since when a deprecation msg is logged ) ( I can take this one over if you want )
Which other params do you mean? And remove them from the the rest api specs and the ref docs ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
parameters.putParam("request", Boolean.toString(clearIndicesCacheRequest.requestCache())); | ||
parameters.putParam("fields", String.join(",", clearIndicesCacheRequest.fields())); | ||
return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), null); | ||
} | ||
|
||
static Request info() { | ||
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; | ||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; | ||
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; | ||
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest; | ||
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse; | ||
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; | ||
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; | ||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
|
@@ -769,6 +771,91 @@ public void onFailure(Exception e) { | |
} | ||
} | ||
|
||
public void testClearCache() throws Exception { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
{ | ||
createIndex("index1", Settings.EMPTY); | ||
} | ||
|
||
{ | ||
// tag::clear-cache-request | ||
ClearIndicesCacheRequest request = new ClearIndicesCacheRequest("index1"); // <1> | ||
ClearIndicesCacheRequest requestMultiple = new ClearIndicesCacheRequest("index1", "index2"); // <2> | ||
ClearIndicesCacheRequest requestAll = new ClearIndicesCacheRequest(); // <3> | ||
// end::clear-cache-request | ||
|
||
// tag::clear-cache-request-indicesOptions | ||
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1> | ||
// end::clear-cache-request-indicesOptions | ||
|
||
// tag::clear-cache-request-query | ||
request.queryCache(true); // <1> | ||
// end::clear-cache-request-query | ||
|
||
// tag::clear-cache-request-request | ||
request.requestCache(true); // <1> | ||
// end::clear-cache-request-request | ||
|
||
// tag::clear-cache-request-fielddata | ||
request.fieldDataCache(true); // <1> | ||
// end::clear-cache-request-fielddata | ||
|
||
// tag::clear-cache-request-fields | ||
request.fields("field1", "field2", "field3"); // <1> | ||
// end::clear-cache-request-fields | ||
|
||
// tag::clear-cache-execute | ||
ClearIndicesCacheResponse clearCacheResponse = client.indices().clearCache(request); | ||
// end::clear-cache-execute | ||
|
||
// tag::clear-cache-response | ||
int totalShards = clearCacheResponse.getTotalShards(); // <1> | ||
int successfulShards = clearCacheResponse.getSuccessfulShards(); // <2> | ||
int failedShards = clearCacheResponse.getFailedShards(); // <3> | ||
DefaultShardOperationFailedException[] failures = clearCacheResponse.getShardFailures(); // <4> | ||
// end::clear-cache-response | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe check the returned values ? |
||
// tag::clear-cache-execute-listener | ||
ActionListener<ClearIndicesCacheResponse> listener = new ActionListener<ClearIndicesCacheResponse>() { | ||
@Override | ||
public void onResponse(ClearIndicesCacheResponse clearCacheResponse) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}; | ||
// end::clear-cache-execute-listener | ||
|
||
// Replace the empty listener by a blocking listener in test | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
listener = new LatchedActionListener<>(listener, latch); | ||
|
||
// tag::clear-cache-execute-async | ||
client.indices().clearCacheAsync(request, listener); // <1> | ||
// end::clear-cache-execute-async | ||
|
||
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
|
||
{ | ||
// tag::clear-cache-notfound | ||
try { | ||
ClearIndicesCacheRequest request = new ClearIndicesCacheRequest("does_not_exist"); | ||
client.indices().clearCache(request); | ||
} catch (ElasticsearchException exception) { | ||
if (exception.status() == RestStatus.NOT_FOUND) { | ||
// <1> | ||
} | ||
} | ||
// end::clear-cache-notfound | ||
} | ||
} | ||
|
||
|
||
public void testCloseIndex() throws Exception { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
[[java-rest-high-clear-cache]] | ||
=== Clear Cache API | ||
|
||
[[java-rest-high-clear-cache-request]] | ||
==== Clear Cache Request | ||
|
||
A `ClearIndicesCacheRquest` can be applied to one or more indices, or even on | ||
`_all` the indices: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-request] | ||
-------------------------------------------------- | ||
<1> Clears the cache of one index | ||
<2> Clears the cache of multiple indices | ||
<3> Clears the cache of all the indices | ||
|
||
==== Optional arguments | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-request-indicesOptions] | ||
-------------------------------------------------- | ||
<1> Setting `IndicesOptions` controls how unavailable indices are resolved and | ||
how wildcard expressions are expanded | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-request-query] | ||
-------------------------------------------------- | ||
<1> Set the `query` flag to `true` | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-request-fielddata] | ||
-------------------------------------------------- | ||
<1> Set the `fielddata` flag to `true` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit : s/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-request-request] | ||
-------------------------------------------------- | ||
<1> Set the `request` flag to `true` | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-request-fields] | ||
-------------------------------------------------- | ||
<1> Set the `fields` parameter | ||
|
||
[[java-rest-high-clear-cache-sync]] | ||
==== Synchronous Execution | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-execute] | ||
-------------------------------------------------- | ||
|
||
[[java-rest-high-clear-cache-async]] | ||
==== Asynchronous Execution | ||
|
||
The asynchronous execution of a clear cache request requires both the `ClearIndicesCacheRequest` | ||
instance and an `ActionListener` instance to be passed to the asynchronous | ||
method: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-execute-async] | ||
-------------------------------------------------- | ||
<1> The `ClearIndicesCacheRequest` to execute and the `ActionListener` to use when | ||
the execution completes | ||
|
||
The asynchronous method does not block and returns immediately. Once it is | ||
completed the `ActionListener` is called back using the `onResponse` method | ||
if the execution successfully completed or using the `onFailure` method if | ||
it failed. | ||
|
||
A typical listener for `ClearIndicesCacheResponse` looks like: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-execute-listener] | ||
-------------------------------------------------- | ||
<1> Called when the execution is successfully completed. The response is | ||
provided as an argument | ||
<2> Called in case of failure. The raised exception is provided as an argument | ||
|
||
[[java-rest-high-clear-cache-response]] | ||
==== Clear Cache Response | ||
|
||
The returned `ClearIndicesCacheResponse` allows to retrieve information about the | ||
executed operation as follows: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-response] | ||
-------------------------------------------------- | ||
<1> Total number of shards hit by the clear cache request | ||
<2> Number of shards where the clear cache has succeeded | ||
<3> Number of shards where the clear cache has failed | ||
<4> A list of failures if the operation failed on one or more shards | ||
|
||
By default, if the indices were not found, an `ElasticsearchException` will be thrown: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[clear-cache-notfound] | ||
-------------------------------------------------- | ||
<1> Do something if the indices to be cleared were not found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit : do we need this
\t
here? ( we did not use it in the rest of the javadocs )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need it here otherwise the line is too long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @olcbean talks about the indent of the new line here (line 268), to remove it and align with other lines :) like in other comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right the tab :) that we don't need