-
Notifications
You must be signed in to change notification settings - Fork 188
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
Github-issue#1048 : s3 object index. #2586
Merged
chenqi0805
merged 4 commits into
opensearch-project:main
from
deepaksahu562:s3-object-index
Apr 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
12a8618
Github-issue#1048 : s3 object index.
deepaksahu562 ab5d600
Github-issue#1048 Incorporate review comments.
deepaksahu562 229d2d6
Merge branch 'opensearch-project:main' into s3-object-index
deepaksahu562 5188573
Github-issue#1048 Incorporate review comments.
deepaksahu562 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
135 changes: 135 additions & 0 deletions
135
...ins/common/src/main/java/org/opensearch/dataprepper/plugins/s3keyindex/S3ObjectIndex.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,135 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.s3keyindex; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Set; | ||
import java.util.TimeZone; | ||
import java.util.UUID; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Class responsible for creation of s3 key pattern based on date time stamp | ||
*/ | ||
public class S3ObjectIndex { | ||
|
||
private static final String TIME_PATTERN_STARTING_SYMBOLS = "\\%{"; | ||
|
||
// For matching a string that begins with a "%{" and ends with a "}". | ||
// For a string like "data-prepper-%{yyyy-MM-dd}", "%{yyyy-MM-dd}" is matched. | ||
private static final String TIME_PATTERN_REGULAR_EXPRESSION = "\\%\\{.*?\\}"; | ||
|
||
// For matching a string enclosed by "%{" and "}". | ||
// For a string like "data-prepper-%{yyyy-MM}", "yyyy-MM" is matched. | ||
private static final String TIME_PATTERN_INTERNAL_EXTRACTOR_REGULAR_EXPRESSION = "\\%\\{(.*?)\\}"; | ||
|
||
private static final ZoneId UTC_ZONE_ID = ZoneId.of(TimeZone.getTimeZone("UTC").getID()); | ||
|
||
S3ObjectIndex() { | ||
} | ||
|
||
/** | ||
* Create Object Name with date,time and UniqueID prepended. | ||
*/ | ||
public static String getObjectNameWithDateTimeId(final String indexAlias) { | ||
DateTimeFormatter dateFormatter = getDatePatternFormatter(indexAlias); | ||
String suffix = (dateFormatter != null) ? dateFormatter.format(getCurrentUtcTime()) : ""; | ||
return indexAlias.replaceAll(TIME_PATTERN_REGULAR_EXPRESSION, "") + suffix + "-" + getTimeNanos() + "-" | ||
+ UUID.randomUUID(); | ||
} | ||
|
||
/** | ||
* Create Object path prefix. | ||
*/ | ||
public static String getObjectPathPrefix(final String indexAlias) { | ||
DateTimeFormatter dateFormatter = getDatePatternFormatter(indexAlias); | ||
String suffix = (dateFormatter != null) ? dateFormatter.format(getCurrentUtcTime()) : ""; | ||
return indexAlias.replaceAll(TIME_PATTERN_REGULAR_EXPRESSION, "") + suffix; | ||
} | ||
|
||
/** | ||
* Creates epoch seconds. | ||
*/ | ||
public static long getTimeNanos() { | ||
Instant time = Instant.now(); | ||
final long NANO_MULTIPLIER = 1_000 * 1_000 * 1_000; | ||
long currentTimeNanos = time.getEpochSecond() * NANO_MULTIPLIER + time.getNano(); | ||
return currentTimeNanos; | ||
} | ||
|
||
/** | ||
* Validate the index with the regular expression pattern. Throws exception if validation fails | ||
*/ | ||
public static DateTimeFormatter getDatePatternFormatter(final String indexAlias) { | ||
final Pattern pattern = Pattern.compile(TIME_PATTERN_INTERNAL_EXTRACTOR_REGULAR_EXPRESSION); | ||
final Matcher timePatternMatcher = pattern.matcher(indexAlias); | ||
if (timePatternMatcher.find()) { | ||
final String timePattern = timePatternMatcher.group(1); | ||
if (timePatternMatcher.find()) { // check if there is a one more match. | ||
throw new IllegalArgumentException("An index only allows one date-time pattern."); | ||
} | ||
if (timePattern.contains(TIME_PATTERN_STARTING_SYMBOLS)) { // check if it is a nested pattern such as | ||
// "data-prepper-%{%{yyyy.MM.dd}}" | ||
throw new IllegalArgumentException("An index doesn't allow nested date-time patterns."); | ||
} | ||
validateTimePatternIsAtTheEnd(indexAlias, timePattern); | ||
validateNoSpecialCharsInTimePattern(timePattern); | ||
validateTimePatternGranularity(timePattern); | ||
return DateTimeFormatter.ofPattern(timePattern); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Data Prepper only allows time pattern as a suffix. | ||
*/ | ||
private static void validateTimePatternIsAtTheEnd(final String indexAlias, final String timePattern) { | ||
if (!indexAlias.endsWith(timePattern + "}")) { | ||
throw new IllegalArgumentException("Time pattern can only be a suffix of an index."); | ||
} | ||
} | ||
|
||
/** | ||
* Special characters can cause failures in creating indexes. | ||
*/ | ||
private static final Set<Character> INVALID_CHARS = Set.of('#', '\\', '/', '*', '?', '"', '<', '>', '|', ',', ':'); | ||
|
||
public static void validateNoSpecialCharsInTimePattern(String timePattern) { | ||
boolean containsInvalidCharacter = timePattern.chars().mapToObj(c -> (char) c) | ||
.anyMatch(character -> INVALID_CHARS.contains(character)); | ||
if (containsInvalidCharacter) { | ||
throw new IllegalArgumentException( | ||
"Index time pattern contains one or multiple special characters: " + INVALID_CHARS); | ||
} | ||
} | ||
|
||
/** | ||
* Validates the time pattern, support creating indexes with time patterns that are too granular | ||
* hour, minute and second | ||
*/ | ||
private static final Set<Character> UNSUPPORTED_TIME_GRANULARITY_CHARS = Set.of('A', 'n', 'N'); | ||
|
||
public static void validateTimePatternGranularity(String timePattern) { | ||
boolean containsUnsupportedTimeSymbol = timePattern.chars().mapToObj(c -> (char) c) | ||
.anyMatch(character -> UNSUPPORTED_TIME_GRANULARITY_CHARS.contains(character)); | ||
if (containsUnsupportedTimeSymbol) { | ||
throw new IllegalArgumentException("Index time pattern contains time patterns that are less than one hour: " | ||
+ UNSUPPORTED_TIME_GRANULARITY_CHARS); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the current UTC Date and Time | ||
*/ | ||
public static ZonedDateTime getCurrentUtcTime() { | ||
return LocalDateTime.now().atZone(ZoneId.systemDefault()).withZoneSameInstant(UTC_ZONE_ID); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...common/src/test/java/org/opensearch/dataprepper/plugins/s3keyindex/S3ObjectIndexTest.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,116 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.s3keyindex; | ||
|
||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class S3ObjectIndexTest { | ||
|
||
@Test | ||
void testObjectDateTimePatterns_not_equal() throws IllegalArgumentException { | ||
|
||
String expectedIndex = S3ObjectIndex.getObjectNameWithDateTimeId("events-%{yyyy-MM-dd}"); | ||
String actualIndex = S3ObjectIndex.getObjectNameWithDateTimeId("events-%{yyyy-MM-dd}"); | ||
assertFalse(actualIndex.contains(expectedIndex)); | ||
} | ||
|
||
@Test | ||
void testgetObjectPathPrefix_not_equal() throws IllegalArgumentException { | ||
|
||
String expectedIndex = S3ObjectIndex.getObjectPathPrefix("events-%{yyyy}"); | ||
String actualIndex = S3ObjectIndex.getObjectPathPrefix("events-%{yyyy}"); | ||
assertTrue(actualIndex.contains(expectedIndex)); | ||
} | ||
|
||
@Test | ||
void testObjectTimePattern_Exceptional_time_TooGranular() throws IllegalArgumentException { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
S3ObjectIndex.getDatePatternFormatter("events-%{yyyy-AA-dd}"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testObjectTimePatterns_equal() throws IllegalArgumentException { | ||
|
||
DateTimeFormatter expectedIndex = S3ObjectIndex.getDatePatternFormatter("events-%{yyyy-MM-dd}"); | ||
DateTimeFormatter actualIndex = S3ObjectIndex.getDatePatternFormatter("events-%{yyyy-MM-dd}"); | ||
assertEquals(actualIndex.toString(), expectedIndex.toString()); | ||
} | ||
|
||
@Test | ||
void test_utc_current_time() throws IllegalArgumentException { | ||
|
||
ZonedDateTime expectedIndex = S3ObjectIndex.getCurrentUtcTime(); | ||
ZonedDateTime actualIndex = S3ObjectIndex.getCurrentUtcTime(); | ||
|
||
assertEquals(expectedIndex.getDayOfYear(), actualIndex.getDayOfYear()); | ||
assertEquals(expectedIndex.getDayOfMonth(), actualIndex.getDayOfMonth()); | ||
assertEquals(expectedIndex.getDayOfWeek(), actualIndex.getDayOfWeek()); | ||
} | ||
|
||
@Test | ||
void testObjectTimePattern_Exceptional_TooGranular() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
S3ObjectIndex.getDatePatternFormatter("events-%{yyyy-AA-ddThh:mm}"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testObjectTimePattern_Exceptional_at_theEnd() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
S3ObjectIndex.getDatePatternFormatter("events-%{yyy{MM}dd}"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testObject_allows_one_date_time_pattern_Exceptional() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
S3ObjectIndex.getDatePatternFormatter("events-%{yyyy-MM-dd}-%{yyyy-MM-dd}"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testObject_nested_pattern_Exceptional() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
S3ObjectIndex.getDatePatternFormatter("bucket-name-\\%{\\%{yyyy.MM.dd}}"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testObject_null_time_pattern() throws NullPointerException { | ||
assertNull(S3ObjectIndex.getDatePatternFormatter("bucket-name")); | ||
} | ||
|
||
@Test | ||
void testObjectAliasWithDatePrefix_Exceptional_time_TooGranular() throws IllegalArgumentException { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
S3ObjectIndex.getObjectNameWithDateTimeId("events-%{yyyy-AA-dd}"); | ||
}); | ||
} | ||
|
||
@Test | ||
void testObjectAliasWithDatePrefix_equal() throws IllegalArgumentException { | ||
|
||
String expectedIndex = S3ObjectIndex.getObjectNameWithDateTimeId("events-%{yyyy-MM-dd}"); | ||
String actualIndex = S3ObjectIndex.getObjectNameWithDateTimeId("events-%{yyyy-MM-dd}"); | ||
assertNotEquals(actualIndex.toString(), expectedIndex.toString()); | ||
} | ||
|
||
@Test | ||
void test_default_constructor() { | ||
S3ObjectIndex object = new S3ObjectIndex(); | ||
assertNotNull(object); | ||
} | ||
} |
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.
S3ObjectIndexUtility
may be a more accurate name for this class, since it is performing validations and creation of the s3 key patternThere 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.
Resolved.