|
| 1 | +package convex.node; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.InetSocketAddress; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Random; |
| 8 | + |
| 9 | +import convex.api.Convex; |
| 10 | +import convex.api.ConvexRemote; |
| 11 | +import convex.core.data.ACell; |
| 12 | +import convex.core.data.Hash; |
| 13 | +import convex.core.data.Index; |
| 14 | +import convex.core.data.Keyword; |
| 15 | +import convex.core.data.prim.CVMLong; |
| 16 | +import convex.core.store.AStore; |
| 17 | +import convex.core.store.MemoryStore; |
| 18 | +import convex.lattice.ALattice; |
| 19 | +import convex.lattice.Lattice; |
| 20 | + |
| 21 | +/** |
| 22 | + * Demo application showing automatic lattice synchronization across multiple nodes. |
| 23 | + * |
| 24 | + * This demo: |
| 25 | + * - Creates 3 NodeServers with automatic propagation |
| 26 | + * - Performs multiple merge operations on node 1 |
| 27 | + * - Verifies all nodes converge to the same lattice value |
| 28 | + */ |
| 29 | +public class LatticeDemo { |
| 30 | + |
| 31 | + private static final int NUM_NODES = 3; |
| 32 | + private static final int MERGES = 100; |
| 33 | + private static final int MODS = 100; |
| 34 | + private static final int BASE_PORT = 19700; |
| 35 | + |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + System.out.println("=== Lattice Synchronization Demo ==="); |
| 38 | + System.out.println("Creating " + NUM_NODES + " nodes with automatic propagation...\n"); |
| 39 | + |
| 40 | + // Create nodes and stores |
| 41 | + List<NodeServer<?>> servers = new ArrayList<>(); |
| 42 | + List<AStore> stores = new ArrayList<>(); |
| 43 | + ALattice<?> lattice = Lattice.ROOT; |
| 44 | + |
| 45 | + try { |
| 46 | + // Launch all servers |
| 47 | + for (int i = 0; i < NUM_NODES; i++) { |
| 48 | + AStore store = new MemoryStore(); |
| 49 | + stores.add(store); |
| 50 | + |
| 51 | + NodeServer<?> server = new NodeServer<>(lattice, store, BASE_PORT + i); |
| 52 | + server.launch(); |
| 53 | + servers.add(server); |
| 54 | + |
| 55 | + System.out.println("Node " + (i + 1) + " started on port " + (BASE_PORT + i)); |
| 56 | + } |
| 57 | + |
| 58 | + System.out.println("\nEstablishing peer connections (full mesh)..."); |
| 59 | + |
| 60 | + // Establish full mesh peer connections |
| 61 | + for (int i = 0; i < NUM_NODES; i++) { |
| 62 | + NodeServer<?> server = servers.get(i); |
| 63 | + for (int j = 0; j < NUM_NODES; j++) { |
| 64 | + if (i != j) { |
| 65 | + InetSocketAddress peerAddress = new InetSocketAddress("localhost", BASE_PORT + j); |
| 66 | + Convex peer = ConvexRemote.connect(peerAddress); |
| 67 | + server.addPeer(peer); |
| 68 | + } |
| 69 | + } |
| 70 | + System.out.println("Node " + (i + 1) + " connected to " + (NUM_NODES - 1) + " peers"); |
| 71 | + } |
| 72 | + |
| 73 | + System.out.println("\n=== Starting Merge Operations ==="); |
| 74 | + System.out.println("Performing " + MERGES + " merges with " + MODS + " modifications each on Node 1...\n"); |
| 75 | + |
| 76 | + Random random = new Random(12345); // Fixed seed for reproducibility |
| 77 | + Keyword dataKeyword = Keyword.intern("data"); |
| 78 | + NodeServer<?> node1 = servers.get(0); |
| 79 | + |
| 80 | + long startTime = System.currentTimeMillis(); |
| 81 | + |
| 82 | + // Perform merge operations |
| 83 | + for (int merge = 0; merge < MERGES; merge++) { |
| 84 | + // Get current lattice value from node 1 |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + Index<Hash, ACell> dataIndex = (Index<Hash, ACell>) node1.getCursor().get(dataKeyword); |
| 87 | + if (dataIndex == null) { |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + Index<Hash, ACell> emptyIndex = (Index<Hash, ACell>) Index.EMPTY; |
| 90 | + dataIndex = emptyIndex; |
| 91 | + } |
| 92 | + |
| 93 | + // Make modifications |
| 94 | + for (int mod = 0; mod < MODS; mod++) { |
| 95 | + long value = random.nextLong(1000000); |
| 96 | + ACell cellValue = CVMLong.create(value); |
| 97 | + Hash valueHash = Hash.get(cellValue); |
| 98 | + dataIndex = dataIndex.assoc(valueHash, cellValue); |
| 99 | + } |
| 100 | + |
| 101 | + // Update node 1 with modified value (triggers automatic broadcast) |
| 102 | + node1.updateLocalPath(dataIndex, dataKeyword); |
| 103 | + |
| 104 | + if ((merge + 1) % 10 == 0) { |
| 105 | + System.out.println("Completed " + (merge + 1) + " merges (" + ((merge + 1) * MODS) + " total modifications)"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + long mergeTime = System.currentTimeMillis() - startTime; |
| 110 | + System.out.println("\nMerge operations completed in " + mergeTime + "ms"); |
| 111 | + |
| 112 | + System.out.println("\n=== Synchronizing All Nodes ==="); |
| 113 | + |
| 114 | + // Sync all nodes to ensure convergence |
| 115 | + long syncStart = System.currentTimeMillis(); |
| 116 | + for (int i = 0; i < NUM_NODES; i++) { |
| 117 | + NodeServer<?> server = servers.get(i); |
| 118 | + boolean syncResult = server.sync(); |
| 119 | + System.out.println("Node " + (i + 1) + " sync: " + (syncResult ? "SUCCESS" : "FAILED")); |
| 120 | + } |
| 121 | + long syncTime = System.currentTimeMillis() - syncStart; |
| 122 | + System.out.println("Synchronization completed in " + syncTime + "ms"); |
| 123 | + |
| 124 | + System.out.println("\n=== Verifying Convergence ==="); |
| 125 | + |
| 126 | + // Get final values from all nodes |
| 127 | + List<ACell> finalValues = new ArrayList<>(); |
| 128 | + for (int i = 0; i < NUM_NODES; i++) { |
| 129 | + ACell value = servers.get(i).getLocalValue(); |
| 130 | + finalValues.add(value); |
| 131 | + System.out.println("Node " + (i + 1) + " final value hash: " + Hash.get(value)); |
| 132 | + } |
| 133 | + |
| 134 | + // Verify all nodes have the same value |
| 135 | + boolean allEqual = true; |
| 136 | + ACell reference = finalValues.get(0); |
| 137 | + for (int i = 1; i < finalValues.size(); i++) { |
| 138 | + if (!reference.equals(finalValues.get(i))) { |
| 139 | + allEqual = false; |
| 140 | + System.out.println("\nERROR: Node " + (i + 1) + " has different value than Node 1!"); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (allEqual) { |
| 145 | + System.out.println("\n✓ SUCCESS: All nodes converged to the same lattice value!"); |
| 146 | + |
| 147 | + // Calculate statistics |
| 148 | + @SuppressWarnings("unchecked") |
| 149 | + Index<Hash, ACell> finalDataIndex = (Index<Hash, ACell>) node1.getCursor().get(dataKeyword); |
| 150 | + long entryCount = finalDataIndex != null ? finalDataIndex.count() : 0; |
| 151 | + |
| 152 | + System.out.println("\nStatistics:"); |
| 153 | + System.out.println(" Total merges: " + MERGES); |
| 154 | + System.out.println(" Modifications per merge: " + MODS); |
| 155 | + System.out.println(" Total modifications: " + (MERGES * MODS)); |
| 156 | + System.out.println(" Final lattice entries: " + entryCount); |
| 157 | + System.out.println(" Merge time: " + mergeTime + "ms"); |
| 158 | + System.out.println(" Sync time: " + syncTime + "ms"); |
| 159 | + System.out.println(" Total time: " + (mergeTime + syncTime) + "ms"); |
| 160 | + |
| 161 | + // Show propagator statistics |
| 162 | + System.out.println("\nPropagator Statistics:"); |
| 163 | + for (int i = 0; i < NUM_NODES; i++) { |
| 164 | + LatticePropagator<?> propagator = servers.get(i).getPropagator(); |
| 165 | + System.out.println(" Node " + (i + 1) + ": " + |
| 166 | + propagator.getBroadcastCount() + " delta broadcasts, " + |
| 167 | + propagator.getRootSyncCount() + " root syncs"); |
| 168 | + } |
| 169 | + } else { |
| 170 | + System.out.println("\n✗ FAILURE: Nodes did not converge!"); |
| 171 | + System.exit(1); |
| 172 | + } |
| 173 | + |
| 174 | + } finally { |
| 175 | + // Cleanup |
| 176 | + System.out.println("\nShutting down nodes..."); |
| 177 | + for (int i = 0; i < servers.size(); i++) { |
| 178 | + try { |
| 179 | + servers.get(i).close(); |
| 180 | + System.out.println("Node " + (i + 1) + " shutdown complete"); |
| 181 | + } catch (Exception e) { |
| 182 | + System.err.println("Error closing node " + (i + 1) + ": " + e.getMessage()); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + for (int i = 0; i < stores.size(); i++) { |
| 187 | + try { |
| 188 | + stores.get(i).close(); |
| 189 | + } catch (Exception e) { |
| 190 | + System.err.println("Error closing store " + (i + 1) + ": " + e.getMessage()); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + System.out.println("\n=== Demo Complete ==="); |
| 196 | + } |
| 197 | +} |
0 commit comments