Skip to content

Commit b153917

Browse files
Code for 1.1.2045
* Fixed bug in GetRangeFromSet * Simplify code in ExpirationManager * Code cleanup
1 parent a73708c commit b153917

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+281
-288
lines changed

Hangfire.FluentNHibernateStorage/CountersAggregator.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ public void Execute(CancellationToken cancellationToken)
5353
expireAt = i.Max(counter => counter.ExpireAt)
5454
})
5555
.ToList();
56-
var query = session.CreateQuery(SQLHelper.UpdateAggregateCounterSql);
56+
var query = session.CreateQuery(SqlUtil.UpdateAggregateCounterSql);
5757

5858
foreach (var item in countersByName)
5959
{
6060
if (item.expireAt.HasValue)
6161
{
62-
query.SetParameter(SQLHelper.ValueParameter2Name, item.expireAt.Value);
62+
query.SetParameter(SqlUtil.ValueParameter2Name, item.expireAt.Value);
6363
}
6464
else
6565
{
66-
query.SetParameter(SQLHelper.ValueParameter2Name, null);
66+
query.SetParameter(SqlUtil.ValueParameter2Name, null);
6767
}
68-
if (query.SetString(SQLHelper.IdParameterName, item.Key)
69-
.SetParameter(SQLHelper.ValueParameterName, item.value)
68+
if (query.SetString(SqlUtil.IdParameterName, item.Key)
69+
.SetParameter(SqlUtil.ValueParameterName, item.value)
7070
.ExecuteUpdate() == 0)
7171
{
7272
session.Insert(new _AggregatedCounter

Hangfire.FluentNHibernateStorage/DateHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ namespace Hangfire.FluentNHibernateStorage
44
{
55
internal static class DateHelper
66
{
7-
public static readonly DateTime horizon = new DateTime(1970, 1, 1);
7+
public static readonly DateTime Horizon = new DateTime(1970, 1, 1);
88

99
public static long ToUnixDate(this DateTime dt)
1010
{
11-
return Convert.ToInt64(dt.Subtract(horizon).TotalMilliseconds);
11+
return Convert.ToInt64(dt.Subtract(Horizon).TotalMilliseconds);
1212
}
1313
}
1414
}

Hangfire.FluentNHibernateStorage/Entities/EntityBase0.cs renamed to Hangfire.FluentNHibernateStorage/Entities/Int64IdBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public abstract class EntityBase0 : IInt64Id
3+
public abstract class Int64IdBase : IInt64Id
44
{
55
public virtual long Id { get; set; }
66
}

Hangfire.FluentNHibernateStorage/Entities/EntityBase1.cs renamed to Hangfire.FluentNHibernateStorage/Entities/KeyValueTypeBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Hangfire.FluentNHibernateStorage.Entities
44
{
5-
public abstract class EntityBase1<T> : EntityBase0, IExpirableWithKey, IExpirableWithId
5+
public abstract class KeyValueTypeBase<T> : Int64IdBase, IExpirableWithKey, IExpirableWithId
66
{
77
public virtual T Value { get; set; }
88
public virtual string Key { get; set; }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _AggregatedCounter : EntityBase1<long>
3+
public class _AggregatedCounter : KeyValueTypeBase<long>
44
{
55
}
66
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _Counter : EntityBase1<long>
3+
public class _Counter : KeyValueTypeBase<long>
44
{
55
}
66
}

Hangfire.FluentNHibernateStorage/Entities/_DistributedLock.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace Hangfire.FluentNHibernateStorage.Entities
44
{
5-
public class _DistributedLock : EntityBase0
5+
public class _DistributedLock : Int64IdBase
66
{
77
public _DistributedLock()
88
{
99
CreatedAt = DateTime.UtcNow;
1010
ExpireAtAsLong = DateTime.Now.ToUnixDate();
1111
}
1212

13+
/// <summary>
14+
/// This is a long integer because NHibernate's default storage for dates
15+
/// doesn't have accuracy smaller than 1 second.
16+
/// </summary>
1317
public virtual long ExpireAtAsLong { get; set; }
1418
public virtual string Resource { get; set; }
1519
public virtual DateTime CreatedAt { get; set; }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _Dual : EntityBase0
3+
public class _Dual : Int64IdBase
44
{
55
}
66
}

Hangfire.FluentNHibernateStorage/Entities/_Hash.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _Hash : EntityBase1<string>
3+
public class _Hash : KeyValueTypeBase<string>
44
{
55
public virtual string Field { get; set; }
66
}

Hangfire.FluentNHibernateStorage/Entities/_JobParameter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _JobParameter : EntityBase0
3+
public class _JobParameter : Int64IdBase
44
{
55
public virtual string Name { get; set; }
66
public virtual string Value { get; set; }

Hangfire.FluentNHibernateStorage/Entities/_JobQueue.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Hangfire.FluentNHibernateStorage.Entities
44
{
5-
public class _JobQueue : EntityBase0
5+
public class _JobQueue : Int64IdBase
66
{
77
public virtual _Job Job { get; set; }
88
public virtual string Queue { get; set; }

Hangfire.FluentNHibernateStorage/Entities/_JobState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Hangfire.FluentNHibernateStorage.Entities
44
{
5-
public class _JobState : EntityBase0
5+
public class _JobState : Int64IdBase
66
{
77
public virtual _Job Job { get; set; }
88
public virtual string Name { get; set; }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _List : EntityBase1<string>, IKeyWithStringValue
3+
public class _List : KeyValueTypeBase<string>, IKeyWithStringValue
44
{
55
}
66
}

Hangfire.FluentNHibernateStorage/Entities/_Set.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hangfire.FluentNHibernateStorage.Entities
22
{
3-
public class _Set : EntityBase1<string>, IKeyWithStringValue
3+
public class _Set : KeyValueTypeBase<string>, IKeyWithStringValue
44
{
55
public virtual double Score { get; set; }
66
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Threading;
54
using Hangfire.FluentNHibernateStorage.Entities;
@@ -11,7 +10,6 @@ namespace Hangfire.FluentNHibernateStorage
1110
#pragma warning disable 618
1211
public class ExpirationManager : IBackgroundProcess, IServerComponent
1312
{
14-
#pragma warning restore 618
1513
private const string DistributedLockKey = "expirationmanager";
1614
private const int NumberOfRecordsInSinglePass = 1000;
1715
private static readonly ILog Logger = LogProvider.GetCurrentClassLogger();
@@ -38,72 +36,11 @@ public void Execute(BackgroundProcessContext context)
3836
{
3937
var cancellationToken = context.CancellationToken;
4038
Execute(cancellationToken);
41-
42-
}
43-
44-
private long DeleteExpirableWithId<T>(SessionWrapper session, DateTime baseDate) where T : IExpirableWithId
45-
46-
{
47-
List<long> ids;
48-
ids = session.Query<T>()
49-
.Where(i => i.ExpireAt < baseDate)
50-
.Take(NumberOfRecordsInSinglePass)
51-
.Select(i => i.Id)
52-
.ToList();
53-
return session.DeleteByInt64Id<T>(ids);
54-
}
55-
56-
57-
private void BatchDelete<T>(CancellationToken cancellationToken,
58-
Func<SessionWrapper, DateTime, long> deleteFunc)
59-
60-
{
61-
var entityName = typeof(T).Name;
62-
Logger.InfoFormat("Removing outdated records from table '{0}'...", entityName);
63-
64-
long removedCount = 0;
65-
66-
do
67-
{
68-
try
69-
{
70-
using (var distributedLock =
71-
new FluentNHibernateDistributedLock(_storage, DistributedLockKey, DefaultLockTimeout,
72-
cancellationToken).Acquire())
73-
{
74-
using (var session = _storage.GetSession())
75-
{
76-
removedCount = deleteFunc(session, _storage.UtcNow);
77-
}
78-
}
79-
80-
Logger.InfoFormat("removed records count={0}", removedCount);
81-
}
82-
catch (Exception ex)
83-
{
84-
Logger.Error(ex.ToString());
85-
}
86-
87-
88-
if (removedCount > 0)
89-
{
90-
Logger.Info(string.Format("Removed {0} outdated record(s) from '{1}' table.", removedCount,
91-
entityName));
92-
93-
cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
94-
cancellationToken.ThrowIfCancellationRequested();
95-
}
96-
} while (removedCount != 0);
97-
}
98-
99-
public override string ToString()
100-
{
101-
return GetType().ToString();
10239
}
10340

10441
public void Execute(CancellationToken cancellationToken)
10542
{
106-
BatchDelete<_JobState>(cancellationToken, (session, baseDate2) =>
43+
BatchDelete<_JobState>(cancellationToken, session =>
10744
{
10845
var idList = session.Query<_JobState>()
10946
.Where(i => i.Job.ExpireAt < session.Storage.UtcNow)
@@ -112,7 +49,7 @@ public void Execute(CancellationToken cancellationToken)
11249
.ToList();
11350
return session.DeleteByInt64Id<_JobState>(idList);
11451
});
115-
BatchDelete<_JobQueue>(cancellationToken, (session, baseDate2) =>
52+
BatchDelete<_JobQueue>(cancellationToken, session =>
11653
{
11754
var idList = session.Query<_JobQueue>()
11855
.Where(i => i.Job.ExpireAt < session.Storage.UtcNow)
@@ -121,7 +58,7 @@ public void Execute(CancellationToken cancellationToken)
12158
.ToList();
12259
return session.DeleteByInt64Id<_JobState>(idList);
12360
});
124-
BatchDelete<_JobParameter>(cancellationToken, (session, baseDate2) =>
61+
BatchDelete<_JobParameter>(cancellationToken, session =>
12562
{
12663
var idList = session.Query<_JobParameter>()
12764
.Where(i => i.Job.ExpireAt < session.Storage.UtcNow)
@@ -130,7 +67,7 @@ public void Execute(CancellationToken cancellationToken)
13067
.ToList();
13168
return session.DeleteByInt64Id<_JobParameter>(idList);
13269
});
133-
BatchDelete<_DistributedLock>(cancellationToken, (session, baseDate2) =>
70+
BatchDelete<_DistributedLock>(cancellationToken, session =>
13471
{
13572
var idList = session.Query<_DistributedLock>()
13673
.Where(i => i.ExpireAtAsLong < session.Storage.UtcNow.ToUnixDate())
@@ -147,5 +84,68 @@ public void Execute(CancellationToken cancellationToken)
14784

14885
cancellationToken.WaitHandle.WaitOne(_checkInterval);
14986
}
87+
88+
internal static long DeleteExpirableWithId<T>(SessionWrapper session) where T : IExpirableWithId
89+
90+
{
91+
return session.DeleteExpirableWithId<T>(NumberOfRecordsInSinglePass);
92+
}
93+
94+
95+
private void BatchDelete<T>(CancellationToken cancellationToken,
96+
Func<SessionWrapper, long> deleteFunc)
97+
98+
{
99+
try
100+
{
101+
var entityName = typeof(T).Name;
102+
Logger.InfoFormat("Removing outdated records from table '{0}'...", entityName);
103+
104+
long removedCount = 0;
105+
106+
while (true)
107+
{
108+
try
109+
{
110+
using (new FluentNHibernateDistributedLock(_storage, DistributedLockKey, DefaultLockTimeout,
111+
cancellationToken).Acquire())
112+
{
113+
using (var session = _storage.GetSession())
114+
{
115+
removedCount = deleteFunc(session);
116+
}
117+
}
118+
119+
Logger.InfoFormat("removed records count={0}", removedCount);
120+
}
121+
catch (Exception ex)
122+
{
123+
Logger.Error(ex.ToString());
124+
}
125+
126+
127+
if (removedCount <= 0)
128+
{
129+
break;
130+
}
131+
132+
Logger.Info(string.Format("Removed {0} outdated record(s) from '{1}' table.", removedCount,
133+
entityName));
134+
135+
cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
136+
cancellationToken.ThrowIfCancellationRequested();
137+
}
138+
}
139+
catch (Exception) when (cancellationToken.IsCancellationRequested)
140+
{
141+
//do nothing
142+
}
143+
}
144+
145+
public override string ToString()
146+
{
147+
return GetType().ToString();
148+
}
149+
#pragma warning restore 618
150150
}
151151
}

Hangfire.FluentNHibernateStorage/FluentNHibernateDistributedLock.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ internal FluentNHibernateDistributedLock Acquire()
5555
_cancellationToken.ThrowIfCancellationRequested();
5656

5757

58-
if (SQLHelper.WrapForTransaction(() =>
58+
if (SqlUtil.WrapForTransaction(() =>
5959
{
6060
using (var session = _storage.GetSession())
6161
{
6262
using (var transaction = session.BeginTransaction(IsolationLevel.Serializable))
6363
{
6464
var realnow1 = session.Storage.UtcNow;
65-
var count = session.CreateQuery(SQLHelper.GetCreateDistributedLockStatement())
66-
.SetParameter(SQLHelper.ResourceParameterName, _resource)
67-
.SetParameter(SQLHelper.ExpireAtAsLongParameterName,
65+
var count = session.CreateQuery(SqlUtil.GetCreateDistributedLockStatement())
66+
.SetParameter(SqlUtil.ResourceParameterName, _resource)
67+
.SetParameter(SqlUtil.ExpireAtAsLongParameterName,
6868
realnow1.Add(_timeout).ToUnixDate())
69-
.SetParameter(SQLHelper.NowParameterName, realnow1)
70-
.SetParameter(SQLHelper.NowAsLongParameterName, realnow1.ToUnixDate());
69+
.SetParameter(SqlUtil.NowParameterName, realnow1)
70+
.SetParameter(SqlUtil.NowAsLongParameterName, realnow1.ToUnixDate());
7171

7272

7373
if (count.ExecuteUpdate() > 0)
@@ -98,12 +98,12 @@ internal FluentNHibernateDistributedLock Acquire()
9898

9999
internal void Release()
100100
{
101-
SQLHelper.WrapForTransaction(() =>
101+
SqlUtil.WrapForTransaction(() =>
102102
{
103103
using (var session = _storage.GetSession())
104104
{
105-
session.CreateQuery(SQLHelper.DeleteDistributedLockSql)
106-
.SetParameter(SQLHelper.IdParameterName, _resource)
105+
session.CreateQuery(SqlUtil.DeleteDistributedLockSql)
106+
.SetParameter(SqlUtil.IdParameterName, _resource)
107107
.ExecuteUpdate();
108108
session.Flush();
109109
Logger.DebugFormat("Released distributed lock for {0}", _resource);

0 commit comments

Comments
 (0)