-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: group versions * Move offsets logic to the service out of the records storage, introduce GroupId * after merge fixes * extract getOffsetsByGroup * update deps * add more tests * key-per-partition map * add `isLatest` to Prometheus exporter
- Loading branch information
Showing
19 changed files
with
524 additions
and
94 deletions.
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
68 changes: 68 additions & 0 deletions
68
api/src/main/java/com/github/bsideup/liiklus/positions/GroupId.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,68 @@ | ||
package com.github.bsideup.liiklus.positions; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Value; | ||
|
||
import java.util.Comparator; | ||
import java.util.Optional; | ||
import java.util.regex.MatchResult; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Value | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class GroupId implements Comparable<GroupId> { | ||
|
||
public static final Comparator<GroupId> COMPARATOR = Comparator | ||
.comparing(GroupId::getName) | ||
.thenComparing(it -> it.getVersion().orElse(0)); | ||
|
||
public static final String VERSION_SEPARATOR = "-v"; | ||
public static final Pattern VERSION_PATTERN = Pattern.compile("^(.*)-v(\\d+)$"); | ||
|
||
public static GroupId of(String name, int version) { | ||
return of(name, Optional.of(version)); | ||
} | ||
|
||
public static GroupId of(String name, Optional<Integer> version) { | ||
if (version.orElse(0) < 0) { | ||
throw new IllegalArgumentException("version must be >= 0"); | ||
} | ||
return new GroupId(name, version.filter(it -> it != 0)); | ||
} | ||
|
||
public static GroupId ofString(String str) { | ||
Matcher matcher = VERSION_PATTERN.matcher(str); | ||
|
||
if (matcher.matches()) { | ||
MatchResult result = matcher.toMatchResult(); | ||
|
||
return GroupId.of( | ||
result.group(1), | ||
Optional.ofNullable(result.group(2)).map(Integer::parseInt) | ||
); | ||
} else { | ||
return GroupId.of( | ||
str, | ||
Optional.empty() | ||
); | ||
} | ||
} | ||
|
||
@NonNull | ||
String name; | ||
|
||
@NonNull | ||
Optional<Integer> version; | ||
|
||
public String asString() { | ||
return name + version.map(it -> VERSION_SEPARATOR + it).orElse(""); | ||
} | ||
|
||
@Override | ||
public int compareTo(GroupId other) { | ||
return COMPARATOR.compare(this, other); | ||
} | ||
} |
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
api/src/test/java/com/github/bsideup/liiklus/positions/GroupIdTest.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 @@ | ||
package com.github.bsideup.liiklus.positions; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(Parameterized.class) | ||
@RequiredArgsConstructor | ||
public class GroupIdTest { | ||
|
||
@Parameterized.Parameters(name = "{index}: {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{"hello", GroupId.of("hello", Optional.empty())}, | ||
{"hello-", GroupId.of("hello-", Optional.empty())}, | ||
{"hello-v", GroupId.of("hello-v", Optional.empty())}, | ||
{"hello-v-v", GroupId.of("hello-v-v", Optional.empty())}, | ||
|
||
{"hello-v1", GroupId.of("hello", Optional.of(1))}, | ||
{"hello-v100", GroupId.of("hello", Optional.of(100))}, | ||
|
||
{"hello-v10-v5", GroupId.of("hello-v10", Optional.of(5))}, | ||
|
||
{"hello-v-1", GroupId.of("hello-v-1", Optional.empty())}, | ||
{"hello-v10-alpha", GroupId.of("hello-v10-alpha", Optional.empty())}, | ||
}); | ||
} | ||
|
||
final String string; | ||
|
||
final GroupId object; | ||
|
||
@Test | ||
public void testParsing() { | ||
assertThat(GroupId.ofString(string)).isEqualTo(object); | ||
} | ||
|
||
@Test | ||
public void testStringRepresentation() { | ||
assertThat(object.asString()).isEqualTo(string); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
api/src/test/java/com/github/bsideup/liiklus/positions/GroupIdValidationTest.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,24 @@ | ||
package com.github.bsideup.liiklus.positions; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class GroupIdValidationTest { | ||
|
||
@Test | ||
public void testWrongExplicitVersion() { | ||
assertThatThrownBy( | ||
() -> GroupId.of("test", -1) | ||
).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void testWrongExplicitOptionalVersion() { | ||
assertThatThrownBy( | ||
() -> GroupId.of("test", Optional.of(-1)) | ||
).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
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.