Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding QueryGroup schema #13669

Merged
merged 31 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d417d17
rebase with opensearch/main
kaushalmahi12 Apr 10, 2024
be38bae
add resourceLimitGroupId propagation logic from coordinator to data n…
kaushalmahi12 Apr 12, 2024
8651420
add sandbox schema
kaushalmahi12 Apr 15, 2024
07db8cc
add resourceLimitGroupTests
kaushalmahi12 Apr 16, 2024
f531c53
add resourceLimitGroupMetadata tests
kaushalmahi12 Apr 18, 2024
bfdfb88
run spotlessApply
kaushalmahi12 Apr 18, 2024
7631713
add mode field in ResourceLimitGroup schema
kaushalmahi12 Apr 18, 2024
49b63fa
fix breaking testcases
kaushalmahi12 Apr 19, 2024
0270812
add task cancellation skeleton
kaushalmahi12 Apr 22, 2024
942e142
add multitenant labels in searchSource builder
kaushalmahi12 May 8, 2024
0ca624c
write custom xcontent parser for ResourceLimitGroup
kaushalmahi12 May 14, 2024
163031f
remove unrelated changes
kaushalmahi12 May 14, 2024
c07e5f8
remove non-existing import fro cluster settings
kaushalmahi12 May 14, 2024
098e4df
remove non releated changes
kaushalmahi12 May 14, 2024
fba1dac
add _id as the resourceLimitGroup key
kaushalmahi12 May 15, 2024
7498237
add change to register resource limit group metadata
kaushalmahi12 May 20, 2024
34d22a9
add updatedAt in resource limit group
kaushalmahi12 Jun 3, 2024
a918d35
rename resourceLimitGroup to queryGroup
kaushalmahi12 Jun 13, 2024
a494037
address the comments on PR
kaushalmahi12 Jun 19, 2024
fb58576
rename the mode member var to resiliency mode
kaushalmahi12 Jun 19, 2024
3a1a3ef
address comments
kaushalmahi12 Jun 20, 2024
0da3b68
add change in CHANGELOG
kaushalmahi12 Jun 21, 2024
4508c9d
add tests for custom namedWritable QueryGroupMetadata
kaushalmahi12 Jun 21, 2024
1e45475
structure resourceLimits into an object
kaushalmahi12 Jun 25, 2024
769a46f
add QueryGroup.toXContent test case
kaushalmahi12 Jun 26, 2024
15076eb
Merge branch 'main' into feature/sandbox-schemaPR
kaushalmahi12 Jul 2, 2024
5a7f405
fix precommit errors
kaushalmahi12 Jul 2, 2024
5153a74
fix precommit errors
kaushalmahi12 Jul 2, 2024
c5a70e9
fix assemble errors
kaushalmahi12 Jul 2, 2024
42d3494
fix checkstyle errors
kaushalmahi12 Jul 2, 2024
8a45648
address comments
kaushalmahi12 Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
address comments
Signed-off-by: Kaushal Kumar <ravi.kaushal97@gmail.com>
  • Loading branch information
kaushalmahi12 committed Jul 3, 2024
commit 8a456485c3c3e706f3b12b27bf03e4248218a7ca
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.opensearch.cluster.AbstractDiffable;
import org.opensearch.cluster.Diff;
import org.opensearch.common.UUIDs;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
Expand All @@ -37,10 +37,10 @@
* "updatedAt": 4513232415
* }
*/
@PublicApi(since = "2.15")
@ExperimentalApi
public class QueryGroup extends AbstractDiffable<QueryGroup> implements ToXContentObject {

public static final int MAX_CHARS_ALLOWED_IN_NAME = 50;
private static final int MAX_CHARS_ALLOWED_IN_NAME = 50;
private final String name;
private final String _id;
private final ResiliencyMode resiliencyMode;
Expand All @@ -49,8 +49,8 @@
private final Map<ResourceType, Object> resourceLimits;

public QueryGroup(String name, ResiliencyMode resiliencyMode, Map<ResourceType, Object> resourceLimits) {
this(name, UUIDs.randomBase64UUID(), resiliencyMode, resourceLimits, Instant.now().getMillis());
}

Check warning on line 53 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L52-L53

Added lines #L52 - L53 were not covered by tests

public QueryGroup(String name, String _id, ResiliencyMode resiliencyMode, Map<ResourceType, Object> resourceLimits, long updatedAt) {
Objects.requireNonNull(name, "QueryGroup.name can't be null");
Expand All @@ -59,7 +59,7 @@
Objects.requireNonNull(_id, "QueryGroup._id can't be null");

if (name.length() > MAX_CHARS_ALLOWED_IN_NAME) {
throw new IllegalArgumentException("QueryGroup.name shouldn't be more than 50 chars long");

Check warning on line 62 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L62

Added line #L62 was not covered by tests
}

if (resourceLimits.isEmpty()) {
Expand All @@ -67,7 +67,7 @@
}
validateResourceLimits(resourceLimits);
if (!isValid(updatedAt)) {
throw new IllegalArgumentException("QueryGroup.updatedAtInMillis is not a valid epoch");

Check warning on line 70 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L70

Added line #L70 was not covered by tests
}

this.name = name;
Expand Down Expand Up @@ -148,13 +148,13 @@
XContentParser.Token token = parser.currentToken();

if (token != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("Expected START_OBJECT token but found [" + parser.currentName() + "]");

Check warning on line 151 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L151

Added line #L151 was not covered by tests
}

String fieldName = "";
kaushalmahi12 marked this conversation as resolved.
Show resolved Hide resolved
// Map to hold resources
final Map<ResourceType, Object> resourceLimits = new HashMap<>();
while ((token = parser.nextToken()) != null) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token.isValue()) {
Expand All @@ -167,12 +167,12 @@
} else if (fieldName.equals("updatedAt")) {
builder.updatedAt(parser.longValue());
} else {
throw new IllegalArgumentException(fieldName + " is not a valid field in QueryGroup");

Check warning on line 170 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L170

Added line #L170 was not covered by tests
}
} else if (token == XContentParser.Token.START_OBJECT) {

if (!fieldName.equals("resourceLimits")) {
throw new IllegalArgumentException(

Check warning on line 175 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L175

Added line #L175 was not covered by tests
"QueryGroup.resourceLimits is an object and expected token was { " + " but found " + token
);
}
Expand Down Expand Up @@ -245,7 +245,7 @@
* ENFORCED - means that it will never breach the assigned limits and will cancel as soon as the limits are breached
* MONITOR - it will not cause any cancellation but just log the eligible task cancellations
*/
@PublicApi(since = "2.15")
@ExperimentalApi
public enum ResiliencyMode {
SOFT("soft"),
ENFORCED("enforced"),
Expand All @@ -266,7 +266,7 @@
if (mode.getName().equalsIgnoreCase(s)) return mode;

}
throw new IllegalArgumentException("Invalid value for QueryGroupMode: " + s);

Check warning on line 269 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroup.java#L269

Added line #L269 was not covered by tests
}

}
Expand All @@ -274,7 +274,7 @@
/**
* Builder class for {@link QueryGroup}
*/
@PublicApi(since = "2.15")
@ExperimentalApi
public static class Builder {
private String name;
private String _id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -47,22 +46,6 @@

private final Map<String, QueryGroup> queryGroups;

@SuppressWarnings("unchecked")
static final ConstructingObjectParser<QueryGroupMetadata, Void> PARSER = new ConstructingObjectParser<>(
"queryGroupsParser",
args -> new QueryGroupMetadata((Map<String, QueryGroup>) args[0])
);

static {
PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> {
Map<String, QueryGroup> queryGroupMap = new HashMap<>();
while (p.nextToken() != XContentParser.Token.END_OBJECT) {
queryGroupMap.put(p.currentName(), QueryGroup.fromXContent(p));
}
return queryGroupMap;
}, QUERY_GROUP_FIELD);
}

public QueryGroupMetadata(Map<String, QueryGroup> queryGroups) {
this.queryGroups = queryGroups;
}
Expand All @@ -72,7 +55,7 @@
}

public Map<String, QueryGroup> queryGroups() {
return this.queryGroups;

Check warning on line 58 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L58

Added line #L58 was not covered by tests
}

/**
Expand All @@ -80,7 +63,7 @@
*/
@Override
public String getWriteableName() {
return TYPE;

Check warning on line 66 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L66

Added line #L66 was not covered by tests
}

/**
Expand All @@ -88,7 +71,7 @@
*/
@Override
public Version getMinimalSupportedVersion() {
return Version.V_3_0_0;

Check warning on line 74 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L74

Added line #L74 was not covered by tests
}

@Override
Expand All @@ -105,7 +88,29 @@
}
kaushalmahi12 marked this conversation as resolved.
Show resolved Hide resolved

public static QueryGroupMetadata fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
Map<String, QueryGroup> queryGroupMap = new HashMap<>();

if (parser.currentToken() == null) {
parser.nextToken();
}

if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException(
"QueryGroupMetadata.fromXContent was expecting a { token but found : " + parser.currentToken()

Check warning on line 99 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L98-L99

Added lines #L98 - L99 were not covered by tests
);
}
XContentParser.Token token = parser.currentToken();
String fieldName = parser.currentName();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else {
QueryGroup queryGroup = QueryGroup.fromXContent(parser);
queryGroupMap.put(fieldName, queryGroup);
}
}

return new QueryGroupMetadata(queryGroupMap);
}

@Override
Expand All @@ -119,7 +124,7 @@

@Override
public EnumSet<Metadata.XContentContext> context() {
return ALL_CONTEXTS;

Check warning on line 127 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L127

Added line #L127 was not covered by tests
}

@Override
Expand All @@ -137,7 +142,7 @@

@Override
public String toString() {
return Strings.toString(MediaTypeRegistry.JSON, this);

Check warning on line 145 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L145

Added line #L145 was not covered by tests
}

/**
Expand All @@ -164,7 +169,7 @@
*/
@Override
public String getWriteableName() {
return TYPE;

Check warning on line 172 in server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/QueryGroupMetadata.java#L172

Added line #L172 was not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Enum to hold the resource type
*/
@PublicApi(since = "1.3")
@PublicApi(since = "2.x")
kaushalmahi12 marked this conversation as resolved.
Show resolved Hide resolved
public enum ResourceType {
CPU("cpu"),
MEMORY("memory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,46 @@

package org.opensearch.cluster.metadata;

import org.opensearch.cluster.Diff;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.test.AbstractNamedWriteableTestCase;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.ResourceType;
import org.opensearch.test.AbstractDiffableSerializationTestCase;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import static org.opensearch.cluster.metadata.QueryGroupTests.createRandomQueryGroup;

public class QueryGroupMetadataTests extends AbstractNamedWriteableTestCase<QueryGroupMetadata> {
public class QueryGroupMetadataTests extends AbstractDiffableSerializationTestCase<Metadata.Custom> {

public void testToXContent() throws IOException {
long updatedAt = 1720047207;
QueryGroupMetadata queryGroupMetadata = new QueryGroupMetadata(
Map.of(
"ajakgakg983r92_4242",
new QueryGroup(
"test",
"ajakgakg983r92_4242",
QueryGroup.ResiliencyMode.ENFORCED,
Map.of(ResourceType.MEMORY, 0.5),
updatedAt
)
)
);
XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
queryGroupMetadata.toXContent(builder, null);
builder.endObject();
assertEquals(
"{\"ajakgakg983r92_4242\":{\"_id\":\"ajakgakg983r92_4242\",\"name\":\"test\",\"resiliency_mode\":\"enforced\",\"updatedAt\":1720047207,\"resourceLimits\":{\"memory\":0.5}}}",
builder.toString()
);
}

@Override
protected NamedWriteableRegistry getNamedWriteableRegistry() {
Expand All @@ -28,8 +59,25 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() {
}

@Override
protected Class<QueryGroupMetadata> categoryClass() {
return QueryGroupMetadata.class;
protected Metadata.Custom makeTestChanges(Metadata.Custom testInstance) {
final QueryGroup queryGroup = createRandomQueryGroup("asdfakgjwrir23r25");
final QueryGroupMetadata queryGroupMetadata = new QueryGroupMetadata(Map.of(queryGroup.get_id(), queryGroup));
return queryGroupMetadata;
}

@Override
protected Writeable.Reader<Diff<Metadata.Custom>> diffReader() {
return QueryGroupMetadata::readDiffFrom;
}

@Override
protected Metadata.Custom doParseInstance(XContentParser parser) throws IOException {
return QueryGroupMetadata.fromXContent(parser);
}

@Override
protected Writeable.Reader<Metadata.Custom> instanceReader() {
return QueryGroupMetadata::new;
}

@Override
Expand Down
Loading