Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
de8fd561
Commit
de8fd561
authored
Oct 01, 2021
by
David Fernandez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move the cleanup policies cache object
It's not really a service so it's better placed in the "lib" folder
parent
f431f18e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
72 deletions
+74
-72
app/services/projects/container_repository/cache_tags_created_at_service.rb
...cts/container_repository/cache_tags_created_at_service.rb
+0
-70
app/services/projects/container_repository/cleanup_tags_service.rb
...ces/projects/container_repository/cleanup_tags_service.rb
+1
-1
lib/gitlab/container_repository/tags/cache.rb
lib/gitlab/container_repository/tags/cache.rb
+72
-0
spec/lib/gitlab/container_repository/tags/cache_spec.rb
spec/lib/gitlab/container_repository/tags/cache_spec.rb
+1
-1
No files found.
app/services/projects/container_repository/cache_tags_created_at_service.rb
deleted
100644 → 0
View file @
f431f18e
# frozen_string_literal: true
module
Projects
module
ContainerRepository
class
CacheTagsCreatedAtService
def
initialize
(
container_repository
)
@container_repository
=
container_repository
@cached_tag_names
=
Set
.
new
end
def
populate
(
tags
)
return
if
tags
.
empty?
# This will load all tags in one Redis roundtrip
# the maximum number of tags is configurable and is set to 200 by default.
# https://gitlab.com/gitlab-org/gitlab/blob/master/doc/user/packages/container_registry/index.md#set-cleanup-limits-to-conserve-resources
keys
=
tags
.
map
(
&
method
(
:cache_key
))
cached_tags_count
=
0
::
Gitlab
::
Redis
::
Cache
.
with
do
|
redis
|
tags
.
zip
(
redis
.
mget
(
keys
)).
each
do
|
tag
,
created_at
|
next
unless
created_at
tag
.
created_at
=
DateTime
.
rfc3339
(
created_at
)
@cached_tag_names
<<
tag
.
name
cached_tags_count
+=
1
end
end
cached_tags_count
end
def
insert
(
tags
,
max_ttl_in_seconds
)
return
unless
max_ttl_in_seconds
return
if
tags
.
empty?
# tags with nil created_at are not cacheable
# tags already cached don't need to be cached again
cacheable_tags
=
tags
.
select
do
|
tag
|
tag
.
created_at
.
present?
&&
!
tag
.
name
.
in?
(
@cached_tag_names
)
end
return
if
cacheable_tags
.
empty?
now
=
Time
.
zone
.
now
::
Gitlab
::
Redis
::
Cache
.
with
do
|
redis
|
# we use a pipeline instead of a MSET because each tag has
# a specific ttl
redis
.
pipelined
do
cacheable_tags
.
each
do
|
tag
|
created_at
=
tag
.
created_at
# ttl is the max_ttl_in_seconds reduced by the number
# of seconds that the tag has already existed
ttl
=
max_ttl_in_seconds
-
(
now
-
created_at
).
seconds
ttl
=
ttl
.
to_i
redis
.
set
(
cache_key
(
tag
),
created_at
.
rfc3339
,
ex:
ttl
)
if
ttl
>
0
end
end
end
end
private
def
cache_key
(
tag
)
"container_repository:{
#{
@container_repository
.
id
}
}:tag:
#{
tag
.
name
}
:created_at"
end
end
end
end
app/services/projects/container_repository/cleanup_tags_service.rb
View file @
de8fd561
...
...
@@ -140,7 +140,7 @@ module Projects
def
cache
strong_memoize
(
:cache
)
do
::
Projects
::
ContainerRepository
::
CacheTagsCreatedAtServic
e
.
new
(
@container_repository
)
::
Gitlab
::
ContainerRepository
::
Tags
::
Cach
e
.
new
(
@container_repository
)
end
end
...
...
lib/gitlab/container_repository/tags/cache.rb
0 → 100644
View file @
de8fd561
# frozen_string_literal: true
module
Gitlab
module
ContainerRepository
module
Tags
class
Cache
def
initialize
(
container_repository
)
@container_repository
=
container_repository
@cached_tag_names
=
Set
.
new
end
def
populate
(
tags
)
return
if
tags
.
empty?
# This will load all tags in one Redis roundtrip
# the maximum number of tags is configurable and is set to 200 by default.
# https://gitlab.com/gitlab-org/gitlab/blob/master/doc/user/packages/container_registry/index.md#set-cleanup-limits-to-conserve-resources
keys
=
tags
.
map
(
&
method
(
:cache_key
))
cached_tags_count
=
0
::
Gitlab
::
Redis
::
Cache
.
with
do
|
redis
|
tags
.
zip
(
redis
.
mget
(
keys
)).
each
do
|
tag
,
created_at
|
next
unless
created_at
tag
.
created_at
=
DateTime
.
rfc3339
(
created_at
)
@cached_tag_names
<<
tag
.
name
cached_tags_count
+=
1
end
end
cached_tags_count
end
def
insert
(
tags
,
max_ttl_in_seconds
)
return
unless
max_ttl_in_seconds
return
if
tags
.
empty?
# tags with nil created_at are not cacheable
# tags already cached don't need to be cached again
cacheable_tags
=
tags
.
select
do
|
tag
|
tag
.
created_at
.
present?
&&
!
tag
.
name
.
in?
(
@cached_tag_names
)
end
return
if
cacheable_tags
.
empty?
now
=
Time
.
zone
.
now
::
Gitlab
::
Redis
::
Cache
.
with
do
|
redis
|
# we use a pipeline instead of a MSET because each tag has
# a specific ttl
redis
.
pipelined
do
cacheable_tags
.
each
do
|
tag
|
created_at
=
tag
.
created_at
# ttl is the max_ttl_in_seconds reduced by the number
# of seconds that the tag has already existed
ttl
=
max_ttl_in_seconds
-
(
now
-
created_at
).
seconds
ttl
=
ttl
.
to_i
redis
.
set
(
cache_key
(
tag
),
created_at
.
rfc3339
,
ex:
ttl
)
if
ttl
>
0
end
end
end
end
private
def
cache_key
(
tag
)
"container_repository:{
#{
@container_repository
.
id
}
}:tag:
#{
tag
.
name
}
:created_at"
end
end
end
end
end
spec/
services/projects/container_repository/cache_tags_created_at_servic
e_spec.rb
→
spec/
lib/gitlab/container_repository/tags/cach
e_spec.rb
View file @
de8fd561
...
...
@@ -2,7 +2,7 @@
require
'spec_helper'
RSpec
.
describe
::
Projects
::
ContainerRepository
::
CacheTagsCreatedAtServic
e
,
:clean_gitlab_redis_cache
do
RSpec
.
describe
::
Gitlab
::
ContainerRepository
::
Tags
::
Cach
e
,
:clean_gitlab_redis_cache
do
let_it_be
(
:dummy_tag_class
)
{
Struct
.
new
(
:name
,
:created_at
)
}
let_it_be
(
:repository
)
{
create
(
:container_repository
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment