-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
LogsDB data generator - support nested object field #111206
Merged
lkts
merged 2 commits into
elastic:main
from
lkts:logs_testing_mapping_generation_fields
Jul 24, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
test/framework/src/main/java/org/elasticsearch/logsdb/datageneration/fields/Context.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.logsdb.datageneration.fields; | ||
|
||
import org.elasticsearch.logsdb.datageneration.DataGeneratorSpecification; | ||
|
||
class Context { | ||
private final DataGeneratorSpecification specification; | ||
private final int objectDepth; | ||
private final int nestedFieldsCount; | ||
|
||
Context(DataGeneratorSpecification specification) { | ||
this(specification, 0, 0); | ||
} | ||
|
||
private Context(DataGeneratorSpecification specification, int objectDepth, int nestedFieldsCount) { | ||
this.specification = specification; | ||
this.objectDepth = objectDepth; | ||
this.nestedFieldsCount = nestedFieldsCount; | ||
} | ||
|
||
public DataGeneratorSpecification specification() { | ||
return specification; | ||
} | ||
|
||
public Context subObject() { | ||
return new Context(specification, objectDepth + 1, nestedFieldsCount); | ||
} | ||
|
||
public Context nestedObject() { | ||
return new Context(specification, objectDepth + 1, nestedFieldsCount + 1); | ||
} | ||
|
||
public boolean shouldAddObjectField() { | ||
return specification.arbitrary().generateSubObject() && objectDepth < specification.maxObjectDepth(); | ||
} | ||
|
||
public boolean shouldAddNestedField() { | ||
return specification.arbitrary().generateNestedObject() | ||
&& objectDepth < specification.maxObjectDepth() | ||
&& nestedFieldsCount < specification.nestedFieldsLimit(); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...va/org/elasticsearch/logsdb/datageneration/fields/GenericSubObjectFieldDataGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.logsdb.datageneration.fields; | ||
|
||
import org.elasticsearch.core.CheckedConsumer; | ||
import org.elasticsearch.logsdb.datageneration.FieldDataGenerator; | ||
import org.elasticsearch.logsdb.datageneration.FieldType; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Generic generator for any type of object field (e.g. "object", "nested"). | ||
*/ | ||
public class GenericSubObjectFieldDataGenerator { | ||
private final Context context; | ||
|
||
private final List<ChildField> childFields; | ||
|
||
public GenericSubObjectFieldDataGenerator(Context context) { | ||
this.context = context; | ||
|
||
childFields = new ArrayList<>(); | ||
generateChildFields(); | ||
} | ||
|
||
public CheckedConsumer<XContentBuilder, IOException> mappingWriter( | ||
CheckedConsumer<XContentBuilder, IOException> customMappingParameters | ||
) { | ||
return b -> { | ||
b.startObject(); | ||
customMappingParameters.accept(b); | ||
|
||
b.startObject("properties"); | ||
for (var childField : childFields) { | ||
b.field(childField.fieldName); | ||
childField.generator.mappingWriter().accept(b); | ||
} | ||
b.endObject(); | ||
|
||
b.endObject(); | ||
}; | ||
} | ||
|
||
public CheckedConsumer<XContentBuilder, IOException> fieldValueGenerator() { | ||
return b -> { | ||
b.startObject(); | ||
|
||
for (var childField : childFields) { | ||
b.field(childField.fieldName); | ||
childField.generator.fieldValueGenerator().accept(b); | ||
} | ||
|
||
b.endObject(); | ||
}; | ||
} | ||
|
||
private void generateChildFields() { | ||
var existingFields = new HashSet<String>(); | ||
// no child fields is legal | ||
var childFieldsCount = context.specification().arbitrary().childFieldCount(0, context.specification().maxFieldCountPerLevel()); | ||
|
||
for (int i = 0; i < childFieldsCount; i++) { | ||
var fieldName = generateFieldName(existingFields); | ||
|
||
if (context.shouldAddObjectField()) { | ||
childFields.add(new ChildField(fieldName, new ObjectFieldDataGenerator(context.subObject()))); | ||
} else if (context.shouldAddNestedField()) { | ||
childFields.add(new ChildField(fieldName, new NestedFieldDataGenerator(context.nestedObject()))); | ||
} else { | ||
var fieldType = context.specification().arbitrary().fieldType(); | ||
addLeafField(fieldType, fieldName); | ||
} | ||
} | ||
} | ||
|
||
private void addLeafField(FieldType type, String fieldName) { | ||
var generator = switch (type) { | ||
case LONG -> new LongFieldDataGenerator(context.specification().arbitrary()); | ||
case KEYWORD -> new KeywordFieldDataGenerator(context.specification().arbitrary()); | ||
}; | ||
|
||
childFields.add(new ChildField(fieldName, generator)); | ||
} | ||
|
||
private String generateFieldName(Set<String> existingFields) { | ||
var fieldName = context.specification().arbitrary().fieldName(1, 10); | ||
while (existingFields.contains(fieldName)) { | ||
fieldName = context.specification().arbitrary().fieldName(1, 10); | ||
} | ||
existingFields.add(fieldName); | ||
|
||
return fieldName; | ||
} | ||
|
||
private record ChildField(String fieldName, FieldDataGenerator generator) {} | ||
} |
33 changes: 33 additions & 0 deletions
33
...rc/main/java/org/elasticsearch/logsdb/datageneration/fields/NestedFieldDataGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.logsdb.datageneration.fields; | ||
|
||
import org.elasticsearch.core.CheckedConsumer; | ||
import org.elasticsearch.logsdb.datageneration.FieldDataGenerator; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
public class NestedFieldDataGenerator implements FieldDataGenerator { | ||
private final GenericSubObjectFieldDataGenerator delegate; | ||
|
||
public NestedFieldDataGenerator(Context context) { | ||
this.delegate = new GenericSubObjectFieldDataGenerator(context); | ||
} | ||
|
||
@Override | ||
public CheckedConsumer<XContentBuilder, IOException> mappingWriter() { | ||
return delegate.mappingWriter(b -> b.field("type", "nested")); | ||
} | ||
|
||
@Override | ||
public CheckedConsumer<XContentBuilder, IOException> fieldValueGenerator() { | ||
return delegate.fieldValueGenerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we give an equal probability of 50% to generating object and nested fields? It currently seems like we prioritize shouldAddObjectField.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is just based on my impression that
nested
is less used in reality. I'll do 10% each.