2018-08-20 19:21:41 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-14 20:17:30 +00:00
|
|
|
"context"
|
|
|
|
|
2018-08-20 19:21:41 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-12-04 20:18:26 +00:00
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-12-12 13:15:34 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-11-16 16:31:14 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/redis"
|
|
|
|
"storj.io/storj/storage/storelogger"
|
2018-08-20 19:21:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cacheConfig struct {
|
|
|
|
NodesPath string `help:"the path to a JSON file containing an object with IP keys and nodeID values"`
|
|
|
|
DatabaseURL string `help:"the database connection string to use"`
|
|
|
|
}
|
|
|
|
|
2018-12-14 20:17:30 +00:00
|
|
|
func (c cacheConfig) open(ctx context.Context) (*overlay.Cache, error) {
|
2018-12-12 13:15:34 +00:00
|
|
|
driver, source, err := utils.SplitDBURL(c.DatabaseURL)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
var db storage.KeyValueStore
|
|
|
|
|
2018-12-12 13:15:34 +00:00
|
|
|
switch driver {
|
2018-08-20 19:21:41 +01:00
|
|
|
case "bolt":
|
2018-12-12 13:15:34 +00:00
|
|
|
db, err = boltdb.New(source, overlay.OverlayBucket)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
2018-11-16 16:31:14 +00:00
|
|
|
return nil, Error.New("invalid overlay cache database: %s", err)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
zap.S().Info("Starting overlay cache with BoltDB")
|
|
|
|
case "redis":
|
2018-11-16 16:31:14 +00:00
|
|
|
db, err = redis.NewClientFrom(c.DatabaseURL)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
2018-11-16 16:31:14 +00:00
|
|
|
return nil, Error.New("invalid overlay cache database: %s", err)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
zap.S().Info("Starting overlay cache with Redis")
|
|
|
|
default:
|
2018-12-12 13:15:34 +00:00
|
|
|
return nil, Error.New("database scheme not supported: %s", driver)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
// add logger
|
2018-12-12 15:39:16 +00:00
|
|
|
db = storelogger.New(zap.L().Named("oc"), db)
|
2018-12-14 20:17:30 +00:00
|
|
|
sdb, ok := ctx.Value("masterdb").(interface {
|
|
|
|
StatDB() statdb.DB
|
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
return nil, Error.New("unable to get master db instance")
|
2018-12-04 20:18:26 +00:00
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-12-14 20:17:30 +00:00
|
|
|
return overlay.NewOverlayCache(db, nil, sdb.StatDB()), nil
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|