Skip to content

Commit

Permalink
Generate the ml namespace (opensearch-project#1158)
Browse files Browse the repository at this point in the history
* Generate ml.register_model_group

Signed-off-by: Thomas Farr <[email protected]>

* Start neural search sample

Signed-off-by: Thomas Farr <[email protected]>

* Re-generate ShardStatistics

Signed-off-by: Thomas Farr <[email protected]>

* Re-generate ShardFailure

Signed-off-by: Thomas Farr <[email protected]>

* Re-generate Result

Signed-off-by: Thomas Farr <[email protected]>

* Re-generate WriteResponseBase

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.delete_model_group

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.register_model

Signed-off-by: Thomas Farr <[email protected]>

* Exclude legacy license from ml namespace

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.get_task

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.delete_task

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.delete_model

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.deploy_model

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.undeploy_model

Signed-off-by: Thomas Farr <[email protected]>

* Complete neural search sample

Signed-off-by: Thomas Farr <[email protected]>

* Generate ml.get_model

Signed-off-by: Thomas Farr <[email protected]>

* Add changelog entry

Signed-off-by: Thomas Farr <[email protected]>

* note

Signed-off-by: Thomas Farr <[email protected]>

* Fix tests

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit c6e61e1)
  • Loading branch information
Xtansia committed Aug 30, 2024
1 parent cd49cec commit ef61281
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 87 deletions.
3 changes: 3 additions & 0 deletions java-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ dependencies {
implementation("org.apache.maven.resolver", "maven-resolver-api", "1.9.20")
implementation("org.apache.maven.resolver", "maven-resolver-supplier", "1.9.20")

// MIT
implementation("org.semver4j", "semver4j", "5.3.0")

// EPL-2.0
testImplementation(platform("org.junit:junit-bom:5.10.3"))
testImplementation("org.junit.jupiter", "junit-jupiter")
Expand Down
43 changes: 36 additions & 7 deletions java-codegen/opensearch-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3887,6 +3887,20 @@ paths:
'200':
$ref: '#/components/responses/ml.undeploy_model@200'
/_plugins/_ml/tasks/{task_id}:
delete:
operationId: ml.delete_task.0
x-operation-group: ml.delete_task
description: Deletes a task.
parameters:
- $ref: '#/components/parameters/ml.delete_task::path.task_id'
- $ref: '#/components/parameters/_global::query.pretty'
- $ref: '#/components/parameters/_global::query.human'
- $ref: '#/components/parameters/_global::query.error_trace'
- $ref: '#/components/parameters/_global::query.source'
- $ref: '#/components/parameters/_global::query.filter_path'
responses:
'200':
$ref: '#/components/responses/ml.delete_task@200'
get:
operationId: ml.get_task.0
x-operation-group: ml.get_task
Expand Down Expand Up @@ -20036,6 +20050,12 @@ components:
required: true
schema:
type: string
ml.delete_task::path.task_id:
name: task_id
in: path
required: true
schema:
type: string
ml.deploy_model::path.model_id:
name: model_id
in: path
Expand Down Expand Up @@ -26139,6 +26159,11 @@ components:
application/json:
schema:
$ref: '#/components/schemas/_common:WriteResponseBase'
ml.delete_task@200:
content:
application/json:
schema:
$ref: '#/components/schemas/_common:WriteResponseBase'
ml.deploy_model@200:
content:
application/json:
Expand Down Expand Up @@ -29286,6 +29311,7 @@ components:
- version_map_memory_in_bytes
_common:SequenceNumber:
type: number
format: int64
_common:ShardFailure:
type: object
properties:
Expand Down Expand Up @@ -29602,6 +29628,7 @@ components:
type: string
_common:VersionNumber:
type: number
format: int64
_common:VersionString:
type: string
_common:VersionType:
Expand Down Expand Up @@ -29671,6 +29698,7 @@ components:
$ref: '#/components/schemas/_common:IndexName'
_primary_term:
type: number
format: int64
result:
$ref: '#/components/schemas/_common:Result'
_seq_no:
Expand Down Expand Up @@ -46333,18 +46361,19 @@ components:
type: string
required:
- state
ml._common:UndeployModelResponse:
type: object
additionalProperties:
$ref: '#/components/schemas/ml._common:UndeployModelResponseModels'
ml._common:UndeployModelResponseModels:
ml._common:UndeployModelNode:
type: object
properties:
stats:
$ref: '#/components/schemas/ml._common:UndeployModelResponseStats'
ml._common:UndeployModelResponseStats:
$ref: '#/components/schemas/ml._common:UndeployModelNodeStats'
ml._common:UndeployModelNodeStats:
type: object
additionalProperties: true
ml._common:UndeployModelResponse:
type: object
additionalProperties:
title: nodes
$ref: '#/components/schemas/ml._common:UndeployModelNode'
nodes._common:AdaptiveSelection:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.opensearch.client.codegen;

import static org.opensearch.client.codegen.model.OperationGroupMatcher.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -26,14 +28,18 @@
import org.opensearch.client.codegen.exceptions.ApiSpecificationParseException;
import org.opensearch.client.codegen.exceptions.RenderException;
import org.opensearch.client.codegen.model.Namespace;
import org.opensearch.client.codegen.model.OperationGroup;
import org.opensearch.client.codegen.model.OperationGroupMatcher;
import org.opensearch.client.codegen.model.ShapeRenderingContext;
import org.opensearch.client.codegen.model.SpecTransformer;
import org.opensearch.client.codegen.openapi.OpenApiSpecification;

public class CodeGenerator {
private static final Logger LOGGER = LogManager.getLogger();
private static final OperationGroup.Matcher OPERATION_MATCHER = OperationGroup.matcher();
private static final OperationGroupMatcher OPERATION_MATCHER = none();
// private static final OperationGroupMatcher OPERATION_MATCHER = or(
// and(namespace(""), named("info")),
// and(namespace("ml"), not(named("search_models"))) // TODO: search_models is complex and ideally should re-use the search structures
// );

public static void main(String[] args) {
var inputOpt = Option.builder("i")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.semver4j.Semver;

public class Deprecation {
@Nullable
private final String description;
@Nullable
private final String version;
private final Semver version;

public Deprecation(@Nullable String description, @Nullable String version) {
public Deprecation(@Nullable String description, @Nullable Semver version) {
this.description = description;
this.version = version;
}
Expand All @@ -29,7 +30,7 @@ public Optional<String> getDescription() {
}

@Nonnull
public Optional<String> getVersion() {
public Optional<Semver> getVersion() {
return Optional.ofNullable(version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public void addShape(Shape shape) {
}

public String getPackageName() {
return parent != null ? parent.getPackageName() + "." + getPackageNamePart() : "org.opensearch.client.opensearch";
return parent != null ? parent.getPackageName() + "." + getName() : "org.opensearch.client.opensearch";
}

private String getPackageNamePart() {
@Nonnull
public String getName() {
return name;
}

Expand All @@ -70,7 +71,7 @@ public Namespace child(@Nullable String name) {

public void render(ShapeRenderingContext ctx) throws RenderException {
for (Namespace child : children.values()) {
child.render(ctx.forSubDir(child.getPackageNamePart()));
child.render(ctx.forSubDir(child.getName()));
}

for (Shape shape : shapes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

package org.opensearch.client.codegen.model;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.builder.EqualsBuilder;
Expand Down Expand Up @@ -76,58 +71,4 @@ public boolean equals(Object o) {
public int hashCode() {
return new HashCodeBuilder(17, 37).append(namespace).append(name).toHashCode();
}

@Nonnull
public static Matcher matcher() {
return new Matcher();
}

public static class Matcher {
private final Set<String> namespaces = new HashSet<>();
private final Set<OperationGroup> operations = new HashSet<>();
private final Collection<Pattern> patterns = new HashSet<>();

private Matcher() {}

@Nonnull
public Matcher add(@Nullable String namespace, @Nullable String... operations) {
if (operations == null || operations.length == 0) {
namespaces.add(Strings.requireNonBlank(namespace, "namespace must not be blank"));
} else {
for (String operation : operations) {
add(new OperationGroup(namespace, operation));
}
}
return this;
}

@Nonnull
public Matcher add(@Nonnull OperationGroup operation) {
operations.add(Objects.requireNonNull(operation, "operation must not be null"));
return this;
}

@Nonnull
public Matcher add(@Nonnull Pattern pattern) {
patterns.add(Objects.requireNonNull(pattern, "pattern must not be null"));
return this;
}

public boolean matches(@Nonnull OperationGroup operation) {
Objects.requireNonNull(operation, "operation must not be null");
if (operation.getNamespace().map(namespaces::contains).orElse(false)) {
return true;
}
if (operations.contains(operation)) {
return true;
}
var str = operation.toString();
for (Pattern pattern : patterns) {
if (pattern.matcher(str).matches()) {
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -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.client.codegen.model;

import java.util.Arrays;
import java.util.HashSet;
import javax.annotation.Nonnull;

@FunctionalInterface
public interface OperationGroupMatcher {
boolean matches(OperationGroup group);

static OperationGroupMatcher all() {
return group -> true;
}

static OperationGroupMatcher none() {
return group -> false;
}

static OperationGroupMatcher not(OperationGroupMatcher matcher) {
return group -> !matcher.matches(group);
}

static OperationGroupMatcher or(OperationGroupMatcher... matchers) {
return group -> {
for (OperationGroupMatcher matcher : matchers) {
if (matcher.matches(group)) {
return true;
}
}
return false;
};
}

static OperationGroupMatcher and(OperationGroupMatcher... matchers) {
return group -> {
for (OperationGroupMatcher matcher : matchers) {
if (!matcher.matches(group)) {
return false;
}
}
return true;
};
}

static OperationGroupMatcher named(String... names) {
var set = new HashSet<>(Arrays.asList(names));
return group -> set.contains(group.getName());
}

static OperationGroupMatcher namespace(@Nonnull String namespace) {
return group -> namespace.equals(group.getNamespace().orElse(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public Collection<Field> getPathParams() {
return pathParams.values();
}

@Override
public void addBodyField(Field field) {
super.addBodyField(field);
addField(field);
}

private void addField(Field field) {
fields.put(field.getName(), field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,8 @@ public Set<String> getImports() {
}
return imports;
}

public boolean needsLegacyLicense() {
return !"ml".equals(parent.getName());
}
}
Loading

0 comments on commit ef61281

Please sign in to comment.