-
Notifications
You must be signed in to change notification settings - Fork 933
Add monitor based sync only locker for ReadWrite cache #2944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cba6d77
Add monitor sync only locker for ReadWrite cache
bahusoid 3c2aefd
Apply suggestions from code review
bahusoid 4f49372
Fix build
bahusoid 625092c
DRY test case
bahusoid b1fe614
Better exception
bahusoid 843ba18
Docs
bahusoid f4582d7
whites
bahusoid 23dfac5
Code review
bahusoid dff55a9
whitespaces
bahusoid ecb5bd8
Merge branch '5.3.x' into gh2862
bahusoid 8616fa9
Merge branch '5.3.x' into gh2862
fredericDelaporte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using NHibernate.Cache; | ||
using NHibernate.Cache.Access; | ||
using NHibernate.Cfg; | ||
using NUnit.Framework; | ||
using Environment = NHibernate.Cfg.Environment; | ||
|
||
namespace NHibernate.Test.CacheTest | ||
{ | ||
[TestFixture] | ||
public class SyncOnlyCacheFixture : TestCase | ||
{ | ||
protected override void Configure(Configuration cfg) | ||
{ | ||
base.Configure(cfg); | ||
cfg.SetProperty(Environment.CacheReadWriteLockFactory, "sync"); | ||
} | ||
|
||
[Test] | ||
public void TestSimpleCache() | ||
{ | ||
DoTestCache(new HashtableCacheProvider()); | ||
} | ||
|
||
private CacheKey CreateCacheKey(string text) | ||
{ | ||
return new CacheKey(text, NHibernateUtil.String, "Foo", null, null); | ||
} | ||
|
||
public void DoTestCache(ICacheProvider cacheProvider) | ||
{ | ||
var cache = (CacheBase) cacheProvider.BuildCache(typeof(String).FullName, new Dictionary<string, string>()); | ||
|
||
long longBefore = Timestamper.Next(); | ||
|
||
Thread.Sleep(15); | ||
|
||
long before = Timestamper.Next(); | ||
|
||
Thread.Sleep(15); | ||
|
||
ICacheConcurrencyStrategy ccs = CreateCache(cache); | ||
|
||
// cache something | ||
CacheKey fooKey = CreateCacheKey("foo"); | ||
|
||
Assert.IsTrue(ccs.Put(fooKey, "foo", before, null, null, false)); | ||
|
||
Thread.Sleep(15); | ||
|
||
long after = Timestamper.Next(); | ||
|
||
Assert.IsNull(ccs.Get(fooKey, longBefore)); | ||
Assert.AreEqual("foo", ccs.Get(fooKey, after)); | ||
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false)); | ||
|
||
// update it; | ||
|
||
ISoftLock fooLock = ccs.Lock(fooKey, null); | ||
|
||
Assert.IsNull(ccs.Get(fooKey, after)); | ||
Assert.IsNull(ccs.Get(fooKey, longBefore)); | ||
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false)); | ||
|
||
Thread.Sleep(15); | ||
|
||
long whileLocked = Timestamper.Next(); | ||
|
||
Assert.IsFalse(ccs.Put(fooKey, "foo", whileLocked, null, null, false)); | ||
|
||
Thread.Sleep(15); | ||
|
||
ccs.Release(fooKey, fooLock); | ||
|
||
Assert.IsNull(ccs.Get(fooKey, after)); | ||
Assert.IsNull(ccs.Get(fooKey, longBefore)); | ||
Assert.IsFalse(ccs.Put(fooKey, "bar", whileLocked, null, null, false)); | ||
Assert.IsFalse(ccs.Put(fooKey, "bar", after, null, null, false)); | ||
|
||
Thread.Sleep(15); | ||
|
||
long longAfter = Timestamper.Next(); | ||
|
||
Assert.IsTrue(ccs.Put(fooKey, "baz", longAfter, null, null, false)); | ||
Assert.IsNull(ccs.Get(fooKey, after)); | ||
Assert.IsNull(ccs.Get(fooKey, whileLocked)); | ||
|
||
Thread.Sleep(15); | ||
|
||
long longLongAfter = Timestamper.Next(); | ||
|
||
Assert.AreEqual("baz", ccs.Get(fooKey, longLongAfter)); | ||
|
||
// update it again, with multiple locks | ||
|
||
ISoftLock fooLock1 = ccs.Lock(fooKey, null); | ||
ISoftLock fooLock2 = ccs.Lock(fooKey, null); | ||
|
||
Assert.IsNull(ccs.Get(fooKey, longLongAfter)); | ||
|
||
Thread.Sleep(15); | ||
|
||
whileLocked = Timestamper.Next(); | ||
|
||
Assert.IsFalse(ccs.Put(fooKey, "foo", whileLocked, null, null, false)); | ||
|
||
Thread.Sleep(15); | ||
|
||
ccs.Release(fooKey, fooLock2); | ||
|
||
Thread.Sleep(15); | ||
|
||
long betweenReleases = Timestamper.Next(); | ||
|
||
Assert.IsFalse(ccs.Put(fooKey, "bar", betweenReleases, null, null, false)); | ||
Assert.IsNull(ccs.Get(fooKey, betweenReleases)); | ||
|
||
Thread.Sleep(15); | ||
|
||
ccs.Release(fooKey, fooLock1); | ||
|
||
Assert.IsFalse(ccs.Put(fooKey, "bar", whileLocked, null, null, false)); | ||
|
||
Thread.Sleep(15); | ||
|
||
longAfter = Timestamper.Next(); | ||
|
||
Assert.IsTrue(ccs.Put(fooKey, "baz", longAfter, null, null, false)); | ||
Assert.IsNull(ccs.Get(fooKey, whileLocked)); | ||
|
||
Thread.Sleep(15); | ||
|
||
longLongAfter = Timestamper.Next(); | ||
|
||
Assert.AreEqual("baz", ccs.Get(fooKey, longLongAfter)); | ||
} | ||
|
||
private ICacheConcurrencyStrategy CreateCache(CacheBase cache, string strategy = CacheFactory.ReadWrite) | ||
{ | ||
return CacheFactory.CreateCache(strategy, cache, Sfi.Settings); | ||
} | ||
|
||
private void DoTestMinValueTimestampOnStrategy(CacheBase cache, ICacheConcurrencyStrategy strategy) | ||
{ | ||
CacheKey key = CreateCacheKey("key"); | ||
strategy.Cache = cache; | ||
strategy.Put(key, "value", long.MinValue, 0, null, false); | ||
|
||
Assert.IsNull(strategy.Get(key, long.MinValue), "{0} strategy fails the test", strategy.GetType()); | ||
Assert.IsNull(strategy.Get(key, long.MaxValue), "{0} strategy fails the test", strategy.GetType()); | ||
} | ||
|
||
[Test] | ||
public void MinValueTimestamp() | ||
{ | ||
var cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>()); | ||
|
||
DoTestMinValueTimestampOnStrategy(cache, CreateCache(cache)); | ||
DoTestMinValueTimestampOnStrategy(cache, CreateCache(cache, CacheFactory.NonstrictReadWrite)); | ||
DoTestMinValueTimestampOnStrategy(cache, CreateCache(cache, CacheFactory.ReadOnly)); | ||
} | ||
|
||
[Test] | ||
public void AsyncOperationsThrow() | ||
{ | ||
var cache = new HashtableCacheProvider().BuildCache("region", new Dictionary<string, string>()); | ||
var strategy = CreateCache(cache); | ||
CacheKey key = CreateCacheKey("key"); | ||
var stamp = Timestamper.Next(); | ||
Assert.ThrowsAsync<InvalidOperationException>( | ||
() => | ||
strategy.PutAsync(key, "value", stamp, 0, null, false, default(CancellationToken))); | ||
Assert.ThrowsAsync<InvalidOperationException>(() => strategy.GetAsync(key, stamp, default(CancellationToken))); | ||
} | ||
|
||
protected override string[] Mappings => Array.Empty<string>(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Cache | ||
{ | ||
public interface ICacheLock : IDisposable | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
IDisposable ReadLock(); | ||
IDisposable WriteLock(); | ||
Task<IDisposable> ReadLockAsync(); | ||
Task<IDisposable> WriteLockAsync(); | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public interface ICacheReadWriteLockFactory | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ICacheLock Create(); | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
class AsyncCacheReadWriteLockFactory : ICacheReadWriteLockFactory | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public ICacheLock Create() | ||
{ | ||
return new AsyncReaderWriterLock(); | ||
} | ||
} | ||
|
||
class SyncCacheReadWriteLockFactory : ICacheReadWriteLockFactory | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public ICacheLock Create() | ||
{ | ||
return new SyncCacheLock(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NHibernate.Cache | ||
{ | ||
class SyncCacheLock : ICacheLock | ||
{ | ||
class MonitorLock : IDisposable | ||
{ | ||
private readonly object _lockObj; | ||
|
||
public MonitorLock(object lockObj) | ||
{ | ||
Monitor.Enter(lockObj); | ||
_lockObj = lockObj; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Monitor.Exit(_lockObj); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public IDisposable ReadLock() | ||
{ | ||
return new MonitorLock(this); | ||
} | ||
|
||
public IDisposable WriteLock() | ||
{ | ||
return new MonitorLock(this); | ||
} | ||
|
||
public Task<IDisposable> ReadLockAsync() | ||
{ | ||
throw new InvalidOperationException("This locker supports only sync operations."); | ||
} | ||
|
||
public Task<IDisposable> WriteLockAsync() | ||
{ | ||
throw new InvalidOperationException("This locker supports only sync operations."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.