Skip to content
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

Change signature of Get Repositories Response #30333

Merged
merged 4 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@
package org.elasticsearch.action.admin.cluster.repositories.get;

import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
* Get repositories response
*/
public class GetRepositoriesResponse extends ActionResponse implements Iterable<RepositoryMetaData> {

private List<RepositoryMetaData> repositories = Collections.emptyList();
public class GetRepositoriesResponse extends ActionResponse implements ToXContentObject {

private RepositoriesMetaData repositories;

GetRepositoriesResponse() {
repositories = new RepositoriesMetaData(Collections.emptyList());
}

GetRepositoriesResponse(List<RepositoryMetaData> repositories) {
GetRepositoriesResponse(RepositoriesMetaData repositories) {
this.repositories = repositories;
}

Expand All @@ -52,43 +53,25 @@ public class GetRepositoriesResponse extends ActionResponse implements Iterable<
* @return list or repositories
*/
public List<RepositoryMetaData> repositories() {
Copy link
Member

@rjernst rjernst May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the idea was to return Iterable<RepositoryMetaData> here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the way the code was before this change was the class implements Iterable<RepositoryMetaData> , so i was considering using that, but it only solved 1 case, and there were still many cases where repositories().repositories().size() was used in tests, and some other cases where the iterator approach would work. Regardless of how it was implemented, we need to get the size of the list in some tests, so i figure it best to just return the list itself.

That being said, we should probably consider making the List inside the RepositoriesMetaData a unmodifiable List unless it has to be otherwise. I dont see any indication that it needs to be otherwise modifyable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do make it unmodifiable!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming in a following PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for posterity that other pr was #30361

return repositories;
return repositories.repositories();
}


@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
int size = in.readVInt();
List<RepositoryMetaData> repositoryListBuilder = new ArrayList<>(size);
for (int j = 0; j < size; j++) {
repositoryListBuilder.add(new RepositoryMetaData(
in.readString(),
in.readString(),
Settings.readSettingsFromStream(in))
);
}
repositories = Collections.unmodifiableList(repositoryListBuilder);
repositories = new RepositoriesMetaData(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(repositories.size());
for (RepositoryMetaData repository : repositories) {
out.writeString(repository.name());
out.writeString(repository.type());
Settings.writeSettingsToStream(repository.settings(), out);
}
repositories.writeTo(out);
}

/**
* Iterator over the repositories data
*
* @return iterator over the repositories data
*/
@Override
public Iterator<RepositoryMetaData> iterator() {
return repositories.iterator();
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
repositories.toXContent(builder, params);
builder.endObject();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ protected void masterOperation(final GetRepositoriesRequest request, ClusterStat
RepositoriesMetaData repositories = metaData.custom(RepositoriesMetaData.TYPE);
if (request.repositories().length == 0 || (request.repositories().length == 1 && "_all".equals(request.repositories()[0]))) {
if (repositories != null) {
listener.onResponse(new GetRepositoriesResponse(repositories.repositories()));
listener.onResponse(new GetRepositoriesResponse(repositories));
} else {
listener.onResponse(new GetRepositoriesResponse(Collections.<RepositoryMetaData>emptyList()));
listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetaData(Collections.emptyList())));
}
} else {
if (repositories != null) {
Expand All @@ -102,7 +102,7 @@ protected void masterOperation(final GetRepositoriesRequest request, ClusterStat
}
repositoryListBuilder.add(repositoryMetaData);
}
listener.onResponse(new GetRepositoriesResponse(Collections.unmodifiableList(repositoryListBuilder)));
listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetaData(repositoryListBuilder)));
} else {
listener.onFailure(new RepositoryMissingException(request.repositories()[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,19 @@
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest;
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;

import java.io.IOException;
import java.util.Set;

import static org.elasticsearch.client.Requests.getRepositoryRequest;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;

/**
* Returns repository information
Expand Down Expand Up @@ -69,18 +63,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
getRepositoriesRequest.local(request.paramAsBoolean("local", getRepositoriesRequest.local()));
settingsFilter.addFilterSettingParams(request);
return channel ->
client.admin().cluster().getRepositories(getRepositoriesRequest, new RestBuilderListener<GetRepositoriesResponse>(channel) {
@Override
public RestResponse buildResponse(GetRepositoriesResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
for (RepositoryMetaData repositoryMetaData : response.repositories()) {
RepositoriesMetaData.toXContent(repositoryMetaData, builder, request);
}
builder.endObject();

return new BytesRestResponse(OK, builder);
}
});
client.admin().cluster().getRepositories(getRepositoriesRequest,
new RestToXContentListener<>(channel));
}

@Override
Expand Down