-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from Desiki-high/feat-lrfu
feat: add leaseCache to change gc policy
- Loading branch information
Showing
5 changed files
with
243 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package content | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"sort" | ||
"strconv" | ||
|
||
"github.com/containerd/containerd/leases" | ||
"github.com/containerd/containerd/namespaces" | ||
lru "github.com/hashicorp/golang-lru/v2" | ||
) | ||
|
||
// This is not thread-safe, which means it will depend on the parent implementation to do the locking mechanism. | ||
type leaseCache struct { | ||
caches map[string]*lru.Cache[string, any] | ||
cachesIndex []int | ||
size int | ||
} | ||
|
||
// newleaseCache return new empty leaseCache | ||
func newLeaseCache() *leaseCache { | ||
return &leaseCache{ | ||
caches: make(map[string]*lru.Cache[string, any]), | ||
cachesIndex: make([]int, 0), | ||
size: 0, | ||
} | ||
} | ||
|
||
// Init leaseCache by leases manager from db | ||
func (leaseCache *leaseCache) Init(lm leases.Manager) error { | ||
ls, err := lm.List(namespaces.WithNamespace(context.Background(), accelerationServiceNamespace)) | ||
if err != nil { | ||
return err | ||
} | ||
sort.Slice(ls, func(i, j int) bool { | ||
return ls[i].Labels[usedAtLabel] > ls[j].Labels[usedAtLabel] | ||
}) | ||
for _, lease := range ls { | ||
if err := leaseCache.Add(lease.ID, lease.Labels[usedCountLabel]); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Add the key into cache | ||
func (leaseCache *leaseCache) Add(key string, usedCount string) error { | ||
count, err := strconv.Atoi(usedCount) | ||
if err != nil { | ||
return err | ||
} | ||
if cache, ok := leaseCache.caches[usedCount]; ok { | ||
cache.Add(key, nil) | ||
} else { | ||
cache, err := lru.New[string, any](math.MaxInt) | ||
if err != nil { | ||
return err | ||
} | ||
cache.Add(key, nil) | ||
leaseCache.caches[usedCount] = cache | ||
usedCount, err := strconv.Atoi(usedCount) | ||
if err != nil { | ||
return err | ||
} | ||
leaseCache.cachesIndex = append(leaseCache.cachesIndex, usedCount) | ||
sort.Ints(leaseCache.cachesIndex) | ||
} | ||
// remove old cache | ||
if usedCount != "1" { | ||
if cache, ok := leaseCache.caches[strconv.Itoa(count-1)]; ok { | ||
if cache.Contains(key) { | ||
leaseCache.remove(key, strconv.Itoa(count-1)) | ||
leaseCache.size-- | ||
} | ||
} | ||
} | ||
leaseCache.size++ | ||
return nil | ||
} | ||
|
||
// Remove oldest key from cache | ||
func (leaseCache *leaseCache) Remove() (string, error) { | ||
if key, _, ok := leaseCache.caches[strconv.Itoa(leaseCache.cachesIndex[0])].GetOldest(); ok { | ||
leaseCache.remove(key, strconv.Itoa(leaseCache.cachesIndex[0])) | ||
leaseCache.size-- | ||
return key, nil | ||
} | ||
return "", fmt.Errorf("leaseCache have empty cache with cachesIndex") | ||
} | ||
|
||
func (leaseCache *leaseCache) remove(key string, usedCount string) { | ||
leaseCache.caches[usedCount].Remove(key) | ||
if leaseCache.caches[usedCount].Len() == 0 { | ||
delete(leaseCache.caches, usedCount) | ||
var newCachesIndex []int | ||
for _, index := range leaseCache.cachesIndex { | ||
if usedCount != strconv.Itoa(index) { | ||
newCachesIndex = append(newCachesIndex, index) | ||
} | ||
} | ||
leaseCache.cachesIndex = newCachesIndex | ||
} | ||
} | ||
|
||
// Len return the size of leaseCache | ||
func (leaseCache *leaseCache) Len() int { | ||
return leaseCache.size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package content | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/opencontainers/go-digest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLeaseCache(t *testing.T) { | ||
lc := newLeaseCache() | ||
require.NoError(t, lc.Add("test_1", "1")) | ||
require.NoError(t, lc.Add("test_2", "1")) | ||
require.NoError(t, lc.Add("test_3", "2")) | ||
require.NoError(t, lc.Add("test_2", "2")) | ||
require.Error(t, lc.Add("test_3", "test")) | ||
require.Equal(t, 3, lc.Len()) | ||
key, err := lc.Remove() | ||
require.NoError(t, err) | ||
require.Equal(t, "test_1", key) | ||
key, err = lc.Remove() | ||
require.NoError(t, err) | ||
require.Equal(t, "test_3", key) | ||
key, err = lc.Remove() | ||
require.NoError(t, err) | ||
require.Equal(t, "test_2", key) | ||
} | ||
|
||
func TestLeaseCacheInit(t *testing.T) { | ||
os.MkdirAll("./tmp", 0755) | ||
defer os.RemoveAll("./tmp") | ||
content, err := NewContent("./tmp", "./tmp", "100MB") | ||
require.NoError(t, err) | ||
testDigest := []string{ | ||
"sha256:9bb13890319dc01e5f8a4d3d0c4c72685654d682d568350fd38a02b1d70aee6b", | ||
"sha256:17dc42e40d4af0a9e84c738313109f3a95e598081beef6c18a05abb57337aa5d", | ||
"sha256:9bb13890319dc01e5f8a4d3d0c4c72685654d682d568350fd38a02b1d70aee6b", | ||
"sha256:17dc42e40d4af0a9e84c738313109f3a95e598081beef6c18a05abb57337aa5d", | ||
"sha256:aeb53f8db8c94d2cd63ca860d635af4307967aa11a2fdead98ae0ab3a329f470", | ||
"sha256:613f4797d2b6653634291a990f3e32378c7cfe3cdd439567b26ca340b8946013", | ||
} | ||
for _, digestString := range testDigest { | ||
require.NoError(t, content.updateLease((*digest.Digest)(&digestString))) | ||
} | ||
lc := newLeaseCache() | ||
require.NoError(t, lc.Init(content.lm)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters