Replace metadata keys in OpenSearchException during serialization and deserialization (#905)

Signed-off-by: Vlad Rozov <vrozov@users.noreply.github.com>
This commit is contained in:
Vlad Rozov 2021-06-30 08:39:26 -10:00 committed by GitHub
parent 06228bc25b
commit 362f116abe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 2 deletions

View File

@ -0,0 +1,61 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.backwards;
import org.apache.http.util.EntityUtils;
import org.opensearch.Version;
import org.opensearch.client.Node;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.test.rest.OpenSearchRestTestCase;
import org.opensearch.test.rest.yaml.ObjectPath;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
public class ExceptionIT extends OpenSearchRestTestCase {
public void testOpensearchException() throws Exception {
logClusterNodes();
Request request = new Request("GET", "/no_such_index");
for (Node node : client().getNodes()) {
try {
client().setNodes(Collections.singletonList(node));
logger.info("node: {}", node.getHost());
client().performRequest(request);
fail();
} catch (ResponseException e) {
logger.debug(e.getMessage());
Response response = e.getResponse();
assertEquals(SC_NOT_FOUND, response.getStatusLine().getStatusCode());
assertEquals("no_such_index", ObjectPath.createFromResponse(response).evaluate("error.index"));
}
}
}
private void logClusterNodes() throws IOException {
ObjectPath objectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "_nodes")));
Map<String, ?> nodes = objectPath.evaluate("nodes");
String master = EntityUtils.toString(client().performRequest(new Request("GET", "_cat/master?h=id")).getEntity()).trim();
logger.info("cluster discovered: master id='{}'", master);
for (String id : nodes.keySet()) {
logger.info("{}: id='{}', name='{}', version={}",
objectPath.evaluate("nodes." + id + ".http.publish_address"),
id,
objectPath.evaluate("nodes." + id + ".name"),
Version.fromString(objectPath.evaluate("nodes." + id + ".version"))
);
}
}
}

View File

@ -61,6 +61,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.util.Collections.emptyMap;
@ -108,6 +109,10 @@ public class OpenSearchException extends RuntimeException implements ToXContentF
private static final Map<Integer, CheckedFunction<StreamInput, ? extends OpenSearchException, IOException>> ID_TO_SUPPLIER;
private static final Map<Class<? extends OpenSearchException>, OpenSearchExceptionHandle> CLASS_TO_OPENSEARCH_EXCEPTION_HANDLE;
private static final Pattern OS_METADATA = Pattern.compile("^opensearch\\.");
private static final Pattern ES_METADATA = Pattern.compile("^es\\.");
private final Map<String, List<String>> metadata = new HashMap<>();
private final Map<String, List<String>> headers = new HashMap<>();
@ -150,7 +155,16 @@ public class OpenSearchException extends RuntimeException implements ToXContentF
super(in.readOptionalString(), in.readException());
readStackTrace(this, in);
headers.putAll(in.readMapOfLists(StreamInput::readString, StreamInput::readString));
metadata.putAll(in.readMapOfLists(StreamInput::readString, StreamInput::readString));
metadata.putAll(in.readMapOfLists(OpenSearchException::readAndReplace, StreamInput::readString));
}
private static String readAndReplace(StreamInput in) throws IOException {
String str = in.readString();
return in.getVersion().onOrBefore(LegacyESVersion.V_7_10_2) ? ES_METADATA.matcher(str).replaceFirst("opensearch.") : str;
}
private static void replaceAndWrite(StreamOutput out, String str) throws IOException {
out.writeString(out.getVersion().onOrBefore(LegacyESVersion.V_7_10_2) ? OS_METADATA.matcher(str).replaceFirst("es.") : str);
}
/**
@ -293,7 +307,7 @@ public class OpenSearchException extends RuntimeException implements ToXContentF
out.writeException(this.getCause());
writeStackTraces(this, out, StreamOutput::writeException);
out.writeMapOfLists(headers, StreamOutput::writeString, StreamOutput::writeString);
out.writeMapOfLists(metadata, StreamOutput::writeString, StreamOutput::writeString);
out.writeMapOfLists(metadata, OpenSearchException::replaceAndWrite, StreamOutput::writeString);
}
public static OpenSearchException readException(StreamInput input, int id) throws IOException {