Skip to content

Initial support for Redis cluster client. #52

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 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/test-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Test Cluster

on: [push, pull_request]

permissions:
contents: read

env:
CONSOLE_OUTPUT: XTerm

jobs:
test:
name: ${{matrix.ruby}} on ${{matrix.os}}
runs-on: ${{matrix.os}}-latest
continue-on-error: ${{matrix.experimental}}

strategy:
matrix:
os:
- ubuntu

ruby:
- "3.1"
- "3.2"
- "3.3"

experimental: [false]

steps:
- uses: actions/checkout@v4

- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose

- name: Run tests
timeout-minutes: 10
env:
RUBY_VERSION: ${{matrix.ruby}}
run: docker-compose -f cluster/docker-compose.yaml up --exit-code-from tests
32 changes: 32 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,42 @@ jobs:
name: coverage-${{matrix.os}}-${{matrix.ruby}}
path: .covered.db

test-cluster:
name: ${{matrix.ruby}} on ${{matrix.os}} (Cluster)
runs-on: ${{matrix.os}}-latest

strategy:
matrix:
os:
- ubuntu

ruby:
- "3.3"

steps:
- uses: actions/checkout@v4

- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose

- name: Run tests
timeout-minutes: 10
env:
RUBY_VERSION: ${{matrix.ruby}}
run: docker-compose -f cluster/docker-compose.yaml up --exit-code-from tests

- uses: actions/upload-artifact@v3
with:
name: coverage-${{matrix.os}}-${{matrix.ruby}}
path: .covered.db

validate:
needs:
- test
- test-sentinel
- test-cluster
runs-on: ubuntu-latest

steps:
Expand Down
43 changes: 43 additions & 0 deletions cluster/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
redis-a:
image: redis
command: redis-server /etc/redis/redis.conf
volumes:
- ./node-a/cluster.conf:/etc/redis/redis.conf
redis-b:
image: redis
command: redis-server /etc/redis/redis.conf
volumes:
- ./node-b/cluster.conf:/etc/redis/redis.conf
redis-c:
image: redis
command: redis-server /etc/redis/redis.conf
volumes:
- ./node-c/cluster.conf:/etc/redis/redis.conf
controller:
image: redis
command: >
bash -c "
redis-cli --cluster create --cluster-yes --cluster-replicas 0 redis-a:6379 redis-b:6379 redis-c:6379;
while true; do
redis-cli -h redis-a cluster info | grep cluster_state:fail;
sleep 1;
done"
healthcheck:
test: "redis-cli -h redis-a cluster info | grep cluster_state:ok"
interval: 1s
timeout: 3s
retries: 30
depends_on:
- redis-a
- redis-b
- redis-c
tests:
image: ruby:${RUBY_VERSION:-latest}
volumes:
- ../:/code
command: bash -c "cd /code && bundle install && bundle exec sus cluster/test"
environment:
- COVERAGE=${COVERAGE}
depends_on:
- controller
4 changes: 4 additions & 0 deletions cluster/node-a/cluster.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
4 changes: 4 additions & 0 deletions cluster/node-b/cluster.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
4 changes: 4 additions & 0 deletions cluster/node-c/cluster.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
25 changes: 25 additions & 0 deletions cluster/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Cluster Testing

To test clusters, you need to set up three redis instances (shards) and bind them together into a cluster.

## Running Tests

``` bash
$ cd cluster
$ docker-compose up tests
[+] Running 5/0
✔ Container cluster-redis-b-1 Running
✔ Container cluster-redis-c-1 Running
✔ Container cluster-redis-a-1 Running
✔ Container cluster-controller-1 Running
✔ Container cluster-tests-1 Created
Attaching to tests-1
tests-1 | Bundle complete! 13 Gemfile dependencies, 41 gems now installed.
tests-1 | Use `bundle info [gemname]` to see where a bundled gem is installed.
tests-1 | 6 installed gems you directly depend on are looking for funding.
tests-1 | Run `bundle fund` for details
tests-1 | 0 assertions
tests-1 | 🏁 Finished in 4.9ms; 0.0 assertions per second.
tests-1 | 🐇 No slow tests found! Well done!
tests-1 exited with code 0
```
46 changes: 46 additions & 0 deletions cluster/test/async/redis/cluster_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'async/redis/cluster_client'
require 'sus/fixtures/async'
require 'securerandom'

describe Async::Redis::ClusterClient do
include Sus::Fixtures::Async::ReactorContext

let(:node_a) {"redis://redis-a:6379"}
let(:node_b) {"redis://redis-b:6379"}
let(:node_c) {"redis://redis-c:6379"}

let(:endpoints) {[
Async::Redis::Endpoint.parse(node_a),
Async::Redis::Endpoint.parse(node_b),
Async::Redis::Endpoint.parse(node_c)
]}

let(:cluster) {subject.new(endpoints)}

let(:key) {"cluster-test:fixed"}
let(:value) {"cluster-test-value"}

it "can get a client for a given key" do
slot = cluster.slot_for(key)
client = cluster.client_for(slot)

expect(client).not.to be_nil
end

it "can get and set values" do
result = nil

cluster.clients_for(key) do |client|
client.set(key, value)

result = client.get(key)
end

expect(result).to be == value
end
end
2 changes: 2 additions & 0 deletions lib/async/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

require_relative 'redis/version'
require_relative 'redis/client'

require_relative 'redis/cluster_client'
require_relative 'redis/sentinel_client'
Loading
Loading