feat: Improve Mycelium-based data synchronization
- Refactor data synchronization logic to use Mycelium messages for efficient updates between master and worker nodes. This removes the previous inefficient polling method and simplifies the code. - Update the slave node to receive and apply updates from the master, improving synchronization efficiency and robustness. - Change the default slave port to 9000. - Rename `db` variable to `worker` for clarity.
This commit is contained in:
@@ -9,19 +9,10 @@ import os
|
||||
import encoding.base64
|
||||
import json
|
||||
|
||||
// NOTE: Before running this script, ensure that the mycelium binary is installed and in the PATH
|
||||
|
||||
const slave_port = 9001
|
||||
const slave_port = 9000
|
||||
const master_public_key = '89c2eeb24bcdfaaac78c0023a166d88f760c097c1a57748770e432ba10757179'
|
||||
const master_address = '458:90d4:a3ef:b285:6d32:a22d:9e73:697f'
|
||||
|
||||
// Struct to hold data for syncing
|
||||
struct SyncData {
|
||||
id u32
|
||||
data string
|
||||
topic string = 'db_sync'
|
||||
}
|
||||
|
||||
mycelium.delete()!
|
||||
|
||||
// Initialize mycelium clients
|
||||
@@ -35,7 +26,7 @@ slave_inspect := mycelium.inspect(key_file_path: '/tmp/mycelium_server1/priv_key
|
||||
println('Server 2 (slave Node) public key: ${slave_inspect.public_key}')
|
||||
|
||||
// Initialize ourdb instances
|
||||
mut db := ourdb.new(
|
||||
mut worker := ourdb.new(
|
||||
record_nr_max: 16777216 - 1
|
||||
record_size_max: 1024
|
||||
path: '/tmp/ourdb1'
|
||||
@@ -43,7 +34,7 @@ mut db := ourdb.new(
|
||||
)!
|
||||
|
||||
defer {
|
||||
db.destroy() or { panic('failed to destroy db1: ${err}') }
|
||||
worker.destroy() or { panic('failed to destroy db1: ${err}') }
|
||||
}
|
||||
|
||||
// Receive messages
|
||||
@@ -52,24 +43,5 @@ received := slave.receive_msg(wait: true, peek: false, topic: 'db_sync')!
|
||||
println('Received message from: ${received.src_pk}')
|
||||
println('Message payload: ${base64.decode_str(received.payload)}')
|
||||
|
||||
// Send the last inserted record id to the master
|
||||
// data := 'Test data for sync - ' + time.now().str()
|
||||
id := db.get_last_index()!
|
||||
println('Last inserted record id: ${id}')
|
||||
|
||||
// Send sync message to slave
|
||||
println('\nSending sync message to slave...')
|
||||
msg := slave.send_msg(
|
||||
public_key: master_public_key
|
||||
payload: 'last_inserted_record_id,${id}'
|
||||
topic: 'db_sync'
|
||||
)!
|
||||
|
||||
// slave.reply_msg(
|
||||
// id: received.id
|
||||
// public_key: received.src_pk
|
||||
// payload: 'Got your message!'
|
||||
// topic: 'db_sync'
|
||||
// )!
|
||||
|
||||
// println('Message sent to master with ID: ${msg.id}')
|
||||
payload := received.payload
|
||||
worker.sync_updates(payload.bytes()) or { error('Failed to sync updates to worker due to: ${err}') }
|
||||
|
||||
Reference in New Issue
Block a user