diff --git a/.github/ISSUE_TEMPLATE/package-redis-store--bug_report.md b/.github/ISSUE_TEMPLATE/package-redis-store--bug_report.md new file mode 100644 index 0000000..1cfa416 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/package-redis-store--bug_report.md @@ -0,0 +1,36 @@ +--- +name: 'Bug report for the java-server-sdk-redis-store package' +about: Create a report to help us improve +title: '' +labels: 'package: java-server-sdk-redis-store, bug' +assignees: '' +--- + +**Is this a support request?** +This issue tracker is maintained by LaunchDarkly SDK developers and is intended for feedback on the code in this library. If you're not sure whether the problem you are having is specifically related to this library, or to the LaunchDarkly service overall, it may be more appropriate to contact the LaunchDarkly support team; they can help to investigate the problem and will consult the SDK team if necessary. You can submit a support request by going [here](https://support.launchdarkly.com/) and clicking "submit a request", or by emailing support@launchdarkly.com. + +Note that issues filed on this issue tracker are publicly accessible. Do not provide any private account information on your issues. If your problem is specific to your account, you should submit a support request as described above. + +**Describe the bug** +A clear and concise description of what the bug is. + +**To reproduce** +Steps to reproduce the behavior. + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Logs** +If applicable, add any log output related to your problem. + +**SDK version** +The version of this SDK that you are using. + +**Language version, developer tools** +For instance, Go 1.11 or Ruby 2.5.3. If you are using a language that requires a separate compiler, such as C, please include the name and version of the compiler too. + +**OS/platform** +For instance, Ubuntu 16.04, Windows 10, or Android 4.0.3. If your code is running in a browser, please also include the browser type and version. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/package-redis-store--feature_request.md b/.github/ISSUE_TEMPLATE/package-redis-store--feature_request.md new file mode 100644 index 0000000..5188ff2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/package-redis-store--feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request for the java-server-sdk-redis-store package +about: Suggest an idea for this project +title: '' +labels: 'package: java-server-sdk-redis-store, enhancement' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I would love to see the SDK [...does something new...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context about the feature request here. \ No newline at end of file diff --git a/.github/actions/ci/action.yml b/.github/actions/ci/action.yml index 5fc5754..910fe8e 100644 --- a/.github/actions/ci/action.yml +++ b/.github/actions/ci/action.yml @@ -27,6 +27,14 @@ runs: distribution: ${{ inputs.java_distribution }} java-version: ${{ inputs.java_version }} + - name: Setup Redis Service + shell: bash + if: ${{ inputs.workspace_path == 'lib/java-server-sdk-redis-store' }} + run: | + sudo apt-get update -y + sudo apt-get install redis-server -y + sudo service redis-server start + - name: Restore dependencies shell: bash id: restore diff --git a/.github/workflows/java-server-sdk-redis-store.yml b/.github/workflows/java-server-sdk-redis-store.yml new file mode 100644 index 0000000..6e4a3d7 --- /dev/null +++ b/.github/workflows/java-server-sdk-redis-store.yml @@ -0,0 +1,87 @@ +name: java-server-sdk-redis-store + +on: + push: + branches: [main, 'feat/**'] + paths-ignore: + - '**.md' #Do not need to run CI for markdown changes. + pull_request: + branches: [main, 'feat/**'] + paths-ignore: + - '**.md' + +jobs: + build-test-java-server-sdk-redis-store: + strategy: + matrix: + jedis-version: [2.9.0, 3.0.0] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Edit build.gradle to change Jedis version + shell: bash + run: | + cd lib/java-server-sdk-redis-store + sed -i.bak 's#"jedis":.*"[0-9.]*"#"jedis":"${{ matrix.jedis-version }}"#' build.gradle + + - name: Shared CI Steps + uses: ./.github/actions/ci + with: + workspace_path: 'lib/java-server-sdk-redis-store' + java_version: 8 + + build-test-java-server-sdk-windows: + strategy: + matrix: + jedis-version: [2.9.0, 3.0.0] + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + + - run: | + $ProgressPreference = "SilentlyContinue" + iwr -outf redis.zip https://github.com/MicrosoftArchive/redis/releases/download/win-3.0.504/Redis-x64-3.0.504.zip + mkdir redis + Expand-Archive -Path redis.zip -DestinationPath redis + cd redis + .\redis-server --service-install + .\redis-server --service-start + Start-Sleep -s 5 + .\redis-cli ping + + - name: Edit build.gradle to change Jedis version + shell: bash + run: | + cd lib/java-server-sdk-redis-store + sed -i.bak 's#"jedis":.*"[0-9.]*"#"jedis":"${{ matrix.jedis-version }}"#' build.gradle + + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: 8 + + - name: Restore dependencies + shell: bash + id: restore + run: lib/java-server-sdk-redis-store/gradlew dependencies -p lib/java-server-sdk-redis-store + + - name: Build + shell: bash + id: build + run: lib/java-server-sdk-redis-store/gradlew build -p lib/java-server-sdk-redis-store + + - name: Build Jar + shell: bash + id: buildjar + run: lib/java-server-sdk-redis-store/gradlew jar -p lib/java-server-sdk-redis-store + + - name: Run Tests + if: steps.build.outcome == 'success' && inputs.run_tests == 'true' + shell: bash + run: lib/java-server-sdk-redis-store/gradlew test -p lib/java-server-sdk-redis-store + + - name: Build Documentation + shell: bash + run: lib/java-server-sdk-redis-store/gradlew javadoc -p lib/java-server-sdk-redis-store diff --git a/.github/workflows/manual-publish-docs.yml b/.github/workflows/manual-publish-docs.yml index 9cc5052..c011c70 100644 --- a/.github/workflows/manual-publish-docs.yml +++ b/.github/workflows/manual-publish-docs.yml @@ -6,10 +6,11 @@ on: required: true type: choice options: - - lib/java-server-sdk-otel - lib/shared/common - lib/shared/internal - lib/sdk/server + - lib/java-server-sdk-otel + - lib/java-server-sdk-redis-store dry_run: description: 'Is this a dry run. If so no docs will be published.' type: boolean diff --git a/.github/workflows/manual-publish.yml b/.github/workflows/manual-publish.yml index 2e88854..fed4b9e 100644 --- a/.github/workflows/manual-publish.yml +++ b/.github/workflows/manual-publish.yml @@ -7,10 +7,11 @@ on: required: true type: choice options: - - lib/java-server-sdk-otel - lib/shared/common - lib/shared/internal - lib/sdk/server + - lib/java-server-sdk-otel + - lib/java-server-sdk-redis-store prerelease: description: 'Is this a prerelease.' type: boolean diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index 6b592d1..4aae3b2 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -10,10 +10,11 @@ jobs: runs-on: ubuntu-latest outputs: - package-server-sdk-otel-released: ${{ steps.release.outputs['lib/java-server-sdk-otel--release_created'] }} package-sdk-common-released: ${{ steps.release.outputs['lib/java-sdk-common--release_created'] }} package-sdk-internal-released: ${{ steps.release.outputs['lib/java-sdk-internal--release_created'] }} package-server-sdk-released: ${{ steps.release.outputs['lib/java-server-sdk--release_created'] }} + package-server-sdk-otel-released: ${{ steps.release.outputs['lib/java-server-sdk-otel--release_created'] }} + package-server-sdk-redis-store-released: ${{ steps.release.outputs['lib/java-server-sdk-redis-store--release_created'] }} steps: - uses: google-github-actions/release-please-action@v4 @@ -86,6 +87,38 @@ jobs: aws_role: ${{ vars.AWS_ROLE_ARN }} token: ${{ secrets.GITHUB_TOKEN }} + release-server-sdk-redis-store: + runs-on: ubuntu-latest + needs: release-please + permissions: + id-token: write + contents: write + pull-requests: write + if: ${{ needs.release-please.outputs.package-server-sdk-redis-store-released == 'true'}} + steps: + - uses: actions/checkout@v4 + + - uses: launchdarkly/gh-actions/actions/release-secrets@release-secrets-v1.1.0 + name: Get secrets + with: + aws_assume_role: ${{ vars.AWS_ROLE_ARN }} + ssm_parameter_pairs: '/production/common/releasing/sonatype/username = SONATYPE_USER_NAME, + /production/common/releasing/sonatype/password = SONATYPE_PASSWORD' + s3_path_pairs: 'launchdarkly-releaser/java/code-signing-keyring.gpg = code-signing-keyring.gpg' + + - uses: ./.github/actions/full-release + with: + workspace_path: lib/java-server-sdk-redis-store + dry_run: false + prerelease: false + code_signing_keyring: 'code-signing-keyring.gpg' + signing_key_id: ${{ env.SIGNING_KEY_ID }} + signing_key_passphrase: ${{ env.SIGNING_KEY_PASSPHRASE }} + sonatype_username: ${{ env.SONATYPE_USER_NAME }} + sonatype_password: ${{ env.SONATYPE_PASSWORD }} + aws_role: ${{ vars.AWS_ROLE_ARN }} + token: ${{ secrets.GITHUB_TOKEN }} + release-sdk-internal: runs-on: ubuntu-latest needs: release-please diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 12b5b21..3ea9764 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,5 +1,6 @@ { "lib/java-server-sdk-otel": "0.1.0", + "lib/java-server-sdk-redis-store": "3.0.0", "lib/shared/common": "2.1.1", "lib/shared/internal": "1.3.0", "lib/sdk/server": "7.7.0" diff --git a/README.md b/README.md index d80f475..2a05665 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,10 @@ This includes shared libraries, used by SDKs and other tools, as well as SDKs. | [@launchdarkly/java-sdk-internal](lib/shared/internal/README.md) | [![Documentation][sdk-internal-docs-badge]][sdk-internal-docs-link] | [![maven][sdk-internal-maven-badge]][sdk-internal-maven-link] | [Issues][sdk-internal-issues] | [![Actions Status][sdk-internal-ci-badge]][sdk-internal-ci-link] | | [@launchdarkly/java-sdk-common](lib/shared/common/README.md) | [![Documentation][sdk-common-docs-badge]][sdk-common-docs-link] | [![maven][sdk-common-maven-badge]][sdk-common-maven-link] | [Issues][sdk-common-issues] | [![Actions Status][sdk-common-ci-badge]][sdk-common-ci-link] | -| Telemetry Packages | API Docs | maven | issues | tests | +| Other Packages | API Docs | maven | issues | tests | | ---------------------------------------------------------------------------- |--------------------------------------------------------------| ---------------------------------------------------------- | ------------------------------------- | ------------------------------------------------------------- | | [@launchdarkly/java-server-sdk-otel](lib/java-server-sdk-otel/README.md) | [![Documentation][server-otel-docs-badge]][server-otel-docs-link] | [![maven][server-otel-maven-badge]][server-otel-maven-link] | [Issues][server-otel-issues] | [![Actions Status][server-otel-ci-badge]][server-otel-ci-link] | +| [@launchdarkly/java-server-sdk-redis-store](lib/java-server-sdk-redis-store/README.md) | [![Documentation][server-redis-docs-badge]][server-redis-docs-link] | [![maven][server-redis-maven-badge]][server-redis-maven-link] | [Issues][server-redis-issues] | [![Actions Status][server-redis-ci-badge]][server-redis-ci-link] | ## Organization @@ -71,6 +72,15 @@ We encourage pull requests and other contributions from the community. Check out [server-otel-docs-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8 [server-otel-docs-link]: https://launchdarkly.github.io/java-core/lib/java-server-sdk-otel/ +[//]: # 'java-server-sdk-redis-store' +[server-redis-issues]: https://github.com/launchdarkly/java-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+java-server-sdk-redis-store%22+ +[server-redis-maven-badge]: https://img.shields.io/maven-central/v/com.launchdarkly/launchdarkly-java-server-sdk-redis-store +[server-redis-maven-link]: https://central.sonatype.com/artifact/com.launchdarkly/launchdarkly-java-server-sdk-redis-store +[server-redis-ci-badge]: https://github.com/launchdarkly/java-core/actions/workflows/java-server-sdk-redis-store.yml/badge.svg +[server-redis-ci-link]: https://github.com/launchdarkly/java-core/actions/workflows/java-server-sdk-redis-store.yml +[server-redis-docs-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8 +[server-redis-docs-link]: https://launchdarkly.github.io/java-core/lib/java-server-sdk-redis-store/ + [//]: # 'java-sdk-internal' [sdk-internal-issues]: https://github.com/launchdarkly/java-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+java-sdk-internal%22+ [sdk-internal-maven-badge]: https://img.shields.io/maven-central/v/com.launchdarkly/launchdarkly-java-sdk-internal diff --git a/lib/java-server-sdk-redis-store/CHANGELOG.md b/lib/java-server-sdk-redis-store/CHANGELOG.md new file mode 100644 index 0000000..23dcd29 --- /dev/null +++ b/lib/java-server-sdk-redis-store/CHANGELOG.md @@ -0,0 +1,33 @@ +# Change log + +All notable changes to the LaunchDarkly Java SDK Redis integration will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org). + +## [3.0.0] - 2022-12-07 +This release corresponds to the 6.0.0 release of the LaunchDarkly Java SDK. Any application code that is being updated to use the 6.0.0 SDK, and was using a 2.x version of `launchdarkly-java-server-sdk-redis-store`, should now use a 3.x version instead. + +There are no functional differences in the behavior of the Redis integration; the differences are only related to changes in the usage of interface types for configuration in the SDK. + +### Added: +- `Redis.bigSegmentStore()`, which creates a configuration builder for use with Big Segments. Previously, the `Redis.dataStore()` builder was used for both regular data stores and Big Segment stores. + +### Changed: +- The type `RedisDataStoreBuilder` has been removed, replaced by a generic type `RedisStoreBuilder`. Application code would not normally need to reference these types by name, but if necessary, use either `RedisStoreBuilder` or `RedisStoreBuilder` depending on whether you are configuring a regular data store or a Big Segment store. + +## [2.0.0] - 2022-07-29 +This release updates the package to use the new logging mechanism that was introduced in version 5.10.0 of the LaunchDarkly Java SDK, so that log output from the Redis integration is handled in whatever way was specified by the SDK's logging configuration. + +This version of the package will not work with SDK versions earlier than 5.10.0; that is the only reason for the 2.0.0 major version increment. The functionality of the package is otherwise unchanged, and there are no API changes. + +## [1.1.0] - 2022-01-28 +### Added: +- Added support for Big Segments. An Early Access Program for creating and syncing Big Segments from customer data platforms is available to enterprise customers. + +## [1.0.1] - 2021-08-06 +### Fixed: +- This integration now works with Jedis 3.x as well as Jedis 2.9.x. The default dependency is still 2.9.x, but an application can override this with a dependency on a 3.x version. (Thanks, [robotmlg](https://github.com/launchdarkly/java-server-sdk-redis/pull/3)!) + +## [1.0.0] - 2020-06-02 +Initial release, corresponding to the 5.0.0 release of [`launchdarkly-java-server-sdk`](https://github.com/launchdarkly/java-server-sdk). + +Prior to that release, the Redis integration was built into the main SDK library. For more information about changes in the SDK database integrations, see the [4.x to 5.0 migration guide](https://docs-stg.launchdarkly.com/252/sdk/server-side/java/migration-4-to-5/). + diff --git a/lib/java-server-sdk-redis-store/README.md b/lib/java-server-sdk-redis-store/README.md new file mode 100644 index 0000000..d2d2820 --- /dev/null +++ b/lib/java-server-sdk-redis-store/README.md @@ -0,0 +1,72 @@ +# LaunchDarkly SDK for Java - Redis integration + +This library provides a Redis-backed persistence mechanism (feature store) for the [LaunchDarkly Java SDK](https://github.com/launchdarkly/java-server-sdk), replacing the default in-memory feature store. The Redis API implementation it uses is [Jedis](https://github.com/xetorthio/jedis). + +This version of the library requires at least version 6.0.0 of the LaunchDarkly Java SDK; for versions of the library to use with earlier SDK versions, see the changelog. The minimum Java version is 8. + +For more information, see also: [Using Redis as a persistent feature store](https://docs.launchdarkly.com/sdk/features/storing-data/redis#java). + +## Quick setup + +This assumes that you have already installed the LaunchDarkly Java SDK. + +1. Add this library to your project (substitute the latest version number for `XXX`): + + + com.launchdarkly + launchdarkly-java-server-sdk-redis-store + XXX + + +2. The Redis client library (Jedis) should be pulled in automatically if you do not specify a dependency for it. If you want to use a different version, you may add your own dependency: + + + redis.clients + jedis + 2.9.0 + + + This library is compatible with Jedis 2.x versions greater than or equal to 2.9.0, and also with Jedis 3.x. + +3. Import the LaunchDarkly package and the package for this library: + + import com.launchdarkly.sdk.server.*; + import com.launchdarkly.sdk.server.integrations.*; + +4. When configuring your SDK client, add the Redis data store as a `persistentDataStore`. You may specify any custom Redis options using the methods of `RedisDataStoreBuilder`. For instance, to customize the Redis URL: + + LDConfig config = new LDConfig.Builder() + .dataStore( + Components.persistentDataStore( + Redis.dataStore().url("redis://my-redis-host") + ) + ) + .build(); + +By default, the store will try to connect to a local Redis instance on port 6379. + +## Caching behavior + +The LaunchDarkly SDK has a standard caching mechanism for any persistent data store, to reduce database traffic. This is configured through the SDK's `PersistentDataStoreBuilder` class as described the SDK documentation. For instance, to specify a cache TTL of 5 minutes: + + LDConfig config = new LDConfig.Builder() + .dataStore( + Components.persistentDataStore( + Redis.dataStore() + ).cacheTime(Duration.ofMinutes(5)) + ) + .build(); + +## About LaunchDarkly + +* LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can: + * Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases. + * Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?). + * Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file. + * Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline. +* LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read [our documentation](https://docs.launchdarkly.com/sdk) for a complete list. +* Explore LaunchDarkly + * [launchdarkly.com](https://www.launchdarkly.com/ "LaunchDarkly Main Website") for more information + * [docs.launchdarkly.com](https://docs.launchdarkly.com/ "LaunchDarkly Documentation") for our documentation and SDK reference guides + * [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ "LaunchDarkly API Documentation") for our API documentation + * [blog.launchdarkly.com](https://blog.launchdarkly.com/ "LaunchDarkly Blog Documentation") for the latest product updates diff --git a/lib/java-server-sdk-redis-store/build.gradle b/lib/java-server-sdk-redis-store/build.gradle new file mode 100644 index 0000000..5fc3bae --- /dev/null +++ b/lib/java-server-sdk-redis-store/build.gradle @@ -0,0 +1,157 @@ + +buildscript { + repositories { + mavenCentral() + mavenLocal() + } +} + +plugins { + id "java" + id "java-library" + id "checkstyle" + id "signing" + id "maven-publish" + id "de.marcphilipp.nexus-publish" version "0.3.0" + id "io.codearte.nexus-staging" version "0.30.0" + id "idea" +} + +configurations.all { + // check for updates every build for dependencies with: 'changing: true' + resolutionStrategy.cacheChangingModulesFor 0, 'seconds' +} + +repositories { + mavenLocal() + // Before LaunchDarkly release artifacts get synced to Maven Central they are here along with snapshots: + maven { url "https://oss.sonatype.org/content/groups/public/" } + mavenCentral() +} + +allprojects { + group = 'com.launchdarkly' + version = "${version}" + archivesBaseName = 'launchdarkly-java-server-sdk-redis-store' + sourceCompatibility = 1.8 + targetCompatibility = 1.8 +} + +ext { + sdkBasePackage = "com.launchdarkly.client.redis" +} + +ext.versions = [ + "sdk": "6.2.1", // the *lowest* version we're compatible with + "jedis": "2.9.0" +] + +ext.libraries = [:] + +dependencies { + api "com.launchdarkly:launchdarkly-java-server-sdk:${versions.sdk}" + api "redis.clients:jedis:${versions.jedis}" + testImplementation "org.hamcrest:hamcrest-all:1.3" + testImplementation "junit:junit:4.12" + testImplementation "com.launchdarkly:launchdarkly-java-server-sdk:${versions.sdk}:test" // our unit tests use helper classes from the SDK + testImplementation "com.google.guava:guava:28.2-jre" // required by SDK tests, not used in this library itself + testImplementation "com.google.code.gson:gson:2.7" // same as above +} + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +artifacts { + archives sourcesJar, javadocJar +} + +test { + testLogging { + events "passed", "skipped", "failed", "standardOut", "standardError" + showStandardStreams = true + exceptionFormat = 'full' + } +} + +checkstyle { + toolVersion = "9.3" + configFile = file("${project.rootDir}/checkstyle.xml") +} + +idea { + module { + downloadJavadoc = true + downloadSources = true + } +} + + +nexusStaging { + packageGroup = "com.launchdarkly" + numberOfRetries = 40 // we've seen extremely long delays in closing repositories +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + + groupId = 'com.launchdarkly' + artifactId = project.archivesBaseName + + artifact sourcesJar + artifact javadocJar + + pom { + name = project.archivesBaseName + description = 'LaunchDarkly Java SDK Redis integration' + url = 'https://github.com/launchdarkly/java-server-sdk-redis' + licenses { + license { + name = 'The Apache License, Version 2.0' + url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' + } + } + developers { + developer { + name = 'LaunchDarkly' + email = 'team@launchdarkly.com' + } + } + scm { + connection = 'scm:git:git://github.com/launchdarkly/java-server-sdk-redis.git' + developerConnection = 'scm:git:ssh:git@github.com:launchdarkly/java-server-sdk-redis.git' + url = 'https://github.com/launchdarkly/java-server-sdk-redis' + } + } + } + } + repositories { + mavenLocal() + } +} + +nexusPublishing { + clientTimeout = java.time.Duration.ofMinutes(2) // we've seen extremely long delays in creating repositories + repositories { + sonatype { + username = ossrhUsername + password = ossrhPassword + } + } +} + +signing { + sign publishing.publications.mavenJava +} + +tasks.withType(Sign) { + onlyIf { !"1".equals(project.findProperty("LD_SKIP_SIGNING")) } // so we can build jars for testing in CI +} diff --git a/lib/java-server-sdk-redis-store/checkstyle.xml b/lib/java-server-sdk-redis-store/checkstyle.xml new file mode 100644 index 0000000..0101956 --- /dev/null +++ b/lib/java-server-sdk-redis-store/checkstyle.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/java-server-sdk-redis-store/gradle.properties b/lib/java-server-sdk-redis-store/gradle.properties new file mode 100644 index 0000000..7e6a212 --- /dev/null +++ b/lib/java-server-sdk-redis-store/gradle.properties @@ -0,0 +1,6 @@ +version=3.0.0 +ossrhUsername= +ossrhPassword= + +# See https://github.com/gradle/gradle/issues/11308 regarding the following property +systemProp.org.gradle.internal.publish.checksums.insecure=true diff --git a/lib/java-server-sdk-redis-store/gradle/wrapper/gradle-wrapper.jar b/lib/java-server-sdk-redis-store/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/lib/java-server-sdk-redis-store/gradle/wrapper/gradle-wrapper.jar differ diff --git a/lib/java-server-sdk-redis-store/gradle/wrapper/gradle-wrapper.properties b/lib/java-server-sdk-redis-store/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..070cb70 --- /dev/null +++ b/lib/java-server-sdk-redis-store/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/lib/java-server-sdk-redis-store/gradlew b/lib/java-server-sdk-redis-store/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/lib/java-server-sdk-redis-store/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed 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 +# +# https://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. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/lib/java-server-sdk-redis-store/gradlew.bat b/lib/java-server-sdk-redis-store/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/lib/java-server-sdk-redis-store/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/lib/java-server-sdk-redis-store/settings.gradle b/lib/java-server-sdk-redis-store/settings.gradle new file mode 100644 index 0000000..f76074f --- /dev/null +++ b/lib/java-server-sdk-redis-store/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'launchdarkly-java-server-sdk-redis-store' diff --git a/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/Redis.java b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/Redis.java new file mode 100644 index 0000000..92eb78f --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/Redis.java @@ -0,0 +1,91 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.sdk.server.Components; +import com.launchdarkly.sdk.server.subsystems.BigSegmentStore; +import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; +import com.launchdarkly.sdk.server.subsystems.PersistentDataStore; + +/** + * Integration between the LaunchDarkly SDK and Redis. + * + * @since 4.12.0 + */ +public abstract class Redis { + /** + * Returns a builder object for creating a Redis-backed persistent data store. + *

+ * This is for the main data store that holds feature flag data. To configure a + * Big Segment store, use {@link #bigSegmentStore()} instead. + *

+ * You can use methods of the builder to specify any non-default Redis options + * you may want, before passing the builder to {@link Components#persistentDataStore(ComponentConfigurer)}. + * In this example, the store is configured to use a Redis host called "host1": + *


+   *     LDConfig config = new LDConfig.Builder()
+   *         .dataStore(
+   *             Components.persistentDataStore(
+   *                 Redis.dataStore().uri(URI.create("redis://host1:6379")
+   *             )
+   *         )
+   *         .build();
+   * 
+ *

+ * Note that the SDK also has its own options related to data storage that are configured + * at a different level, because they are independent of what database is being used. For + * instance, the builder returned by {@link Components#persistentDataStore(ComponentConfigurer)} + * has options for caching: + *


+   *     LDConfig config = new LDConfig.Builder()
+   *         .dataStore(
+   *             Components.persistentDataStore(
+   *                 Redis.dataStore().uri(URI.create("redis://my-redis-host"))
+   *             ).cacheSeconds(15)
+   *         )
+   *         .build();
+   * 
+ * + * @return a data store configuration object + */ + public static RedisStoreBuilder dataStore() { + return new RedisStoreBuilder.ForDataStore(); + } + + /** + * Returns a builder object for creating a Redis-backed Big Segment store. + *

+ * You can use methods of the builder to specify any non-default Redis options + * you may want, before passing the builder to {@link Components#bigSegments(ComponentConfigurer)}. + * In this example, the store is configured to use a Redis host called "host2": + *


+   *     LDConfig config = new LDConfig.Builder()
+   *         .bigSegments(
+   *             Components.bigSegments(
+   *                 Redis.bigSegmentStore().uri(URI.create("redis://host2:6379")
+   *             )
+   *         )
+   *         .build();
+   * 
+ *

+ * Note that the SDK also has its own options related to Big Segments that are configured + * at a different level, because they are independent of what database is being used. For + * instance, the builder returned by {@link Components#bigSegments(ComponentConfigurer)} + * has an option for the status polling interval: + *


+   *     LDConfig config = new LDConfig.Builder()
+   *         .dataStore(
+   *             Components.bigSegments(
+   *                 Redis.bigSegmentStore().uri(URI.create("redis://my-redis-host"))
+   *             ).statusPollInterval(Duration.ofSeconds(30))
+   *         )
+   *         .build();
+   * 
+ * + * @return a Big Segment store configuration object + * @since 3.0.0 + */ + public static RedisStoreBuilder bigSegmentStore() { + return new RedisStoreBuilder.ForBigSegments(); + } + + private Redis() {} +} diff --git a/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisBigSegmentStoreImpl.java b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisBigSegmentStoreImpl.java new file mode 100644 index 0000000..a287776 --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisBigSegmentStoreImpl.java @@ -0,0 +1,42 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.logging.LDLogger; +import com.launchdarkly.sdk.server.subsystems.BigSegmentStore; +import com.launchdarkly.sdk.server.subsystems.BigSegmentStoreTypes; + +import java.util.Set; + +import redis.clients.jedis.Jedis; + +final class RedisBigSegmentStoreImpl extends RedisStoreImplBase implements BigSegmentStore { + private final String syncTimeKey; + private final String includedKeyPrefix; + private final String excludedKeyPrefix; + + RedisBigSegmentStoreImpl(RedisStoreBuilder builder, LDLogger baseLogger) { + super(builder, baseLogger.subLogger("BigSegments").subLogger("Redis")); + syncTimeKey = prefix + ":big_segments_synchronized_on"; + includedKeyPrefix = prefix + ":big_segment_include:"; + excludedKeyPrefix = prefix + ":big_segment_exclude:"; + } + + @Override + public BigSegmentStoreTypes.Membership getMembership(String userHash) { + try (Jedis jedis = pool.getResource()) { + Set includedRefs = jedis.smembers(includedKeyPrefix + userHash); + Set excludedRefs = jedis.smembers(excludedKeyPrefix + userHash); + return BigSegmentStoreTypes.createMembershipFromSegmentRefs(includedRefs, excludedRefs); + } + } + + @Override + public BigSegmentStoreTypes.StoreMetadata getMetadata() { + try (Jedis jedis = pool.getResource()) { + String value = jedis.get(syncTimeKey); + if (value == null || value.isEmpty()) { + return null; + } + return new BigSegmentStoreTypes.StoreMetadata(Long.parseLong(value)); + } + } +} diff --git a/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreImpl.java b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreImpl.java new file mode 100644 index 0000000..aebd26b --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreImpl.java @@ -0,0 +1,163 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.logging.LDLogger; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.DataKind; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.FullDataSet; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.ItemDescriptor; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.KeyedItems; +import com.launchdarkly.sdk.server.subsystems.DataStoreTypes.SerializedItemDescriptor; +import com.launchdarkly.sdk.server.subsystems.PersistentDataStore; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.Transaction; + +final class RedisDataStoreImpl extends RedisStoreImplBase implements PersistentDataStore { + private UpdateListener updateListener; + + RedisDataStoreImpl(RedisStoreBuilder builder, LDLogger baseLogger) { + super(builder, baseLogger.subLogger("DataStore").subLogger("Redis")); + } + + @Override + public SerializedItemDescriptor get(DataKind kind, String key) { + try (Jedis jedis = pool.getResource()) { + String item = getRedis(kind, key, jedis); + return item == null ? null : new SerializedItemDescriptor(0, false, item); + } + } + + @Override + public KeyedItems getAll(DataKind kind) { + try (Jedis jedis = pool.getResource()) { + Map allJson = jedis.hgetAll(itemsKey(kind)); + List> itemsOut = new ArrayList<>(allJson.size()); + for (Map.Entry e: allJson.entrySet()) { + itemsOut.add(new AbstractMap.SimpleEntry<>(e.getKey(), new SerializedItemDescriptor(0, false, e.getValue()))); + } + return new KeyedItems<>(itemsOut); + } + } + + @Override + public void init(FullDataSet allData) { + try (Jedis jedis = pool.getResource()) { + Transaction t = jedis.multi(); + + for (Map.Entry> e0: allData.getData()) { + DataKind kind = e0.getKey(); + String baseKey = itemsKey(kind); + t.del(baseKey); + for (Map.Entry e1: e0.getValue().getItems()) { + t.hset(baseKey, e1.getKey(), jsonOrPlaceholder(kind, e1.getValue())); + } + } + + t.set(initedKey(), ""); + t.exec(); + } + } + + @Override + public boolean upsert(DataKind kind, String key, SerializedItemDescriptor newItem) { + while (true) { + Jedis jedis = null; + try { + jedis = pool.getResource(); + String baseKey = itemsKey(kind); + jedis.watch(baseKey); + + if (updateListener != null) { + updateListener.aboutToUpdate(baseKey, key); + } + + String oldItemJson = getRedis(kind, key, jedis); + // In this implementation, we have to parse the existing item in order to determine its version. + int oldVersion = oldItemJson == null ? -1 : kind.deserialize(oldItemJson).getVersion(); + + if (oldVersion >= newItem.getVersion()) { + logger.debug("Attempted to {} key: {} version: {}" + + " with a version that is the same or older: {} in \"{}\"", + newItem.getSerializedItem() == null ? "delete" : "update", + key, oldVersion, newItem.getVersion(), kind.getName()); + return false; + } + + Transaction tx = jedis.multi(); + tx.hset(baseKey, key, jsonOrPlaceholder(kind, newItem)); + List result = tx.exec(); + if (result == null || result.isEmpty()) { + // if exec failed, it means the watch was triggered and we should retry + logger.debug("Concurrent modification detected, retrying"); + continue; + } + + return true; + } finally { + if (jedis != null) { + jedis.unwatch(); + jedis.close(); + } + } + } + } + + @Override + public boolean isInitialized() { + try (Jedis jedis = pool.getResource()) { + return jedis.exists(initedKey()); + } + } + + @Override + public boolean isStoreAvailable() { + try { + isInitialized(); // don't care about the return value, just that it doesn't throw an exception + return true; + } catch (Exception e) { // don't care about exception class, since any exception means the Redis request couldn't be made + return false; + } + } + + // package-private for testing + void setUpdateListener(UpdateListener updateListener) { + this.updateListener = updateListener; + } + + private String itemsKey(DataKind kind) { + return prefix + ":" + kind.getName(); + } + + private String initedKey() { + return prefix + ":$inited"; + } + + private String getRedis(DataKind kind, String key, Jedis jedis) { + String json = jedis.hget(itemsKey(kind), key); + + if (json == null) { + logger.debug("[get] Key: {} not found in \"{}\". Returning null", key, kind.getName()); + } + + return json; + } + + private static String jsonOrPlaceholder(DataKind kind, SerializedItemDescriptor serializedItem) { + String s = serializedItem.getSerializedItem(); + if (s != null) { + return s; + } + // For backward compatibility with previous implementations of the Redis integration, we must store a + // special placeholder string for deleted items. DataKind.serializeItem() will give us this string if + // we pass a deleted ItemDescriptor. + return kind.serialize(ItemDescriptor.deletedItem(serializedItem.getVersion())); + } + + static interface UpdateListener { + void aboutToUpdate(String baseKey, String itemKey); + } +} diff --git a/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisStoreBuilder.java b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisStoreBuilder.java new file mode 100644 index 0000000..d8df2ec --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisStoreBuilder.java @@ -0,0 +1,205 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.sdk.LDValue; +import com.launchdarkly.sdk.server.Components; +import com.launchdarkly.sdk.server.subsystems.BigSegmentStore; +import com.launchdarkly.sdk.server.subsystems.ClientContext; +import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; +import com.launchdarkly.sdk.server.subsystems.DiagnosticDescription; +import com.launchdarkly.sdk.server.subsystems.PersistentDataStore; + +import java.net.URI; +import java.time.Duration; + +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.Protocol; + +/** + * A builder for configuring the + * Redis-based persistent data store and/or Big Segment store. + *

+ * Both {@link Redis#dataStore()} and {@link Redis#bigSegmentStore()} return instances of + * this class. You can use methods of the builder to specify any non-default Redis options + * you may want, before passing the builder to either {@link Components#persistentDataStore(ComponentConfigurer)} + * or {@link Components#bigSegments(ComponentConfigurer)} as appropriate. The two types of + * stores are independent of each other; you do not need a Big Segment store if you are not + * using the Big Segments feature, and you do not need to use the same database for both. + * + * In this example, the main data store uses a Redis host called "host1", and the Big Segment + * store uses a Redis host called "host2": + *


+ *     LDConfig config = new LDConfig.Builder()
+ *         .dataStore(
+ *             Components.persistentDataStore(
+ *                 Redis.dataStore().uri(URI.create("redis://host1:6379")
+ *             )
+ *         )
+ *         .bigSegments(
+ *             Components.bigSegments(
+ *                 Redis.dataStore().uri(URI.create("redis://host2:6379")
+ *             )
+ *         )
+ *         .build();
+ * 
+ *

+ * Note that the SDK also has its own options related to data storage that are configured + * at a different level, because they are independent of what database is being used. For + * instance, the builder returned by {@link Components#persistentDataStore(ComponentConfigurer)} + * has options for caching: + *


+ *     LDConfig config = new LDConfig.Builder()
+ *         .dataStore(
+ *             Components.persistentDataStore(
+ *                 Redis.dataStore().uri(URI.create("redis://my-redis-host"))
+ *             ).cacheSeconds(15)
+ *         )
+ *         .build();
+ * 
+ * + * @param the component type that this builder is being used for + * + * @since 5.0.0 + */ +public abstract class RedisStoreBuilder implements ComponentConfigurer, DiagnosticDescription { + /** + * The default value for the Redis URI: {@code redis://localhost:6379} + */ + public static final URI DEFAULT_URI = URI.create("redis://localhost:6379"); + + /** + * The default value for {@link #prefix(String)}. + */ + public static final String DEFAULT_PREFIX = "launchdarkly"; + + URI uri = DEFAULT_URI; + String prefix = DEFAULT_PREFIX; + Duration connectTimeout = Duration.ofMillis(Protocol.DEFAULT_TIMEOUT); + Duration socketTimeout = Duration.ofMillis(Protocol.DEFAULT_TIMEOUT); + Integer database = null; + String password = null; + boolean tls = false; + JedisPoolConfig poolConfig = null; + + // These constructors are called only from Implementations + RedisStoreBuilder() { + } + + /** + * Specifies the database number to use. + *

+ * The database number can also be specified in the Redis URI, in the form {@code redis://host:port/NUMBER}. Any + * non-null value that you set with {@link #database(Integer)} will override the URI. + * + * @param database the database number, or null to fall back to the URI or the default + * @return the builder + */ + public RedisStoreBuilder database(Integer database) { + this.database = database; + return this; + } + + /** + * Specifies a password that will be sent to Redis in an AUTH command. + *

+ * It is also possible to include a password in the Redis URI, in the form {@code redis://:PASSWORD@host:port}. Any + * password that you set with {@link #password(String)} will override the URI. + * + * @param password the password + * @return the builder + */ + public RedisStoreBuilder password(String password) { + this.password = password; + return this; + } + + /** + * Optionally enables TLS for secure connections to Redis. + *

+ * This is equivalent to specifying a Redis URI that begins with {@code rediss:} rather than {@code redis:}. + *

+ * Note that not all Redis server distributions support TLS. + * + * @param tls true to enable TLS + * @return the builder + */ + public RedisStoreBuilder tls(boolean tls) { + this.tls = tls; + return this; + } + + /** + * Specifies a Redis host URI other than {@link #DEFAULT_URI}. + * + * @param redisUri the URI of the Redis host + * @return the builder + */ + public RedisStoreBuilder uri(URI redisUri) { + this.uri = redisUri; + return this; + } + + /** + * Optionally configures the namespace prefix for all keys stored in Redis. + * + * @param prefix the namespace prefix + * @return the builder + */ + public RedisStoreBuilder prefix(String prefix) { + this.prefix = prefix; + return this; + } + + /** + * Optional override if you wish to specify your own configuration to the underlying Jedis pool. + * + * @param poolConfig the Jedis pool configuration. + * @return the builder + */ + public RedisStoreBuilder poolConfig(JedisPoolConfig poolConfig) { + this.poolConfig = poolConfig; + return this; + } + + /** + * Optional override which sets the connection timeout for the underlying Jedis pool which otherwise defaults to + * {@link redis.clients.jedis.Protocol#DEFAULT_TIMEOUT} milliseconds. + * + * @param connectTimeout the timeout + * @return the builder + */ + public RedisStoreBuilder connectTimeout(Duration connectTimeout) { + this.connectTimeout = connectTimeout == null ? Duration.ofMillis(Protocol.DEFAULT_TIMEOUT) : connectTimeout; + return this; + } + + /** + * Optional override which sets the connection timeout for the underlying Jedis pool which otherwise defaults to + * {@link redis.clients.jedis.Protocol#DEFAULT_TIMEOUT} milliseconds. + * + * @param socketTimeout the socket timeout + * @return the builder + */ + public RedisStoreBuilder socketTimeout(Duration socketTimeout) { + this.socketTimeout = socketTimeout == null ? Duration.ofMillis(Protocol.DEFAULT_TIMEOUT) : socketTimeout; + return this; + } + + @Override + public LDValue describeConfiguration(ClientContext clientContext) { + return LDValue.of("Redis"); + } + + static final class ForDataStore extends RedisStoreBuilder { + @Override + public PersistentDataStore build(ClientContext clientContext) { + return new RedisDataStoreImpl(this, clientContext.getBaseLogger()); + } + } + + static final class ForBigSegments extends RedisStoreBuilder { + @Override + public BigSegmentStore build(ClientContext clientContext) { + return new RedisBigSegmentStoreImpl(this, clientContext.getBaseLogger()); + } + } +} diff --git a/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisStoreImplBase.java b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisStoreImplBase.java new file mode 100644 index 0000000..fa1ff5c --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisStoreImplBase.java @@ -0,0 +1,59 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.logging.LDLogger; + +import java.io.Closeable; +import java.io.IOException; + +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +abstract class RedisStoreImplBase implements Closeable { + protected final LDLogger logger; + protected final JedisPool pool; + protected final String prefix; + + protected RedisStoreImplBase(RedisStoreBuilder builder, LDLogger logger) { + this.logger = logger; + + // There is no builder for JedisPool, just a large number of constructor overloads. Unfortunately, + // the overloads that accept a URI do not accept the other parameters we need to set, so we need + // to decompose the URI. + String host = builder.uri.getHost(); + int port = builder.uri.getPort(); + String password = builder.password == null ? RedisURIComponents.getPassword(builder.uri) : builder.password; + int database = builder.database == null ? RedisURIComponents.getDBIndex(builder.uri) : builder.database; + boolean tls = builder.tls || builder.uri.getScheme().equals("rediss"); + + String extra = tls ? " with TLS" : ""; + if (password != null) { + extra = extra + (extra.isEmpty() ? " with" : " and") + " password"; + } + logger.info("Using Redis data store at {}:{}/{}{}", host, port, database, extra); + + JedisPoolConfig poolConfig = (builder.poolConfig != null) ? builder.poolConfig : new JedisPoolConfig(); + + this.prefix = (builder.prefix == null || builder.prefix.isEmpty()) ? + RedisStoreBuilder.DEFAULT_PREFIX : + builder.prefix; + this.pool = new JedisPool(poolConfig, + host, + port, + (int) builder.connectTimeout.toMillis(), + (int) builder.socketTimeout.toMillis(), + password, + database, + null, // clientName + tls, + null, // sslSocketFactory + null, // sslParameters + null // hostnameVerifier + ); + } + + @Override + public void close() throws IOException { + logger.info("Closing Redis store"); + pool.destroy(); + } +} diff --git a/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisURIComponents.java b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisURIComponents.java new file mode 100644 index 0000000..3c39ccc --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/main/java/com/launchdarkly/sdk/server/integrations/RedisURIComponents.java @@ -0,0 +1,26 @@ +package com.launchdarkly.sdk.server.integrations; + +import java.net.URI; + +/** + * This class contains methods equivalent to those in JedisURIHelper. Avoiding the use of + * JedisURIHelper allows us to be compatible with both Jedis 2.x and Jedis 3.x, because + * that class doesn't exist in the same location in both versions. + */ +abstract class RedisURIComponents { + static String getPassword(URI uri) { + if (uri.getUserInfo() == null) { + return null; + } + String[] parts = uri.getUserInfo().split(":", 2); + return parts.length < 2 ? null : parts[1]; + } + + static int getDBIndex(URI uri) { + String[] parts = uri.getPath().split("/", 2); + if (parts.length < 2 || parts[1].isEmpty()) { + return 0; + } + return Integer.parseInt(parts[1]); + } +} diff --git a/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisBigSegmentStoreImplTest.java b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisBigSegmentStoreImplTest.java new file mode 100644 index 0000000..0ece4be --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisBigSegmentStoreImplTest.java @@ -0,0 +1,51 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.sdk.server.subsystems.BigSegmentStore; +import com.launchdarkly.sdk.server.subsystems.BigSegmentStoreTypes; +import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; + +import redis.clients.jedis.Jedis; + +@SuppressWarnings("javadoc") +public class RedisBigSegmentStoreImplTest extends BigSegmentStoreTestBase { + + @Override + protected ComponentConfigurer makeStore(String prefix) { + return Redis.bigSegmentStore().prefix(prefix); + } + + @Override + protected void clearData(String prefix) { + prefix = prefix == null || prefix.isEmpty() ? RedisStoreBuilder.DEFAULT_PREFIX : prefix; + try (Jedis client = new Jedis("localhost")) { + for (String key : client.keys(prefix + ":*")) { + client.del(key); + } + } + } + + @Override + protected void setMetadata(String prefix, BigSegmentStoreTypes.StoreMetadata storeMetadata) { + try (Jedis client = new Jedis("localhost")) { + client.set(prefix + ":big_segments_synchronized_on", + storeMetadata != null ? Long.toString(storeMetadata.getLastUpToDate()) : ""); + } + } + + @Override + protected void setSegments(String prefix, + String userHashKey, + Iterable includedSegmentRefs, + Iterable excludedSegmentRefs) { + try (Jedis client = new Jedis("localhost")) { + String includeKey = prefix + ":big_segment_include:" + userHashKey; + String excludeKey = prefix + ":big_segment_exclude:" + userHashKey; + for (String includedSegmentRef : includedSegmentRefs) { + client.sadd(includeKey, includedSegmentRef); + } + for (String excludedSegmentRef : excludedSegmentRefs) { + client.sadd(excludeKey, excludedSegmentRef); + } + } + } +} diff --git a/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreBuilderTest.java b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreBuilderTest.java new file mode 100644 index 0000000..45b840a --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreBuilderTest.java @@ -0,0 +1,81 @@ +package com.launchdarkly.sdk.server.integrations; + +import org.junit.Test; + +import java.net.URI; +import java.net.URISyntaxException; +import java.time.Duration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.Protocol; + +@SuppressWarnings("javadoc") +public class RedisDataStoreBuilderTest { + @Test + public void testDefaultValues() { + RedisStoreBuilder conf = Redis.dataStore(); + assertEquals(RedisStoreBuilder.DEFAULT_URI, conf.uri); + assertNull(conf.database); + assertNull(conf.password); + assertFalse(conf.tls); + assertEquals(Duration.ofMillis(Protocol.DEFAULT_TIMEOUT), conf.connectTimeout); + assertEquals(Duration.ofMillis(Protocol.DEFAULT_TIMEOUT), conf.socketTimeout); + assertEquals(RedisStoreBuilder.DEFAULT_PREFIX, conf.prefix); + assertNull(conf.poolConfig); + } + + @Test + public void testUriConfigured() { + URI uri = URI.create("redis://other:9999"); + RedisStoreBuilder conf = Redis.dataStore().uri(uri); + assertEquals(uri, conf.uri); + } + + @Test + public void testDatabaseConfigured() { + RedisStoreBuilder conf = Redis.dataStore().database(3); + assertEquals(Integer.valueOf(3), conf.database); + } + + @Test + public void testPasswordConfigured() { + RedisStoreBuilder conf = Redis.dataStore().password("secret"); + assertEquals("secret", conf.password); + } + + @Test + public void testTlsConfigured() { + RedisStoreBuilder conf = Redis.dataStore().tls(true); + assertTrue(conf.tls); + } + + @Test + public void testPrefixConfigured() throws URISyntaxException { + RedisStoreBuilder conf = Redis.dataStore().prefix("prefix"); + assertEquals("prefix", conf.prefix); + } + + @Test + public void testConnectTimeoutConfigured() throws URISyntaxException { + RedisStoreBuilder conf = Redis.dataStore().connectTimeout(Duration.ofSeconds(1)); + assertEquals(Duration.ofSeconds(1), conf.connectTimeout); + } + + @Test + public void testSocketTimeoutConfigured() throws URISyntaxException { + RedisStoreBuilder conf = Redis.dataStore().socketTimeout(Duration.ofSeconds(1)); + assertEquals(Duration.ofSeconds(1), conf.socketTimeout); + } + + @Test + public void testPoolConfigConfigured() throws URISyntaxException { + JedisPoolConfig poolConfig = new JedisPoolConfig(); + RedisStoreBuilder conf = Redis.dataStore().poolConfig(poolConfig); + assertEquals(poolConfig, conf.poolConfig); + } +} diff --git a/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreImplTest.java b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreImplTest.java new file mode 100644 index 0000000..63e7f08 --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisDataStoreImplTest.java @@ -0,0 +1,38 @@ +package com.launchdarkly.sdk.server.integrations; + +import com.launchdarkly.sdk.server.integrations.RedisDataStoreImpl.UpdateListener; +import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; +import com.launchdarkly.sdk.server.subsystems.PersistentDataStore; + +import java.net.URI; + +import redis.clients.jedis.Jedis; + +@SuppressWarnings("javadoc") +public class RedisDataStoreImplTest extends PersistentDataStoreTestBase { + + private static final URI REDIS_URI = URI.create("redis://localhost:6379"); + + @Override + protected ComponentConfigurer buildStore(String prefix) { + return Redis.dataStore().uri(REDIS_URI).prefix(prefix); + } + + @Override + protected void clearAllData() { + try (Jedis client = new Jedis("localhost")) { + client.flushDB(); + } + } + + @Override + protected boolean setUpdateHook(RedisDataStoreImpl storeUnderTest, final Runnable hook) { + storeUnderTest.setUpdateListener(new UpdateListener() { + @Override + public void aboutToUpdate(String baseKey, String itemKey) { + hook.run(); + } + }); + return true; + } +} diff --git a/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisURIComponentsTest.java b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisURIComponentsTest.java new file mode 100644 index 0000000..daaca7f --- /dev/null +++ b/lib/java-server-sdk-redis-store/src/test/java/com/launchdarkly/sdk/server/integrations/RedisURIComponentsTest.java @@ -0,0 +1,45 @@ +package com.launchdarkly.sdk.server.integrations; + +import org.junit.Test; + +import java.net.URI; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class RedisURIComponentsTest { + @Test + public void getPasswordForURIWithoutUserInfo() { + assertNull(RedisURIComponents.getPassword(URI.create("redis://hostname:6379"))); + } + + @Test + public void getPasswordForURIWithUsernameAndNoPassword() { + assertNull(RedisURIComponents.getPassword(URI.create("redis://username@hostname:6379"))); + } + + @Test + public void getPasswordForURIWithUsernameAndPassword() { + assertEquals("secret", RedisURIComponents.getPassword(URI.create("redis://username:secret@hostname:6379"))); + } + + @Test + public void getPasswordForURIWithPasswordAndNoUsername() { + assertEquals("secret", RedisURIComponents.getPassword(URI.create("redis://:secret@hostname:6379"))); + } + + @Test + public void getDBIndexForURIWithoutPath() { + assertEquals(0, RedisURIComponents.getDBIndex(URI.create("redis://hostname:6379"))); + } + + @Test + public void getDBIndexForURIWithRootPath() { + assertEquals(0, RedisURIComponents.getDBIndex(URI.create("redis://hostname:6379/"))); + } + + @Test + public void getDBIndexForURIWithNumberInPath() { + assertEquals(2, RedisURIComponents.getDBIndex(URI.create("redis://hostname:6379/2"))); + } +} diff --git a/release-please-config.json b/release-please-config.json index 0b476be..aafc294 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -11,6 +11,14 @@ "gradle.properties" ] }, + "lib/java-server-sdk-redis-store": { + "release-type": "simple", + "bump-minor-pre-major": true, + "include-v-in-tag": false, + "extra-files": [ + "gradle.properties" + ] + }, "lib/shared/internal": { "release-type": "simple", "bump-minor-pre-major": true,