internal SimpleReplicaCreator(int blockId, ShortCircuitCache cache, TestShortCircuitCache.TestFileDescriptorPair pair) { this.blockId = blockId; this.cache = cache; this.pair = pair; }
/// <exception cref="System.Exception"/> public virtual void TestExpiry() { ShortCircuitCache cache = new ShortCircuitCache(2, 1, 1, 10000000, 1, 10000000, 0 ); TestShortCircuitCache.TestFileDescriptorPair pair = new TestShortCircuitCache.TestFileDescriptorPair (); ShortCircuitReplicaInfo replicaInfo1 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new TestShortCircuitCache.SimpleReplicaCreator(123, cache, pair)); Preconditions.CheckNotNull(replicaInfo1.GetReplica()); Preconditions.CheckState(replicaInfo1.GetInvalidTokenException() == null); pair.CompareWith(replicaInfo1.GetReplica().GetDataStream(), replicaInfo1.GetReplica ().GetMetaStream()); replicaInfo1.GetReplica().Unref(); MutableBoolean triedToCreate = new MutableBoolean(false); do { Sharpen.Thread.Sleep(10); ShortCircuitReplicaInfo replicaInfo2 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new _ShortCircuitReplicaCreator_229(triedToCreate)); if ((replicaInfo2 != null) && (replicaInfo2.GetReplica() != null)) { replicaInfo2.GetReplica().Unref(); } }while (triedToCreate.IsFalse()); cache.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestEviction() { ShortCircuitCache cache = new ShortCircuitCache(2, 10000000, 1, 10000000, 1, 10000 , 0); TestShortCircuitCache.TestFileDescriptorPair[] pairs = new TestShortCircuitCache.TestFileDescriptorPair [] { new TestShortCircuitCache.TestFileDescriptorPair(), new TestShortCircuitCache.TestFileDescriptorPair (), new TestShortCircuitCache.TestFileDescriptorPair() }; ShortCircuitReplicaInfo[] replicaInfos = new ShortCircuitReplicaInfo[] { null, null , null }; for (int i = 0; i < pairs.Length; i++) { replicaInfos[i] = cache.FetchOrCreate(new ExtendedBlockId(i, "test_bp1"), new TestShortCircuitCache.SimpleReplicaCreator (i, cache, pairs[i])); Preconditions.CheckNotNull(replicaInfos[i].GetReplica()); Preconditions.CheckState(replicaInfos[i].GetInvalidTokenException() == null); pairs[i].CompareWith(replicaInfos[i].GetReplica().GetDataStream(), replicaInfos[i ].GetReplica().GetMetaStream()); } // At this point, we have 3 replicas in use. // Let's close them all. for (int i_1 = 0; i_1 < pairs.Length; i_1++) { replicaInfos[i_1].GetReplica().Unref(); } // The last two replicas should still be cached. for (int i_2 = 1; i_2 < pairs.Length; i_2++) { int iVal = i_2; replicaInfos[i_2] = cache.FetchOrCreate(new ExtendedBlockId(i_2, "test_bp1"), new _ShortCircuitReplicaCreator_277(iVal)); Preconditions.CheckNotNull(replicaInfos[i_2].GetReplica()); Preconditions.CheckState(replicaInfos[i_2].GetInvalidTokenException() == null); pairs[i_2].CompareWith(replicaInfos[i_2].GetReplica().GetDataStream(), replicaInfos [i_2].GetReplica().GetMetaStream()); } // The first (oldest) replica should not be cached. MutableBoolean calledCreate = new MutableBoolean(false); replicaInfos[0] = cache.FetchOrCreate(new ExtendedBlockId(0, "test_bp1"), new _ShortCircuitReplicaCreator_293 (calledCreate)); Preconditions.CheckState(replicaInfos[0].GetReplica() == null); NUnit.Framework.Assert.IsTrue(calledCreate.IsTrue()); // Clean up for (int i_3 = 1; i_3 < pairs.Length; i_3++) { replicaInfos[i_3].GetReplica().Unref(); } for (int i_4 = 0; i_4 < pairs.Length; i_4++) { pairs[i_4].Close(); } cache.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestTimeBasedStaleness() { // Set up the cache with a short staleness time. ShortCircuitCache cache = new ShortCircuitCache(2, 10000000, 1, 10000000, 1, 10, 0); TestShortCircuitCache.TestFileDescriptorPair[] pairs = new TestShortCircuitCache.TestFileDescriptorPair [] { new TestShortCircuitCache.TestFileDescriptorPair(), new TestShortCircuitCache.TestFileDescriptorPair () }; ShortCircuitReplicaInfo[] replicaInfos = new ShortCircuitReplicaInfo[] { null, null }; long HourInMs = 60 * 60 * 1000; for (int i = 0; i < pairs.Length; i++) { int iVal = i; ExtendedBlockId key = new ExtendedBlockId(i, "test_bp1"); replicaInfos[i] = cache.FetchOrCreate(key, new _ShortCircuitReplicaCreator_330(key , pairs, iVal, cache, HourInMs)); Preconditions.CheckNotNull(replicaInfos[i].GetReplica()); Preconditions.CheckState(replicaInfos[i].GetInvalidTokenException() == null); pairs[i].CompareWith(replicaInfos[i].GetReplica().GetDataStream(), replicaInfos[i ].GetReplica().GetMetaStream()); } // Keep trying to getOrCreate block 0 until it goes stale (and we must re-create.) GenericTestUtils.WaitFor(new _Supplier_351(cache), 500, 60000); // Make sure that second replica did not go stale. ShortCircuitReplicaInfo info = cache.FetchOrCreate(new ExtendedBlockId(1, "test_bp1" ), new _ShortCircuitReplicaCreator_371()); info.GetReplica().Unref(); // Clean up for (int i_1 = 1; i_1 < pairs.Length; i_1++) { replicaInfos[i_1].GetReplica().Unref(); } cache.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestAddAndRetrieve() { ShortCircuitCache cache = new ShortCircuitCache(10, 10000000, 10, 10000000, 1, 10000 , 0); TestShortCircuitCache.TestFileDescriptorPair pair = new TestShortCircuitCache.TestFileDescriptorPair (); ShortCircuitReplicaInfo replicaInfo1 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new TestShortCircuitCache.SimpleReplicaCreator(123, cache, pair)); Preconditions.CheckNotNull(replicaInfo1.GetReplica()); Preconditions.CheckState(replicaInfo1.GetInvalidTokenException() == null); pair.CompareWith(replicaInfo1.GetReplica().GetDataStream(), replicaInfo1.GetReplica ().GetMetaStream()); ShortCircuitReplicaInfo replicaInfo2 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new _ShortCircuitReplicaCreator_175()); Preconditions.CheckNotNull(replicaInfo2.GetReplica()); Preconditions.CheckState(replicaInfo2.GetInvalidTokenException() == null); Preconditions.CheckState(replicaInfo1 == replicaInfo2); pair.CompareWith(replicaInfo2.GetReplica().GetDataStream(), replicaInfo2.GetReplica ().GetMetaStream()); replicaInfo1.GetReplica().Unref(); replicaInfo2.GetReplica().Unref(); // Even after the reference count falls to 0, we still keep the replica // around for a while (we have configured the expiry period to be really, // really long here) ShortCircuitReplicaInfo replicaInfo3 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new _ShortCircuitReplicaCreator_195()); Preconditions.CheckNotNull(replicaInfo3.GetReplica()); Preconditions.CheckState(replicaInfo3.GetInvalidTokenException() == null); replicaInfo3.GetReplica().Unref(); pair.Close(); cache.Close(); }