Customized logger for badger

This commit is contained in:
anhthii
2025-07-28 21:51:42 +07:00
parent defa94434a
commit c03e996579
7 changed files with 75 additions and 25 deletions
+26 -1
View File
@@ -107,6 +107,32 @@ Each Mpcium node:
- **Scalable and pluggable**: Easily expand the cluster or integrate additional tools
- **Secure peer authentication**: All inter-node messages are signed and verified using Ed25519
## Configuration
The application uses a YAML configuration file (`config.yaml`) with the following key settings:
### Database Configuration
- `badger_password`: Password for encrypting the BadgerDB database
- `db_path`: Path where the database files are stored
### Backup Configuration
- `backup_enabled`: Enable/disable automatic backups (default: true)
- `backup_period_seconds`: How often to perform backups in seconds (default: 300)
- `backup_dir`: Directory where encrypted backups are stored
### Network Configuration
- `nats.url`: NATS server URL
- `consul.address`: Consul server address
### MPC Configuration
- `mpc_threshold`: Threshold for multi-party computation
- `event_initiator_pubkey`: Public key of the event initiator
- `max_concurrent_keygen`: Maximum concurrent key generation operations
## Installation and Run
For full installation and run instructions, see [INSTALLATION.md](./INSTALLATION.md).
@@ -176,4 +202,3 @@ go test ./... -v
cd e2e
make test
```
-5
View File
@@ -141,11 +141,6 @@ func main() {
Usage: "Target path for database recovery",
Required: true,
},
&cli.StringFlag{
Name: "backup-encryption-key",
Aliases: []string{"k"},
Usage: "Encryption key for backup files (will prompt if not provided)",
},
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
+7 -14
View File
@@ -15,7 +15,6 @@ import (
func recoverDatabase(ctx context.Context, c *cli.Command) error {
backupDir := c.String("backup-dir")
recoveryPath := c.String("recovery-path")
encryptionKey := c.String("backup-encryption-key")
force := c.Bool("force")
// Validate backup directory
@@ -28,21 +27,15 @@ func recoverDatabase(ctx context.Context, c *cli.Command) error {
return fmt.Errorf("recovery path already exists: %s (use --force to overwrite)", recoveryPath)
}
// Get encryption key
// Prompt for encryption key
var key []byte
if encryptionKey != "" {
key = []byte(encryptionKey)
} else {
// Prompt for encryption key
fmt.Print("Enter backup encryption key: ")
keyBytes, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return fmt.Errorf("failed to read encryption key: %w", err)
}
fmt.Println() // Add newline after password input
key = keyBytes
fmt.Print("Enter backup encryption key: ")
keyBytes, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return fmt.Errorf("failed to read encryption key: %w", err)
}
fmt.Println() // Add newline after password input
key = keyBytes
if len(key) == 0 {
return fmt.Errorf("encryption key cannot be empty")
}
+5 -3
View File
@@ -200,7 +200,7 @@ func TestBadgerBackupExecutor_BackupMetadata(t *testing.T) {
assert.NotEmpty(t, meta.NonceB64)
assert.NotEmpty(t, meta.CreatedAt)
assert.GreaterOrEqual(t, meta.Since, uint64(0)) // Since can be 0 for first backup
assert.Greater(t, meta.NextVersion, meta.Since)
assert.Greater(t, meta.NextSince, meta.Since)
assert.NotEmpty(t, meta.EncryptionKeyID)
})
@@ -234,7 +234,8 @@ func TestBadgerBackupExecutor_VersionTracking(t *testing.T) {
t.Run("should create version file on first save", func(t *testing.T) {
version := uint64(12345)
err := executor.SaveVersionInfo(version)
since := uint64(100)
err := executor.SaveVersionInfo(version, since)
require.NoError(t, err)
// Check file was created
@@ -260,7 +261,8 @@ func TestBadgerBackupExecutor_VersionTracking(t *testing.T) {
time.Sleep(100 * time.Millisecond) // Increased from 10ms to 100ms
newVersion := uint64(67890)
err = executor.SaveVersionInfo(newVersion)
newSince := uint64(200)
err = executor.SaveVersionInfo(newVersion, newSince)
require.NoError(t, err)
// Check that the file modification time changed
+3 -1
View File
@@ -44,7 +44,9 @@ func NewBadgerKVStore(config BadgerConfig) (*BadgerKVStore, error) {
WithBlockCacheSize(256 << 20).
WithSyncWrites(true).
WithVerifyValueChecksum(true). // validate every value-log entry's checksum on read, surfacing corruption instead of masking it
WithCompactL0OnClose(true) // compacts level-0 SSTables on shutdown, reducing startup work and avoiding stalls on open
WithCompactL0OnClose(true). // compacts level-0 SSTables on shutdown, reducing startup work and avoiding stalls on open
WithLogger(newQuietBadgerLogger())
db, err := badger.Open(opts)
if err != nil {
return nil, err
+2 -1
View File
@@ -180,7 +180,8 @@ func (b *badgerBackupExecutor) RestoreAllBackupsEncrypted(restorePath string, en
opts := badger.DefaultOptions(restorePath).
WithEncryptionKey(encryptionKey).
WithIndexCacheSize(10 << 20)
WithIndexCacheSize(10 << 20).
WithLogger(newQuietBadgerLogger())
restoreDB, err := badger.Open(opts)
if err != nil {
return err
+32
View File
@@ -0,0 +1,32 @@
package kvstore
import (
"fmt"
"github.com/dgraph-io/badger/v4"
"github.com/fystack/mpcium/pkg/logger"
)
// quietBadgerLogger is a custom logger that suppresses INFO and DEBUG messages
// but allows ERROR and WARNING messages to be logged
type quietBadgerLogger struct{}
func newQuietBadgerLogger() badger.Logger {
return &quietBadgerLogger{}
}
func (ql *quietBadgerLogger) Errorf(format string, args ...interface{}) {
logger.Error("[BADGER] ERROR", nil, "message", fmt.Sprintf(format, args...))
}
func (ql *quietBadgerLogger) Warningf(format string, args ...interface{}) {
logger.Warn("[BADGER] WARN", "message", fmt.Sprintf(format, args...))
}
func (ql *quietBadgerLogger) Infof(format string, args ...interface{}) {
// Suppress INFO messages - do nothing
}
func (ql *quietBadgerLogger) Debugf(format string, args ...interface{}) {
// Suppress DEBUG messages - do nothing
}