-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Implement redis persistent store and big segment store. #146
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
Merged
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
94939a6
chore: Scaffold node redis store package.
kinyoklion f9c2bcd
Add note on store organization.
kinyoklion a6a3918
Better text.
kinyoklion e57b38e
new
kinyoklion c3263d8
Update disable cache example.
kinyoklion 73ac3bd
Add LICENSE.
kinyoklion e721f0c
chore: Scaffold node redis store package. (#136)
kinyoklion df18981
feat: Implement redis persistent store.
kinyoklion e444fbe
First draft of redis implementation.
kinyoklion 01bdc2c
Start adding tests.
kinyoklion 317f3e8
Work on tests
kinyoklion b958550
Recursive deps for build order.
kinyoklion bf07269
Fix update queue.
kinyoklion 0743f4f
Add remainder of feature store tests.
kinyoklion a44e72b
Add redis core tests.
kinyoklion 25eb6f9
Ignore redis files.
kinyoklion 33146ec
Lint
kinyoklion 708ed53
Add architecture diagram.
kinyoklion 69e3868
Add initial big segments store.
kinyoklion b8b6f1d
Add big segment store to diagram.
kinyoklion 3a1f9d2
Add description for BigSegmentStore.
kinyoklion 77b5974
Add big segments tests. Extend store tests to use multiple prefixes.
kinyoklion f6d0ab6
Linting
kinyoklion 1ab1ee9
Update CI configuration.
kinyoklion 0aca35b
merge feature branch
kinyoklion fa2aa4b
Enable workflow for branch for testing.
kinyoklion a5a3129
Add sudo commands.
kinyoklion 1903d66
Add dev dependency changes to CI build.
kinyoklion 58f2415
Remove running on branch.
kinyoklion 307c171
Correct typo in readme.
kinyoklion aac9142
Fix name of big segement store factory. Update index.
kinyoklion d5f989a
Merge branch 'rlamb/implement-redis-store' of github.com:launchdarkly…
kinyoklion 628a1ef
Change job name
kinyoklion 5c319ce
Rename exports for greater backward compatibility.
kinyoklion 68506c9
Merge branch 'main' into rlamb/implement-redis-store
kinyoklion 3445518
Update node server dep.
kinyoklion b9d8782
Catch on quit.
kinyoklion 57edf27
Fix big segment included/excluded keys.
kinyoklion 127b604
Add doc script.
kinyoklion a472f98
Merge branch 'main' into rlamb/feat/add-redis-persistent-store
kinyoklion e955907
Merge branch 'rlamb/feat/add-redis-persistent-store' into rlamb/imple…
kinyoklion a61bf36
Update node server package.
kinyoklion b466af8
Update packages/store/node-server-sdk-redis/src/RedisClientState.ts
kinyoklion e7c5492
Update packages/store/node-server-sdk-redis/src/RedisClientState.ts
kinyoklion 4b241c1
Update packages/store/node-server-sdk-redis/src/RedisCore.ts
kinyoklion 1c81d9e
Log ioredis quit errors.
kinyoklion 9e4fdbe
Merge branch 'main' into rlamb/implement-redis-store
kinyoklion 6b4c5fc
Merge branch 'main' into rlamb/feat/add-redis-persistent-store
kinyoklion 05cd22a
Merge branch 'rlamb/feat/add-redis-persistent-store' into rlamb/imple…
kinyoklion 8e5dacb
Merge branch 'rlamb/implement-redis-store' of github.com:launchdarkly…
kinyoklion 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
37 changes: 37 additions & 0 deletions
37
.github/ISSUE_TEMPLATE/package-stores-node-server-sdk-redis--bug_report.md
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,37 @@ | ||
| --- | ||
| name: '@launchdarkly/node-server-sdk-redis Bug Report' | ||
| about: Create a report to help us improve | ||
| title: '' | ||
| labels: 'package: store/node-server-sdk-redis, 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. |
20 changes: 20 additions & 0 deletions
20
.github/ISSUE_TEMPLATE/package-stores-node-server-sdk-redis--feature_request.md
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,20 @@ | ||
| --- | ||
| name: '@launchdarkly/node-server-sdk-redis Feature Request' | ||
| about: Suggest an idea for this project | ||
| title: '' | ||
| labels: 'package: store/node-server-sdk-redis, feature' | ||
| 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. |
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,33 @@ | ||
| name: store/node-server-sdk-redis | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| paths-ignore: | ||
| - '**.md' #Do not need to run CI for markdown changes. | ||
| pull_request: | ||
| branches: [main] | ||
| paths-ignore: | ||
| - '**.md' | ||
|
|
||
| jobs: | ||
| build-test-node-redis: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - uses: actions/setup-node@v3 | ||
| with: | ||
| node-version: 16 | ||
| registry-url: 'https://registry.npmjs.org' | ||
| # We may want to consider moving this build to a docker container instead of installing it | ||
| # in the image. | ||
| - run: | | ||
| sudo apt-get update | ||
| sudo apt-get install redis-server | ||
| sudo service redis-server start | ||
| - id: shared | ||
| name: Shared CI Steps | ||
| uses: ./actions/ci | ||
| with: | ||
| workspace_name: '@launchdarkly/node-server-sdk-redis' | ||
| workspace_path: packages/store/node-server-sdk-redis | ||
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 |
|---|---|---|
|
|
@@ -17,3 +17,5 @@ docs/ | |
| !.yarn/versions | ||
| yarn-error.log | ||
| .DS_Store | ||
| .vscode | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is in another PR, and will not be in the diff after it is merged and I update the base. |
||
| dump.rdb | ||
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
Empty file.
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,13 @@ | ||
| Copyright 2023 Catamorphic, Co. | ||
|
|
||
| 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 | ||
|
|
||
| http://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. |
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,89 @@ | ||
| # LaunchDarkly Server-Side SDK for Node.js | ||
|
|
||
| [![NPM][node-redis-npm-badge]][node-redis-npm-link] | ||
| [![Actions Status][node-redis-ci-badge]][node-redis-ci] | ||
| [](https://launchdarkly.github.io/js-core/packages/store/node-server-sdk-redis/docs/) | ||
|
|
||
| This library provides a Redis-backed persistence mechanism (feature store) for the [LaunchDarkly Node.js SDK](https://github.com/launchdarkly/js-core/packages/sdk/server-node), replacing the default in-memory feature store. The underlying Redis client implementation is [ioredis](https://github.com/luin/ioredis). | ||
|
|
||
| The minimum version of the LaunchDarkly Server-Side SDK for Node for use with this library is 8.0.0. | ||
|
|
||
| This SDK is a beta version and should not be considered ready for production use while this message is visible. | ||
|
|
||
| ## LaunchDarkly overview | ||
|
|
||
| [LaunchDarkly](https://www.launchdarkly.com) is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. [Get started](https://docs.launchdarkly.com/home/getting-started) using LaunchDarkly today! | ||
|
|
||
| [](https://twitter.com/intent/follow?screen_name=launchdarkly) | ||
|
|
||
| ## Supported Node versions | ||
|
|
||
| This package is compatible with Node.js versions 14 and above. | ||
|
|
||
| ## Getting started | ||
|
|
||
| Refer to [Using Redis as a persistent feature store](https://docs.launchdarkly.com/sdk/features/storing-data/redis#nodejs-server-side). | ||
|
|
||
| ## Quick setup | ||
|
|
||
| This assumes that you have already installed the LaunchDarkly Node.js SDK. | ||
|
|
||
| 1. Install this package with `npm` or `yarn`: | ||
|
|
||
| ```shell | ||
| npm install @launchdarkly/node-server-sdk-redis --save | ||
| ``` | ||
|
|
||
| 2. If your application does not already have its own dependency on the `ioredis` package, add `ioredis` as well: | ||
|
|
||
| ```shell | ||
| npm install ioredis --save | ||
| ``` | ||
|
|
||
| 3. Import the package: | ||
|
|
||
| ```typescript | ||
| import { RedisFeatureStoreFactory } = from '@launchdarkly/node-server-sdk-redis'; | ||
| ``` | ||
|
|
||
| 4. When configuring your SDK client, add the Redis feature store: | ||
|
|
||
| ```typescript | ||
| const storeFactory = RedisFeatureStoreFactory(); | ||
| const config = { featureStore: storeFactory }; | ||
| const client = LaunchDarkly.init('YOUR SDK KEY', config); | ||
| ``` | ||
|
|
||
| By default, the store will try to connect to a local Redis instance on port 6379. You may specify an alternate configuration as described in the API documentation for `RedisFeatureStoreFactory`. | ||
|
|
||
| ## Caching behavior | ||
|
|
||
| To reduce traffic to Redis, there is an optional in-memory cache that retains the last known data for a configurable amount of time. This is on by default; to turn it off (and guarantee that the latest feature flag data will always be retrieved from Redis for every flag evaluation), configure the store as follows: | ||
|
|
||
| ```typescript | ||
| const factory = RedisFeatureStoreFactory({ cacheTTL: 0 }); | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](CONTRIBUTING.md) for instructions on how to contribute to this SDK. | ||
|
|
||
| ## 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. Check out [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 | ||
|
|
||
| [node-redis-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/node-redis.yml/badge.svg | ||
| [node-redis-ci]: https://github.com/launchdarkly/js-core/actions/workflows/node-redis.yml | ||
|
|
||
| [node-redis-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/node-server-sdk-redis.svg?style=flat-square | ||
| [node-redis-npm-link]: https://www.npmjs.com/package/@launchdarkly/node-server-sdk-redis |
96 changes: 96 additions & 0 deletions
96
packages/store/node-server-sdk-redis/__tests__/RedisBigSegmentStore.test.ts
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,96 @@ | ||
| import Redis from 'ioredis'; | ||
| import { interfaces } from '@launchdarkly/node-server-sdk'; | ||
| import RedisBigSegmentStore, { | ||
| KEY_LAST_SYNCHRONIZED, | ||
| KEY_USER_INCLUDE, | ||
| KEY_USER_EXCLUDE, | ||
| } from '../src/RedisBigSegmentStore'; | ||
| import clearPrefix from './clearPrefix'; | ||
|
|
||
| const FAKE_HASH = 'userhash'; | ||
|
|
||
| async function setMetadata( | ||
| prefix: string, | ||
| metadata: interfaces.BigSegmentStoreMetadata | ||
| ): Promise<void> { | ||
| const client = new Redis(); | ||
| await client.set( | ||
| `${prefix}:${KEY_LAST_SYNCHRONIZED}`, | ||
| metadata.lastUpToDate ? metadata.lastUpToDate.toString() : '' | ||
| ); | ||
| await client.quit(); | ||
| } | ||
|
|
||
| async function setSegments( | ||
| prefix: string, | ||
| userHashKey: string, | ||
| included: string[], | ||
| excluded: string[] | ||
| ): Promise<void> { | ||
| const client = new Redis(); | ||
|
|
||
| // Generators and await in a loop, both of which eslint doesn't like. This is a test, and this | ||
| // is simpler. | ||
| /* eslint-disable */ | ||
| for (const ref of included) { | ||
| await client.sadd(`${prefix}:${KEY_USER_INCLUDE}:${userHashKey}`, ref); | ||
| } | ||
| for (const ref of excluded) { | ||
| await client.sadd(`${prefix}:${KEY_USER_EXCLUDE}:${userHashKey}`, ref); | ||
| } | ||
| /* eslint-enable */ | ||
| await client.quit(); | ||
| } | ||
|
|
||
| describe.each([undefined, 'app1'])('given a redis big segment store', (prefixParam) => { | ||
| let store: RedisBigSegmentStore; | ||
| const prefix = prefixParam || 'launchdarkly'; | ||
|
|
||
| beforeEach(async () => { | ||
| await clearPrefix(prefix); | ||
| // Use param directly to test undefined. | ||
| store = new RedisBigSegmentStore({ prefix: prefixParam }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| store.close(); | ||
| }); | ||
|
|
||
| it('can get populated meta data', async () => { | ||
| const expected = { lastUpToDate: 1234567890 }; | ||
| await setMetadata(prefix, expected); | ||
| const meta = await store.getMetadata(); | ||
| expect(meta).toEqual(expected); | ||
| }); | ||
|
|
||
| it('can get metadata when not populated', async () => { | ||
| const meta = await store.getMetadata(); | ||
| expect(meta?.lastUpToDate).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('can get user membership for a user which has no membership', async () => { | ||
| const membership = await store.getUserMembership(FAKE_HASH); | ||
| expect(membership).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('can get membership for a user that is only included', async () => { | ||
| await setSegments(prefix, FAKE_HASH, ['key1', 'key2'], []); | ||
|
|
||
| const membership = await store.getUserMembership(FAKE_HASH); | ||
| expect(membership).toEqual({ key1: true, key2: true }); | ||
| }); | ||
|
|
||
| it('can get membership for a user that is only excluded', async () => { | ||
| await setSegments(prefix, FAKE_HASH, [], ['key1', 'key2']); | ||
|
|
||
| const membership = await store.getUserMembership(FAKE_HASH); | ||
| expect(membership).toEqual({ key1: false, key2: false }); | ||
| }); | ||
|
|
||
| it('can get membership for a user that is included and excluded', async () => { | ||
| await setSegments(prefix, FAKE_HASH, ['key1', 'key2'], ['key2', 'key3']); | ||
|
|
||
| const membership = await store.getUserMembership(FAKE_HASH); | ||
| expect(membership).toEqual({ key1: true, key2: true, key3: false }); // include of key2 overrides exclude | ||
| }); | ||
| }); |
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.
The test suite needs a redis instance.