mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 01:18:42 +00:00
Remove orbit unused package (#5887)
This commit is contained in:
parent
5527cf3cef
commit
6fdfb1d0d6
2 changed files with 0 additions and 206 deletions
|
|
@ -1,111 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/dgraph-io/badger/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// This is the discard ratio recommended in Badger docs
|
||||
// (https://pkg.go.dev/github.com/dgraph-io/badger#DB.RunValueLogGC)
|
||||
const compactionDiscardRatio = 0.5
|
||||
|
||||
var compactionInterval = 5 * time.Minute
|
||||
|
||||
// BadgerDB is a wrapper around the standard badger.DB that provides a
|
||||
// background compaction routine.
|
||||
type BadgerDB struct {
|
||||
*badger.DB
|
||||
closeChan chan struct{}
|
||||
m sync.Mutex // synchronizes start/stop compaction.
|
||||
}
|
||||
|
||||
// Open opens (initializing if necessary) a Badger database at the specified
|
||||
// path. Users must close the DB with Close().
|
||||
func Open(path string) (*BadgerDB, error) {
|
||||
// DefaultOptions sets synchronous writes to true (maximum data integrity).
|
||||
// TODO implement logging?
|
||||
db, err := badger.Open(badger.DefaultOptions(path).WithLogger(nil))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open badger %s: %w", path, err)
|
||||
}
|
||||
|
||||
b := &BadgerDB{DB: db}
|
||||
b.startBackgroundCompaction()
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// OpenTruncate opens (initializing and/or truncating if necessary) a Badger
|
||||
// database at the specified path. Users must close the DB with Close().
|
||||
//
|
||||
// Prefer Open in the general case, but after a bad shutdown it may be necessary
|
||||
// to call OpenTruncate. This may cause data loss. Detect this situation by
|
||||
// looking for badger.ErrTruncateNeeded.
|
||||
func OpenTruncate(path string) (*BadgerDB, error) {
|
||||
// DefaultOptions sets synchronous writes to true (maximum data integrity).
|
||||
// TODO implement logging?
|
||||
db, err := badger.Open(badger.DefaultOptions(path).WithLogger(nil).WithTruncate(true))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open badger with truncate %s: %w", path, err)
|
||||
}
|
||||
|
||||
b := &BadgerDB{DB: db}
|
||||
b.startBackgroundCompaction()
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// startBackgroundCompaction starts a background loop that will call the
|
||||
// compaction method on the database. Badger does not do this automatically, so
|
||||
// we need to be sure to do so here (or elsewhere).
|
||||
func (b *BadgerDB) startBackgroundCompaction() {
|
||||
b.m.Lock()
|
||||
defer b.m.Unlock()
|
||||
|
||||
if b.closeChan != nil {
|
||||
panic("background compaction already running")
|
||||
}
|
||||
b.closeChan = make(chan struct{})
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(compactionInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-b.closeChan:
|
||||
return
|
||||
case <-ticker.C:
|
||||
err := b.DB.RunValueLogGC(compactionDiscardRatio)
|
||||
if err == nil || errors.Is(err, badger.ErrNoRewrite) {
|
||||
continue
|
||||
}
|
||||
log.Error().Err(err).Msg("compact badger")
|
||||
if errors.Is(err, badger.ErrDBClosed) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// stopBackgroundCompaction stops the background compaction routine.
|
||||
func (b *BadgerDB) stopBackgroundCompaction() {
|
||||
b.m.Lock()
|
||||
defer b.m.Unlock()
|
||||
|
||||
if b.closeChan != nil {
|
||||
b.closeChan <- struct{}{}
|
||||
b.closeChan = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes the database connection and releases the associated resources.
|
||||
func (b *BadgerDB) Close() error {
|
||||
b.stopBackgroundCompaction()
|
||||
return b.DB.Close()
|
||||
}
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/dgraph-io/badger/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func init() {
|
||||
compactionInterval = 100 * time.Millisecond
|
||||
}
|
||||
|
||||
func TestDatabase(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "orbit-test")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
os.RemoveAll(tmpDir)
|
||||
})
|
||||
|
||||
// Open and write
|
||||
db, err := Open(tmpDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.Update(func(tx *badger.Txn) error {
|
||||
require.NoError(t, tx.Set([]byte("key"), []byte("value")))
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.Close())
|
||||
|
||||
// Reopen and read
|
||||
db, err = Open(tmpDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.View(func(tx *badger.Txn) error {
|
||||
item, err := tx.Get([]byte("key"))
|
||||
require.NoError(t, err)
|
||||
err = item.Value(func(val []byte) error {
|
||||
assert.Equal(t, []byte("value"), val)
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.Close())
|
||||
}
|
||||
|
||||
func TestCompactionPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "orbit-test")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
os.RemoveAll(tmpDir)
|
||||
})
|
||||
|
||||
db, err := Open(tmpDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to start the compaction routine again
|
||||
assert.Panics(t, func() { db.startBackgroundCompaction() })
|
||||
}
|
||||
|
||||
func TestCompactionRestart(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "orbit-test")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
os.RemoveAll(tmpDir)
|
||||
})
|
||||
|
||||
db, err := Open(tmpDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
var g errgroup.Group
|
||||
g.Go(func() error {
|
||||
return db.Close()
|
||||
})
|
||||
|
||||
db.stopBackgroundCompaction()
|
||||
assert.NotPanics(t, func() { db.startBackgroundCompaction() })
|
||||
|
||||
require.NoError(t, g.Wait())
|
||||
}
|
||||
Loading…
Reference in a new issue