radiance: add compact command
This commit is contained in:
parent
2a591e5a99
commit
f5c050b8c3
|
@ -0,0 +1,53 @@
|
|||
//go:build !lite
|
||||
|
||||
package compact
|
||||
|
||||
import (
|
||||
"github.com/linxGnu/grocksdb"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
var Cmd = cobra.Command{
|
||||
Use: "compact <rocksdb>",
|
||||
Short: "Compact RocksDB database",
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
|
||||
func init() {
|
||||
Cmd.Run = run
|
||||
}
|
||||
|
||||
func run(_ *cobra.Command, args []string) {
|
||||
dbOpts := grocksdb.NewDefaultOptions()
|
||||
|
||||
cfNames, err := grocksdb.ListColumnFamilies(dbOpts, args[0])
|
||||
if err != nil {
|
||||
klog.Exitf("Failed to list column families: %s", err)
|
||||
}
|
||||
cfOpts := make([]*grocksdb.Options, len(cfNames))
|
||||
for i := range cfOpts {
|
||||
cfOpts[i] = grocksdb.NewDefaultOptions()
|
||||
}
|
||||
|
||||
db, cfs, err := grocksdb.OpenDbColumnFamilies(dbOpts, args[0], cfNames, cfOpts)
|
||||
if err != nil {
|
||||
klog.Exitf("Failed to open blockstore: %s", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
klog.Infof("Flushing WAL")
|
||||
if err := db.FlushWAL(true); err != nil {
|
||||
klog.Exitf("Failed to flush WAL: %s", err)
|
||||
}
|
||||
klog.Infof("Flushed WAL")
|
||||
|
||||
for _, cf := range cfs {
|
||||
name := cf.Name()
|
||||
klog.Infof("Compacting %s", name)
|
||||
db.CompactRangeCF(cf, grocksdb.Range{})
|
||||
klog.Infof("Compacted %s", name)
|
||||
}
|
||||
|
||||
klog.Infof("Done")
|
||||
}
|
|
@ -21,12 +21,16 @@ type DB struct {
|
|||
CfTxStatus *grocksdb.ColumnFamilyHandle
|
||||
}
|
||||
|
||||
func OpenReadWrite(path string) (*DB, error) {
|
||||
return open(path, "", true)
|
||||
}
|
||||
|
||||
// OpenReadOnly attaches to a blockstore in read-only mode.
|
||||
//
|
||||
// Attaching to running validators is supported.
|
||||
// The DB handle will be a point-in-time view at the time of attaching.
|
||||
func OpenReadOnly(path string) (*DB, error) {
|
||||
return open(path, "")
|
||||
return open(path, "", false)
|
||||
}
|
||||
|
||||
// OpenSecondary attaches to a blockstore in secondary mode.
|
||||
|
@ -36,10 +40,10 @@ func OpenReadOnly(path string) (*DB, error) {
|
|||
//
|
||||
// `secondaryPath` points to a directory where the secondary instance stores its info log.
|
||||
func OpenSecondary(path string, secondaryPath string) (*DB, error) {
|
||||
return open(path, secondaryPath)
|
||||
return open(path, secondaryPath, false)
|
||||
}
|
||||
|
||||
func open(path string, secondaryPath string) (*DB, error) {
|
||||
func open(path string, secondaryPath string, write bool) (*DB, error) {
|
||||
// List all available column families
|
||||
dbOpts := grocksdb.NewDefaultOptions()
|
||||
allCfNames, err := grocksdb.ListColumnFamilies(dbOpts, path)
|
||||
|
@ -64,7 +68,16 @@ func open(path string, secondaryPath string) (*DB, error) {
|
|||
}
|
||||
|
||||
var openFn func() (*grocksdb.DB, []*grocksdb.ColumnFamilyHandle, error)
|
||||
if secondaryPath != "" {
|
||||
if write {
|
||||
openFn = func() (*grocksdb.DB, []*grocksdb.ColumnFamilyHandle, error) {
|
||||
return grocksdb.OpenDbColumnFamilies(
|
||||
dbOpts,
|
||||
path,
|
||||
cfNames,
|
||||
cfOptList,
|
||||
)
|
||||
}
|
||||
} else if secondaryPath != "" {
|
||||
openFn = func() (*grocksdb.DB, []*grocksdb.ColumnFamilyHandle, error) {
|
||||
return grocksdb.OpenDbAsSecondaryColumnFamilies(
|
||||
dbOpts,
|
||||
|
@ -118,6 +131,7 @@ func open(path string, secondaryPath string) (*DB, error) {
|
|||
}
|
||||
|
||||
func getCfOpts(db *DB, name string) (**grocksdb.ColumnFamilyHandle, *grocksdb.Options) {
|
||||
var handle *grocksdb.ColumnFamilyHandle
|
||||
switch name {
|
||||
case CfDefault:
|
||||
return &db.CfDefault, grocksdb.NewDefaultOptions()
|
||||
|
@ -130,7 +144,7 @@ func getCfOpts(db *DB, name string) (**grocksdb.ColumnFamilyHandle, *grocksdb.Op
|
|||
case CfCodeShred:
|
||||
return &db.CfCodeShred, grocksdb.NewDefaultOptions()
|
||||
default:
|
||||
return nil, nil
|
||||
return &handle, grocksdb.NewDefaultOptions()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue