mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-26 14:54:56 +00:00
Clean serialization for cli's CommandResponse
* Switch `data` member from Object to `String` * Compress packages on server so easier to build `data` as `String` * Move write of `data` member into `encode` method * Move read of `data` member into ctor Original commit: elastic/x-pack-elasticsearch@e3a52e7493
This commit is contained in:
parent
9a08c93f38
commit
0fe1e4bb48
@ -17,9 +17,9 @@ public class CommandResponse extends Response {
|
|||||||
|
|
||||||
public final long serverTimeQueryReceived, serverTimeResponseSent;
|
public final long serverTimeQueryReceived, serverTimeResponseSent;
|
||||||
public final String requestId;
|
public final String requestId;
|
||||||
public final Object data; // NOCOMMIT should be a string? serialization is weird too.
|
public final String data;
|
||||||
|
|
||||||
public CommandResponse(long serverTimeQueryReceived, long serverTimeResponseSent, String requestId, Object data) {
|
public CommandResponse(long serverTimeQueryReceived, long serverTimeResponseSent, String requestId, String data) {
|
||||||
super(Action.COMMAND);
|
super(Action.COMMAND);
|
||||||
|
|
||||||
this.serverTimeQueryReceived = serverTimeQueryReceived;
|
this.serverTimeQueryReceived = serverTimeQueryReceived;
|
||||||
@ -34,15 +34,16 @@ public class CommandResponse extends Response {
|
|||||||
serverTimeQueryReceived = in.readLong();
|
serverTimeQueryReceived = in.readLong();
|
||||||
serverTimeResponseSent = in.readLong();
|
serverTimeResponseSent = in.readLong();
|
||||||
requestId = in.readUTF();
|
requestId = in.readUTF();
|
||||||
data = null;
|
data = in.readUTF();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void encode(DataOutput out) throws IOException {
|
public void encode(DataOutput out) throws IOException {
|
||||||
out.writeInt(Status.toSuccess(action)); // NOCOMMIT no symetric!
|
out.writeInt(Status.toSuccess(action)); // NOCOMMIT not symetric!
|
||||||
|
|
||||||
out.writeLong(serverTimeQueryReceived);
|
out.writeLong(serverTimeQueryReceived);
|
||||||
out.writeLong(serverTimeResponseSent);
|
out.writeLong(serverTimeResponseSent);
|
||||||
out.writeUTF(requestId);
|
out.writeUTF(requestId);
|
||||||
|
out.writeUTF(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -19,15 +19,9 @@ public class CommandResponseTests extends ESTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testRoundTrip() throws IOException {
|
public void testRoundTrip() throws IOException {
|
||||||
assertRoundTrip(randomCommandResponse(), (response, out) -> {
|
assertRoundTrip(randomCommandResponse(), CommandResponse::encode, in -> {
|
||||||
// NOCOMMIT make this simpler
|
// NOCOMMIT make symmetric
|
||||||
response.encode(out);
|
return (CommandResponse) ProtoUtils.readResponse(in, in.readInt());
|
||||||
out.writeUTF(response.data.toString());
|
|
||||||
}, in -> {
|
|
||||||
// NOCOMMIT make this simpler
|
|
||||||
CommandResponse response = (CommandResponse) ProtoUtils.readResponse(in, in.readInt());
|
|
||||||
return new CommandResponse(response.serverTimeQueryReceived, response.serverTimeResponseSent,
|
|
||||||
response.requestId, in.readUTF());
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ package org.elasticsearch.xpack.sql.cli.net.client;
|
|||||||
import org.elasticsearch.xpack.sql.cli.CliConfiguration;
|
import org.elasticsearch.xpack.sql.cli.CliConfiguration;
|
||||||
import org.elasticsearch.xpack.sql.cli.CliException;
|
import org.elasticsearch.xpack.sql.cli.CliException;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.CommandRequest;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.CommandRequest;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.CommandResponse;
|
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.InfoRequest;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.InfoRequest;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.Proto.Action;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.Proto.Action;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
||||||
@ -37,17 +36,7 @@ public class CliHttpClient implements AutoCloseable {
|
|||||||
|
|
||||||
public Response command(String command, String requestId) {
|
public Response command(String command, String requestId) {
|
||||||
Bytes ba = http.put(out -> ProtoUtils.write(out, new CommandRequest(command)));
|
Bytes ba = http.put(out -> ProtoUtils.write(out, new CommandRequest(command)));
|
||||||
return doIO(ba, in -> {
|
return doIO(ba, in -> readResponse(in, Action.COMMAND));
|
||||||
Response response = readResponse(in, Action.COMMAND);
|
|
||||||
// read data
|
|
||||||
if (response instanceof CommandResponse) {
|
|
||||||
// NOCOMMIT embed data in response
|
|
||||||
String result = in.readUTF();
|
|
||||||
CommandResponse cr = (CommandResponse) response;
|
|
||||||
return new CommandResponse(cr.serverTimeQueryReceived, cr.serverTimeResponseSent, cr.requestId, result);
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> T doIO(Bytes ba, DataInputFunction<T> action) {
|
private static <T> T doIO(Bytes ba, DataInputFunction<T> action) {
|
||||||
|
@ -12,8 +12,8 @@ import org.elasticsearch.xpack.sql.TestUtils;
|
|||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.Request;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.Request;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.http.CliServerProtoUtils;
|
import org.elasticsearch.xpack.sql.server.cli.CliServer;
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.server.CliServer;
|
import org.elasticsearch.xpack.sql.server.cli.CliServerProtoUtils;
|
||||||
import org.elasticsearch.xpack.sql.test.server.ProtoHandler;
|
import org.elasticsearch.xpack.sql.test.server.ProtoHandler;
|
||||||
|
|
||||||
import java.io.DataInput;
|
import java.io.DataInput;
|
||||||
|
@ -24,15 +24,15 @@ import org.elasticsearch.script.ScriptService;
|
|||||||
import org.elasticsearch.threadpool.ThreadPool;
|
import org.elasticsearch.threadpool.ThreadPool;
|
||||||
import org.elasticsearch.watcher.ResourceWatcherService;
|
import org.elasticsearch.watcher.ResourceWatcherService;
|
||||||
import org.elasticsearch.xpack.sql.execution.PlanExecutor;
|
import org.elasticsearch.xpack.sql.execution.PlanExecutor;
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.action.CliAction;
|
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.action.TransportCliAction;
|
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.http.CliHttpHandler;
|
|
||||||
import org.elasticsearch.xpack.sql.plugin.jdbc.action.JdbcAction;
|
import org.elasticsearch.xpack.sql.plugin.jdbc.action.JdbcAction;
|
||||||
import org.elasticsearch.xpack.sql.plugin.jdbc.action.TransportJdbcAction;
|
import org.elasticsearch.xpack.sql.plugin.jdbc.action.TransportJdbcAction;
|
||||||
import org.elasticsearch.xpack.sql.plugin.jdbc.http.JdbcHttpHandler;
|
import org.elasticsearch.xpack.sql.plugin.jdbc.http.JdbcHttpHandler;
|
||||||
import org.elasticsearch.xpack.sql.plugin.sql.action.SqlAction;
|
import org.elasticsearch.xpack.sql.plugin.sql.action.SqlAction;
|
||||||
import org.elasticsearch.xpack.sql.plugin.sql.action.TransportSqlAction;
|
import org.elasticsearch.xpack.sql.plugin.sql.action.TransportSqlAction;
|
||||||
import org.elasticsearch.xpack.sql.plugin.sql.rest.RestSqlAction;
|
import org.elasticsearch.xpack.sql.plugin.sql.rest.RestSqlAction;
|
||||||
|
import org.elasticsearch.xpack.sql.server.cli.CliAction;
|
||||||
|
import org.elasticsearch.xpack.sql.server.cli.CliHttpHandler;
|
||||||
|
import org.elasticsearch.xpack.sql.server.cli.TransportCliAction;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.action;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import org.elasticsearch.action.Action;
|
import org.elasticsearch.action.Action;
|
||||||
import org.elasticsearch.client.ElasticsearchClient;
|
import org.elasticsearch.client.ElasticsearchClient;
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.http;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -17,9 +17,6 @@ import org.elasticsearch.rest.RestChannel;
|
|||||||
import org.elasticsearch.rest.RestController;
|
import org.elasticsearch.rest.RestController;
|
||||||
import org.elasticsearch.rest.RestRequest;
|
import org.elasticsearch.rest.RestRequest;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.action.CliAction;
|
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.action.CliRequest;
|
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.action.CliResponse;
|
|
||||||
import org.elasticsearch.xpack.sql.util.StringUtils;
|
import org.elasticsearch.xpack.sql.util.StringUtils;
|
||||||
|
|
||||||
import static org.elasticsearch.action.ActionListener.wrap;
|
import static org.elasticsearch.action.ActionListener.wrap;
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.action;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.action;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import org.elasticsearch.action.ActionRequestBuilder;
|
import org.elasticsearch.action.ActionRequestBuilder;
|
||||||
import org.elasticsearch.client.ElasticsearchClient;
|
import org.elasticsearch.client.ElasticsearchClient;
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.action;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import org.elasticsearch.action.ActionResponse;
|
import org.elasticsearch.action.ActionResponse;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.server;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import org.elasticsearch.Build;
|
import org.elasticsearch.Build;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
@ -18,7 +18,6 @@ import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
|||||||
import org.elasticsearch.xpack.sql.execution.PlanExecutor;
|
import org.elasticsearch.xpack.sql.execution.PlanExecutor;
|
||||||
import org.elasticsearch.xpack.sql.execution.search.SearchHitRowSetCursor;
|
import org.elasticsearch.xpack.sql.execution.search.SearchHitRowSetCursor;
|
||||||
import org.elasticsearch.xpack.sql.jdbc.net.protocol.QueryPageRequest;
|
import org.elasticsearch.xpack.sql.jdbc.net.protocol.QueryPageRequest;
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.http.CliServerProtoUtils;
|
|
||||||
import org.elasticsearch.xpack.sql.util.StringUtils;
|
import org.elasticsearch.xpack.sql.util.StringUtils;
|
||||||
|
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
@ -71,7 +70,7 @@ public class CliServer {
|
|||||||
requestId = StringUtils.nullAsEmpty(((SearchHitRowSetCursor) c).scrollId());
|
requestId = StringUtils.nullAsEmpty(((SearchHitRowSetCursor) c).scrollId());
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.onResponse(new CommandResponse(start, stop, requestId, c));
|
listener.onResponse(new CommandResponse(start, stop, requestId, CliUtils.toString(c)));
|
||||||
},
|
},
|
||||||
ex -> listener.onResponse(CliServerProtoUtils.exception(ex, req.action))));
|
ex -> listener.onResponse(CliServerProtoUtils.exception(ex, req.action))));
|
||||||
}
|
}
|
@ -3,20 +3,18 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.http;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import org.elasticsearch.ResourceNotFoundException;
|
import org.elasticsearch.ResourceNotFoundException;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||||
import org.elasticsearch.xpack.sql.SqlException;
|
import org.elasticsearch.xpack.sql.SqlException;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.CommandResponse;
|
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.ErrorResponse;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.ErrorResponse;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.ExceptionResponse;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.ExceptionResponse;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.Proto.Action;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.Proto.Action;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.ProtoUtils;
|
||||||
import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
import org.elasticsearch.xpack.sql.cli.net.protocol.Response;
|
||||||
import org.elasticsearch.xpack.sql.session.RowSetCursor;
|
|
||||||
|
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -31,16 +29,6 @@ public abstract class CliServerProtoUtils {
|
|||||||
try (BytesStreamOutput array = new BytesStreamOutput();
|
try (BytesStreamOutput array = new BytesStreamOutput();
|
||||||
DataOutputStream out = new DataOutputStream(array)) {
|
DataOutputStream out = new DataOutputStream(array)) {
|
||||||
ProtoUtils.write(out, response);
|
ProtoUtils.write(out, response);
|
||||||
|
|
||||||
// serialize payload (if present)
|
|
||||||
if (response instanceof CommandResponse) {
|
|
||||||
RowSetCursor cursor = (RowSetCursor) ((CommandResponse) response).data;
|
|
||||||
|
|
||||||
if (cursor != null) {
|
|
||||||
out.writeUTF(CliUtils.toString(cursor));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out.flush();
|
out.flush();
|
||||||
return array.bytes();
|
return array.bytes();
|
||||||
}
|
}
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.http;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
@ -3,7 +3,7 @@
|
|||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.sql.plugin.cli.action;
|
package org.elasticsearch.xpack.sql.server.cli;
|
||||||
|
|
||||||
import org.elasticsearch.Build;
|
import org.elasticsearch.Build;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
@ -18,7 +18,6 @@ import org.elasticsearch.threadpool.ThreadPool;
|
|||||||
import org.elasticsearch.transport.TransportService;
|
import org.elasticsearch.transport.TransportService;
|
||||||
import org.elasticsearch.xpack.sql.analysis.catalog.EsCatalog;
|
import org.elasticsearch.xpack.sql.analysis.catalog.EsCatalog;
|
||||||
import org.elasticsearch.xpack.sql.execution.PlanExecutor;
|
import org.elasticsearch.xpack.sql.execution.PlanExecutor;
|
||||||
import org.elasticsearch.xpack.sql.plugin.cli.server.CliServer;
|
|
||||||
|
|
||||||
import static org.elasticsearch.xpack.sql.util.ActionUtils.chain;
|
import static org.elasticsearch.xpack.sql.util.ActionUtils.chain;
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user