Kaynağa Gözat

强一致性修改

xbase 2 hafta önce
ebeveyn
işleme
310888dcab
3 değiştirilmiş dosya ile 16 ekleme ve 4 silme
  1. 4 3
      README.md
  2. 5 0
      raft.go
  3. 7 1
      types.go

+ 4 - 3
README.md

@@ -244,9 +244,10 @@ config.MaxLogEntriesPerRequest = 5000          // 单次 AppendEntries 最大条
 config.MemoryLogCapacity = 10000               // 内存日志容量
 
 // 快照配置
-config.SnapshotThreshold = 100000              // 初始触发阈值(动态调整)
-config.SnapshotMinRetention = 10000            // 快照后保留的日志条数
-config.SnapshotChunkSize = 1024 * 1024         // 快照分块大小 (1MB)
+config.LogCompactionEnabled = false        // 默认不启用日志压缩 (永远保留完整日志)
+config.SnapshotThreshold = 100000          // 压缩触发阈值(仅在 LogCompactionEnabled=true 时生效)
+config.SnapshotMinRetention = 10000        // 快照后保留的日志条数
+config.SnapshotChunkSize = 1024 * 1024     // 快照分块大小 (1MB)
 
 // RPC 超时配置
 config.RPCTimeout = 500 * time.Millisecond     // 普通 RPC 超时

+ 5 - 0
raft.go

@@ -1221,6 +1221,11 @@ func (r *Raft) maybeCompactLog() {
 		return
 	}
 
+	// Skip if log compaction is explicitly disabled
+	if !r.config.LogCompactionEnabled {
+		return
+	}
+
 	// Check if compaction is already in progress (atomic check-and-set)
 	if !atomic.CompareAndSwapInt32(&r.compacting, 0, 1) {
 		return

+ 7 - 1
types.go

@@ -173,6 +173,11 @@ type Config struct {
 	// MemoryLogCapacity is the maximum number of log entries to keep in memory
 	MemoryLogCapacity int
 
+	// LogCompactionEnabled determines if log compaction is enabled
+	// If false, the log will grow indefinitely (safest for data, but consumes disk)
+	// Default: false
+	LogCompactionEnabled bool
+
 	// SnapshotThreshold triggers snapshot when log grows beyond this
 	SnapshotThreshold uint64
 
@@ -269,7 +274,8 @@ func DefaultConfig() *Config {
 		HeartbeatInterval:       50 * time.Millisecond,
 		MaxLogEntriesPerRequest: 5000,
 		MemoryLogCapacity:       10000,
-		SnapshotThreshold:       ^uint64(0),  // 默认不启用压缩 (MaxUint64)
+		LogCompactionEnabled:    false,       // 默认不启用压缩
+		SnapshotThreshold:       100000,      // 默认阈值(仅在 LogCompactionEnabled=true 时生效)
 		SnapshotMinRetention:    10000,       // 保留1万条用于 follower 追赶
 		SnapshotChunkSize:       1024 * 1024, // 1MB chunks
 		RPCTimeout:              500 * time.Millisecond,