Skip to content

Commit

Permalink
Merge branch 'master' into handle-string-cluster-shards
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Feb 11, 2024
2 parents 684d138 + c6fee05 commit c44b5ef
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 128 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ jobs:
strategy:
matrix:
include:
- elixir: 1.14.x
otp: 25.x
- elixir: 1.16.x
otp: 26.x
os: 'ubuntu-latest'
style: true
coverage: true
dialyzer: true
- elixir: 1.13.x
- elixir: 1.15.x
otp: 26.x
os: 'ubuntu-latest'
- elixir: 1.14.x
otp: 24.x
os: 'ubuntu-latest'
- elixir: 1.11.x
- elixir: 1.12.x
otp: 23.x
os: 'ubuntu-20.04'
inch-report: true

env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
Expand All @@ -41,8 +43,8 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Start Redis
run: docker-compose up -d
- name: Start Docker
run: docker-compose up --detach

- name: Install OTP and Elixir
uses: erlef/setup-beam@v1
Expand Down
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.16.1
erlang 26.2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defp deps do
[
{:nebulex_redis_adapter, "~> 2.3"},
{:crc, "~> 0.10"}, #=> Needed when using Redis Cluster
{:jchash, "~> 0.1.3"} #=> Needed when using consistent-hashing
{:jchash, "~> 0.1.4"} #=> Needed when using consistent-hashing
]
end
```
Expand Down
18 changes: 0 additions & 18 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,3 @@ config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.RedisClusterConnErr
# 127.0.0.1, since for tests we use Docker. For prod this should be false.
override_master_host: true
]

# Redis Cluster mode with custom Keyslot
config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.RedisClusterWithKeyslot,
mode: :redis_cluster,
pool_size: 2,
redis_cluster: [
# Configuration endpoints
configuration_endpoints: [
endpoint1_conn_opts: [
url: "redis://127.0.0.1:7000"
]
],
# Overrides the master host with the config endpoint, in this case with
# 127.0.0.1, since for tests we use Docker. For prod this should be false.
override_master_host: true,
# Custom keyslot
keyslot: NebulexRedisAdapter.TestCache.Keyslot
]
4 changes: 2 additions & 2 deletions coveralls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"skip_files": [
"lib/nebulex_redis_adapter/exceptions.ex",
"lib/nebulex_redis_adapter/helpers.ex",
"lib/nebulex_redis_adapter/serializer.ex",
"test/*"
],

"coverage_options": {
"minimum_coverage": 100
}
}
}
8 changes: 1 addition & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ services:
networks:
- redis_net

redis-cluster:
image: grokzen/redis-cluster:6.2.10
ports:
- '7000-7005:7000-7005'
environment:
- 'REDIS_CLUSTER_IP=0.0.0.0'

cluster-initiator:
image: 'redis:7-alpine'
command: >
Expand Down Expand Up @@ -125,6 +118,7 @@ volumes:
redis-node-4-data:
redis-node-5-data:


networks:
redis_net:
driver: bridge
Expand Down
26 changes: 15 additions & 11 deletions lib/nebulex_redis_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ defmodule NebulexRedisAdapter do

defp do_get_all(adapter_meta, keys, opts) do
keys
|> group_keys_by_hash_slot(adapter_meta)
|> group_keys_by_hash_slot(adapter_meta, :keys)
|> Enum.reduce(%{}, fn {hash_slot, keys}, acc ->
return = mget(hash_slot, adapter_meta, keys, opts)

Expand Down Expand Up @@ -599,7 +599,7 @@ defmodule NebulexRedisAdapter do

_ ->
entries
|> group_keys_by_hash_slot(adapter_meta)
|> group_keys_by_hash_slot(adapter_meta, :tuples)
|> Enum.reduce(:ok, fn {hash_slot, group}, acc ->
acc && do_put_all(adapter_meta, hash_slot, group, ttl, on_write, opts)
end)
Expand Down Expand Up @@ -782,7 +782,7 @@ defmodule NebulexRedisAdapter do
when is_list(keys) do
:ok =
keys
|> group_keys_by_hash_slot(adapter_meta)
|> group_keys_by_hash_slot(adapter_meta, :keys)
|> Enum.each(fn {hash_slot, keys_group} ->
Command.exec!(
adapter_meta,
Expand Down Expand Up @@ -876,16 +876,20 @@ defmodule NebulexRedisAdapter do
apply(RedisCluster, :exec!, args ++ extra_args)
end

defp group_keys_by_hash_slot(enum, %{
mode: :client_side_cluster,
nodes: nodes,
keyslot: keyslot
}) do
ClientCluster.group_keys_by_hash_slot(enum, nodes, keyslot)
defp group_keys_by_hash_slot(
enum,
%{
mode: :client_side_cluster,
nodes: nodes,
keyslot: keyslot
},
enum_type
) do
ClientCluster.group_keys_by_hash_slot(enum, nodes, keyslot, enum_type)
end

defp group_keys_by_hash_slot(enum, %{mode: :redis_cluster, keyslot: keyslot}) do
RedisCluster.group_keys_by_hash_slot(enum, keyslot)
defp group_keys_by_hash_slot(enum, %{mode: :redis_cluster, keyslot: keyslot}, enum_type) do
RedisCluster.group_keys_by_hash_slot(enum, keyslot, enum_type)
end

defp enc_key(%{serializer: serializer, encode_key_opts: enc_key_opts}, key) do
Expand Down
18 changes: 6 additions & 12 deletions lib/nebulex_redis_adapter/client_cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,13 @@ defmodule NebulexRedisAdapter.ClientCluster do
Pool.get_conn(registry, {name, node_name}, pool_size)
end

@spec group_keys_by_hash_slot(Enum.t(), nodes_config, module) :: map
def group_keys_by_hash_slot(enum, nodes, module) do
Enum.reduce(enum, %{}, fn
{key, _} = entry, acc ->
hash_slot = hash_slot(module, key, nodes)

Map.put(acc, hash_slot, [entry | Map.get(acc, hash_slot, [])])

key, acc ->
hash_slot = hash_slot(module, key, nodes)
@spec group_keys_by_hash_slot(Enum.t(), nodes_config, module, atom()) :: map
def group_keys_by_hash_slot(enum, nodes, module, :keys) do
Enum.group_by(enum, &hash_slot(module, &1, nodes))
end

Map.put(acc, hash_slot, [key | Map.get(acc, hash_slot, [])])
end)
def group_keys_by_hash_slot(enum, nodes, module, :tuples) do
Enum.group_by(enum, fn {key, _} -> hash_slot(module, key, nodes) end)
end

## Private Functions
Expand Down
2 changes: 2 additions & 0 deletions lib/nebulex_redis_adapter/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule NebulexRedisAdapter.Command do
Keyword.t()
) :: {:ok, term} | {:error, term}
def exec(adapter_meta, command, key \\ nil, opts \\ []) do
# TODO: Handle errors; especially for :redis_cluster mode
adapter_meta
|> conn(key, opts)
|> Redix.command(command, redis_command_opts(opts))
Expand Down Expand Up @@ -72,6 +73,7 @@ defmodule NebulexRedisAdapter.Command do
Keyword.t()
) :: {:ok, [term]} | {:error, term}
def pipeline(adapter_meta, commands, key \\ nil, opts \\ []) do
# TODO: Handle errors; especially for :redis_cluster mode
adapter_meta
|> conn(key, opts)
|> Redix.pipeline(commands, redis_command_opts(opts))
Expand Down
20 changes: 7 additions & 13 deletions lib/nebulex_redis_adapter/redis_cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,13 @@ defmodule NebulexRedisAdapter.RedisCluster do
end)
end

@spec group_keys_by_hash_slot(Enum.t(), module) :: map
def group_keys_by_hash_slot(enum, keyslot) do
Enum.reduce(enum, %{}, fn
{key, _} = entry, acc ->
slot = hash_slot(key, keyslot)

Map.put(acc, slot, [entry | Map.get(acc, slot, [])])

key, acc ->
slot = hash_slot(key, keyslot)
@spec group_keys_by_hash_slot(Enum.t(), module, atom()) :: map
def group_keys_by_hash_slot(enum, keyslot, :keys) do
Enum.group_by(enum, &hash_slot(&1, keyslot))
end

Map.put(acc, slot, [key | Map.get(acc, slot, [])])
end)
def group_keys_by_hash_slot(enum, keyslot, :tuples) do
Enum.group_by(enum, fn {key, _} -> hash_slot(key, keyslot) end)
end

@spec hash_slot(any, module) :: {:"$hash_slot", pos_integer}
Expand Down Expand Up @@ -154,7 +148,7 @@ defmodule NebulexRedisAdapter.RedisCluster do
|> :persistent_term.erase()
end

@spec with_retry(atom, pos_integer, (() -> term)) :: term
@spec with_retry(atom, pos_integer, (-> term)) :: term
def with_retry(name, retries, fun) do
with_retry(name, fun, retries, 1)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/nebulex_redis_adapter/redis_cluster/config_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ defmodule NebulexRedisAdapter.RedisCluster.ConfigManager do

Enum.reduce(config, [], fn
# Redis version >= 7 (["CLUSTER", "SHARDS"])
["slots", [start, stop], "nodes", nodes], acc ->
["slots", slot_ranges, "nodes", nodes], acc ->
case parse_node_attrs(nodes) do
[] ->
# coveralls-ignore-start
Expand All @@ -275,7 +275,9 @@ defmodule NebulexRedisAdapter.RedisCluster.ConfigManager do
host = attrs["endpoint"]
port = attrs["tls-port"] || attrs["port"]

[{start, stop, host, port} | acc]
slot_ranges
|> Enum.chunk_every(2)
|> Enum.reduce(acc, fn [start, stop], acc -> [{start, stop, host, port} | acc] end)
end

# Redis version < 7 (["CLUSTER", "SLOTS"])
Expand Down
18 changes: 9 additions & 9 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ defmodule NebulexRedisAdapter.MixProject do

@source_url "https://github.com/cabol/nebulex_redis_adapter"
@version "2.3.1"
@nbx_tag "2.5.2"
@nbx_vsn "2.5"
@nbx_tag "2.6.0"
@nbx_vsn "2.6"

def project do
[
app: :nebulex_redis_adapter,
version: @version,
elixir: "~> 1.9",
elixir: "~> 1.12",
elixirc_paths: elixirc_paths(Mix.env()),
aliases: aliases(),
deps: deps(),
Expand Down Expand Up @@ -50,24 +50,24 @@ defmodule NebulexRedisAdapter.MixProject do
defp deps do
[
nebulex_dep(),
{:redix, "~> 1.2"},
{:redix, "~> 1.3"},
{:nimble_options, "~> 0.5 or ~> 1.0"},
{:crc, "~> 0.10", optional: true},
{:jchash, "~> 0.1", optional: true},
{:telemetry, "~> 0.4 or ~> 1.0", optional: true},

# Test & Code Analysis
{:excoveralls, "~> 0.16", only: :test},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.2", only: [:dev, :test], runtime: false},
{:excoveralls, "~> 0.18", only: :test},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
{:mimic, "~> 1.7", only: :test},

# Benchmark Test
{:benchee, "~> 1.1", only: :test},
{:benchee, "~> 1.3", only: :test},
{:benchee_html, "~> 1.0", only: :test},

# Docs
{:ex_doc, "~> 0.29", only: [:dev, :test], runtime: false}
{:ex_doc, "~> 0.31", only: [:dev, :test], runtime: false}
]
end

Expand Down
Loading

0 comments on commit c44b5ef

Please sign in to comment.