private void readPage(int pageId, PageCache cache) throws Exception { Page page = null; try { page = pagingStore.createPage(pageId); storageManager.beforePageRead(); page.open(); List<PagedMessage> pgdMessages = page.read(storageManager); cache.setMessages(pgdMessages.toArray(new PagedMessage[pgdMessages.size()])); } finally { try { if (page != null) { page.close(false); } } catch (Throwable ignored) { } storageManager.afterPageRead(); } }
PagedMessage[] pgdMessages; synchronized (softCache) { cache = softCache.get((long) depagedPage.getPageId()); logger.trace("Removing pageNr=" + depagedPage.getPageId() + " from page-cache"); depagedPage.open(); pgdMessagesList = depagedPage.read(storageManager); } finally { try { depagedPage.close(false); } catch (Exception e) { depagedPage.close(false); pgdMessages = pgdMessagesList.toArray(new PagedMessage[pgdMessagesList.size()]); } else { depagedPage.delete(pgdMessages); synchronized (softCache) { softCache.remove((long) depagedPage.getPageId());
returnPage.close(false); currentPage = null; if (returnPage.getNumberOfMessages() == 0) { stopPaging(); returnPage.open(); returnPage.delete(null);
private int readFromMapped(StorageManager storage, List<PagedMessage> messages) throws IOException { file.position(0); //use a readonly mapped view of the file final int mappedSize = size.get(); final MappedByteBuffer mappedByteBuffer = mapFileForRead(this.file.getJavaFile(), mappedSize); ChannelBufferWrapper activeMQBuffer = wrapBuffer(mappedSize, mappedByteBuffer); try { return read(storage, activeMQBuffer, messages); } finally { //unmap the file after read it to avoid GC to take care of it PlatformDependent.freeDirectBuffer(mappedByteBuffer); } }
/** * This is used only on non transactional paging * * @param page * @param increment * @throws Exception */ @Override public synchronized void pendingCounter(Page page, int increment, long size) throws Exception { if (!persistent) { return; // nothing to be done } PendingCounter pendingInfo = pendingCounters.get((long) page.getPageId()); if (pendingInfo == null) { // We have to make sure this is sync here // not syncing this to disk may cause the page files to be out of sync on pages. // we can't afford the case where a page file is written without a record here long id = storage.storePendingCounter(this.subscriptionID, page.getPageId()); pendingInfo = new PendingCounter(id, increment, size); pendingCounters.put((long) page.getPageId(), pendingInfo); } else { pendingInfo.addAndGet(increment, size); } pendingValue.addAndGet(increment); pendingPersistentSize.addAndGet(size); page.addPendingCounter(this); }
/** * Validate if everything we add is recovered */ protected void testAdd(final SequentialFileFactory factory, final int numberOfElements) throws Exception { SequentialFile file = factory.createSequentialFile("00010.page"); Page impl = new Page(new SimpleString("something"), new NullStorageManager(), factory, file, 10); Assert.assertEquals(10, impl.getPageId()); impl.open(); Assert.assertEquals(1, factory.listFiles("page").size()); SimpleString simpleDestination = new SimpleString("Test"); addPageElements(simpleDestination, impl, numberOfElements); impl.sync(); impl.close(); file = factory.createSequentialFile("00010.page"); file.open(); impl = new Page(new SimpleString("something"), new NullStorageManager(), factory, file, 10); List<PagedMessage> msgs = impl.read(new NullStorageManager()); Assert.assertEquals(numberOfElements, msgs.size()); Assert.assertEquals(numberOfElements, impl.getNumberOfMessages()); for (int i = 0; i < msgs.size(); i++) { Assert.assertEquals(simpleDestination, msgs.get(i).getMessage().getAddressSimpleString()); } impl.delete(null); Assert.assertEquals(0, factory.listFiles(".page").size()); }
private void openNewPage() throws Exception { lock.writeLock().lock(); try { numberOfPages++; int tmpCurrentPageId = currentPageId + 1; if (logger.isTraceEnabled()) { logger.trace("new pageNr=" + tmpCurrentPageId, new Exception("trace")); } if (currentPage != null) { currentPage.close(true); } currentPage = createPage(tmpCurrentPageId); LivePageCache pageCache = new LivePageCacheImpl(currentPage); currentPage.setLiveCache(pageCache); cursorProvider.addPageCache(pageCache); currentPageSize.set(0); currentPage.open(); currentPageId = tmpCurrentPageId; if (currentPageId < firstPageId) { firstPageId = currentPageId; } } finally { lock.writeLock().unlock(); } }
if (currentPageSize.addAndGet(bytesToWrite) > pageSize && currentPage.getNumberOfMessages() > 0) { applyPageCounters(tx, getCurrentPage(), listCtx, persistentSize); currentPage.write(pagedMessage); " pageNr=" + currentPage.getPageId());
/** * @param packet */ private void handlePageEvent(final ReplicationPageEventMessage packet) throws Exception { ConcurrentMap<Integer, Page> pages = getPageMap(packet.getStoreName()); Page page = pages.remove(packet.getPageNumber()); if (page == null) { page = getPage(packet.getStoreName(), packet.getPageNumber()); } if (page != null) { if (packet.isDelete()) { if (deletePages) { page.delete(null); } } else { page.close(false); } } }
@Override public int compareTo(Page otherPage) { return otherPage.getPageId() - this.pageId; }
private void readFromSequentialFile(StorageManager storage, List<PagedMessage> messages) throws Exception { final int fileSize = (int) file.size(); //doesn't need to be a direct buffer: that case is covered using the MMAP read final ByteBuffer buffer = this.fileFactory.newBuffer(fileSize); try { file.position(0); file.read(buffer); buffer.rewind(); assert (buffer.limit() == fileSize) : "buffer doesn't contains the whole file"; ChannelBufferWrapper activeMQBuffer = wrapBuffer(fileSize, buffer); read(storage, activeMQBuffer, messages); } finally { this.fileFactory.releaseBuffer(buffer); } }
/** * @param simpleDestination * @param page * @param numberOfElements * @return * @throws Exception */ protected void addPageElements(final SimpleString simpleDestination, final Page page, final int numberOfElements) throws Exception { int initialNumberOfMessages = page.getNumberOfMessages(); for (int i = 0; i < numberOfElements; i++) { ICoreMessage msg = new CoreMessage().initBuffer(100); for (int j = 0; j < 10; j++) { msg.getBodyBuffer().writeByte((byte) 'b'); } msg.setAddress(simpleDestination); page.write(new PagedMessageImpl(msg, new long[0])); Assert.assertEquals(initialNumberOfMessages + i + 1, page.getNumberOfMessages()); } }
public void close() throws Exception { close(false); }
/** * @param pageId * @param map * @return */ private synchronized Page newPage(final int pageId, final SimpleString storeName, final ConcurrentMap<Integer, Page> map) throws Exception { Page page = map.get(pageId); if (page == null) { page = pageManager.getPageStore(storeName).createPage(pageId); page.open(); map.put(pageId, page); } return page; }
/** * sendEvent means it's a close happening from a major event such moveNext. * While reading the cache we don't need (and shouldn't inform the backup */ public synchronized void close(boolean sendEvent) throws Exception { if (sendEvent && storageManager != null) { storageManager.pageClosed(storeName, pageId); } if (pageCache != null) { pageCache.close(); // leave it to the soft cache to decide when to release it now pageCache = null; } file.close(); Set<PageSubscriptionCounter> counters = getPendingCounters(); if (counters != null) { for (PageSubscriptionCounter counter : counters) { counter.cleanupNonTXCounters(this.getPageId()); } } }
@Override public void ioSync() throws Exception { lock.readLock().lock(); try { if (currentPage != null) { currentPage.sync(); } } finally { lock.readLock().unlock(); } }