Commit c27be87a authored by alinamihaila's avatar alinamihaila

Add redis hll key format validation

parent 5a27361c
......@@ -2,6 +2,8 @@
module Gitlab
module Redis
KeyFormatError = Class.new(StandardError)
class HLL
def self.count(params)
self.new.count(params)
......@@ -11,8 +13,6 @@ module Gitlab
self.new.add(params)
end
# NOTE: It is important to make sure the keys are in the same hash slot
# https://redis.io/topics/cluster-spec#keys-hash-tags
def count(keys:)
Gitlab::Redis::SharedState.with do |redis|
redis.pfcount(*keys)
......@@ -20,6 +20,10 @@ module Gitlab
end
def add(key:, value:, expiry:)
unless %r{\A.*\{.*\}.*\z}.match?(key)
raise KeyFormatError.new("Invalid key format. #{key} key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html#multi-key-commands")
end
Gitlab::Redis::SharedState.with do |redis|
redis.multi do |multi|
multi.pfadd(key, value)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do
describe '.add' do
it 'raise an error when using an invalid key format' do
expect { described_class.add(key: 'test', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::KeyFormatError)
expect { described_class.add(key: 'test-{metric', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::KeyFormatError)
end
it "doesn't raise error when having correct format" do
expect { described_class.add(key: 'test-{metric}', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: 'test-{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment