private void Exec(int jobId, double pcPercent, double conflictGuarantee, IConflictResolver resolver) { bool claimIt = false; if (s_rng.NextDouble() < pcPercent) claimIt = true; var cs = BuildChangeSet(jobId); var dc = new Rel.Data.Ef6.TpContext(); var res = resolver; var csp = new ChangeSetProcessor(dc, res); ChangeSet result; if (s_rng.NextDouble() < conflictGuarantee) { Touch(jobId, cs); } using (new Timed(false, "ChangeSet {0}", res.GetType().Name)) result = csp.Process(jobId, claimIt, cs); }
private ChangeSet BuildChangeSet(int jobId) { var dc = new Rel.Data.Ef6.TpContext(); IList<Asset> assets; int count; using (var scope = new TransactionScope()) { int take = s_rng.Next(s_minCsSize, s_maxCsSize); assets = dc.Database.SqlQuery<Asset>( @"SELECT Id ,JobId ,Name ,RowVersion ,ServiceArea ,PercentTolerance ,StaticTolerance ,MonotonicTolerance ,MaximumAndMinimumDecay ,MaxMinDecayWithStepAndTol ,MinimumDecay FROM ( SELECT * FROM dbo.Asset WITH(NOLOCK) WHERE JobId=@p0 ) a ORDER BY PercentTolerance ASC OFFSET 0 ROWS FETCH NEXT @p1 ROWS ONLY", jobId, take).ToList(); count = dc.Database.SqlQuery<int>(@"SELECT COUNT(Id) FROM dbo.Asset WITH(NOLOCK) WHERE JobId=@p0", jobId).Single(); scope.Complete(); } var cs = new ChangeSet() { Assets = assets .Select(ModifyAsset) .Union(NewAssets(jobId, s_rng.Next(1, (int)Math.Ceiling(assets.Count * 0.3)))).ToList() }; s_assetCount[jobId - 1].IncrementBy(count); s_assetCountBase[jobId - 1].Increment(); return cs; }
private void EnsureJobIsNotLocked(int jobId, Exception ex, string username) { var dc = new Rel.Data.Ef6.TpContext(); while (true) { try { using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew)) { using (new Timed(false, "EnsuringNoLock job {0}-{1}", jobId, username)) { var updated = dc.Database.ExecuteSqlCommand( "SET DEADLOCK_PRIORITY 10;UPDATE dbo.Job SET LockedBy=NULL,LockedOn=NULL WHERE Id=@p0 AND LockedBy=@p1", jobId, username); } scope.Complete(); break; } } catch (Exception) { continue; } } if (ex != null) { var e = ex; while (e != null) { if (e is Rel.Data.ConcurrencyException) return; if (e is PessimisticConcurrencyException) { System.Threading.Thread.Sleep(100); return; } if (e is ValidationException) return; if (ex.InnerException != null) ex = ex.InnerException; } throw ex; } }
private static void PreseedDB() { using (var ctx = new Rel.Data.Ef6.TpContext()) { var cmd = @" DECLARE @cnt int = 0; UPDATE Job SET LockedBy=null, LockedOn=null WHERE LockedBy IS NOT NULL AND id=@p0 DELETE FROM Asset WHERE JobId=@p0; WHILE @cnt < @p1 BEGIN INSERT INTO Asset (JobId, Name, ServiceArea,PercentTolerance,StaticTolerance,MonotonicTolerance,MaximumAndMinimumDecay,MaxMinDecayWithStepAndTol,MinimumDecay) SELECT @p0, CAST(NEWID() as char(36)), CAST(NEWID() as char(36)), rand(),rand(),rand(),rand(),rand(),rand() SET @cnt = @cnt+ 1; END "; ctx.Database.ExecuteSqlCommand(cmd, 1, s_initialPoolSize); ctx.Database.ExecuteSqlCommand(cmd, 2, s_initialPoolSize); } }