Skip to content
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

WIP Multi-Tenancy: Separate second-level cache per tenant (prototype for discussion) #2133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/NHibernate.DomainModel/CustomPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ public partial class CustomPersister : IEntityPersister
private static readonly bool[] Nullability = new bool[] { true };
private readonly ISessionFactoryImplementor factory;

[Obsolete]
public CustomPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory,
IMapping mapping)
{
this.factory = factory;
}

//TODO 6.0: Uncomment
//public CustomPersister(PersistentClass model, Func<string, ICacheConcurrencyStrategy> cache, ISessionFactoryImplementor factory,
// IMapping mapping)
//{
// this.factory = factory;
//}

#region IEntityPersister Members

public ISessionFactoryImplementor Factory
Expand Down
42 changes: 22 additions & 20 deletions src/NHibernate.Test/Async/CacheTest/BatchableCacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Multi;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
Expand Down Expand Up @@ -108,8 +110,8 @@ protected override void OnTearDown()
public async Task MultipleGetReadOnlyCollectionTestAsync()
{
var persister = Sfi.GetCollectionPersister($"{typeof(ReadOnly).FullName}.Items");
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var ids = new List<int>();

using (var s = Sfi.OpenSession())
Expand Down Expand Up @@ -218,8 +220,8 @@ public async Task MultipleGetReadOnlyCollectionTestAsync()
public async Task MultipleGetReadOnlyTestAsync()
{
var persister = Sfi.GetEntityPersister(typeof(ReadOnly).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
int[] getIds;
int[] loadIds;

Expand Down Expand Up @@ -380,8 +382,8 @@ public async Task MultipleGetReadOnlyTestAsync()
public async Task MultipleGetReadOnlyItemTestAsync()
{
var persister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
int[] getIds;
int[] loadIds;

Expand Down Expand Up @@ -542,9 +544,9 @@ public async Task MultipleGetReadOnlyItemTestAsync()
public async Task MultiplePutReadWriteTestAsync()
{
var persister = Sfi.GetEntityPersister(typeof(ReadWrite).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.Cache.Cache;
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.GetCache(null).Cache;
var ids = new List<int>();

await (cache.ClearAsync(CancellationToken.None));
Expand Down Expand Up @@ -592,9 +594,9 @@ public async Task MultiplePutReadWriteTestAsync()
public async Task MultiplePutReadWriteItemTestAsync()
{
var persister = Sfi.GetCollectionPersister($"{typeof(ReadWrite).FullName}.Items");
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.Cache.Cache;
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.GetCache(null).Cache;
var ids = new List<int>();

await (cache.ClearAsync(CancellationToken.None));
Expand Down Expand Up @@ -869,7 +871,7 @@ public async Task QueryCacheTestAsync()
public async Task QueryEntityBatchCacheTestAsync(bool clearEntityCacheAfterQuery, CancellationToken cancellationToken = default(CancellationToken))
{
var persister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
var cache = (BatchableCache) persister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
var queryCache = GetDefaultQueryCache();

Sfi.Statistics.Clear();
Expand Down Expand Up @@ -945,9 +947,9 @@ public async Task QueryCacheTestAsync()
var persister = Sfi.GetEntityPersister(typeof(ReadOnly).FullName);
var itemPersister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
var collectionPersister = Sfi.GetCollectionPersister($"{typeof(ReadOnly).FullName}.Items");
var cache = (BatchableCache) persister.Cache.Cache;
var itemCache = (BatchableCache) itemPersister.Cache.Cache;
var collectionCache = (BatchableCache) collectionPersister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
var itemCache = (BatchableCache) itemPersister.GetCache(null).Cache;
var collectionCache = (BatchableCache) collectionPersister.GetCache(null).Cache;
var queryCache = GetDefaultQueryCache();

int middleId;
Expand Down Expand Up @@ -1089,8 +1091,8 @@ public async Task QueryCacheTestAsync()

var persister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
var parentPersister = Sfi.GetEntityPersister(typeof(ReadOnly).FullName);
var cache = (BatchableCache) persister.Cache.Cache;
var parentCache = (BatchableCache) parentPersister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
var parentCache = (BatchableCache) parentPersister.GetCache(null).Cache;
var queryCache = GetDefaultQueryCache();

int middleId;
Expand Down Expand Up @@ -1217,7 +1219,7 @@ private async Task AssertMultipleCacheCallsAsync<TEntity>(IEnumerable<int> loadI
where TEntity : CacheEntity
{
var persister = Sfi.GetEntityPersister(typeof(TEntity).FullName);
var cache = (BatchableCache) persister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
await (cache.ClearAsync(cancellationToken));

if (cacheBeforeLoadFn != null)
Expand Down Expand Up @@ -1283,7 +1285,7 @@ private void AssertEquivalent(List<int> ids, int[][] expectedIdIndexes, List<obj
private async Task AssertMultipleCacheCollectionCallsAsync(List<int> ids, int idIndex, int[][] fetchedIdIndexes, int[] putIdIndexes, Func<int, bool> cacheBeforeLoadFn = null, CancellationToken cancellationToken = default(CancellationToken))
{
var persister = Sfi.GetCollectionPersister($"{typeof(ReadOnly).FullName}.Items");
var cache = (BatchableCache) persister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
await (cache.ClearAsync(cancellationToken));

if (cacheBeforeLoadFn != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
//------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.DomainModel;
using NHibernate.Persister.Entity;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;

Expand Down Expand Up @@ -91,14 +90,14 @@ protected override void OnTearDown()
public async Task BatchableRootEntityTestAsync()
{
var persister = Sfi.GetEntityPersister(typeof(Foo).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var fooCache = (BatchableCache) persister.Cache.Cache;
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var fooCache = (BatchableCache) persister.GetCache(null).Cache;

persister = Sfi.GetEntityPersister(typeof(Bar).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var barCache = (BatchableCache) persister.Cache.Cache;
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var barCache = (BatchableCache) persister.GetCache(null).Cache;

Assert.That(barCache, Is.EqualTo(fooCache));

Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate.Test/Async/CacheTest/BuildCacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Util;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate.Test/Async/FilterTest/DynamicFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using NHibernate.Cache;
using NHibernate.Cache.Entry;
using NHibernate.Criterion;
using NHibernate.Persister.Collection;
using NHibernate.Transform;
using NUnit.Framework;

Expand Down Expand Up @@ -54,7 +55,7 @@ public async Task SecondLevelCachedCollectionsFilteringAsync()
var sp = (Salesperson) await (session.LoadAsync(typeof(Salesperson), testData.steveId));
await (NHibernateUtil.InitializeAsync(sp.Orders));
Assert.IsTrue(persister.HasCache, "No cache for collection");
cachedData = (CollectionCacheEntry) await (persister.Cache.Cache.GetAsync(cacheKey, CancellationToken.None));
cachedData = (CollectionCacheEntry) await (persister.GetCache(null).Cache.GetAsync(cacheKey, CancellationToken.None));
Assert.IsNotNull(cachedData, "collection was not in cache");
}

Expand All @@ -66,7 +67,7 @@ public async Task SecondLevelCachedCollectionsFilteringAsync()
.UniqueResultAsync());
Assert.AreEqual(1, sp.Orders.Count, "Filtered-collection not bypassing 2L-cache");

CollectionCacheEntry cachedData2 = (CollectionCacheEntry) await (persister.Cache.Cache.GetAsync(cacheKey, CancellationToken.None));
CollectionCacheEntry cachedData2 = (CollectionCacheEntry) await (persister.GetCache(null).Cache.GetAsync(cacheKey, CancellationToken.None));
Assert.IsNotNull(cachedData2, "collection no longer in cache!");
Assert.AreSame(cachedData, cachedData2, "Different cache values!");
}
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate.Test/Async/LazyGroup/LazyGroupFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading.Tasks;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Persister.Entity;
using NUnit.Framework;

namespace NHibernate.Test.LazyGroup
Expand Down Expand Up @@ -135,7 +136,7 @@ public async Task TestGroupsAsync()
public async Task TestCacheAsync()
{
var persister = Sfi.GetEntityPersister(typeof(Person).FullName);
var cache = (HashtableCache) persister.Cache.Cache;
var cache = (HashtableCache) persister.GetCache(null).Cache;
await (cache.ClearAsync(CancellationToken.None));

using (var s = OpenSession())
Expand Down Expand Up @@ -171,7 +172,7 @@ public async Task TestCacheAsync()
public async Task TestInitializeFromCacheAsync()
{
var persister = Sfi.GetEntityPersister(typeof(Person).FullName);
var cache = (HashtableCache) persister.Cache.Cache;
var cache = (HashtableCache) persister.GetCache(null).Cache;
await (cache.ClearAsync(CancellationToken.None));
Sfi.Statistics.Clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ public void AfterTransactionCompletionProcess_EvictsFromCache(string querySpaces

if (expectedEntityEvictionCount > 0)
{
_factory.Received(1).EvictEntity(Arg.Is<IEnumerable<string>>(x => x.Count() == expectedEntityEvictionCount));
_factory.Received(1).EvictEntity(Arg.Is<IEnumerable<string>>(x => x.Count() == expectedEntityEvictionCount), null);
}
else
{
_factory.DidNotReceive().EvictEntity(Arg.Any<IEnumerable<string>>());
_factory.DidNotReceive().EvictEntity(Arg.Any<IEnumerable<string>>(), null);
}

if (expectedCollectionEvictionCount > 0)
{
_factory.Received(1).EvictCollection(Arg.Is<IEnumerable<string>>(x => x.Count() == expectedCollectionEvictionCount));
_factory.Received(1).EvictCollection(Arg.Is<IEnumerable<string>>(x => x.Count() == expectedCollectionEvictionCount), null);
}
else
{
_factory.DidNotReceive().EvictCollection(Arg.Any<IEnumerable<string>>());
_factory.DidNotReceive().EvictCollection(Arg.Any<IEnumerable<string>>(), null);
}
}
}
Expand Down
42 changes: 22 additions & 20 deletions src/NHibernate.Test/CacheTest/BatchableCacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Multi;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
Expand Down Expand Up @@ -96,8 +98,8 @@ protected override void OnTearDown()
public void MultipleGetReadOnlyCollectionTest()
{
var persister = Sfi.GetCollectionPersister($"{typeof(ReadOnly).FullName}.Items");
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var ids = new List<int>();

using (var s = Sfi.OpenSession())
Expand Down Expand Up @@ -206,8 +208,8 @@ public void MultipleGetReadOnlyCollectionTest()
public void MultipleGetReadOnlyTest()
{
var persister = Sfi.GetEntityPersister(typeof(ReadOnly).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
int[] getIds;
int[] loadIds;

Expand Down Expand Up @@ -368,8 +370,8 @@ public void MultipleGetReadOnlyTest()
public void MultipleGetReadOnlyItemTest()
{
var persister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
int[] getIds;
int[] loadIds;

Expand Down Expand Up @@ -530,9 +532,9 @@ public void MultipleGetReadOnlyItemTest()
public void MultiplePutReadWriteTest()
{
var persister = Sfi.GetEntityPersister(typeof(ReadWrite).FullName);
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.Cache.Cache;
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.GetCache(null).Cache;
var ids = new List<int>();

cache.Clear();
Expand Down Expand Up @@ -580,9 +582,9 @@ public void MultiplePutReadWriteTest()
public void MultiplePutReadWriteItemTest()
{
var persister = Sfi.GetCollectionPersister($"{typeof(ReadWrite).FullName}.Items");
Assert.That(persister.Cache.Cache, Is.Not.Null);
Assert.That(persister.Cache.Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.Cache.Cache;
Assert.That(persister.GetCache(null).Cache, Is.Not.Null);
Assert.That(persister.GetCache(null).Cache, Is.TypeOf<BatchableCache>());
var cache = (BatchableCache) persister.GetCache(null).Cache;
var ids = new List<int>();

cache.Clear();
Expand Down Expand Up @@ -857,7 +859,7 @@ public void QueryCacheTest()
public void QueryEntityBatchCacheTest(bool clearEntityCacheAfterQuery)
{
var persister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
var cache = (BatchableCache) persister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
var queryCache = GetDefaultQueryCache();

Sfi.Statistics.Clear();
Expand Down Expand Up @@ -933,9 +935,9 @@ public void QueryFetchCollectionBatchCacheTest(bool clearEntityCacheAfterQuery,
var persister = Sfi.GetEntityPersister(typeof(ReadOnly).FullName);
var itemPersister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
var collectionPersister = Sfi.GetCollectionPersister($"{typeof(ReadOnly).FullName}.Items");
var cache = (BatchableCache) persister.Cache.Cache;
var itemCache = (BatchableCache) itemPersister.Cache.Cache;
var collectionCache = (BatchableCache) collectionPersister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
var itemCache = (BatchableCache) itemPersister.GetCache(null).Cache;
var collectionCache = (BatchableCache) collectionPersister.GetCache(null).Cache;
var queryCache = GetDefaultQueryCache();

int middleId;
Expand Down Expand Up @@ -1077,8 +1079,8 @@ public void QueryFetchEntityBatchCacheTest(bool clearEntityCacheAfterQuery, bool

var persister = Sfi.GetEntityPersister(typeof(ReadOnlyItem).FullName);
var parentPersister = Sfi.GetEntityPersister(typeof(ReadOnly).FullName);
var cache = (BatchableCache) persister.Cache.Cache;
var parentCache = (BatchableCache) parentPersister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
var parentCache = (BatchableCache) parentPersister.GetCache(null).Cache;
var queryCache = GetDefaultQueryCache();

int middleId;
Expand Down Expand Up @@ -1205,7 +1207,7 @@ private void AssertMultipleCacheCalls<TEntity>(IEnumerable<int> loadIds, IReadO
where TEntity : CacheEntity
{
var persister = Sfi.GetEntityPersister(typeof(TEntity).FullName);
var cache = (BatchableCache) persister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
cache.Clear();

if (cacheBeforeLoadFn != null)
Expand Down Expand Up @@ -1271,7 +1273,7 @@ private void AssertEquivalent(List<int> ids, int[][] expectedIdIndexes, List<obj
private void AssertMultipleCacheCollectionCalls(List<int> ids, int idIndex, int[][] fetchedIdIndexes, int[] putIdIndexes, Func<int, bool> cacheBeforeLoadFn = null)
{
var persister = Sfi.GetCollectionPersister($"{typeof(ReadOnly).FullName}.Items");
var cache = (BatchableCache) persister.Cache.Cache;
var cache = (BatchableCache) persister.GetCache(null).Cache;
cache.Clear();

if (cacheBeforeLoadFn != null)
Expand Down
Loading