/// <exception cref="System.Exception"/> public virtual void TestAllocateSlots() { FilePath path = new FilePath(TestBase, "testAllocateSlots"); path.Mkdirs(); SharedFileDescriptorFactory factory = SharedFileDescriptorFactory.Create("shm_", new string[] { path.GetAbsolutePath() }); FileInputStream stream = factory.CreateDescriptor("testAllocateSlots", 4096); ShortCircuitShm shm = new ShortCircuitShm(ShortCircuitShm.ShmId.CreateRandom(), stream ); int numSlots = 0; AList <ShortCircuitShm.Slot> slots = new AList <ShortCircuitShm.Slot>(); while (!shm.IsFull()) { ShortCircuitShm.Slot slot = shm.AllocAndRegisterSlot(new ExtendedBlockId(123L, "test_bp1" )); slots.AddItem(slot); numSlots++; } Log.Info("allocated " + numSlots + " slots before running out."); int slotIdx = 0; for (IEnumerator <ShortCircuitShm.Slot> iter = shm.SlotIterator(); iter.HasNext();) { NUnit.Framework.Assert.IsTrue(slots.Contains(iter.Next())); } foreach (ShortCircuitShm.Slot slot_1 in slots) { NUnit.Framework.Assert.IsFalse(slot_1.AddAnchor()); NUnit.Framework.Assert.AreEqual(slotIdx++, slot_1.GetSlotIdx()); } foreach (ShortCircuitShm.Slot slot_2 in slots) { slot_2.MakeAnchorable(); } foreach (ShortCircuitShm.Slot slot_3 in slots) { NUnit.Framework.Assert.IsTrue(slot_3.AddAnchor()); } foreach (ShortCircuitShm.Slot slot_4 in slots) { slot_4.RemoveAnchor(); } foreach (ShortCircuitShm.Slot slot_5 in slots) { shm.UnregisterSlot(slot_5.GetSlotIdx()); slot_5.MakeInvalid(); } shm.Free(); stream.Close(); FileUtil.FullyDelete(path); }
/// <exception cref="System.Exception"/> public virtual void TestStartupShutdown() { FilePath path = new FilePath(TestBase, "testStartupShutdown"); path.Mkdirs(); SharedFileDescriptorFactory factory = SharedFileDescriptorFactory.Create("shm_", new string[] { path.GetAbsolutePath() }); FileInputStream stream = factory.CreateDescriptor("testStartupShutdown", 4096); ShortCircuitShm shm = new ShortCircuitShm(ShortCircuitShm.ShmId.CreateRandom(), stream ); shm.Free(); stream.Close(); FileUtil.FullyDelete(path); }
/// <summary>Handle a DFSClient request to create a new memory segment.</summary> /// <param name="clientName">Client name as reported by the client.</param> /// <param name="sock"> /// The DomainSocket to associate with this memory /// segment. When this socket is closed, or the /// other side writes anything to the socket, the /// segment will be closed. This can happen at any /// time, including right after this function returns. /// </param> /// <returns> /// A NewShmInfo object. The caller must close the /// NewShmInfo object once they are done with it. /// </returns> /// <exception cref="System.IO.IOException">If the new memory segment could not be created. /// </exception> public virtual ShortCircuitRegistry.NewShmInfo CreateNewMemorySegment(string clientName , DomainSocket sock) { ShortCircuitRegistry.NewShmInfo info = null; ShortCircuitRegistry.RegisteredShm shm = null; ShortCircuitShm.ShmId shmId = null; lock (this) { if (!enabled) { if (Log.IsTraceEnabled()) { Log.Trace("createNewMemorySegment: ShortCircuitRegistry is " + "not enabled."); } throw new NotSupportedException(); } FileInputStream fis = null; try { do { shmId = ShortCircuitShm.ShmId.CreateRandom(); }while (segments.Contains(shmId)); fis = shmFactory.CreateDescriptor(clientName, ShmLength); shm = new ShortCircuitRegistry.RegisteredShm(clientName, shmId, fis, this); } finally { if (shm == null) { IOUtils.CloseQuietly(fis); } } info = new ShortCircuitRegistry.NewShmInfo(shmId, fis); segments[shmId] = shm; } // Drop the registry lock to prevent deadlock. // After this point, RegisteredShm#handle may be called at any time. watcher.Add(sock, shm); if (Log.IsTraceEnabled()) { Log.Trace("createNewMemorySegment: created " + info.shmId); } return(info); }