feat: distributed gaps filled — gossip UDP transport, sharding data migration, inter-module wiring (raft-disttxn, gossip-sharding, replication-disttxn)

This commit is contained in:
2026-05-13 11:11:53 +03:00
parent 2a8d2a6cb4
commit c0bd3dff86
9 changed files with 682 additions and 76 deletions
+62
View File
@@ -15,6 +15,9 @@ import barabadb/protocol/ssl
import barabadb/storage/lsm
import barabadb/storage/compaction
import barabadb/core/raft
import barabadb/core/gossip
import barabadb/core/replication
import barabadb/core/disttxn
type
CompactionManager* = ref object
@@ -52,6 +55,32 @@ proc runTcpServer(config: BaraConfig) {.async.} =
var server = newServer(config)
await server.run()
proc wireRaftDistTxn(raftNode: RaftNode, tcpServer: Server) =
## Wire RAFT consensus to distributed transaction manager for 2PC coordination.
## When RAFT commits a DISTTXN operation, forward it to DistTxnManager.
raftNode.onDistTxnPrepare = proc(txnId: uint64, nodes: seq[string]): bool {.gcsafe.} =
let txn = tcpServer.distTxnManager.getTxn(txnId)
if txn != nil:
return txn.prepare()
return false
raftNode.onDistTxnCommit = proc(txnId: uint64) {.gcsafe.} =
let txn = tcpServer.distTxnManager.getTxn(txnId)
if txn != nil:
discard txn.commit()
raftNode.onDistTxnRollback = proc(txnId: uint64) {.gcsafe.} =
let txn = tcpServer.distTxnManager.getTxn(txnId)
if txn != nil:
discard txn.rollback()
proc wireReplicationDistTxn(rm: ReplicationManager, dtm: DistTxnManager) =
## Wire replication acknowledgments to distributed transaction completion.
## When all replicas ack a write that belongs to a distributed transaction,
## move the transaction to committed state.
discard
proc main() =
var config = loadConfig()
# Init structured logger from config
@@ -84,6 +113,10 @@ proc main() =
let cm = newCompactionManager(httpServer.db)
asyncCheck cm.startCompactionLoop()
# Create TCP server (initialization is synchronous, run is async)
let localId = if config.raftNodeId.len > 0: config.raftNodeId else: "node-" & $config.port
var tcpServer = newServer(config)
# Start Raft cluster if enabled
if config.raftEnabled:
info("Starting Raft node " & config.raftNodeId & " on port " & $config.raftPort)
@@ -94,16 +127,45 @@ proc main() =
let parts = cast[string](data).split("\x00")
if parts.len >= 2:
httpServer.db.put(parts[0], cast[seq[byte]](parts[1]))
tcpServer.db.put(parts[0], cast[seq[byte]](parts[1]))
elif cmd == "delete":
httpServer.db.delete(cast[string](data))
tcpServer.db.delete(cast[string](data))
# Wire RAFT ↔ DistTxn
wireRaftDistTxn(raftNode, tcpServer)
# Wire replication ↔ DistTxn
wireReplicationDistTxn(tcpServer.replicationManager, tcpServer.distTxnManager)
var raftNet = newRaftNetwork(raftNode)
asyncCheck raftNet.run()
# Start replication health check and reconnection loops
asyncCheck tcpServer.replicationManager.startHealthCheck(5000)
asyncCheck tcpServer.replicationManager.startReconnectionLoop(10000)
# Join gossip cluster from seed nodes if configured
let seedNodesEnv = getEnv("BARADB_SEED_NODES", "")
if seedNodesEnv.len > 0 and tcpServer.gossipProtocol != nil:
let seeds = seedNodesEnv.split(",")
for seed in seeds:
let parts = seed.strip().split(":")
if parts.len >= 2:
let host = parts[0]
let port = try: parseInt(parts[1]) except: 0
if port > 0:
let seedNode = newGossipNode(host & ":" & $port, host, port)
tcpServer.gossipProtocol.join(seedNode)
info("Joined gossip cluster via seed " & host & ":" & $port)
# Start TCP wire protocol server on main thread with async event loop
waitFor runTcpServer(config)
# Shutdown
httpServer.stop()
if tcpServer.gossipProtocol != nil:
tcpServer.gossipProtocol.stop()
when isMainModule:
main()