Skip to content
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

Fix cache trimming when not sharding #73

Merged
merged 1 commit into from
Sep 21, 2023
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
2 changes: 1 addition & 1 deletion lib/solid_cache/cluster/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def across_shards(list:, async: false)
end

def with_shard(shard, async: false)
if shard
if shard && shard != Entry.current_shard
Record.connected_to(shard: shard) do
configure_for_query(async: async) { yield }
end
Expand Down
6 changes: 5 additions & 1 deletion lib/solid_cache/cluster/trimming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def trim_batch
def trim_counters
# Pre-fill the first counter to prevent herding and to account
# for discarded counters from the last shutdown
@trim_counters ||= shards.to_h { |shard| [shard, Concurrent::AtomicFixnum.new(rand(trim_batch_size).to_i)] }
@trim_counters ||= if shards.any?
shards.to_h { |shard| [shard, Concurrent::AtomicFixnum.new(rand(trim_batch_size).to_i)] }
else
{ SolidCache::Record.default_shard => Concurrent::AtomicFixnum.new(rand(trim_batch_size).to_i) }
end
end

def cache_full?
Expand Down
16 changes: 16 additions & 0 deletions test/unit/trimming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ def test_trims_records_when_the_cache_is_full
assert_equal 2, SolidCache::Entry.count
end

def test_trims_records_no_shards
@cache = ActiveSupport::Cache.lookup_store(:solid_cache_store, trim_batch_size: 2, namespace: @namespace, max_entries: 2, cluster: { shards: [] })
@cache.write("foo", 1)
@cache.write("bar", 2)

sleep 0.1

@cache.write("baz", 3)
@cache.write("haz", 4)

sleep 0.1

# Two records have been deleted
assert_equal 2, SolidCache::Entry.count
end

def test_trims_old_records_multiple_shards
@cache = lookup_store(trim_batch_size: 2)
default_shard_keys, shard_one_keys = 20.times.map { |i| "key#{i}" }.partition { |key| @cache.primary_cluster.send(:shard_for_normalized_key, @cache.send(:normalize_key, key, {})) == :default }
Expand Down