Skip to content

Commit 38eed6d

Browse files
committed
Add hardlink configuration and basic structure
- Adds the EnableHardlink configuration option - Adds the HardlinkCapability interface - Updates the directoryCache struct to support hardlinks - Adds logging for hardlink configuration - Updates the layer package to pass through hardlink configuration - Concurrent access testing Fixes: containerd#1953 Signed-off-by: ChengyuZhu6 <hudson@cyzhu.com>
1 parent dbddc6d commit 38eed6d

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

cache/cache.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"path/filepath"
2626
"sync"
2727

28+
"github.com/containerd/log"
2829
"github.com/containerd/stargz-snapshotter/util/cacheutil"
2930
"github.com/containerd/stargz-snapshotter/util/namedmutex"
3031
)
@@ -61,6 +62,9 @@ type DirectoryCacheConfig struct {
6162
// Direct forcefully enables direct mode for all operation in cache.
6263
// Thus operation won't use on-memory caches.
6364
Direct bool
65+
66+
// EnableHardlink enables hardlinking of cache files to reduce memory usage
67+
EnableHardlink bool
6468
}
6569

6670
// TODO: contents validation.
@@ -166,15 +170,24 @@ func NewDirectoryCache(directory string, config DirectoryCacheConfig) (BlobCache
166170
return nil, err
167171
}
168172
dc := &directoryCache{
169-
cache: dataCache,
170-
fileCache: fdCache,
171-
wipLock: new(namedmutex.NamedMutex),
172-
directory: directory,
173-
wipDirectory: wipdir,
174-
bufPool: bufPool,
175-
direct: config.Direct,
173+
cache: dataCache,
174+
fileCache: fdCache,
175+
wipLock: new(namedmutex.NamedMutex),
176+
directory: directory,
177+
wipDirectory: wipdir,
178+
bufPool: bufPool,
179+
direct: config.Direct,
180+
enableHardlink: config.EnableHardlink,
176181
}
177182
dc.syncAdd = config.SyncAdd
183+
184+
// Log hardlink configuration
185+
if config.EnableHardlink {
186+
log.L.Infof("Hardlink feature is enabled for cache directory: %q", directory)
187+
} else {
188+
log.L.Infof("Hardlink feature is disabled for cache directory: %q", directory)
189+
}
190+
178191
return dc, nil
179192
}
180193

@@ -193,6 +206,9 @@ type directoryCache struct {
193206

194207
closed bool
195208
closedMu sync.Mutex
209+
210+
enableHardlink bool
211+
hlManager *HardlinkManager
196212
}
197213

198214
func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
@@ -463,3 +479,11 @@ func (w *writeCloser) Close() error { return w.closeFunc() }
463479
func nopWriteCloser(w io.Writer) io.WriteCloser {
464480
return &writeCloser{w, func() error { return nil }}
465481
}
482+
483+
// HardlinkCapability represents a cache that supports hardlinking
484+
type HardlinkCapability interface {
485+
// CreateHardlink creates a hardlink for the cached file
486+
CreateHardlink(key string) error
487+
// HasHardlink checks if a hardlink exists for the given key
488+
HasHardlink(key string) bool
489+
}

fs/config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ type DirectoryCacheConfig struct {
139139

140140
// Direct disables on-memory data cache. Default is true for saving memory usage.
141141
Direct bool `toml:"direct" default:"true"`
142+
143+
// EnableHardlink enables hardlinking of cache files to reduce memory usage
144+
EnableHardlink bool `toml:"enable_hardlink"`
142145
}
143146

144147
// FuseConfig is configuration for FUSE fs.

fs/layer/layer.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ func newCache(root string, cacheType string, cfg config.Config) (cache.BlobCache
231231
return cache.NewDirectoryCache(
232232
cachePath,
233233
cache.DirectoryCacheConfig{
234-
SyncAdd: dcc.SyncAdd,
235-
DataCache: dCache,
236-
FdCache: fCache,
237-
BufPool: bufPool,
238-
Direct: dcc.Direct,
234+
EnableHardlink: dcc.EnableHardlink,
235+
SyncAdd: dcc.SyncAdd,
236+
DataCache: dCache,
237+
FdCache: fCache,
238+
BufPool: bufPool,
239+
Direct: dcc.Direct,
239240
},
240241
)
241242
}

0 commit comments

Comments
 (0)