-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add register function for migration cache (#2486)
* Add register function for migration cache * Cleanup * Update off new base commit * Address pr comments
- Loading branch information
1 parent
ac6e720
commit 7c2edc1
Showing
6 changed files
with
244 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "migration_cache", | ||
srcs = [ | ||
"config.go", | ||
"migration_cache.go", | ||
], | ||
importpath = "github.com/buildbuddy-io/buildbuddy/server/backends/migration_cache", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//enterprise/server/backends/pebble_cache", | ||
"//proto:remote_execution_go_proto", | ||
"//server/backends/disk_cache", | ||
"//server/cache/config", | ||
"//server/environment", | ||
"//server/interfaces", | ||
"//server/util/disk", | ||
"//server/util/flagutil", | ||
"//server/util/log", | ||
"//server/util/status", | ||
], | ||
) |
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,37 @@ | ||
package migration_cache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/buildbuddy-io/buildbuddy/server/util/disk" | ||
) | ||
|
||
type MigrationConfig struct { | ||
Src *CacheConfig `yaml:"src"` | ||
Dest *CacheConfig `yaml:"dest"` | ||
} | ||
|
||
type CacheConfig struct { | ||
DiskConfig *DiskCacheConfig `yaml:"disk"` | ||
PebbleConfig *PebbleCacheConfig `yaml:"pebble"` | ||
} | ||
|
||
type DiskCacheConfig struct { | ||
RootDirectory string `yaml:"root_directory"` | ||
Partitions []disk.Partition `yaml:"partitions"` | ||
PartitionMappings []disk.PartitionMapping `yaml:"partition_mappings"` | ||
UseV2Layout bool `yaml:"use_v2_layout"` | ||
} | ||
|
||
type PebbleCacheConfig struct { | ||
RootDirectory string `yaml:"root_directory"` | ||
Partitions []disk.Partition `yaml:"partitions"` | ||
PartitionMappings []disk.PartitionMapping `yaml:"partition_mappings"` | ||
MaxSizeBytes int64 `yaml:"max_size_bytes"` | ||
BlockCacheSizeBytes int64 `yaml:"block_cache_size_bytes"` | ||
MaxInlineFileSizeBytes int64 `yaml:"max_inline_file_size_bytes"` | ||
AtimeUpdateThreshold *time.Duration `yaml:"atime_update_threshold"` | ||
AtimeWriteBatchSize int `yaml:"atime_write_batch_size"` | ||
AtimeBufferSize *int `yaml:"atime_buffer_size"` | ||
MinEvictionAge *time.Duration `yaml:"min_eviction_age"` | ||
} |
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,166 @@ | ||
package migration_cache | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/buildbuddy-io/buildbuddy/enterprise/server/backends/pebble_cache" | ||
"github.com/buildbuddy-io/buildbuddy/server/backends/disk_cache" | ||
"github.com/buildbuddy-io/buildbuddy/server/environment" | ||
"github.com/buildbuddy-io/buildbuddy/server/interfaces" | ||
"github.com/buildbuddy-io/buildbuddy/server/util/flagutil" | ||
"github.com/buildbuddy-io/buildbuddy/server/util/log" | ||
"github.com/buildbuddy-io/buildbuddy/server/util/status" | ||
|
||
repb "github.com/buildbuddy-io/buildbuddy/proto/remote_execution" | ||
cache_config "github.com/buildbuddy-io/buildbuddy/server/cache/config" | ||
) | ||
|
||
var ( | ||
cacheMigrationConfig = flagutil.New("cache.migration", MigrationConfig{}, "Config to specify the details of a cache migration") | ||
) | ||
|
||
type MigrationCache struct { | ||
Src interfaces.Cache | ||
Dest interfaces.Cache | ||
} | ||
|
||
func Register(env environment.Env) error { | ||
if cacheMigrationConfig.Src == nil || cacheMigrationConfig.Dest == nil { | ||
return nil | ||
} | ||
log.Infof("Registering Migration Cache") | ||
|
||
srcCache, err := getCacheFromConfig(env, *cacheMigrationConfig.Src) | ||
if err != nil { | ||
return err | ||
} | ||
destCache, err := getCacheFromConfig(env, *cacheMigrationConfig.Dest) | ||
if err != nil { | ||
return err | ||
} | ||
mc := &MigrationCache{ | ||
Src: srcCache, | ||
Dest: destCache, | ||
} | ||
|
||
if env.GetCache() != nil { | ||
log.Warningf("Overriding configured cache with migration_cache. If running a migration, all cache configs" + | ||
" should be nested under the cache.migration block.") | ||
} | ||
env.SetCache(mc) | ||
|
||
return nil | ||
} | ||
|
||
func validateCacheConfig(config CacheConfig) error { | ||
if config.PebbleConfig != nil && config.DiskConfig != nil { | ||
return status.FailedPreconditionError("only one cache config can be set") | ||
} else if config.PebbleConfig == nil && config.DiskConfig == nil { | ||
return status.FailedPreconditionError("a cache config must be set") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getCacheFromConfig(env environment.Env, cfg CacheConfig) (interfaces.Cache, error) { | ||
err := validateCacheConfig(cfg) | ||
if err != nil { | ||
return nil, status.FailedPreconditionErrorf("error validating migration cache config: %s", err) | ||
} | ||
|
||
if cfg.DiskConfig != nil { | ||
c, err := diskCacheFromConfig(env, cfg.DiskConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} else if cfg.PebbleConfig != nil { | ||
c, err := pebbleCacheFromConfig(env, cfg.PebbleConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
return nil, status.FailedPreconditionErrorf("error getting cache from migration config: no valid cache types") | ||
} | ||
|
||
func diskCacheFromConfig(env environment.Env, cfg *DiskCacheConfig) (*disk_cache.DiskCache, error) { | ||
opts := &disk_cache.Options{ | ||
RootDirectory: cfg.RootDirectory, | ||
Partitions: cfg.Partitions, | ||
PartitionMappings: cfg.PartitionMappings, | ||
UseV2Layout: cfg.UseV2Layout, | ||
} | ||
return disk_cache.NewDiskCache(env, opts, cache_config.MaxSizeBytes()) | ||
} | ||
|
||
func pebbleCacheFromConfig(env environment.Env, cfg *PebbleCacheConfig) (*pebble_cache.PebbleCache, error) { | ||
opts := &pebble_cache.Options{ | ||
RootDirectory: cfg.RootDirectory, | ||
Partitions: cfg.Partitions, | ||
PartitionMappings: cfg.PartitionMappings, | ||
MaxSizeBytes: cfg.MaxSizeBytes, | ||
BlockCacheSizeBytes: cfg.BlockCacheSizeBytes, | ||
MaxInlineFileSizeBytes: cfg.MaxInlineFileSizeBytes, | ||
AtimeUpdateThreshold: cfg.AtimeUpdateThreshold, | ||
AtimeWriteBatchSize: cfg.AtimeWriteBatchSize, | ||
AtimeBufferSize: cfg.AtimeBufferSize, | ||
MinEvictionAge: cfg.MinEvictionAge, | ||
} | ||
c, err := pebble_cache.NewPebbleCache(env, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.Start() | ||
env.GetHealthChecker().RegisterShutdownFunction(func(ctx context.Context) error { | ||
return c.Stop() | ||
}) | ||
return c, nil | ||
} | ||
|
||
func (mc MigrationCache) WithIsolation(ctx context.Context, cacheType interfaces.CacheType, remoteInstanceName string) (interfaces.Cache, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Contains(ctx context.Context, d *repb.Digest) (bool, error) { | ||
return false, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Metadata(ctx context.Context, d *repb.Digest) (*interfaces.CacheMetadata, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) FindMissing(ctx context.Context, digests []*repb.Digest) ([]*repb.Digest, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Get(ctx context.Context, d *repb.Digest) ([]byte, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) GetMulti(ctx context.Context, digests []*repb.Digest) (map[*repb.Digest][]byte, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Set(ctx context.Context, d *repb.Digest, data []byte) error { | ||
return status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) SetMulti(ctx context.Context, kvs map[*repb.Digest][]byte) error { | ||
return status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Delete(ctx context.Context, d *repb.Digest) error { | ||
return status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Reader(ctx context.Context, d *repb.Digest, offset, limit int64) (io.ReadCloser, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} | ||
|
||
func (mc MigrationCache) Writer(ctx context.Context, d *repb.Digest) (io.WriteCloser, error) { | ||
return nil, status.UnimplementedError("not yet implemented") | ||
} |
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