public void TwoUpdaters3() { Assert.Throws <OptimisticLockingFailed>(() => { UInt64 id; try { using (SessionNoServer session = new SessionNoServer(systemDir)) { session.BeginUpdate(); Man man = new Man(); man.Persist(session, man); id = man.Id; session.Commit(); session.BeginUpdate(); man.Age = ++man.Age; session.FlushUpdates(); // fStream set for updated databases will cause other write sessions to fail updating these databases using (SessionNoServer session2 = new SessionNoServer(systemDir)) { session2.BeginUpdate(); Man man2 = (Man)session2.Open(id); Assert.Less(man2.Age, man.Age); man2.Age = ++man.Age; session2.Commit(); // OptimisticLockingFailed here } session.Commit(); session.Verify(); } } finally { System.GC.Collect(); } }); }
public void TwoUpdaters1() { Assert.Throws <OptimisticLockingFailed>(() => { UInt64 id; using (SessionNoServer session = new SessionNoServer(systemDir)) { session.BeginUpdate(); Man man = new Man(); man.Persist(session, man); id = man.Id; session.Commit(); session.BeginUpdate(); man.Age = ++man.Age; Database db = session.NewDatabase(3567); using (SessionNoServer session2 = new SessionNoServer(systemDir)) { session2.BeginUpdate(); Man man2 = (Man)session2.Open(id); Assert.Less(man2.Age, man.Age); man2.Age = ++man.Age; session2.Commit(); } session.DeleteDatabase(db); session.Commit(); // OptimisticLockingFailed here session.Verify(); } }); }
public void Verify(string dir) { using (SessionNoServer session = new SessionNoServer(dir)) { session.BeginRead(); session.Verify(); session.Commit(); } }
public void MultipleThreadsAdding() { bool doClearAll = SessionBase.ClearAllCachedObjectsWhenDetectingUpdatedDatabase; SessionBase.ClearAllCachedObjectsWhenDetectingUpdatedDatabase = false; try { using (var session = new SessionNoServer(systemDir)) { session.BeginUpdate(); session.RegisterClass(typeof(AutoPlacement)); // build in type but not yet registered as a one session.RegisterClass(typeof(ObservableList <int>)); session.RegisterClass(typeof(Dokument)); UInt32 dbNum = session.DatabaseNumberOf(typeof(Dokument)); Database db = session.OpenDatabase(dbNum, false, false); if (db == null) { db = session.NewDatabase(dbNum, 0, typeof(Dokument).ToGenericTypeString()); } Dokument doc = new Dokument(); session.Persist(doc); session.Commit(); } using (var sharedReadSession = new ServerClientSessionShared(systemDir)) { sharedReadSession.BeginRead(); Parallel.ForEach(Enumerable.Range(1, 3), (num) => LockConflict(sharedReadSession)); } using (var session = new SessionNoServer(systemDir)) { session.Verify(); } } finally { SessionBase.ClearAllCachedObjectsWhenDetectingUpdatedDatabase = doClearAll; } }
public void CreateDataAndIterateDb(int numObj) { using (SessionNoServer session = new SessionNoServer(systemDir)) { session.Verify(); session.Commit(); session.BeginUpdate(); UInt32 dbNum = session.DatabaseNumberOf(typeof(NotSharingPage)); Database db = session.OpenDatabase(dbNum, true, false); if (db != null) { session.DeleteDatabase(db); } dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeA)); db = session.OpenDatabase(dbNum, true, false); if (db != null) { session.DeleteDatabase(db); } dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeB)); db = session.OpenDatabase(dbNum, true, false); if (db != null) { session.DeleteDatabase(db); } session.Commit(); } using (SessionNoServer session = new SessionNoServer(systemDir)) { session.Verify(); session.Commit(); session.BeginUpdate(); UInt32 dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeB)); Placement place = new Placement(dbNum, 100); for (int i = 0; i < numObj; i++) { NotSharingPage ns = new NotSharingPage(); session.Persist(ns); SharingPageTypeA sA = new SharingPageTypeA(); session.Persist(sA); SharingPageTypeB sB = new SharingPageTypeB(); if (i % 5 == 0) { sB.Persist(session, place); } else if (i % 1001 == 0) { sB.Persist(session, sA); } else if (i % 3001 == 0) { sB.Persist(session, ns); } else { session.Persist(sB); } } session.Commit(); } using (SessionNoServer session = new SessionNoServer(systemDir)) { session.Verify(); UInt32 dbNum = session.DatabaseNumberOf(typeof(NotSharingPage)); Database db = session.OpenDatabase(dbNum); AllObjects <NotSharingPage> all = db.AllObjects <NotSharingPage>(); int ct = all.Count(); dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeA)); OfType ofType = db.OfType(typeof(NotSharingPage)); int ct2 = (int)ofType.Count; Assert.AreEqual(ct, ct2); Database dbA = session.OpenDatabase(dbNum); dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeB)); Database dbB = session.OpenDatabase(dbNum); AllObjects <SharingPageTypeA> allA = dbA.AllObjects <SharingPageTypeA>(); AllObjects <SharingPageTypeB> allB = dbB.AllObjects <SharingPageTypeB>(); OfType allA2 = dbA.OfType(typeof(SharingPageTypeA)); int start = numObj / 2; NotSharingPage ns = all.ElementAt(numObj); SharingPageTypeA sA = allA.ElementAt(numObj); SharingPageTypeA sA2 = (SharingPageTypeA)allA2.ElementAt(numObj); Assert.AreEqual(sA, sA2); sA = allA.ElementAt(10); sA2 = (SharingPageTypeA)allA2.ElementAt(10); Assert.AreEqual(sA, sA2); //MethodInfo method = typeof(Database).GetMethod("AllObjects"); //MethodInfo generic = method.MakeGenericMethod(sA.GetType()); //dynamic itr = generic.Invoke(dbA, new object[]{ true }); //SharingPageTypeA sAb = itr.ElementAt(numObj); //Assert.AreEqual(sA, sAb); //SharingPageTypeA sAc = itr.ElementAt(numObj); SharingPageTypeB sB = allB.ElementAt(numObj); List <NotSharingPage> notSharingPageList = all.Skip(100).ToList(); List <SharingPageTypeA> sharingPageTypeA = allA.Take(5).Skip(100).ToList(); for (int i = start; i < numObj; i++) { sA = allA.ElementAt(i); } for (int i = start; i < numObj; i += 5) { ns = all.ElementAt(i); } for (int i = start; i < numObj; i += 5) { sB = allB.ElementAt(i); } for (int i = 0; i < numObj; i += 45000) { ns = all.ElementAt(i); } int allB_count = allB.Count(); for (int i = 0; i < allB_count - 1; i++) { Assert.NotNull(allB.ElementAt(i)); } session.Commit(); session.BeginUpdate(); session.DeleteDatabase(db); session.DeleteDatabase(dbA); session.DeleteDatabase(dbB); session.Commit(); } }
public void CreateDataAndIterateDb(int numObj) { using (SessionNoServer session = new SessionNoServer(systemDir)) { session.Verify(); session.BeginUpdate(); UInt32 dbNum = session.DatabaseNumberOf(typeof(NotSharingPage)); Database db = session.OpenDatabase(dbNum, true, false); if (db != null) session.DeleteDatabase(db); dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeA)); db = session.OpenDatabase(dbNum, true, false); if (db != null) session.DeleteDatabase(db); dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeB)); db = session.OpenDatabase(dbNum, true, false); if (db != null) session.DeleteDatabase(db); session.Commit(); } using (SessionNoServer session = new SessionNoServer(systemDir)) { session.Verify(); session.BeginUpdate(); UInt32 dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeB)); Placement place = new Placement(dbNum, 100); for (int i = 0; i < numObj; i++) { NotSharingPage ns = new NotSharingPage(); session.Persist(ns); SharingPageTypeA sA = new SharingPageTypeA(); session.Persist(sA); SharingPageTypeB sB = new SharingPageTypeB(); if (i % 5 == 0) sB.Persist(session, place); else if (i % 1001 == 0) sB.Persist(session, sA); else if (i % 3001 == 0) sB.Persist(session, ns); else session.Persist(sB); } session.Commit(); } using (SessionNoServer session = new SessionNoServer(systemDir)) { session.BeginRead(); session.Verify(); UInt32 dbNum = session.DatabaseNumberOf(typeof(NotSharingPage)); Database db = session.OpenDatabase(dbNum); AllObjects<NotSharingPage> all = db.AllObjects<NotSharingPage>(); ulong ct = all.Count(); dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeA)); OfType ofType = db.OfType(typeof(NotSharingPage)); ulong ct2 = ofType.Count(); Assert.AreEqual(ct, ct2); Database dbA = session.OpenDatabase(dbNum); dbNum = session.DatabaseNumberOf(typeof(SharingPageTypeB)); Database dbB = session.OpenDatabase(dbNum); AllObjects<SharingPageTypeA> allA = dbA.AllObjects<SharingPageTypeA>(); AllObjects<SharingPageTypeB> allB = dbB.AllObjects<SharingPageTypeB>(); OfType allA2 = dbA.OfType(typeof(SharingPageTypeA)); int start = numObj / 2; NotSharingPage ns = all.ElementAt(numObj); SharingPageTypeA sA = allA.ElementAt(numObj); SharingPageTypeA sA2 = (SharingPageTypeA)allA2.ElementAt(numObj); Assert.AreEqual(sA, sA2); sA = allA.ElementAt(10); sA2 = (SharingPageTypeA)allA2.ElementAt(10); Assert.AreEqual(sA, sA2); //MethodInfo method = typeof(Database).GetMethod("AllObjects"); //MethodInfo generic = method.MakeGenericMethod(sA.GetType()); //dynamic itr = generic.Invoke(dbA, new object[]{ true }); //SharingPageTypeA sAb = itr.ElementAt(numObj); //Assert.AreEqual(sA, sAb); //SharingPageTypeA sAc = itr.ElementAt(numObj); SharingPageTypeB sB = allB.ElementAt(numObj); List<NotSharingPage> notSharingPageList = all.Skip(100).ToList(); List<SharingPageTypeA> sharingPageTypeA = allA.Take(5).Skip(100).ToList(); for (int i = start; i < numObj; i++) sA = allA.ElementAt(i); for (int i = start; i < numObj; i += 5) ns = all.ElementAt(i); for (int i = start; i < numObj; i += 5) sB = allB.ElementAt(i); for (int i = 0; i < numObj; i += 45000) ns = all.ElementAt(i); int allB_count = (int) allB.Count(); for (int i = 0; i < allB_count - 1; i++) { Assert.NotNull(allB.ElementAt(i)); } session.Commit(); session.BeginUpdate(); session.DeleteDatabase(db); session.DeleteDatabase(dbA); session.DeleteDatabase(dbB); session.Commit(); } }
[Repeat(3)] // remove when propagtion of optimistic locking flag is done to slave database locations TO DO (issue caused by CopyAllDatabasdesTo that uses pessimistic locking) public void multipleServersOK() { using (SessionNoServer session = new SessionNoServer(systemDir)) { session.BeginRead(); foreach (DatabaseLocation loc in session.DatabaseLocations) { Console.WriteLine(loc.ToStringDetails(session, false)); } session.Commit(); } using (ServerClientSession session = new ServerClientSession(systemDir, systemHost)) { session.SetTraceDbActivity(0); DatabaseLocation localLocation = new DatabaseLocation(systemHost, location2Dir, 10000, 20000, session, PageInfo.compressionKind.LZ4, 0); Placement place = new Placement(10000, 2); session.BeginUpdate(); foreach (DatabaseLocation loc in session.DatabaseLocations) { Console.WriteLine(loc.ToStringDetails(session, false)); } Console.WriteLine(); session.NewLocation(localLocation); foreach (DatabaseLocation loc in session.DatabaseLocations) { Console.WriteLine(loc.ToStringDetails(session, false)); } Man aMan = null; Woman aWoman = null; for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } localLocation = new DatabaseLocation(systemHost2, systemDir, 20001, 30000, session, PageInfo.compressionKind.LZ4, 0); session.NewLocation(localLocation); foreach (DatabaseLocation loc in session.DatabaseLocations) { Console.WriteLine(loc.ToStringDetails(session, false)); } place = new Placement(20001); //localDatabase = session.NewDatabase(20001, localLocation); for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } DatabaseLocation serverLocation = new DatabaseLocation(systemHost2, location2Dir, 30001, 40000, session, PageInfo.compressionKind.LZ4, 0); session.NewLocation(serverLocation); foreach (DatabaseLocation loc in session.DatabaseLocations) { Console.WriteLine(loc.ToStringDetails(session, false)); } place = new Placement(30001); for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } localLocation = new DatabaseLocation(systemHost3, systemDir, 40001, 50000, session, PageInfo.compressionKind.None, 0); session.NewLocation(localLocation); place = new Placement(40001); //localDatabase = session.NewDatabase(20001, localLocation); for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } session.Commit(); } using (ServerClientSession session = new ServerClientSession(systemDir, systemHost)) { session.CopyAllDatabasesTo(copyDir); using (SessionNoServer copySession = new SessionNoServer(copyDir)) { copySession.Verify(); } } using (ServerClientSession session = new ServerClientSession(systemDir, systemHost, 2000, false)) // TO DO, change back to use optimistic locking { //session.SetTraceDbActivity(0); session.BeginUpdate(); Database db = session.OpenDatabase(10000); session.DeleteDatabase(db); db = session.OpenDatabase(20001); session.DeleteDatabase(db); db = session.OpenDatabase(30001); session.DeleteDatabase(db); db = session.OpenDatabase(40001); session.DeleteDatabase(db); session.Commit(); Directory.Delete(copyDir, true); } System.GC.Collect(); System.GC.WaitForPendingFinalizers(); using (ServerClientSession session = new ServerClientSession(systemDir, systemHost)) { Assert.True(session.OptimisticLocking); } }
public void HighAvalailabiltyByReplication() { var alternateSystemBoot = new List <ReplicaInfo> { new ReplicaInfo { Path = "Replica1" }, new ReplicaInfo { Path = "Replica2" } }; var p1 = SessionBase.BaseDatabasePath + "/Replica1"; var p2 = SessionBase.BaseDatabasePath + "/Replica2"; var p3 = SessionBase.BaseDatabasePath + "/Replica3"; var p3remote = $"\\{s_systemHost2}/databases/Replica3"; if (Directory.Exists(p1)) { Directory.Delete(p1, true); } if (Directory.Exists(p2)) { Directory.Delete(p2, true); } if (Directory.Exists(p3)) { Directory.Delete(p3, true); } if (Directory.Exists(p3remote)) { Directory.Delete(p3remote, true); } using (var session = new ServerClientSession(alternateSystemBoot)) { session.BeginUpdate(); for (int i = 0; i < 100; i++) { var s = i.ToString(); session.Persist(s); } session.Commit(); } alternateSystemBoot = new List <ReplicaInfo> { new ReplicaInfo { Path = "Replica1" }, new ReplicaInfo { Path = "Replica2" }, new ReplicaInfo { Path = "Replica3", Host = s_systemHost2 } }; using (var session = new ServerClientSession(alternateSystemBoot)) { session.BeginUpdate(); for (int i = 0; i < 100; i++) { var s = i.ToString(); session.Persist(s); } session.Commit(); } using (var session = new ServerClientSession(alternateSystemBoot)) { session.BeginUpdate(); for (int i = 0; i < 10; i++) { var s = i.ToString(); session.Persist(s); } if (Directory.Exists(p2)) { Directory.Delete(p2, true); } session.Commit(); } using (var session = new ServerClientSession(alternateSystemBoot)) { session.BeginRead(); foreach (var s in session.AllObjects <string>()) { Console.WriteLine(s); } session.Commit(); } using (var session = new ServerClientSession(alternateSystemBoot)) { session.BeginUpdate(); for (int i = 0; i < 10; i++) { var s = i.ToString(); session.Persist(s); if (Directory.Exists(p2)) { Directory.Delete(p2, true); } } session.Commit(); } using (var session = new ServerClientSession(alternateSystemBoot)) { session.BeginRead(); foreach (var s in session.AllObjects <string>()) { Console.WriteLine(s); } session.Commit(); } using (var session = new SessionNoServer("Replica1")) { session.Verify(); } using (var session = new SessionNoServer("Replica2")) { session.Verify(); } using (var session = new ServerClientSession("Replica3", s_systemHost2)) { session.Verify(); } }
[Repeat(3)] // remove when propagation of optimistic locking flag is done to slave database locations TO DO (issue caused by CopyAllDatabasdesTo that uses pessimistic locking) public void multipleServersOK() { using (SessionNoServer session = new SessionNoServer(systemDir)) { session.BeginRead(); foreach (DatabaseLocation loc in session.DatabaseLocations) Console.WriteLine(loc.ToStringDetails(session, false)); session.Commit(); } using (ServerClientSession session = new ServerClientSession(systemDir, systemHost)) { session.SetTraceDbActivity(0); DatabaseLocation localLocation = new DatabaseLocation(systemHost, location2Dir, 10000, 20000, session, PageInfo.compressionKind.LZ4, 0); Placement place = new Placement(10000, 2); session.BeginUpdate(); foreach (DatabaseLocation loc in session.DatabaseLocations) Console.WriteLine(loc.ToStringDetails(session, false)); Console.WriteLine(); session.NewLocation(localLocation); foreach (DatabaseLocation loc in session.DatabaseLocations) Console.WriteLine(loc.ToStringDetails(session, false)); Man aMan = null; Woman aWoman = null; for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } localLocation = new DatabaseLocation(systemHost2, systemDir, 20001, 30000, session, PageInfo.compressionKind.LZ4, 0); session.NewLocation(localLocation); foreach (DatabaseLocation loc in session.DatabaseLocations) Console.WriteLine(loc.ToStringDetails(session, false)); place = new Placement(20001); //localDatabase = session.NewDatabase(20001, localLocation); for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } DatabaseLocation serverLocation = new DatabaseLocation(systemHost2, location2Dir, 30001, 40000, session, PageInfo.compressionKind.LZ4, 0); session.NewLocation(serverLocation); foreach (DatabaseLocation loc in session.DatabaseLocations) Console.WriteLine(loc.ToStringDetails(session, false)); place = new Placement(30001); for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } localLocation = new DatabaseLocation(systemHost3, systemDir, 40001, 50000, session, PageInfo.compressionKind.None, 0); session.NewLocation(localLocation); place = new Placement(40001); //localDatabase = session.NewDatabase(20001, localLocation); for (int j = 1; j <= 5; j++) { aMan = new Man(null, aMan, DateTime.UtcNow); aMan.Persist(place, session); aWoman = new Woman(aMan, aWoman); aWoman.Persist(place, session); aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman); if (j % 1000 == 0) { session.FlushUpdates(); } } session.Commit(); } using (ServerClientSession session = new ServerClientSession(systemDir, systemHost)) { session.CopyAllDatabasesTo(copyDir); using (SessionNoServer copySession = new SessionNoServer(copyDir)) { copySession.Verify(); } } using (ServerClientSession session = new ServerClientSession(systemDir, systemHost, 2000, false)) // TO DO, change back to use optimistic locking { //session.SetTraceDbActivity(0); session.BeginUpdate(); Database db = session.OpenDatabase(10000); session.DeleteDatabase(db); db = session.OpenDatabase(20001); session.DeleteDatabase(db); db = session.OpenDatabase(30001); session.DeleteDatabase(db); db = session.OpenDatabase(40001); session.DeleteDatabase(db); session.Commit(); Directory.Delete(copyDir, true); } System.GC.Collect(); System.GC.WaitForPendingFinalizers(); using (ServerClientSession session = new ServerClientSession(systemDir, systemHost)) { Assert.True(session.OptimisticLocking); } }
static int MoveToDifferentFullClassName() { try { using (SessionNoServer session = new SessionNoServer(s_systemDir)) { session.SetTraceDbActivity(Schema.SchemaDB); session.BeginUpdate(); session.ReplacePersistedType(typeof(VelocityDbSchema.Samples.UpdateClass.UpdatedClass), typeof(UpdateClass.UpdatedClass)); session.Commit(); } using (SessionNoServer session = new SessionNoServer(s_systemDir)) { session.Verify(); session.BeginRead(); foreach (var obj in session.AllObjects <UpdateClass.UpdatedClass>()) { Console.Write(obj.ToString() + " has members: "); foreach (DataMember member in obj.GetDataMembers()) { Console.Write(member.ToString() + " "); } } foreach (var obj in session.AllObjects <VelocityDbSchema.Samples.UpdateClass.UpdatedClass>()) { throw new Exception("not expected"); } session.Commit(); } // change it back to original using (SessionNoServer session = new SessionNoServer(s_systemDir)) { session.SetTraceDbActivity(Schema.SchemaDB); session.BeginUpdate(); session.ReplacePersistedType(typeof(UpdateClass.UpdatedClass).AssemblyQualifiedName, typeof(VelocityDbSchema.Samples.UpdateClass.UpdatedClass)); session.Commit(); } using (SessionNoServer session = new SessionNoServer(s_systemDir)) { session.Verify(); session.BeginRead(); foreach (var obj in session.AllObjects <VelocityDbSchema.Samples.UpdateClass.UpdatedClass>()) { Console.Write(obj.ToString() + " has members: "); foreach (DataMember member in obj.GetDataMembers()) { Console.Write(member.ToString() + " "); } } foreach (var obj in session.AllObjects <UpdateClass.UpdatedClass>()) { throw new Exception("not expected"); } session.Commit(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(1); } return(0); }