-
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.
implement an in-memory positions storage (for testing)
- Loading branch information
Showing
6 changed files
with
120 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
plugins { | ||
id "java" | ||
} | ||
|
||
dependencies { | ||
compileOnly 'org.projectlombok:lombok' | ||
compileOnly 'org.springframework.boot:spring-boot-starter' | ||
|
||
compile project(":api") | ||
|
||
compile 'io.projectreactor:reactor-core' | ||
|
||
testCompileOnly 'org.projectlombok:lombok' | ||
} |
68 changes: 68 additions & 0 deletions
68
...s-storage/src/main/java/com/github/bsideup/liiklus/inmemory/InMemoryPositionsStorage.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.inmemory; | ||
|
||
import com.github.bsideup.liiklus.positions.PositionsStorage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Value; | ||
import lombok.experimental.FieldDefaults; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* WARNING: this storage type should only be used for testing and NOT in production | ||
* | ||
*/ | ||
@RequiredArgsConstructor | ||
@FieldDefaults(makeFinal = true) | ||
public class InMemoryPositionsStorage implements PositionsStorage { | ||
|
||
ConcurrentMap<Key, ConcurrentMap<Integer, Long>> storage = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public Publisher<Positions> findAll() { | ||
return Flux.fromIterable(storage.entrySet()) | ||
.map(entry -> new Positions( | ||
entry.getKey().getTopic(), | ||
entry.getKey().getGroupId(), | ||
entry.getValue() | ||
)); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Map<Integer, Long>> fetch(String topic, String groupId, Set<Integer> partitions, Map<Integer, Long> externalPositions) { | ||
ConcurrentMap<Integer, Long> positions = storage.get(Key.of(topic, groupId)); | ||
|
||
if (positions == null) { | ||
return CompletableFuture.completedFuture(externalPositions); | ||
} | ||
|
||
Map<Integer, Long> result = new HashMap<>(); | ||
result.putAll(externalPositions); | ||
result.putAll(positions); | ||
|
||
return CompletableFuture.completedFuture(result); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Void> update(String topic, String groupId, int partition, long position) { | ||
|
||
storage.computeIfAbsent(Key.of(topic, groupId), __ -> new ConcurrentHashMap<>()).put(partition, position); | ||
|
||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
@Value | ||
@RequiredArgsConstructor(staticName = "of") | ||
private static class Key { | ||
String topic; | ||
|
||
String groupId; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...orage/src/main/java/com/github/bsideup/liiklus/inmemory/config/InMemoryConfiguration.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,32 @@ | ||
package com.github.bsideup.liiklus.inmemory.config; | ||
|
||
import com.github.bsideup.liiklus.config.ExporterProfile; | ||
import com.github.bsideup.liiklus.config.GatewayProfile; | ||
import com.github.bsideup.liiklus.inmemory.InMemoryPositionsStorage; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@Configuration | ||
@ExporterProfile | ||
@GatewayProfile | ||
@ConditionalOnProperty(value = "storage.positions.type", havingValue = "MEMORY") | ||
public class InMemoryConfiguration { | ||
|
||
@Bean | ||
InMemoryPositionsStorage inMemoryPositionsStorage() { | ||
log.warn("\n" + | ||
String.format("%0106d", 0).replace("0", "=") + "\n" + | ||
String.format("%0106d", 0).replace("0", "=") + "\n" + | ||
String.format("%0106d", 0).replace("0", "=") + "\n" + | ||
"=== In-memory position storage is used. Please, DO NOT run it in production if you ACK your positions. ===\n" + | ||
String.format("%0106d", 0).replace("0", "=") + "\n" + | ||
String.format("%0106d", 0).replace("0", "=") + "\n" + | ||
String.format("%0106d", 0).replace("0", "=") | ||
); | ||
return new InMemoryPositionsStorage(); | ||
} | ||
|
||
} |
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