-
Notifications
You must be signed in to change notification settings - Fork 934
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 all commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using NHibernate.Cache; | ||
using NHibernate.Cfg; | ||
using NUnit.Framework; | ||
using Environment = NHibernate.Cfg.Environment; | ||
|
||
namespace NHibernate.Test.CacheTest | ||
{ | ||
[TestFixture] | ||
public class SyncOnlyCacheFixture : CacheFixture | ||
{ | ||
protected override void Configure(Configuration cfg) | ||
{ | ||
base.Configure(cfg); | ||
cfg.SetProperty(Environment.CacheReadWriteLockFactory, "sync"); | ||
} | ||
|
||
[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))); | ||
} | ||
} | ||
} |
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,63 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Cache | ||
{ | ||
/// <summary> | ||
/// Implementors provide a locking mechanism for the cache. | ||
/// </summary> | ||
public interface ICacheLock : IDisposable | ||
{ | ||
/// <summary> | ||
/// Acquire synchronously a read lock. | ||
/// </summary> | ||
/// <returns>A read lock.</returns> | ||
IDisposable ReadLock(); | ||
|
||
/// <summary> | ||
/// Acquire synchronously a write lock. | ||
/// </summary> | ||
/// <returns>A write lock.</returns> | ||
IDisposable WriteLock(); | ||
|
||
/// <summary> | ||
/// Acquire asynchronously a read lock. | ||
/// </summary> | ||
/// <returns>A read lock.</returns> | ||
Task<IDisposable> ReadLockAsync(); | ||
|
||
/// <summary> | ||
/// Acquire asynchronously a write lock. | ||
/// </summary> | ||
/// <returns>A write lock.</returns> | ||
Task<IDisposable> WriteLockAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Define a factory for cache locks. | ||
/// </summary> | ||
public interface ICacheReadWriteLockFactory | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// Create a cache lock provider. | ||
/// </summary> | ||
ICacheLock Create(); | ||
bahusoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
internal class AsyncCacheReadWriteLockFactory : ICacheReadWriteLockFactory | ||
{ | ||
public ICacheLock Create() | ||
{ | ||
return new AsyncReaderWriterLock(); | ||
} | ||
} | ||
|
||
internal class SyncCacheReadWriteLockFactory : ICacheReadWriteLockFactory | ||
{ | ||
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,54 @@ | ||
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 AsyncNotSupporteException(); | ||
} | ||
|
||
public Task<IDisposable> WriteLockAsync() | ||
{ | ||
throw AsyncNotSupporteException(); | ||
} | ||
|
||
private static InvalidOperationException AsyncNotSupporteException() | ||
{ | ||
return new InvalidOperationException("This locker supports only sync operations. Change 'cache.read_write_lock_factory' setting to `async` to support async operations."); | ||
} | ||
} | ||
} |
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.