AgentSkillsCN

babuza-wal

用于配置预写日志(WAL)存储。触发关键词: Raft WAL、预写日志、babuza 存储、Raft 持久化、Raft 磁盘。

SKILL.md
--- frontmatter
name: babuza-wal
description: |
  Use for configuring Write-Ahead Log (WAL) storage. Triggers on:
  raft wal, write ahead log, babuza storage, raft persistence, raft disk

Babuza WAL Skill

Package: github.com/fanaujie/babuza/pkg/wal

Efficient Write-Ahead Log implementations for durable Raft storage.

You are an expert at babuza storage. Help users by:

  • Writing code: Configure different WAL backends (Native, Badger, Pebble, ETCD).
  • Answering questions: Explain memory efficiency and storage options.

Documentation

Refer to the local files for detailed API:

  • ./references/wal-api.md - WAL interfaces, backend types, and architecture.

Key Patterns

1. Configuring WAL via Builder

Select the WAL backend using BabuzaComponentBuilder.

go
import "github.com/fanaujie/babuza/pkg/builder"

func buildComponent() *builder.BabuzaComponent {
    return builder.NewBabuzaComponentBuilder(&builder.BabuzaComponentConfig{
        ClusterId:      1,
        StorageRootDir: "/var/lib/babuza",  // WAL stored in {StorageRootDir}/wal

        // Select WAL Type
        WalType: builder.BabuzaWal,  // Recommended

        // Other required fields
        TransportType: builder.HttpTransport,
        SessionType:   builder.NoOpSession,
        SnapshotType:  builder.DurableSnapshot,
    }).Build()
}

2. Full Example with Raft

go
import (
    "github.com/fanaujie/babuza/pkg/builder"
    "github.com/fanaujie/babuza/raft"
)

func startNode(stateMachine ibabuza.BaseStateMachine) (*raft.Raft, error) {
    component := builder.NewBabuzaComponentBuilder(&builder.BabuzaComponentConfig{
        ClusterId:      1,
        StorageRootDir: "/tmp/node1",
        TransportType:  builder.HttpTransport,
        WalType:        builder.BabuzaWal,      // Memory-efficient native WAL
        SessionType:    builder.NoOpSession,
        SnapshotType:   builder.DurableSnapshot,
    }).Build()

    cfg := raft.DefaultBabuzaConfig(1, 1, "127.0.0.1:9001")

    peers := raft.NewPeersConfiguration()
    peers.AddPeer(1, "127.0.0.1:9001", false)

    bootstrap, err := raft.NewBootstrapRaftCluster(
        cfg, *peers, stateMachine,
        component.Cluster, component.RaftNode, component.SessionManager,
        component.SnapshotManager, component.WalManager, component.Transport,
        component.Logger, component.MetricsController,
    )
    if err != nil {
        return nil, err
    }

    return raft.NewRaft(cfg, bootstrap, nil)
}

WAL Backends

BackendBuilder ConstantDescription
Babuza (Native)builder.BabuzaWalRecommended. Index-based, 94%+ memory savings vs etcd.
ETCDbuilder.ETCDWalStandard etcd WAL. Higher memory usage.
Badger Diskbuilder.BadgerWalDiskLSM tree (Badger). Good for high write throughput.
Badger Memorybuilder.BadgerWalMemoryIn-memory Badger. Testing only.
Pebble Diskbuilder.PebbleWalDiskLSM tree (CockroachDB). High performance.
Pebble Memorybuilder.PebbleWalMemoryIn-memory Pebble. Testing only.

When to Use Each WAL

ScenarioRecommended WAL
General purpose, memory-constrainedBabuzaWal
Compatibility with etcd toolingETCDWal
Very high write throughputBadgerWalDisk or PebbleWalDisk
Testing/Development*WalMemory variants

Memory Efficiency

Babuza's native WAL uses an Index + Cache architecture:

  • Index: Keeps only metadata (term, index, offset) in memory.
  • Cache: Recent entries in ring buffer.
  • Read: Older entries read from disk on demand.

Memory Comparison

EntriesData Sizeetcd MemoryBabuza MemorySavings
100K1 KB102 MB5.35 MB94.8%
100K10 KB981 MB5.35 MB99.5%

Storage Directory Structure

When using StorageRootDir: "/tmp/node1":

  • WAL: /tmp/node1/wal/
  • Snapshots: /tmp/node1/snap/

When Answering Questions

  1. Recommendation: Default to BabuzaWal (native) unless the user has specific needs for LSM trees (Badger/Pebble) or etcd compatibility.
  2. Safety: In-Memory WALs lose data on restart - use only for testing.
  3. Architecture: Babuza's index-based approach is key to memory efficiency. Unlike etcd which keeps all log entries in RAM, Babuza only keeps metadata.
  4. Performance: For most workloads, BabuzaWal is sufficient. LSM-based WALs (Badger/Pebble) may help with very high write throughput but add complexity.