@Override public synchronized PagingStore newStore(final SimpleString address, final AddressSettings settings) { return new PagingStoreImpl(address, scheduledExecutor, syncTimeout, pagingManager, storageManager, null, this, address, settings, executorFactory.getExecutor(), syncNonTransactional); }
private void checkFileFactory() throws Exception { if (fileFactory == null) { fileFactory = storeFactory.newFileFactory(getStoreName()); } }
@Override public boolean checkPageFileExists(final int pageNumber) { String fileName = createFileName(pageNumber); try { checkFileFactory(); } catch (Exception ignored) { } SequentialFile file = fileFactory.createSequentialFile(fileName); return file.exists(); }
PagingStoreImpl storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true); storeImpl.start(); Assert.assertEquals(0, storeImpl.getNumberOfPages()); storeImpl.startPaging(); Assert.assertTrue(storeImpl.page(msg, ctx.getTransaction(), ctx.getContextListing(storeImpl.getStoreName()), lock)); Assert.assertEquals(1, storeImpl.getNumberOfPages()); storeImpl.sync(); Page page = storeImpl.depage(); Assert.assertEquals(1, storeImpl.getNumberOfPages()); page = storeImpl.depage(); Assert.assertEquals(0, storeImpl.getNumberOfPages());
throw new IllegalStateException("PagingStore(" + getStoreName() + ") not initialized"); boolean full = isFull(); PagedMessage pagedMessage = new PagedMessageImpl(message, routeQueues(tx, listCtx), transactionID); openNewPage(); currentPageSize.addAndGet(bytesToWrite); installPageTransaction(tx, listCtx); applyPageCounters(tx, getCurrentPage(), listCtx, persistentSize); sync(); logger.trace("Paging message " + pagedMessage + " on pageStore " + this.getStoreName() + " pageNr=" + currentPage.getPageId());
final int fileId = PagingStoreImpl.getPageIdFromFileName(fileName); currentPage = createPage(currentPageId); currentPage.open(); startPaging();
@Override public void addSize(final int size) { boolean globalFull = pagingManager.addSize(size).isGlobalFull(); long newSize = sizeInBytes.addAndGet(size); if (newSize < 0) { ActiveMQServerLogger.LOGGER.negativeAddressSize(newSize, address.toString()); } if (addressFullMessagePolicy == AddressFullMessagePolicy.BLOCK || addressFullMessagePolicy == AddressFullMessagePolicy.FAIL) { if (usingGlobalMaxSize && !globalFull || maxSize != -1) { checkReleaseMemory(globalFull, newSize); } return; } else if (addressFullMessagePolicy == AddressFullMessagePolicy.PAGE) { if (size > 0) { if (maxSize != -1 && newSize > maxSize || globalFull) { if (startPaging()) { ActiveMQServerLogger.LOGGER.pageStoreStart(storeName, newSize, maxSize, pagingManager.getGlobalSize()); } } } return; } }
@Override public boolean checkMemory(final Runnable runWhenAvailable) { return checkMemory(true, runWhenAvailable); }
@Override public boolean checkReleasedMemory() { return checkReleaseMemory(pagingManager.isGlobalFull(), sizeInBytes.get()); }
applySetting(addressSettings);
PagingStoreImpl store = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true); store.start(); Assert.assertEquals(0, store.getNumberOfPages()); store.startPaging(); Assert.assertEquals(1, store.getNumberOfPages()); store.forceAnotherPage(); Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock)); Assert.assertEquals(2, store.getNumberOfPages()); store.sync(); Page page = store.depage(); System.out.println("numberOfPages = " + store.getNumberOfPages()); Assert.assertEquals(1, store.getNumberOfPages()); Assert.assertTrue(store.isPaging()); Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock)); Page newPage = store.depage();
@Override public Page createPage(final int pageNumber) throws Exception { String fileName = createFileName(pageNumber); checkFileFactory(); SequentialFile file = fileFactory.createSequentialFile(fileName); Page page = new Page(storeName, storageManager, fileFactory, file, pageNumber); // To create the file file.open(); file.position(0); file.close(); return page; }
@Override public synchronized PagingStore newStore(final SimpleString address, final AddressSettings settings) { return new PagingStoreImpl(address, scheduledExecutor, syncTimeout, pagingManager, storageManager, null, this, address, settings, executorFactory.getExecutor(), executorFactory.getExecutor(), syncNonTransactional); }
@Override public void nonDurableDown(Message message, int count) { if (count < 0) { // this could happen on paged messages since they are not routed and incrementRefCount is never called return; } if (count == 0) { this.addSize(-message.getMemoryEstimate() - MessageReferenceImpl.getMemoryEstimate()); } else { this.addSize(-MessageReferenceImpl.getMemoryEstimate()); } }
@Override public List<PagingStore> reloadStores(final HierarchicalRepository<AddressSettings> addressSettingsRepository) throws Exception { File[] files = directory.listFiles(); if (files == null) { return Collections.<PagingStore>emptyList(); } else { ArrayList<PagingStore> storesReturn = new ArrayList<>(files.length); for (File file : files) { final String guid = file.getName(); final File addressFile = new File(file, PagingStoreFactoryNIO.ADDRESS_FILE); if (!addressFile.exists()) { // This means this folder is from a replication copy, nothing to worry about it, we just skip it if (!file.getName().contains(FileMoveManager.PREFIX)) { ActiveMQServerLogger.LOGGER.pageStoreFactoryNoIdFile(file.toString(), PagingStoreFactoryNIO.ADDRESS_FILE); } continue; } String addressString; try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(addressFile)))) { addressString = reader.readLine(); } SimpleString address = new SimpleString(addressString); SequentialFileFactory factory = newFileFactory(guid); AddressSettings settings = addressSettingsRepository.getMatch(address.toString()); PagingStore store = new PagingStoreImpl(address, scheduledExecutor, syncTimeout, pagingManager, storageManager, factory, this, address, settings, executorFactory.getExecutor(), executorFactory.getExecutor(), syncNonTransactional); storesReturn.add(store); } return storesReturn; } }
@Override public synchronized List<PagingStore> reloadStores(final HierarchicalRepository<AddressSettings> addressSettingsRepository) throws Exception { // We assume the directory list < Integer.MAX_VALUE (this is only a list of addresses). JDBCSequentialFile directoryList = (JDBCSequentialFile) pagingFactoryFileFactory.createSequentialFile(DIRECTORY_NAME); directoryList.open(); int size = ((Long) directoryList.size()).intValue(); ActiveMQBuffer buffer = readActiveMQBuffer(directoryList, size); ArrayList<PagingStore> storesReturn = new ArrayList<>(); while (buffer.readableBytes() > 0) { SimpleString table = buffer.readSimpleString(); JDBCSequentialFileFactory factory = (JDBCSequentialFileFactory) newFileFactory(table.toString(), false); factory.start(); JDBCSequentialFile addressFile = (JDBCSequentialFile) factory.createSequentialFile(ADDRESS_FILE); addressFile.open(); size = ((Long) addressFile.size()).intValue(); if (size == 0) { continue; } ActiveMQBuffer addrBuffer = readActiveMQBuffer(addressFile, size); SimpleString address = addrBuffer.readSimpleString(); AddressSettings settings = addressSettingsRepository.getMatch(address.toString()); PagingStore store = new PagingStoreImpl(address, scheduledExecutor, syncTimeout, pagingManager, storageManager, factory, this, address, settings, executorFactory.getExecutor(), syncNonTransactional); storesReturn.add(store); } directoryList.close(); return storesReturn; }
@Test public void testDoubleStart() throws Exception { SequentialFileFactory factory = new FakeSequentialFileFactory(); PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, new FakeStoreFactory(factory), PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true); storeImpl.start(); // this is not supposed to throw an exception. // As you could have start being called twice as Stores are dynamically // created, on a multi-thread environment storeImpl.start(); storeImpl.stop(); }
@Test public void testRestartPage() throws Throwable { clearDataRecreateServerDirs(); SequentialFileFactory factory = new NIOSequentialFileFactory(new File(getPageDir()), 1); PagingStoreFactory storeFactory = new FakeStoreFactory(factory); final int MAX_SIZE = 1024 * 10; AddressSettings settings = new AddressSettings().setPageSizeBytes(MAX_SIZE).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE); final PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, new SimpleString("test"), settings, getExecutorFactory().getExecutor(), true); storeImpl.start(); Assert.assertEquals(0, storeImpl.getNumberOfPages()); // Marked the store to be paged storeImpl.startPaging(); storeImpl.depage(); Assert.assertNull(storeImpl.getCurrentPage()); storeImpl.startPaging(); Assert.assertNotNull(storeImpl.getCurrentPage()); storeImpl.stop(); }
@Test public void testStore() throws Exception { SequentialFileFactory factory = new FakeSequentialFileFactory(); PagingStoreFactory storeFactory = new FakeStoreFactory(factory); AddressSettings addressSettings = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE); PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, addressSettings, getExecutorFactory().getExecutor(), true); storeImpl.start(); Assert.assertEquals(0, storeImpl.getNumberOfPages()); storeImpl.startPaging(); Assert.assertEquals(1, storeImpl.getNumberOfPages()); List<ActiveMQBuffer> buffers = new ArrayList<>(); ActiveMQBuffer buffer = createRandomBuffer(0, 10); buffers.add(buffer); SimpleString destination = new SimpleString("test"); Message msg = createMessage(1, storeImpl, destination, buffer); Assert.assertTrue(storeImpl.isPaging()); final RoutingContextImpl ctx = new RoutingContextImpl(null); Assert.assertTrue(storeImpl.page(msg, ctx.getTransaction(), ctx.getContextListing(storeImpl.getStoreName()), lock)); Assert.assertEquals(1, storeImpl.getNumberOfPages()); storeImpl.sync(); storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, addressSettings, getExecutorFactory().getExecutor(), true); storeImpl.start(); Assert.assertEquals(1, storeImpl.getNumberOfPages()); }