-
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
Add Get Aliases API to the high-level REST client #28799
Changes from 1 commit
6c01eeb
1571cb3
a33ea0f
a919d73
baed505
f74d0b5
7720b71
7238098
e2ffdab
9a2079f
df3b2fb
0e3ad61
519992b
11811ef
474238b
a59a026
b210f49
5b3096b
8660d43
f21b3ca
fa9d4e1
939ae4d
035d823
be1daf7
5da9ed6
5bcc1ce
2e2217a
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 |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.cluster.metadata.AliasMetaData; | ||
|
@@ -51,23 +50,16 @@ public class GetAliasesResponse extends ActionResponse implements StatusToXConte | |
|
||
private ImmutableOpenMap<String, List<AliasMetaData>> aliases = ImmutableOpenMap.of(); | ||
private RestStatus status = RestStatus.OK; | ||
private String errorMsg = ""; | ||
private String errorMessage; | ||
|
||
public GetAliasesResponse(ImmutableOpenMap<String, List<AliasMetaData>> aliases, RestStatus status, String errorMsg) { | ||
public GetAliasesResponse(ImmutableOpenMap<String, List<AliasMetaData>> aliases, RestStatus status, String errorMessage) { | ||
this.aliases = aliases; | ||
if (status == null) { | ||
this.status = RestStatus.OK; | ||
} | ||
this.status = status; | ||
if (errorMsg == null) { | ||
this.errorMsg = ""; | ||
} else { | ||
this.errorMsg = errorMsg; | ||
} | ||
this.status = status == null ? RestStatus.OK : status; | ||
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. when do we get null here? I would prefer to have this logic (default is OK, but also 404 can be provided) in one place, while we have it now in the transport action too. I wonder when this if is needed. 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. did you have a chance to look into this super small comment? |
||
this.errorMessage = errorMessage; | ||
} | ||
|
||
public GetAliasesResponse(ImmutableOpenMap<String, List<AliasMetaData>> aliases) { | ||
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. would it be possible to remove this constructor? It seems that it's only used in tests and we could rather use the above constructor and provide null arguments to it. 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 would prefer not to as I do not know whether users are using it or not ( it is part of the public API ... ) |
||
this(aliases, RestStatus.OK, ""); | ||
this(aliases, RestStatus.OK, null); | ||
} | ||
|
||
GetAliasesResponse() { | ||
|
@@ -82,13 +74,13 @@ public ImmutableOpenMap<String, List<AliasMetaData>> getAliases() { | |
return aliases; | ||
} | ||
|
||
public String errorMsg() { | ||
return errorMsg; | ||
public String errorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this, true, true) + ", status:" + status + ", errorMsg:\"" + errorMsg + "\""; | ||
return Strings.toString(this, true, true); | ||
} | ||
|
||
@Override | ||
|
@@ -107,10 +99,10 @@ public void readFrom(StreamInput in) throws IOException { | |
} | ||
aliases = aliasesBuilder.build(); | ||
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) { | ||
// if (in.getVersion().onOrAfter(Version.V_6_3_0)) { | ||
// if (in.getVersion().onOrAfter(Version.V_6_4_0)) { | ||
status = RestStatus.fromCode(in.readInt()); | ||
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. should we use 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. Interesting, |
||
if (in.readBoolean()) { | ||
status = RestStatus.fromCode(in.readInt()); | ||
errorMsg = in.readString(); | ||
errorMessage = in.readString(); | ||
} | ||
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: readOptionalString? 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. 🤦 |
||
} | ||
} | ||
|
@@ -127,11 +119,11 @@ public void writeTo(StreamOutput out) throws IOException { | |
} | ||
} | ||
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) { | ||
// if (out.getVersion().onOrAfter(Version.V_6_3_0)) { | ||
if (status != RestStatus.OK) { | ||
// if (out.getVersion().onOrAfter(Version.V_6_4_0)) { | ||
out.writeInt(status.getStatus()); | ||
if (null != errorMessage) { | ||
out.writeBoolean(true); | ||
out.writeInt(status.getStatus()); | ||
out.writeString(errorMsg); | ||
out.writeString(errorMessage); | ||
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. use |
||
} else { | ||
out.writeBoolean(false); | ||
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. why the boolean flag? we could otherwise always serialize the status and serialize the error as an optional string (as I asked above to accept null as well)? 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. The boolean flag signals if a status and a string are following. |
||
} | ||
|
@@ -149,15 +141,15 @@ public boolean equals(Object o) { | |
GetAliasesResponse that = (GetAliasesResponse) o; | ||
return Objects.equals(fromListAliasesToSet(aliases), fromListAliasesToSet(that.aliases)) | ||
&& Objects.equals(status, that.status) | ||
&& Objects.equals(errorMsg, that.errorMsg); | ||
&& Objects.equals(errorMessage, that.errorMessage); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(fromListAliasesToSet(aliases), status, errorMsg); | ||
return Objects.hash(fromListAliasesToSet(aliases), status, errorMessage); | ||
} | ||
|
||
private ImmutableOpenMap<String, Set<AliasMetaData>> fromListAliasesToSet(ImmutableOpenMap<String, List<AliasMetaData>> list) { | ||
private static ImmutableOpenMap<String, Set<AliasMetaData>> fromListAliasesToSet(ImmutableOpenMap<String, List<AliasMetaData>> list) { | ||
ImmutableOpenMap.Builder<String, Set<AliasMetaData>> builder = ImmutableOpenMap.builder(); | ||
list.forEach(e -> builder.put(e.key, new HashSet<>(e.value))); | ||
return builder.build(); | ||
|
@@ -182,12 +174,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
builder.startObject(); | ||
{ | ||
if (status != null && RestStatus.OK != status) { | ||
builder.field("error", errorMsg); | ||
builder.field("error", errorMessage); | ||
builder.field("status", status.getStatus()); | ||
} | ||
|
||
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : aliases) { | ||
if (namesProvided == false || indicesToDisplay.contains(entry.key)) { | ||
if (false == namesProvided || indicesToDisplay.contains(entry.key)) { | ||
builder.startObject(entry.key); | ||
{ | ||
builder.startObject("aliases"); | ||
|
@@ -215,11 +207,10 @@ public static GetAliasesResponse fromXContent(XContentParser parser) throws IOEx | |
|
||
String currentFieldName; | ||
Token token; | ||
String exceptionMsg = null; | ||
String exceptionMessage = null; | ||
RestStatus status = RestStatus.OK; | ||
ElasticsearchException exception = null; | ||
|
||
while ((token = parser.nextToken()) != Token.END_OBJECT) { | ||
while (parser.nextToken() != Token.END_OBJECT) { | ||
if (parser.currentToken() == Token.FIELD_NAME) { | ||
currentFieldName = parser.currentName(); | ||
|
||
|
@@ -231,10 +222,10 @@ public static GetAliasesResponse fromXContent(XContentParser parser) throws IOEx | |
} else if ("error".equals(currentFieldName)) { | ||
if ((token = parser.nextToken()) != Token.FIELD_NAME) { | ||
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. this resembles what 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. Thanks for the tip! I will have a look and if I can reuse the |
||
if (token == Token.VALUE_STRING) { | ||
exceptionMsg = parser.text(); | ||
exceptionMessage = parser.text(); | ||
} else if (token == Token.START_OBJECT) { | ||
token = parser.nextToken(); | ||
exception = ElasticsearchException.innerFromXContent(parser, true); | ||
parser.nextToken(); | ||
exceptionMessage = ElasticsearchException.innerFromXContent(parser, true).getMessage(); | ||
} | ||
} | ||
} else { | ||
|
@@ -246,15 +237,7 @@ public static GetAliasesResponse fromXContent(XContentParser parser) throws IOEx | |
} | ||
} | ||
} | ||
if (exception != null) { | ||
throw new ElasticsearchStatusException(exception.getMessage(), status, exception.getCause()); | ||
} | ||
if (RestStatus.OK != status && aliasesBuilder.isEmpty()) { | ||
throw new ElasticsearchStatusException(exceptionMsg, status); | ||
} | ||
GetAliasesResponse getAliasesResponse = new GetAliasesResponse(aliasesBuilder.build(), status, exceptionMsg); | ||
|
||
return getAliasesResponse; | ||
return new GetAliasesResponse(aliasesBuilder.build(), status, exceptionMessage); | ||
} | ||
|
||
private static List<AliasMetaData> parseAliases(XContentParser parser) throws IOException { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,8 +78,8 @@ protected void masterOperation(GetAliasesRequest request, ClusterState state, Ac | |
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request); | ||
ImmutableOpenMap<String, List<AliasMetaData>> result = state.metaData().findAliases(request.aliases(), concreteIndices); | ||
|
||
SetOnce<String> message = new SetOnce<>(); | ||
SetOnce<RestStatus> status = new SetOnce<>(); | ||
String message = null; | ||
RestStatus status = RestStatus.OK; | ||
if (false == Strings.isAllOrWildcard(request.aliases())) { | ||
String[] aliasesNames = Strings.EMPTY_ARRAY; | ||
|
||
|
@@ -113,21 +113,15 @@ protected void masterOperation(GetAliasesRequest request, ClusterState state, Ac | |
|
||
difference.removeAll(matches); | ||
if (false == difference.isEmpty()) { | ||
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 that I would find this more readable if we declared |
||
status.set(RestStatus.NOT_FOUND); | ||
status = RestStatus.NOT_FOUND; | ||
if (difference.size() == 1) { | ||
message.set(String.format(Locale.ROOT, "alias [%s] missing", toNamesString(difference.iterator().next()))); | ||
message = String.format(Locale.ROOT, "alias [%s] missing", toNamesString(difference.iterator().next())); | ||
} else { | ||
message.set(String.format(Locale.ROOT, "aliases [%s] missing", toNamesString(difference.toArray(new String[0])))); | ||
message = String.format(Locale.ROOT, "aliases [%s] missing", toNamesString(difference.toArray(new String[0]))); | ||
} | ||
} | ||
} | ||
if (status.get() == null) { | ||
status.set(RestStatus.OK); | ||
} | ||
if (message.get() == null) { | ||
message.set(""); | ||
} | ||
listener.onResponse(new GetAliasesResponse(result, status.get(), message.get())); | ||
listener.onResponse(new GetAliasesResponse(result, status, message)); | ||
} | ||
|
||
private static String toNamesString(final String... names) { | ||
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: would it be possible to use |
||
|
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 noticed that when a wildcard expressions is provided for the indices, and no indices match, we return 200 and an empty json object. Can we test that too?