-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#10775 Implement Valkey module #11101
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
Open
wwisser
wants to merge
10
commits into
testcontainers:main
Choose a base branch
from
wwisser:feature/valkey-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+474
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
59a0dc4
fix: adjust wrong pinecone description
wwisser ccf3e04
feat: implement ValkeyContainer and test
wwisser c4cde21
feat: implement cli & config support, fix user config expression, ref…
wwisser 1027e33
docs: add module reference to templates
wwisser d52602a
refactor: move normal unit test below container tests; cleanup import
wwisser f04cd7c
refactor: adjust formatting to project standard
wwisser ff3f95f
docs: create valkey module entry
wwisser ef646ba
refactor: don't use lombok for user api
wwisser 9d24f30
refactor: resolve review concerns - shrink try/catch block to cli exe…
wwisser bfeed7d
refactor: make use of StringUtils.isNotEmpty
wwisser 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 hidden or 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 |
---|---|---|
|
@@ -68,6 +68,7 @@ body: | |
- ToxiProxy | ||
- Trino | ||
- Typesense | ||
- Valkey | ||
- Vault | ||
- Weaviate | ||
- YugabyteDB | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -68,6 +68,7 @@ body: | |
- ToxiProxy | ||
- Trino | ||
- Typesense | ||
- Valkey | ||
- Vault | ||
- Weaviate | ||
- YugabyteDB | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -68,6 +68,7 @@ body: | |
- ToxiProxy | ||
- Trino | ||
- Typesense | ||
- Valkey | ||
- Vault | ||
- Weaviate | ||
- YugabyteDB | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
# Valkey | ||
|
||
!!! note This module is INCUBATING. | ||
While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. | ||
See our [contributing guidelines](../contributing.md#incubating-modules) for more information on our incubating modules policy. | ||
|
||
Testcontainers module for [Valkey](https://hub.docker.com/r/valkey/valkey) | ||
|
||
## Valkey's usage examples | ||
|
||
You can start a Valkey container instance from any Java application by using: | ||
|
||
<!--codeinclude--> | ||
[Default Valkey container](../../modules/valkey/src/test/java/org/testcontainers/valkey/ValkeyContainerTest.java) inside_block:container | ||
<!--/codeinclude--> | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Add the following dependency to your `pom.xml`/`build.gradle` file: | ||
|
||
=== "Gradle" | ||
```groovy | ||
testImplementation "org.testcontainers:valkey:{{latest_version}}" | ||
``` | ||
|
||
=== "Maven" | ||
```xml | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>valkey</artifactId> | ||
<version>{{latest_version}}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
description = "Testcontainers :: Valkey" | ||
|
||
dependencies { | ||
api project(':testcontainers') | ||
|
||
testImplementation("io.valkey:valkey-java:5.5.0") | ||
} |
250 changes: 250 additions & 0 deletions
250
modules/valkey/src/main/java/org/testcontainers/valkey/ValkeyContainer.java
This file contains hidden or 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,250 @@ | ||
package org.testcontainers.valkey; | ||
|
||
import com.google.common.base.Preconditions; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Testcontainers implementation for Valkey. | ||
* <p> | ||
* Supported image: {@code valkey} | ||
* <p> | ||
* Exposed ports: | ||
* <ul> | ||
* <li>Server: 6379</li> | ||
* </ul> | ||
*/ | ||
public class ValkeyContainer extends GenericContainer<ValkeyContainer> { | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
private static class SnapshottingSettings { | ||
|
||
int seconds; | ||
|
||
int changedKeys; | ||
} | ||
|
||
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse("valkey/valkey:8.1"); | ||
|
||
private static final String DEFAULT_CONFIG_FILE = "/usr/local/valkey.conf"; | ||
|
||
private static final int CONTAINER_PORT = 6379; | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
private String persistenceVolume; | ||
|
||
private String initialImportScriptFile; | ||
|
||
private String configFile; | ||
|
||
private ValkeyLogLevel logLevel; | ||
|
||
private SnapshottingSettings snapshottingSettings; | ||
|
||
public ValkeyContainer() { | ||
this(DEFAULT_IMAGE); | ||
} | ||
|
||
public ValkeyContainer(String dockerImageName) { | ||
this(DockerImageName.parse(dockerImageName)); | ||
} | ||
|
||
public ValkeyContainer(DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
withExposedPorts(CONTAINER_PORT); | ||
withStartupTimeout(Duration.ofMinutes(2)); | ||
waitingFor(Wait.forLogMessage(".*Ready to accept connections.*", 1)); | ||
} | ||
|
||
public ValkeyContainer withUsername(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public ValkeyContainer withPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets a host path to be mounted as a volume for Valkey persistence. The path must exist on the | ||
* host system. Valkey will store its data in this directory. | ||
*/ | ||
public ValkeyContainer withPersistenceVolume(String persistenceVolume) { | ||
this.persistenceVolume = persistenceVolume; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets an initial import script file to be executed via the Valkey CLI after startup. | ||
* <p> | ||
* Example line of an import script file: SET key1 "value1" | ||
*/ | ||
public ValkeyContainer withInitialData(String initialImportScriptFile) { | ||
this.initialImportScriptFile = initialImportScriptFile; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the log level for the valkey server process. | ||
*/ | ||
public ValkeyContainer withLogLevel(ValkeyLogLevel logLevel) { | ||
this.logLevel = logLevel; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the snapshotting configuration for the valkey server process. You can configure Valkey | ||
* to have it save the dataset every N seconds if there are at least M changes in the dataset. | ||
* This method allows Valkey to benefit from copy-on-write semantics. | ||
* | ||
* @see <a href="https://valkey.io/topics/persistence/#snapshotting"/> | ||
*/ | ||
public ValkeyContainer withSnapshotting(int seconds, int changedKeys) { | ||
Preconditions.checkArgument(seconds > 0, "seconds must be greater than 0"); | ||
Preconditions.checkArgument(changedKeys > 0, "changedKeys must be non-negative"); | ||
|
||
this.snapshottingSettings = new SnapshottingSettings(seconds, changedKeys); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the config file to be used for the Valkey container. | ||
*/ | ||
public ValkeyContainer withConfigFile(String configFile) { | ||
this.configFile = configFile; | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
List<String> command = new ArrayList<>(); | ||
command.add("valkey-server"); | ||
|
||
if (StringUtils.isNotEmpty(configFile)) { | ||
withCopyToContainer(MountableFile.forHostPath(configFile), DEFAULT_CONFIG_FILE); | ||
command.add(DEFAULT_CONFIG_FILE); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(password)) { | ||
command.add("--requirepass"); | ||
command.add(password); | ||
|
||
if (StringUtils.isNotEmpty(username)) { | ||
command.add("--user " + username + " on >" + password + " ~* +@all"); | ||
} | ||
} | ||
|
||
if (StringUtils.isNotEmpty(persistenceVolume)) { | ||
command.addAll(Arrays.asList("--appendonly", "yes")); | ||
withFileSystemBind(persistenceVolume, "/data"); | ||
} | ||
|
||
if (snapshottingSettings != null) { | ||
command.addAll( | ||
Arrays.asList("--save", | ||
snapshottingSettings.getSeconds() + " " + snapshottingSettings.getChangedKeys()) | ||
); | ||
} | ||
|
||
if (logLevel != null) { | ||
command.addAll(Arrays.asList("--loglevel", logLevel.getLevel())); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(initialImportScriptFile)) { | ||
withCopyToContainer(MountableFile.forHostPath(initialImportScriptFile), | ||
"/tmp/import.valkey"); | ||
withCopyToContainer(MountableFile.forClasspathResource("import.sh"), "/tmp/import.sh"); | ||
} | ||
|
||
withCommand(command.toArray(new String[0])); | ||
|
||
super.start(); | ||
|
||
evaluateImportScript(); | ||
} | ||
|
||
public int getPort() { | ||
return getMappedPort(CONTAINER_PORT); | ||
} | ||
|
||
/** | ||
* Executes a command in the Valkey CLI inside the container. | ||
*/ | ||
public String executeCli(String cmd, String... flags) { | ||
List<String> args = new ArrayList<>(); | ||
args.add("redis-cli"); | ||
|
||
if (StringUtils.isNotEmpty(password)) { | ||
args.addAll( | ||
StringUtils.isNotEmpty(username) | ||
? Arrays.asList("--user", username, "--pass", password) | ||
: Arrays.asList("--pass", password) | ||
); | ||
} | ||
|
||
args.add(cmd); | ||
args.addAll(Arrays.asList(flags)); | ||
|
||
try { | ||
ExecResult result = execInContainer(args.toArray(new String[0])); | ||
wwisser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (result.getExitCode() != 0) { | ||
throw new RuntimeException(result.getStdout() + result.getStderr()); | ||
} | ||
|
||
return result.getStdout(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("failed to execute CLI command", e); | ||
} | ||
} | ||
|
||
public String createConnectionUrl() { | ||
String userInfo = null; | ||
wwisser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) { | ||
userInfo = username + ":" + password; | ||
} else if (StringUtils.isNotEmpty(password)) { | ||
userInfo = ":" + password; | ||
} | ||
|
||
try { | ||
URI uri = new URI("redis", userInfo, getHost(), getPort(), null, null, null); | ||
return uri.toString(); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException("Failed to build Redis URI", e); | ||
} | ||
} | ||
|
||
private void evaluateImportScript() { | ||
if (StringUtils.isEmpty(initialImportScriptFile)) { | ||
return; | ||
} | ||
|
||
try { | ||
ExecResult result = execInContainer("/bin/sh", "/tmp/import.sh", | ||
password != null ? password : ""); | ||
|
||
if (result.getExitCode() != 0 || result.getStdout().contains("ERR")) { | ||
throw new RuntimeException("Could not import initial data: " + result.getStdout()); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
modules/valkey/src/main/java/org/testcontainers/valkey/ValkeyLogLevel.java
This file contains hidden or 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,18 @@ | ||
package org.testcontainers.valkey; | ||
|
||
public enum ValkeyLogLevel { | ||
DEBUG("debug"), | ||
VERBOSE("verbose"), | ||
NOTICE("notice"), | ||
WARNING("warning"); | ||
|
||
private final String level; | ||
|
||
ValkeyLogLevel(String level) { | ||
this.level = level; | ||
} | ||
|
||
public String getLevel() { | ||
return level; | ||
} | ||
} |
This file contains hidden or 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,4 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
valkey-cli $([[ -n "$1" ]] && echo "-a $1") < "/tmp/import.valkey" | ||
echo "Imported" |
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.
nit: This should be final.
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.
I'm not a fan of this because excessive usage of
final
in Java clutters the code and worsens readability. It's pretty uncommon and I don't see a presence of this convention in the codebase either.I'm happy to discuss it, but contribution guidelines state we should adapt to code base standards.