Skip to content

Commit 0b3a0e9

Browse files
committed
Reduce Redis key size
1 parent 155db16 commit 0b3a0e9

File tree

7 files changed

+90
-7
lines changed

7 files changed

+90
-7
lines changed

ruby/lib/ci/queue/redis.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require 'ci/queue/redis/supervisor'
1212
require 'ci/queue/redis/grind_supervisor'
1313
require 'ci/queue/redis/test_time_record'
14+
require 'ci/queue/redis/key_shortener'
1415

1516
module CI
1617
module Queue

ruby/lib/ci/queue/redis/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def measure
214214
end
215215

216216
def key(*args)
217-
['build', build_id, *args].join(':')
217+
KeyShortener.key(config.build_id, *args)
218218
end
219219

220220
def build_id

ruby/lib/ci/queue/redis/build_record.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,45 @@ def record_stats(stats, pipeline: redis)
138138
end
139139
end
140140

141+
SUFFIX_ALIASES = {
142+
'running' => 'r',
143+
'processed' => 'p',
144+
'queue' => 'q',
145+
'owners' => 'o',
146+
'error-reports' => 'e',
147+
'requeues-count' => 'rc',
148+
'assertions' => 'a',
149+
'errors' => 'er',
150+
'failures' => 'f',
151+
'skips' => 's',
152+
'requeues' => 'rq',
153+
'total_time' => 't',
154+
'test_failed_count' => 'fc',
155+
'completed' => 'c',
156+
'master-status' => 'm',
157+
'created-at' => 'ca',
158+
'workers' => 'w',
159+
'worker' => 'w',
160+
'warnings' => 'wn',
161+
'worker-errors' => 'we',
162+
'flaky-reports' => 'fl',
163+
}.freeze
164+
165+
# We're transforming the key to a shorter format to minimize inter zone network traffic.
166+
#
167+
# Strategy:
168+
# - Shorten prefix: 'b' instead of 'build'
169+
# - Hash UUID: 8-char MD5 instead of 36-char UUID
170+
# - Alias suffixes: single letters instead of full words
171+
#
172+
# Example:
173+
# build:unit:019aef0e-c010-433e-b706-c658d3c16372:running (55 bytes)
174+
# -> b:f03d3bef:r (13 bytes, 76% reduction)
141175
def key(*args)
142-
['build', config.build_id, *args].join(':')
176+
digest = Digest::MD5.hexdigest(config.build_id)[0..7]
177+
shortened_args = args.map { |arg| KeyShortener::SUFFIX_ALIASES[arg] || arg }
178+
179+
['b', digest, *shortened_args].join(':')
143180
end
144181
end
145182
end

ruby/lib/ci/queue/redis/grind_record.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def pop_warnings
5252
attr_reader :redis, :config
5353

5454
def key(*args)
55-
['build', config.build_id, *args].join(':')
55+
digest = Digest::MD5.hexdigest(config.build_id)[0..7]
56+
shortened_args = args.map { |arg| KeyShortener::SUFFIX_ALIASES[arg] || arg }
57+
58+
['b', digest, *shortened_args].join(':')
5659
end
5760

5861
def record_stats(stats, pipeline: redis)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
require 'digest/md5'
3+
4+
module CI
5+
module Queue
6+
module Redis
7+
module KeyShortener
8+
# Suffix mapping for common key patterns
9+
SUFFIX_ALIASES = {
10+
'running' => 'r',
11+
'processed' => 'p',
12+
'queue' => 'q',
13+
'owners' => 'o',
14+
'error-reports' => 'e',
15+
'requeues-count' => 'rc',
16+
'assertions' => 'a',
17+
'errors' => 'er',
18+
'failures' => 'f',
19+
'skips' => 's',
20+
'requeues' => 'rq',
21+
'total_time' => 't',
22+
'test_failed_count' => 'fc',
23+
'completed' => 'c',
24+
'master-status' => 'm',
25+
'created-at' => 'ca',
26+
'workers' => 'w',
27+
'worker' => 'w',
28+
'warnings' => 'wn',
29+
'worker-errors' => 'we',
30+
'flaky-reports' => 'fl',
31+
}.freeze
32+
33+
def self.key(build_id, *args)
34+
digest = Digest::MD5.hexdigest(build_id)[0..7]
35+
shortened_args = args.map { |arg| SUFFIX_ALIASES[arg] || arg }
36+
37+
['b', digest, *shortened_args].join(':')
38+
end
39+
end
40+
end
41+
end
42+
end

ruby/test/ci/queue/redis_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ def test_acknowledge_returns_false_if_the_test_was_picked_up_by_another_worker
207207
end
208208

209209
def test_workers_register
210-
assert_equal 1, @redis.scard(('build:42:workers'))
210+
assert_equal 1, @redis.scard(CI::Queue::Redis::KeyShortener.key('42', 'workers'))
211211
worker(2)
212-
assert_equal 2, @redis.scard(('build:42:workers'))
212+
assert_equal 2, @redis.scard(CI::Queue::Redis::KeyShortener.key('42', 'workers'))
213213
end
214214

215215
def test_timeout_warning

ruby/test/integration/minitest_redis_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_debug_log
139139
)
140140
end
141141

142-
assert_includes File.read(log_file.path), 'INFO -- : Finished \'["exists", "build:1:worker:1:queue"]\': 0'
142+
assert_includes File.read(log_file.path), 'INFO -- : Finished \'["exists", "b:c4ca4238:w:1:q"]\': 0'
143143
assert_empty err
144144
result = normalize(out.lines.last.strip)
145145
assert_equal '--- Ran 11 tests, 8 assertions, 2 failures, 1 errors, 1 skips, 4 requeues in X.XXs', result
@@ -444,7 +444,7 @@ def test_retry_fails_when_test_run_is_expired
444444
assert_equal 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues in X.XXs', output
445445

446446
one_day = 60 * 60 * 24
447-
key = ['build', "1", "created-at"].join(':')
447+
key = CI::Queue::Redis::KeyShortener.key("1", "created-at")
448448
@redis.set(key, Time.now - one_day)
449449

450450
out, err = capture_subprocess_io do

0 commit comments

Comments
 (0)