Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
matrix-media-repo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
matrix-media-repo
Commits
09f94b47
Commit
09f94b47
authored
5 years ago
by
Travis Ralston
Browse files
Options
Downloads
Patches
Plain Diff
Clear the cache's space more efficiently
parent
041d1f0c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CHANGELOG.md
+5
-1
5 additions, 1 deletion
CHANGELOG.md
internal_cache/media_cache.go
+11
-11
11 additions, 11 deletions
internal_cache/media_cache.go
with
16 additions
and
12 deletions
CHANGELOG.md
+
5
−
1
View file @
09f94b47
...
@@ -9,13 +9,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
...
@@ -9,13 +9,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added
### Added
*
Added support for a
`forKinds: ["all"]`
option on datastores.
## [1.0.2] - March 3, 2020
## [1.0.2] - March 3, 2020
### Added
*
Added support for a
`forKinds: ["all"]`
option on datastores.
### Fixed
### Fixed
*
Fixed a bug with the cache where it would never expire old entries unless it was pressed for space.
*
Fixed a bug with the cache where it would never expire old entries unless it was pressed for space.
*
Fixed a bug with the cache where the minimum cache time trigger would not work.
## [1.0.1] - February 27, 2020
## [1.0.1] - February 27, 2020
...
...
This diff is collapsed.
Click to expand it.
internal_cache/media_cache.go
+
11
−
11
View file @
09f94b47
...
@@ -70,12 +70,11 @@ func Get() *MediaCache {
...
@@ -70,12 +70,11 @@ func Get() *MediaCache {
go
func
()
{
go
func
()
{
rctx
:=
rcontext
.
Initial
()
.
LogWithFields
(
logrus
.
Fields
{
"task"
:
"cache_cleanup"
})
rctx
:=
rcontext
.
Initial
()
.
LogWithFields
(
logrus
.
Fields
{
"task"
:
"cache_cleanup"
})
for
_
=
range
instance
.
cleanupTimer
.
C
{
for
_
=
range
instance
.
cleanupTimer
.
C
{
rctx
.
Log
.
Info
(
"Cleanup timer fired
at
"
)
rctx
.
Log
.
Info
(
"C
ache c
leanup timer fired"
)
maxSize
:=
config
.
Get
()
.
Downloads
.
Cache
.
MaxSizeBytes
maxSize
:=
config
.
Get
()
.
Downloads
.
Cache
.
MaxSizeBytes
maxDownloads
:=
config
.
Get
()
.
Downloads
.
Cache
.
MinDownloads
*
10
b
:=
instance
.
clearSpace
(
maxSize
,
ma
xDownloads
,
maxSize
,
rctx
)
b
:=
instance
.
clearSpace
(
maxSize
,
ma
th
.
MaxInt32
,
maxSize
,
true
,
rctx
)
rctx
.
Log
.
Infof
(
"Cleared %d bytes from cache during cleanup
"
,
b
)
rctx
.
Log
.
Infof
(
"Cleared %d bytes from cache during cleanup
(%d bytes remain)"
,
b
,
instance
.
size
)
}
}
}()
}()
}
}
...
@@ -204,7 +203,7 @@ func (c *MediaCache) updateItemInCache(recordId string, mediaSize int64, cacheFn
...
@@ -204,7 +203,7 @@ func (c *MediaCache) updateItemInCache(recordId string, mediaSize int64, cacheFn
// We need to clean up some space
// We need to clean up some space
maxSizeClear
:=
int64
(
math
.
Ceil
(
float64
(
mediaSize
)
*
1.25
))
maxSizeClear
:=
int64
(
math
.
Ceil
(
float64
(
mediaSize
)
*
1.25
))
ctx
.
Log
.
Info
(
fmt
.
Sprintf
(
"Attempting to clear %d bytes from media cache (max evict size %d bytes)"
,
mediaSize
,
maxSizeClear
))
ctx
.
Log
.
Info
(
fmt
.
Sprintf
(
"Attempting to clear %d bytes from media cache (max evict size %d bytes)"
,
mediaSize
,
maxSizeClear
))
clearedSpace
:=
c
.
clearSpace
(
mediaSize
,
downloads
,
maxSizeClear
,
ctx
)
clearedSpace
:=
c
.
clearSpace
(
mediaSize
,
downloads
,
maxSizeClear
,
false
,
ctx
)
ctx
.
Log
.
Info
(
fmt
.
Sprintf
(
"Cleared %d bytes from media cache"
,
clearedSpace
))
ctx
.
Log
.
Info
(
fmt
.
Sprintf
(
"Cleared %d bytes from media cache"
,
clearedSpace
))
freeSpace
+=
clearedSpace
freeSpace
+=
clearedSpace
if
freeSpace
>=
mediaSize
{
if
freeSpace
>=
mediaSize
{
...
@@ -233,7 +232,7 @@ func (c *MediaCache) updateItemInCache(recordId string, mediaSize int64, cacheFn
...
@@ -233,7 +232,7 @@ func (c *MediaCache) updateItemInCache(recordId string, mediaSize int64, cacheFn
// set the maximum file size that can be cleared to the size of the cache which
// set the maximum file size that can be cleared to the size of the cache which
// essentially allows us to remove anything.
// essentially allows us to remove anything.
downloadsLessThan
:=
config
.
Get
()
.
Downloads
.
Cache
.
MinDownloads
*
4
downloadsLessThan
:=
config
.
Get
()
.
Downloads
.
Cache
.
MinDownloads
*
4
overageCleared
:=
c
.
clearSpace
(
overage
,
downloadsLessThan
,
maxSpace
,
ctx
)
// metrics handled internally
overageCleared
:=
c
.
clearSpace
(
overage
,
downloadsLessThan
,
maxSpace
,
true
,
ctx
)
// metrics handled internally
ctx
.
Log
.
Infof
(
"Cleared %d bytes from media cache"
,
overageCleared
)
ctx
.
Log
.
Infof
(
"Cleared %d bytes from media cache"
,
overageCleared
)
}
}
...
@@ -253,7 +252,7 @@ func (c *MediaCache) updateItemInCache(recordId string, mediaSize int64, cacheFn
...
@@ -253,7 +252,7 @@ func (c *MediaCache) updateItemInCache(recordId string, mediaSize int64, cacheFn
return
nil
,
nil
return
nil
,
nil
}
}
func
(
c
*
MediaCache
)
clearSpace
(
neededBytes
int64
,
withDownloadsLessThan
int
,
withSizeLessThan
int64
,
ctx
rcontext
.
RequestContext
)
int64
{
func
(
c
*
MediaCache
)
clearSpace
(
neededBytes
int64
,
withDownloadsLessThan
int
,
withSizeLessThan
int64
,
deleteEvenIfNotEnough
bool
,
ctx
rcontext
.
RequestContext
)
int64
{
// This should never happen, but we'll protect against it anyways. If we clear negative space we
// This should never happen, but we'll protect against it anyways. If we clear negative space we
// end up assuming that a very small amount being cleared is enough space for the file we're about
// end up assuming that a very small amount being cleared is enough space for the file we're about
// to put in, which results in the cache growing beyond the file size limit.
// to put in, which results in the cache growing beyond the file size limit.
...
@@ -271,9 +270,6 @@ func (c *MediaCache) clearSpace(neededBytes int64, withDownloadsLessThan int, wi
...
@@ -271,9 +270,6 @@ func (c *MediaCache) clearSpace(neededBytes int64, withDownloadsLessThan int, wi
var
preppedSpace
int64
=
0
var
preppedSpace
int64
=
0
for
k
,
item
:=
range
c
.
cache
.
Items
()
{
for
k
,
item
:=
range
c
.
cache
.
Items
()
{
record
:=
item
.
Object
.
(
*
cachedFile
)
record
:=
item
.
Object
.
(
*
cachedFile
)
if
int64
(
record
.
Contents
.
Len
())
>=
withSizeLessThan
{
continue
// file too large, cannot evict
}
var
recordId
string
var
recordId
string
if
record
.
thumbnail
!=
nil
{
if
record
.
thumbnail
!=
nil
{
...
@@ -282,6 +278,10 @@ func (c *MediaCache) clearSpace(neededBytes int64, withDownloadsLessThan int, wi
...
@@ -282,6 +278,10 @@ func (c *MediaCache) clearSpace(neededBytes int64, withDownloadsLessThan int, wi
recordId
=
record
.
media
.
Sha256Hash
recordId
=
record
.
media
.
Sha256Hash
}
}
if
int64
(
record
.
Contents
.
Len
())
>=
withSizeLessThan
{
continue
// file too large, cannot evict
}
downloads
:=
c
.
tracker
.
NumDownloads
(
recordId
)
downloads
:=
c
.
tracker
.
NumDownloads
(
recordId
)
if
downloads
>=
withDownloadsLessThan
{
if
downloads
>=
withDownloadsLessThan
{
continue
// too many downloads, cannot evict
continue
// too many downloads, cannot evict
...
@@ -299,7 +299,7 @@ func (c *MediaCache) clearSpace(neededBytes int64, withDownloadsLessThan int, wi
...
@@ -299,7 +299,7 @@ func (c *MediaCache) clearSpace(neededBytes int64, withDownloadsLessThan int, wi
}
}
}
}
if
preppedSpace
<
neededBytes
{
if
preppedSpace
<
neededBytes
&&
!
deleteEvenIfNotEnough
{
// not enough space prepared - don't evict anything
// not enough space prepared - don't evict anything
return
0
return
0
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment