Search in sources :

Example 86 with Version

use of org.opensearch.Version in project OpenSearch by opensearch-project.

the class GeoJsonShapeParserTests method testParse3DPolygon.

public void testParse3DPolygon() throws IOException, ParseException {
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).value(10.0).endArray().startArray().value(101.0).value(1.0).value(10.0).endArray().startArray().value(101.0).value(0.0).value(10.0).endArray().startArray().value(100.0).value(0.0).value(10.0).endArray().startArray().value(100.0).value(1.0).value(10.0).endArray().endArray().endArray().endObject();
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0, 10));
    shellCoordinates.add(new Coordinate(101, 0, 10));
    shellCoordinates.add(new Coordinate(101, 1, 10));
    shellCoordinates.add(new Coordinate(100, 1, 10));
    shellCoordinates.add(new Coordinate(100, 0, 10));
    Coordinate[] coordinates = shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]);
    Version randomVersion = VersionUtils.randomIndexCompatibleVersion(random());
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, randomVersion).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
    Polygon expected = GEOMETRY_FACTORY.createPolygon(shell, null);
    Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
    final LegacyGeoShapeFieldMapper mapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").ignoreZValue(true).build(mockBuilderContext));
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        OpenSearchGeoAssertions.assertEquals(jtsGeom(expected), ShapeParser.parse(parser, mapperBuilder).buildS4J());
    }
    org.opensearch.geometry.Polygon p = new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(Arrays.stream(coordinates).mapToDouble(i -> i.x).toArray(), Arrays.stream(coordinates).mapToDouble(i -> i.y).toArray()));
    assertGeometryEquals(p, polygonGeoJson, false);
}
Also used : Arrays(java.util.Arrays) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShapeParser(org.opensearch.common.geo.parsers.ShapeParser) LinearRing(org.locationtech.jts.geom.LinearRing) ContentPath(org.opensearch.index.mapper.ContentPath) Coordinate(org.locationtech.jts.geom.Coordinate) Version(org.opensearch.Version) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Strings(org.opensearch.common.Strings) MultiPoint(org.opensearch.geometry.MultiPoint) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) Rectangle(org.locationtech.spatial4j.shape.Rectangle) SPATIAL_CONTEXT(org.opensearch.common.geo.builders.ShapeBuilder.SPATIAL_CONTEXT) VersionUtils(org.opensearch.test.VersionUtils) XContentFactory(org.opensearch.common.xcontent.XContentFactory) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) UUIDs(org.opensearch.common.UUIDs) ParseException(java.text.ParseException) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) MultiLine(org.opensearch.geometry.MultiLine) MultiLineString(org.locationtech.jts.geom.MultiLineString) GeometryCollection(org.opensearch.geometry.GeometryCollection) Circle(org.locationtech.spatial4j.shape.Circle) OpenSearchParseException(org.opensearch.OpenSearchParseException) Settings(org.opensearch.common.settings.Settings) Point(org.locationtech.jts.geom.Point) IOException(java.io.IOException) Geometry(org.opensearch.geometry.Geometry) Mapper(org.opensearch.index.mapper.Mapper) Shape(org.locationtech.spatial4j.shape.Shape) GeoShapeIndexer(org.opensearch.index.mapper.GeoShapeIndexer) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LineString(org.locationtech.jts.geom.LineString) List(java.util.List) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) Polygon(org.locationtech.jts.geom.Polygon) OpenSearchGeoAssertions(org.opensearch.test.hamcrest.OpenSearchGeoAssertions) Line(org.opensearch.geometry.Line) Collections(java.util.Collections) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ArrayList(java.util.ArrayList) ContentPath(org.opensearch.index.mapper.ContentPath) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) Coordinate(org.locationtech.jts.geom.Coordinate) Version(org.opensearch.Version) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Settings(org.opensearch.common.settings.Settings) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 87 with Version

use of org.opensearch.Version in project OpenSearch by opensearch-project.

the class InboundHandler method handleRequest.

private <T extends TransportRequest> void handleRequest(TcpChannel channel, Header header, InboundMessage message) throws IOException {
    final String action = header.getActionName();
    final long requestId = header.getRequestId();
    final Version version = header.getVersion();
    if (header.isHandshake()) {
        messageListener.onRequestReceived(requestId, action);
        // Cannot short circuit handshakes
        assert message.isShortCircuit() == false;
        final StreamInput stream = namedWriteableStream(message.openOrGetStreamInput());
        assertRemoteVersion(stream, header.getVersion());
        final TransportChannel transportChannel = new TcpTransportChannel(outboundHandler, channel, action, requestId, version, header.getFeatures(), header.isCompressed(), header.isHandshake(), message.takeBreakerReleaseControl());
        try {
            handshaker.handleHandshake(transportChannel, requestId, stream);
        } catch (Exception e) {
            if (Version.CURRENT.isCompatible(header.getVersion())) {
                sendErrorResponse(action, transportChannel, e);
            } else {
                logger.warn(new ParameterizedMessage("could not send error response to handshake received on [{}] using wire format version [{}], closing channel", channel, header.getVersion()), e);
                channel.close();
            }
        }
    } else {
        final TransportChannel transportChannel = new TcpTransportChannel(outboundHandler, channel, action, requestId, version, header.getFeatures(), header.isCompressed(), header.isHandshake(), message.takeBreakerReleaseControl());
        try {
            messageListener.onRequestReceived(requestId, action);
            if (message.isShortCircuit()) {
                sendErrorResponse(action, transportChannel, message.getException());
            } else {
                final StreamInput stream = namedWriteableStream(message.openOrGetStreamInput());
                assertRemoteVersion(stream, header.getVersion());
                final RequestHandlerRegistry<T> reg = requestHandlers.getHandler(action);
                assert reg != null;
                final T request = newRequest(requestId, action, stream, reg);
                request.remoteAddress(new TransportAddress(channel.getRemoteAddress()));
                checkStreamIsFullyConsumed(requestId, action, stream);
                final String executor = reg.getExecutor();
                if (ThreadPool.Names.SAME.equals(executor)) {
                    try {
                        reg.processMessageReceived(request, transportChannel);
                    } catch (Exception e) {
                        sendErrorResponse(reg.getAction(), transportChannel, e);
                    }
                } else {
                    threadPool.executor(executor).execute(new RequestHandler<>(reg, request, transportChannel));
                }
            }
        } catch (Exception e) {
            sendErrorResponse(action, transportChannel, e);
        }
    }
}
Also used : Version(org.opensearch.Version) TransportAddress(org.opensearch.common.transport.TransportAddress) StreamInput(org.opensearch.common.io.stream.StreamInput) ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 88 with Version

use of org.opensearch.Version in project OpenSearch by opensearch-project.

the class OutboundHandler method sendResponse.

/**
 * Sends the response to the given channel. This method should be used to send {@link TransportResponse}
 * objects back to the caller.
 *
 * @see #sendErrorResponse(Version, Set, TcpChannel, long, String, Exception) for sending error responses
 */
void sendResponse(final Version nodeVersion, final Set<String> features, final TcpChannel channel, final long requestId, final String action, final TransportResponse response, final boolean compress, final boolean isHandshake) throws IOException {
    Version version = Version.min(this.version, nodeVersion);
    OutboundMessage.Response message = new OutboundMessage.Response(threadPool.getThreadContext(), features, response, version, requestId, isHandshake, compress);
    ActionListener<Void> listener = ActionListener.wrap(() -> messageListener.onResponseSent(requestId, action, response));
    sendMessage(channel, message, listener);
}
Also used : Version(org.opensearch.Version)

Example 89 with Version

use of org.opensearch.Version in project OpenSearch by opensearch-project.

the class SniffConnectionStrategy method collectRemoteNodes.

private void collectRemoteNodes(Iterator<Supplier<DiscoveryNode>> seedNodes, ActionListener<Void> listener) {
    if (Thread.currentThread().isInterrupted()) {
        listener.onFailure(new InterruptedException("remote connect thread got interrupted"));
        return;
    }
    if (seedNodes.hasNext()) {
        final Consumer<Exception> onFailure = e -> {
            if (e instanceof ConnectTransportException || e instanceof IOException || e instanceof IllegalStateException) {
                // ISE if we fail the handshake with an version incompatible node
                if (seedNodes.hasNext()) {
                    logger.debug(() -> new ParameterizedMessage("fetching nodes from external cluster [{}] failed moving to next seed node", clusterAlias), e);
                    collectRemoteNodes(seedNodes, listener);
                    return;
                }
            }
            logger.warn(new ParameterizedMessage("fetching nodes from external cluster [{}] failed", clusterAlias), e);
            listener.onFailure(e);
        };
        final DiscoveryNode seedNode = seedNodes.next().get();
        logger.trace("[{}] opening transient connection to seed node: [{}]", clusterAlias, seedNode);
        final StepListener<Transport.Connection> openConnectionStep = new StepListener<>();
        try {
            connectionManager.openConnection(seedNode, null, openConnectionStep);
        } catch (Exception e) {
            onFailure.accept(e);
        }
        final StepListener<TransportService.HandshakeResponse> handshakeStep = new StepListener<>();
        openConnectionStep.whenComplete(connection -> {
            ConnectionProfile connectionProfile = connectionManager.getConnectionProfile();
            transportService.handshake(connection, connectionProfile.getHandshakeTimeout().millis(), getRemoteClusterNamePredicate(), handshakeStep);
        }, onFailure);
        final StepListener<Void> fullConnectionStep = new StepListener<>();
        handshakeStep.whenComplete(handshakeResponse -> {
            final DiscoveryNode handshakeNode = handshakeResponse.getDiscoveryNode();
            if (nodePredicate.test(handshakeNode) && shouldOpenMoreConnections()) {
                logger.trace("[{}] opening managed connection to seed node: [{}] proxy address: [{}]", clusterAlias, handshakeNode, proxyAddress);
                final DiscoveryNode handshakeNodeWithProxy = maybeAddProxyAddress(proxyAddress, handshakeNode);
                connectionManager.connectToNode(handshakeNodeWithProxy, null, transportService.connectionValidator(handshakeNodeWithProxy), fullConnectionStep);
            } else {
                fullConnectionStep.onResponse(null);
            }
        }, e -> {
            final Transport.Connection connection = openConnectionStep.result();
            final DiscoveryNode node = connection.getNode();
            logger.debug(() -> new ParameterizedMessage("[{}] failed to handshake with seed node: [{}]", clusterAlias, node), e);
            IOUtils.closeWhileHandlingException(connection);
            onFailure.accept(e);
        });
        fullConnectionStep.whenComplete(aVoid -> {
            if (remoteClusterName.get() == null) {
                TransportService.HandshakeResponse handshakeResponse = handshakeStep.result();
                assert handshakeResponse.getClusterName().value() != null;
                remoteClusterName.set(handshakeResponse.getClusterName());
            }
            final Transport.Connection connection = openConnectionStep.result();
            ClusterStateRequest request = new ClusterStateRequest();
            request.clear();
            request.nodes(true);
            // here we pass on the connection since we can only close it once the sendRequest returns otherwise
            // due to the async nature (it will return before it's actually sent) this can cause the request to fail
            // due to an already closed connection.
            ThreadPool threadPool = transportService.getThreadPool();
            ThreadContext threadContext = threadPool.getThreadContext();
            TransportService.ContextRestoreResponseHandler<ClusterStateResponse> responseHandler = new TransportService.ContextRestoreResponseHandler<>(threadContext.newRestorableContext(false), new SniffClusterStateResponseHandler(connection, listener, seedNodes));
            try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
                // we stash any context here since this is an internal execution and should not leak any
                // existing context information.
                threadContext.markAsSystemContext();
                transportService.sendRequest(connection, ClusterStateAction.NAME, request, TransportRequestOptions.EMPTY, responseHandler);
            }
        }, e -> {
            final Transport.Connection connection = openConnectionStep.result();
            final DiscoveryNode node = connection.getNode();
            logger.debug(() -> new ParameterizedMessage("[{}] failed to open managed connection to seed node: [{}]", clusterAlias, node), e);
            IOUtils.closeWhileHandlingException(openConnectionStep.result());
            onFailure.accept(e);
        });
    } else {
        listener.onFailure(new NoSeedNodeLeftException(clusterAlias));
    }
}
Also used : Arrays(java.util.Arrays) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) Version(org.opensearch.Version) StreamOutput(org.opensearch.common.io.stream.StreamOutput) Writeable(org.opensearch.common.io.stream.Writeable) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Supplier(java.util.function.Supplier) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Booleans(org.opensearch.common.Booleans) Setting.intSetting(org.opensearch.common.settings.Setting.intSetting) ActionListener(org.opensearch.action.ActionListener) UUIDs(org.opensearch.common.UUIDs) StreamInput(org.opensearch.common.io.stream.StreamInput) Setting(org.opensearch.common.settings.Setting) SetOnce(org.apache.lucene.util.SetOnce) Iterator(java.util.Iterator) ClusterStateAction(org.opensearch.action.admin.cluster.state.ClusterStateAction) Predicate(java.util.function.Predicate) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOUtils(org.opensearch.core.internal.io.IOUtils) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) List(java.util.List) Stream(java.util.stream.Stream) ClusterName(org.opensearch.cluster.ClusterName) StepListener(org.opensearch.action.StepListener) Collections(java.util.Collections) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) ThreadPool(org.opensearch.threadpool.ThreadPool) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) IOException(java.io.IOException) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) StepListener(org.opensearch.action.StepListener)

Example 90 with Version

use of org.opensearch.Version in project OpenSearch by opensearch-project.

the class TransportHandshaker method sendHandshake.

void sendHandshake(long requestId, DiscoveryNode node, TcpChannel channel, TimeValue timeout, ActionListener<Version> listener) {
    numHandshakes.inc();
    final HandshakeResponseHandler handler = new HandshakeResponseHandler(requestId, version, listener);
    pendingHandshakes.put(requestId, handler);
    channel.addCloseListener(ActionListener.wrap(() -> handler.handleLocalException(new TransportException("handshake failed because connection reset"))));
    boolean success = false;
    try {
        // for the request we use the minCompatVersion since we don't know what's the version of the node we talk to
        // we also have no payload on the request but the response will contain the actual version of the node we talk
        // to as the payload.
        Version minCompatVersion = version.minimumCompatibilityVersion();
        if (version.onOrAfter(Version.V_1_0_0) && version.before(Version.V_2_0_0)) {
            // the minCompatibleVersion for OpenSearch 1.x is sent as 6.7.99 instead of 6.8.0
            // as this helps in (indirectly) identifying the remote node version during handle HandshakeRequest itself
            // and then send appropriate version (7.10.2/ OpenSearch 1.x version) in response.
            // The advantage of doing this is early identification of remote node version as otherwise
            // if OpenSearch node also sends 6.8.0, there is no way to differentiate ES 7.x version from
            // OpenSearch version and OpenSearch node will end up sending BC version to both ES & OpenSearch remote node.
            // Sending only BC version to ElasticSearch node provide easy deprecation path for this BC version logic
            // in OpenSearch 2.0.0.
            minCompatVersion = Version.fromId(6079999);
        } else if (version.onOrAfter(Version.V_2_0_0)) {
            minCompatVersion = Version.fromId(7099999);
        }
        handshakeRequestSender.sendRequest(node, channel, requestId, minCompatVersion);
        threadPool.schedule(() -> handler.handleLocalException(new ConnectTransportException(node, "handshake_timeout[" + timeout + "]")), timeout, ThreadPool.Names.GENERIC);
        success = true;
    } catch (Exception e) {
        handler.handleLocalException(new ConnectTransportException(node, "failure to send " + HANDSHAKE_ACTION_NAME, e));
    } finally {
        if (success == false) {
            TransportResponseHandler<?> removed = pendingHandshakes.remove(requestId);
            assert removed == null : "Handshake should not be pending if exception was thrown";
        }
    }
}
Also used : Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) IOException(java.io.IOException) EOFException(java.io.EOFException)

Aggregations

Version (org.opensearch.Version)242 Settings (org.opensearch.common.settings.Settings)86 LegacyESVersion (org.opensearch.LegacyESVersion)84 ArrayList (java.util.ArrayList)59 IOException (java.io.IOException)54 List (java.util.List)54 Map (java.util.Map)50 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)42 Collections (java.util.Collections)39 HashMap (java.util.HashMap)38 ClusterState (org.opensearch.cluster.ClusterState)38 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)37 HashSet (java.util.HashSet)36 BytesReference (org.opensearch.common.bytes.BytesReference)36 TimeValue (org.opensearch.common.unit.TimeValue)36 Set (java.util.Set)35 Collectors (java.util.stream.Collectors)34 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)33 StreamInput (org.opensearch.common.io.stream.StreamInput)32 BytesArray (org.opensearch.common.bytes.BytesArray)30