Search in sources :

Example 76 with Version

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

the class PluginInfo method readFromProperties.

/**
 * Reads the plugin descriptor file.
 *
 * @param path           the path to the root directory for the plugin
 * @return the plugin info
 * @throws IOException if an I/O exception occurred reading the plugin descriptor
 */
public static PluginInfo readFromProperties(final Path path) throws IOException {
    final Path descriptor = path.resolve(OPENSEARCH_PLUGIN_PROPERTIES);
    final Map<String, String> propsMap;
    {
        final Properties props = new Properties();
        try (InputStream stream = Files.newInputStream(descriptor)) {
            props.load(stream);
        }
        propsMap = props.stringPropertyNames().stream().collect(Collectors.toMap(Function.identity(), props::getProperty));
    }
    final String name = propsMap.remove("name");
    if (name == null || name.isEmpty()) {
        throw new IllegalArgumentException("property [name] is missing in [" + descriptor + "]");
    }
    final String description = propsMap.remove("description");
    if (description == null) {
        throw new IllegalArgumentException("property [description] is missing for plugin [" + name + "]");
    }
    final String version = propsMap.remove("version");
    if (version == null) {
        throw new IllegalArgumentException("property [version] is missing for plugin [" + name + "]");
    }
    final String opensearchVersionString = propsMap.remove("opensearch.version");
    if (opensearchVersionString == null) {
        throw new IllegalArgumentException("property [opensearch.version] is missing for plugin [" + name + "]");
    }
    final Version opensearchVersion = Version.fromString(opensearchVersionString);
    final String javaVersionString = propsMap.remove("java.version");
    if (javaVersionString == null) {
        throw new IllegalArgumentException("property [java.version] is missing for plugin [" + name + "]");
    }
    JarHell.checkVersionFormat(javaVersionString);
    final String classname = propsMap.remove("classname");
    if (classname == null) {
        throw new IllegalArgumentException("property [classname] is missing for plugin [" + name + "]");
    }
    final String customFolderNameValue = propsMap.remove("custom.foldername");
    final String customFolderName;
    if (opensearchVersion.onOrAfter(Version.V_1_1_0)) {
        customFolderName = customFolderNameValue;
    } else {
        customFolderName = name;
    }
    final String extendedString = propsMap.remove("extended.plugins");
    final List<String> extendedPlugins;
    if (extendedString == null) {
        extendedPlugins = Collections.emptyList();
    } else {
        extendedPlugins = Arrays.asList(Strings.delimitedListToStringArray(extendedString, ","));
    }
    final String hasNativeControllerValue = propsMap.remove("has.native.controller");
    final boolean hasNativeController;
    if (hasNativeControllerValue == null) {
        hasNativeController = false;
    } else {
        switch(hasNativeControllerValue) {
            case "true":
                hasNativeController = true;
                break;
            case "false":
                hasNativeController = false;
                break;
            default:
                final String message = String.format(Locale.ROOT, "property [%s] must be [%s], [%s], or unspecified but was [%s]", "has_native_controller", "true", "false", hasNativeControllerValue);
                throw new IllegalArgumentException(message);
        }
    }
    if (propsMap.isEmpty() == false) {
        throw new IllegalArgumentException("Unknown properties in plugin descriptor: " + propsMap.keySet());
    }
    return new PluginInfo(name, description, version, opensearchVersion, javaVersionString, classname, customFolderName, extendedPlugins, hasNativeController);
}
Also used : Path(java.nio.file.Path) Version(org.opensearch.Version) InputStream(java.io.InputStream) Properties(java.util.Properties)

Example 77 with Version

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

the class SearchHit method toInnerXContent.

// public because we render hit as part of completion suggestion option
public XContentBuilder toInnerXContent(XContentBuilder builder, Params params) throws IOException {
    // Even if this was included in the inner_hit hits this would be the same, so better leave it out.
    if (getExplanation() != null && shard != null) {
        builder.field(Fields._SHARD, shard.getShardId());
        builder.field(Fields._NODE, shard.getNodeIdText());
    }
    if (index != null) {
        builder.field(Fields._INDEX, RemoteClusterAware.buildRemoteIndexName(clusterAlias, index));
    }
    if (id != null) {
        builder.field(Fields._ID, id);
    }
    if (nestedIdentity != null) {
        nestedIdentity.toXContent(builder, params);
    }
    if (version != -1) {
        builder.field(Fields._VERSION, version);
    }
    if (seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
        builder.field(Fields._SEQ_NO, seqNo);
        builder.field(Fields._PRIMARY_TERM, primaryTerm);
    }
    if (Float.isNaN(score)) {
        builder.nullField(Fields._SCORE);
    } else {
        builder.field(Fields._SCORE, score);
    }
    for (DocumentField field : metaFields.values()) {
        // ignore empty metadata fields
        if (field.getValues().size() == 0) {
            continue;
        }
        // TODO: can we avoid having an exception here?
        if (field.getName().equals(IgnoredFieldMapper.NAME)) {
            builder.field(field.getName(), field.getValues());
        } else {
            builder.field(field.getName(), field.<Object>getValue());
        }
    }
    if (source != null) {
        XContentHelper.writeRawField(SourceFieldMapper.NAME, source, builder, params);
    }
    if (documentFields.isEmpty() == false && // ignore fields all together if they are all empty
    documentFields.values().stream().anyMatch(df -> df.getValues().size() > 0)) {
        builder.startObject(Fields.FIELDS);
        for (DocumentField field : documentFields.values()) {
            if (field.getValues().size() > 0) {
                field.toXContent(builder, params);
            }
        }
        builder.endObject();
    }
    if (highlightFields != null && !highlightFields.isEmpty()) {
        builder.startObject(Fields.HIGHLIGHT);
        for (HighlightField field : highlightFields.values()) {
            field.toXContent(builder, params);
        }
        builder.endObject();
    }
    sortValues.toXContent(builder, params);
    if (matchedQueries.length > 0) {
        builder.startArray(Fields.MATCHED_QUERIES);
        for (String matchedFilter : matchedQueries) {
            builder.value(matchedFilter);
        }
        builder.endArray();
    }
    if (getExplanation() != null) {
        builder.field(Fields._EXPLANATION);
        buildExplanation(builder, getExplanation());
    }
    if (innerHits != null) {
        builder.startObject(Fields.INNER_HITS);
        for (Map.Entry<String, SearchHits> entry : innerHits.entrySet()) {
            builder.startObject(entry.getKey());
            entry.getValue().toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
    }
    return builder;
}
Also used : RemoteClusterAware(org.opensearch.transport.RemoteClusterAware) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) Arrays(java.util.Arrays) ConstructingObjectParser(org.opensearch.common.xcontent.ConstructingObjectParser) ToXContentFragment(org.opensearch.common.xcontent.ToXContentFragment) IgnoredFieldMapper(org.opensearch.index.mapper.IgnoredFieldMapper) Version(org.opensearch.Version) Strings(org.opensearch.common.Strings) XContentParser(org.opensearch.common.xcontent.XContentParser) SourceLookup(org.opensearch.search.lookup.SourceLookup) MapperService(org.opensearch.index.mapper.MapperService) Token(org.opensearch.common.xcontent.XContentParser.Token) Map(java.util.Map) ParsingException(org.opensearch.common.ParsingException) Explanation(org.apache.lucene.search.Explanation) Lucene.readExplanation(org.opensearch.common.lucene.Lucene.readExplanation) OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) OriginalIndices(org.opensearch.action.OriginalIndices) Nullable(org.opensearch.common.Nullable) Objects(java.util.Objects) List(java.util.List) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) ObjectParser(org.opensearch.common.xcontent.ObjectParser) ValueType(org.opensearch.common.xcontent.ObjectParser.ValueType) BytesReference(org.opensearch.common.bytes.BytesReference) HighlightField(org.opensearch.search.fetch.subphase.highlight.HighlightField) StreamOutput(org.opensearch.common.io.stream.StreamOutput) HashMap(java.util.HashMap) ParseField(org.opensearch.common.ParseField) Writeable(org.opensearch.common.io.stream.Writeable) ConstructingObjectParser.constructorArg(org.opensearch.common.xcontent.ConstructingObjectParser.constructorArg) ArrayList(java.util.ArrayList) LegacyESVersion(org.opensearch.LegacyESVersion) CompressorFactory(org.opensearch.common.compress.CompressorFactory) Collections.singletonMap(java.util.Collections.singletonMap) ConstructingObjectParser.optionalConstructorArg(org.opensearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg) Lucene.writeExplanation(org.opensearch.common.lucene.Lucene.writeExplanation) StreamInput(org.opensearch.common.io.stream.StreamInput) Collections.emptyMap(java.util.Collections.emptyMap) XContentParserUtils.ensureFieldName(org.opensearch.common.xcontent.XContentParserUtils.ensureFieldName) Iterator(java.util.Iterator) IOException(java.io.IOException) DocumentField(org.opensearch.common.document.DocumentField) SourceFieldMapper(org.opensearch.index.mapper.SourceFieldMapper) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) ShardId(org.opensearch.index.shard.ShardId) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) Collections(java.util.Collections) Text(org.opensearch.common.text.Text) DocumentField(org.opensearch.common.document.DocumentField) HighlightField(org.opensearch.search.fetch.subphase.highlight.HighlightField) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 78 with Version

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

the class MainResponseTests method createTestInstance.

@Override
protected MainResponse createTestInstance() {
    String clusterUuid = randomAlphaOfLength(10);
    ClusterName clusterName = new ClusterName(randomAlphaOfLength(10));
    String nodeName = randomAlphaOfLength(10);
    final String date = new Date(randomNonNegativeLong()).toString();
    Version version = VersionUtils.randomVersionBetween(random(), LegacyESVersion.V_7_0_0, Version.CURRENT);
    Build build = new Build(Build.Type.UNKNOWN, randomAlphaOfLength(8), date, randomBoolean(), version.toString(), version.onOrAfter(Version.V_1_0_0) ? randomAlphaOfLength(10) : "");
    return new MainResponse(nodeName, version, clusterName, clusterUuid, build);
}
Also used : Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) Build(org.opensearch.Build) ClusterName(org.opensearch.cluster.ClusterName) Date(java.util.Date)

Example 79 with Version

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

the class TransportBulkActionIndicesThatCannotBeCreatedTests method indicesThatCannotBeCreatedTestCase.

private void indicesThatCannotBeCreatedTestCase(Set<String> expected, BulkRequest bulkRequest, Function<String, Boolean> shouldAutoCreate) {
    ClusterService clusterService = mock(ClusterService.class);
    ClusterState state = mock(ClusterState.class);
    when(state.getMetadata()).thenReturn(Metadata.EMPTY_METADATA);
    when(state.metadata()).thenReturn(Metadata.EMPTY_METADATA);
    when(clusterService.state()).thenReturn(state);
    DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
    when(state.getNodes()).thenReturn(discoveryNodes);
    when(discoveryNodes.getMinNodeVersion()).thenReturn(VersionUtils.randomCompatibleVersion(random(), Version.CURRENT));
    DiscoveryNode localNode = mock(DiscoveryNode.class);
    when(clusterService.localNode()).thenReturn(localNode);
    when(localNode.isIngestNode()).thenReturn(randomBoolean());
    final ThreadPool threadPool = mock(ThreadPool.class);
    final ExecutorService direct = OpenSearchExecutors.newDirectExecutorService();
    when(threadPool.executor(anyString())).thenReturn(direct);
    TransportBulkAction action = new TransportBulkAction(threadPool, mock(TransportService.class), clusterService, null, null, null, mock(ActionFilters.class), null, null, new IndexingPressureService(Settings.EMPTY, new ClusterService(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), null)), new SystemIndices(emptyMap())) {

        @Override
        void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
            assertEquals(expected, indicesThatCannotBeCreated.keySet());
        }

        @Override
        boolean needToCheck() {
            // Use "null" to mean "no indices can be created so don't bother checking"
            return null != shouldAutoCreate;
        }

        @Override
        boolean shouldAutoCreate(String index, ClusterState state) {
            return shouldAutoCreate.apply(index);
        }

        @Override
        void createIndex(String index, TimeValue timeout, Version minNodeVersion, ActionListener<CreateIndexResponse> listener) {
            // If we try to create an index just immediately assume it worked
            listener.onResponse(new CreateIndexResponse(true, true, index) {
            });
        }
    };
    action.doExecute(null, bulkRequest, null);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexingPressureService(org.opensearch.index.IndexingPressureService) Task(org.opensearch.tasks.Task) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionFilters(org.opensearch.action.support.ActionFilters) Mockito.anyString(org.mockito.Mockito.anyString) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) Version(org.opensearch.Version) ExecutorService(java.util.concurrent.ExecutorService) SystemIndices(org.opensearch.indices.SystemIndices) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) TimeValue(org.opensearch.common.unit.TimeValue)

Example 80 with Version

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

the class FeatureAwareTests method testMissingFeature.

public void testMissingFeature() {
    final Version version = VersionUtils.randomVersion(random());
    final Version afterVersion = randomVersionBetween(random(), version, Version.CURRENT);
    final Custom custom = new RequiredFeatureCustom(version);
    // the feature is missing
    final BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(afterVersion);
    assertTrue(FeatureAware.shouldSerialize(out, custom));
}
Also used : Version(org.opensearch.Version) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

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