-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
ingest: Introduction of a bytes processor #31733
Merged
Merged
Changes from 1 commit
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
61 changes: 61 additions & 0 deletions
61
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/BytesProcessor.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,61 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.common.unit.ByteSizeValue; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Processor that converts the content of string fields to the byte value. | ||
* Throws exception is the field is not of type string or can not convert to the numeric byte value | ||
*/ | ||
|
||
public final class BytesProcessor extends AbstractStringProcessor { | ||
|
||
public static final String TYPE = "bytes"; | ||
|
||
BytesProcessor(String processorTag, String field, boolean ignoreMissing, String targetField) { | ||
super(processorTag, field, ignoreMissing, targetField); | ||
} | ||
|
||
@Override | ||
protected Long process(String value) { | ||
return ByteSizeValue.parseBytesSizeValue(value, null, getField()).getBytes(); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public static final class Factory extends AbstractStringProcessor.Factory { | ||
|
||
public Factory() { | ||
super(TYPE); | ||
} | ||
|
||
@Override | ||
protected BytesProcessor newProcessor(String tag, Map<String, Object> config, String field, | ||
boolean ignoreMissing, String targetField) { | ||
return new BytesProcessor(tag, field, ignoreMissing, targetField); | ||
} | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...gest-common/src/test/java/org/elasticsearch/ingest/common/BytesProcessorFactoryTests.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,27 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
public class BytesProcessorFactoryTests extends AbstractStringProcessorFactoryTestCase { | ||
@Override | ||
protected AbstractStringProcessor.Factory newFactory() { | ||
return new BytesProcessor.Factory(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/BytesProcessorTests.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,98 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.common.unit.ByteSizeUnit; | ||
import org.elasticsearch.common.unit.ByteSizeValue; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.Processor; | ||
import org.elasticsearch.ingest.RandomDocumentPicks; | ||
import org.hamcrest.CoreMatchers; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class BytesProcessorTests extends AbstractStringProcessorTestCase { | ||
|
||
private String modifiedInput; | ||
|
||
@Override | ||
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) { | ||
return new BytesProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField); | ||
} | ||
|
||
@Override | ||
protected String modifyInput(String input) { | ||
//largest value that allows all results < Long.MAX_VALUE bytes | ||
long randomNumber = randomLongBetween(1, Long.MAX_VALUE / ByteSizeUnit.PB.toBytes(1)); | ||
ByteSizeUnit randomUnit = randomFrom(ByteSizeUnit.values()); | ||
modifiedInput = randomNumber + randomUnit.getSuffix(); | ||
return modifiedInput; | ||
} | ||
|
||
@Override | ||
protected Long expectedResult(String input) { | ||
return ByteSizeValue.parseBytesSizeValue(modifiedInput, null, "").getBytes(); | ||
} | ||
|
||
@Override | ||
protected Class<Long> expectedResultType() { | ||
return Long.class; | ||
} | ||
|
||
public void testTooLarge() { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "8912pb"); | ||
Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); | ||
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); | ||
assertThat(exception.getMessage(), | ||
CoreMatchers.equalTo("failed to parse setting [" + fieldName + "] with value [8912pb] as a size in bytes")); | ||
assertThat(exception.getCause().getMessage(), | ||
CoreMatchers.containsString("Values greater than 9223372036854775807 bytes are not supported")); | ||
} | ||
|
||
public void testNotBytes() { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "junk"); | ||
Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); | ||
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); | ||
assertThat(exception.getMessage(), | ||
CoreMatchers.equalTo("failed to parse [junk]")); | ||
} | ||
|
||
public void testMissingUnits() { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "1"); | ||
Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); | ||
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); | ||
assertThat(exception.getMessage(), | ||
CoreMatchers.containsString("unit is missing or unrecognized")); | ||
} | ||
|
||
public void testFractional() throws Exception { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "1.1kb"); | ||
Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); | ||
processor.execute(ingestDocument); | ||
assertThat(ingestDocument.getFieldValue(fieldName, expectedResultType()), equalTo(1126L)); | ||
assertWarnings("Fractional bytes values are deprecated. Use non-fractional bytes values instead: [1.1kb] found for setting " + | ||
"[" + fieldName + "]"); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/180_bytes_processor.yml
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,42 @@ | ||
--- | ||
teardown: | ||
- do: | ||
ingest.delete_pipeline: | ||
id: "my_pipeline" | ||
ignore: 404 | ||
|
||
--- | ||
"Test bytes processor": | ||
- do: | ||
ingest.put_pipeline: | ||
id: "my_pipeline" | ||
body: > | ||
{ | ||
"description": "_description", | ||
"processors": [ | ||
{ | ||
"bytes" : { | ||
"field" : "bytes_source_field", | ||
"target_field" : "bytes_target_field" | ||
} | ||
} | ||
] | ||
} | ||
- match: { acknowledged: true } | ||
|
||
- do: | ||
index: | ||
index: test | ||
type: test | ||
id: 1 | ||
pipeline: "my_pipeline" | ||
body: {bytes_source_field: "1kb"} | ||
|
||
- do: | ||
get: | ||
index: test | ||
type: test | ||
id: 1 | ||
- match: { _source.bytes_source_field: "1kb" } | ||
- match: { _source.bytes_target_field: 1024 } | ||
|
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.
nit: unneeded newline?
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.
good eye! fixed.