void Master(int port, bool async, bool ack, int nIterations) { Console.WriteLine("Starting a replication master"); ReplicationMasterDatabase db = DatabaseFactory.CreateReplicationMasterDatabase(new string[] { "localhost:" + port }, async ? asyncBufSize : 0); string dbName = "replicmaster.dbs"; Tests.TryDeleteFile(dbName); db.ReplicationAck = ack; var dbFile = new OsFile(dbName); dbFile.NoFlush = true; db.Open(dbFile, cacheSizeInBytes); IFieldIndex <int, Record> root = (IFieldIndex <int, Record>)db.Root; if (root == null) { root = db.CreateFieldIndex <int, Record>("key", IndexType.Unique); db.Root = root; } DateTime start = DateTime.Now; int i; int lastKey = 0; for (i = 0; i < nIterations; i++) { if (i >= count) { root.Remove(new Key(i - count)); } Record rec = new Record(); rec.key = i; lastKey = rec.key; root.Put(rec); if (i >= count && i % transSize == 0) { db.Commit(); } if (log && i % 1000 == 0) { Console.WriteLine("Master processed {0} rounds", i); } } db.Commit(); while (true) { long slaveKey = Interlocked.Read(ref slaveCurrKey); if (slaveKey == lastKey) { break; } Thread.Sleep(100); // 1/10th sec } db.Close(); Console.WriteLine("Replication master finished", i); }
void Slave(int port, bool async, bool ack) { Console.WriteLine("Starting a replication slave"); int i; ReplicationSlaveDatabase db = DatabaseFactory.CreateReplicationSlaveDatabase(port); db.ReplicationAck = ack; string dbName = "replicslave.dbs"; Tests.TryDeleteFile(dbName); var dbFile = new OsFile(dbName); dbFile.NoFlush = true; db.Open(dbFile, cacheSizeInBytes); DateTime total = new DateTime(0); int n = 0; long lastKey = 0; while (db.IsConnected()) { db.WaitForModification(); db.BeginThreadTransaction(TransactionMode.ReplicationSlave); IFieldIndex <int, Record> root = (IFieldIndex <int, Record>)db.Root; if (root != null && root.Count == count) { DateTime start = DateTime.Now; int prevKey = -1; i = 0; foreach (Record rec in root) { int key = rec.key; lastKey = rec.key; Debug.Assert(prevKey < 0 || key == prevKey + 1); prevKey = key; i += 1; } Debug.Assert(i == count); n += i; total += (DateTime.Now - start); } db.EndThreadTransaction(); Interlocked.Exchange(ref slaveCurrKey, lastKey); if (log && n % 1000 == 0) { Console.WriteLine("Slave processed {0} transactions", n); } } db.Close(); Console.WriteLine("Replication slave finished", n); }
void Master(int port, bool async, bool ack, int nIterations) { Console.WriteLine("Starting a replication master"); ReplicationMasterDatabase db = DatabaseFactory.CreateReplicationMasterDatabase(new string[] { "localhost:" + port }, async ? asyncBufSize : 0); string dbName = "replicmaster.dbs"; Tests.TryDeleteFile(dbName); db.ReplicationAck = ack; var dbFile = new OsFile(dbName); dbFile.NoFlush = true; db.Open(dbFile, cacheSizeInBytes); IFieldIndex<int, Record> root = (IFieldIndex<int, Record>)db.Root; if (root == null) { root = db.CreateFieldIndex<int, Record>("key", IndexType.Unique); db.Root = root; } DateTime start = DateTime.Now; int i; int lastKey = 0; for (i = 0; i < nIterations; i++) { if (i >= count) root.Remove(new Key(i - count)); Record rec = new Record(); rec.key = i; lastKey = rec.key; root.Put(rec); if (i >= count && i % transSize == 0) db.Commit(); if (log && i % 1000 == 0) Console.WriteLine("Master processed {0} rounds", i); } db.Commit(); while (true) { long slaveKey = Interlocked.Read(ref slaveCurrKey); if (slaveKey == lastKey) break; Thread.Sleep(100); // 1/10th sec } db.Close(); Console.WriteLine("Replication master finished", i); }
public void Run(TestConfig config) { Debug.Assert(!config.IsTransient); IDatabase db = DatabaseFactory.CreateDatabase(); Tests.AssertDatabaseException( () => { var r = db.Root; }, DatabaseException.ErrorCode.DATABASE_NOT_OPENED); db = config.GetDatabase(); Root root = new Root(); var idx = db.CreateIndex <string, RecordFull>(IndexType.NonUnique); root.idx = idx; db.Root = root; db.Commit(); for (int i = 0; i < 10; i++) { var r = new RecordFull(i); idx.Put(r.StrVal, r); } var f = db.File; OsFile of = (OsFile)f; of.Close(); IDatabase db2 = config.GetDatabase(false); try { db.Close(); } catch { } db2.Close(); }
void Slave(int port, bool async, bool ack) { Console.WriteLine("Starting a replication slave"); int i; ReplicationSlaveDatabase db = DatabaseFactory.CreateReplicationSlaveDatabase(port); db.ReplicationAck = ack; string dbName = "replicslave.dbs"; Tests.TryDeleteFile(dbName); var dbFile = new OsFile(dbName); dbFile.NoFlush = true; db.Open(dbFile, cacheSizeInBytes); DateTime total = new DateTime(0); int n = 0; long lastKey = 0; while (db.IsConnected()) { db.WaitForModification(); db.BeginThreadTransaction(TransactionMode.ReplicationSlave); IFieldIndex<int, Record> root = (IFieldIndex<int, Record>)db.Root; if (root != null && root.Count == count) { DateTime start = DateTime.Now; int prevKey = -1; i = 0; foreach (Record rec in root) { int key = rec.key; lastKey = rec.key; Debug.Assert(prevKey < 0 || key == prevKey + 1); prevKey = key; i += 1; } Debug.Assert(i == count); n += i; total += (DateTime.Now - start); } db.EndThreadTransaction(); Interlocked.Exchange(ref slaveCurrKey, lastKey); if (log && n % 1000 == 0) Console.WriteLine("Slave processed {0} transactions", n); } db.Close(); Console.WriteLine("Replication slave finished", n); }