@Test public void testMetaFileCreationMult() throws InvalidConfigException, IOException { int maxEventBufferSize = 1144; int maxIndividualBufferSize = 500; int bufNum = maxEventBufferSize / maxIndividualBufferSize; if (maxEventBufferSize % maxIndividualBufferSize > 0) bufNum++; DbusEventBuffer.StaticConfig config = getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true); // create buffer mult DbusEventBufferMult bufMult = createBufferMult(config); // go over all the buffers for (DbusEventBuffer dbusBuf : bufMult.bufIterable()) { File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); // check that we don't have the files Assert.assertFalse(metaFile.exists()); } bufMult.saveBufferMetaInfo(false); // now the files should be there for (DbusEventBuffer dbusBuf : bufMult.bufIterable()) { File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); // check that we don't have the files Assert.assertTrue(metaFile.exists()); validateFiles(metaFile, bufNum); } }
@Test public void testMetaFileCreation() throws InvalidConfigException, IOException { int maxEventBufferSize = 1144; int maxIndividualBufferSize = 500; DbusEventBuffer dbusBuf = new DbusEventBuffer( getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.HEAP_MEMORY, _mmapDirStr, true)); File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); dbusBuf.saveBufferMetaInfo(false); Assert.assertFalse(metaFile.exists()); // because of Allocation policy HEAP dbusBuf = new DbusEventBuffer( getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true)); // after buffer is created - meta file should be removed Assert.assertFalse(metaFile.exists()); dbusBuf.saveBufferMetaInfo(false); // new file should be created Assert.assertTrue(metaFile.exists()); int bufNum = maxEventBufferSize / maxIndividualBufferSize; if (maxEventBufferSize % maxIndividualBufferSize > 0) bufNum++; validateFiles(metaFile, bufNum); // now files are there - but if we create a buffer again - the meta file should be moved dbusBuf = new DbusEventBuffer( getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true)); Assert.assertFalse(metaFile.exists()); dbusBuf.saveBufferMetaInfo(true); // .info file should be create , but not a regular meta file Assert.assertFalse(metaFile.exists()); Assert.assertTrue(new File(metaFile.getAbsolutePath() + ".info").exists()); }
public void testMetaFileCleanup(boolean persistBuffer) throws InvalidConfigException, IOException { int maxEventBufferSize = 1144; int maxIndividualBufferSize = 500; DbusEventBuffer dbusBuf = new DbusEventBuffer( getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true)); File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); // after buffer is created - meta file should be removed Assert.assertFalse(metaFile.exists()); dbusBuf.saveBufferMetaInfo(false); // new file should be created Assert.assertTrue(metaFile.exists()); DbusEventBufferMetaInfo mi = new DbusEventBufferMetaInfo(metaFile); mi.loadMetaInfo(); Assert.assertTrue(mi.isValid()); dbusBuf.closeBuffer(persistBuffer); // this should close buffer and removed the files File sessionDir = new File(metaFile.getParent(), mi.getSessionId()); if (persistBuffer) { // dir and meta data should be still around Assert.assertTrue( sessionDir.exists() && sessionDir.isDirectory(), "mmap session dir still exists"); // now let's touch one of the mmap files File writeBufferFile = new File(sessionDir, "writeBuffer_0"); Assert.assertTrue( writeBufferFile.exists() && writeBufferFile.isFile(), "writeBufferFile still exist"); mi = new DbusEventBufferMetaInfo(metaFile); mi.loadMetaInfo(); Assert.assertTrue(mi.isValid(), "MetaInfo file is valid"); } else { // dir and meta files both should be gone Assert.assertFalse(sessionDir.exists(), "mmap session dir doesn't exist"); Assert.assertFalse(metaFile.exists(), "meta file session dir doesn't exist"); } }
private void pushEventsToBuffer(DbusEventBuffer dbusBuf, int numEvents) { dbusBuf.start(1); dbusBuf.startEvents(); DbusEventGenerator generator = new DbusEventGenerator(); Vector<DbusEvent> events = new Vector<DbusEvent>(); generator.generateEvents(numEvents, 1, 100, 10, events); // set end of windows for (int i = 0; i < numEvents - 1; ++i) { long scn = events.get(i).sequence(); ++i; DbusEventInternalWritable writableEvent; try { writableEvent = DbusEventCorrupter.makeWritable(events.get(i)); } catch (InvalidEventException ie) { LOG.error("Exception trace is " + ie); Assert.fail(); return; } writableEvent.setSrcId((short) -2); writableEvent.setSequence(scn); writableEvent.applyCrc(); assertTrue("invalid event #" + i, writableEvent.isValid(true)); } // set up the ReadChannel with 2 events ByteArrayOutputStream oStream = new ByteArrayOutputStream(); WritableByteChannel oChannel = Channels.newChannel(oStream); for (int i = 0; i < numEvents; ++i) { ((DbusEventInternalReadable) events.get(i)).writeTo(oChannel, Encoding.BINARY); } byte[] writeBytes = oStream.toByteArray(); ByteArrayInputStream iStream = new ByteArrayInputStream(writeBytes); ReadableByteChannel rChannel = Channels.newChannel(iStream); try { dbusBuf.readEvents(rChannel); } catch (InvalidEventException ie) { LOG.error("Exception trace is " + ie); Assert.fail(); return; } }
@Test public void testMetaFileTimeStamp() throws InvalidConfigException, IOException { int maxEventBufferSize = 1144; int maxIndividualBufferSize = 500; DbusEventBuffer dbusBuf = new DbusEventBuffer( getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true)); File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); // after buffer is created - meta file should be removed Assert.assertFalse(metaFile.exists()); dbusBuf.saveBufferMetaInfo(false); // new file should be created Assert.assertTrue(metaFile.exists()); DbusEventBufferMetaInfo mi = new DbusEventBufferMetaInfo(metaFile); mi.loadMetaInfo(); Assert.assertTrue(mi.isValid()); // now let's touch one of the mmap files File writeBufferFile = new File(new File(metaFile.getParent(), mi.getSessionId()), "writeBuffer_0"); long modTime = System.currentTimeMillis() + 1000; // Precision of the mod time is 1 sec writeBufferFile.setLastModified(modTime); LOG.debug( "setting mod time for " + writeBufferFile + " to " + modTime + " now val = " + writeBufferFile.lastModified()); mi = new DbusEventBufferMetaInfo(metaFile); mi.loadMetaInfo(); Assert.assertTrue(mi.isValid()); // we don't invalidate the meta file based on mod time }
@Test /** * test: 1. create buffer, push events , validate events are there 2. save meta info 3. create a * new buffer - it should pick up all the events.Validate it 4. create another buffer (without * calling save metainfo again) - this one should be empty. * * @throws InvalidConfigException * @throws IOException */ public void testRestoringBufferWithEvents() throws InvalidConfigException, IOException { int maxEventBufferSize = 1144; int maxIndividualBufferSize = 500; int numEvents = 10; // must be even DbusEventBuffer.StaticConfig conf = getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true); DbusEventBuffer dbusBuf = new DbusEventBuffer(conf); pushEventsToBuffer(dbusBuf, numEvents); // verify events are in the buffer DbusEventIterator it = dbusBuf.acquireIterator("allevents"); int count = -1; // first event is "prev" event while (it.hasNext()) { DbusEvent e = it.next(); Assert.assertTrue(e.isValid()); count++; } dbusBuf.releaseIterator(it); Assert.assertEquals(count, numEvents); // save meta data dbusBuf.saveBufferMetaInfo(false); // create another similar buffer and see if has the events dbusBuf = new DbusEventBuffer(conf); dbusBuf.validateEventsInBuffer(); it = dbusBuf.acquireIterator("alleventsNew"); count = -1; // first event is "prev" event while (it.hasNext()) { DbusEvent e = it.next(); Assert.assertTrue(e.isValid()); count++; } Assert.assertEquals(count, numEvents); dbusBuf.releaseIterator(it); // meta file should be gone by now File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); Assert.assertFalse(metaFile.exists()); // we can start a buffer and would be empty dbusBuf = new DbusEventBuffer(conf); it = dbusBuf.acquireIterator("alleventsNewEmpty"); Assert.assertFalse(it.hasNext()); dbusBuf.releaseIterator(it); }
@Test public void testMetaFileCloseMult() throws Exception { int maxEventBufferSize = 1144; int maxIndividualBufferSize = 500; int bufNum = maxEventBufferSize / maxIndividualBufferSize; if (maxEventBufferSize % maxIndividualBufferSize > 0) bufNum++; DbusEventBuffer.StaticConfig config = getConfig( maxEventBufferSize, maxIndividualBufferSize, 100, 500, AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true); // create buffer mult DbusEventBufferMult bufMult = createBufferMult(config); // Save all the files and validate the meta files. bufMult.close(); for (DbusEventBuffer dbusBuf : bufMult.bufIterable()) { File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); // check that we don't have the files Assert.assertTrue(metaFile.exists()); validateFiles(metaFile, bufNum); } File[] entries = _mmapDir.listFiles(); // When we create a new multi-buffer, we should get renamed files as well as new files. bufMult = createBufferMult(config); entries = _mmapDir.listFiles(); // Has session dirs and renamed meta files. // Create an info file for one buffer. DbusEventBuffer buf = bufMult.bufIterable().iterator().next(); buf.saveBufferMetaInfo(true); File infoFile = new File(_mmapDir, buf.metaFileName() + ".info"); Assert.assertTrue(infoFile.exists()); // Create a session directory that has one file in it. File badSes1 = new File(_mmapDir, DbusEventBuffer.getSessionPrefix() + "m"); badSes1.mkdir(); badSes1.deleteOnExit(); File junkFile = new File(badSes1.getAbsolutePath() + "/junkFile"); junkFile.createNewFile(); junkFile.deleteOnExit(); // Create a directory that is empty File badSes2 = new File(_mmapDir, DbusEventBuffer.getSessionPrefix() + "n"); badSes2.mkdir(); badSes2.deleteOnExit(); // Create a good file under mmap directory that we don't want to see removed. final String goodFile = "GoodFile"; File gf = new File(_mmapDir, goodFile); gf.createNewFile(); // Now close the multibuf, and see that the new files are still there. // We should have deleted the unused sessions and info files. bufMult.close(); HashSet<String> validEntries = new HashSet<String>(bufNum); for (DbusEventBuffer dbusBuf : bufMult.bufIterable()) { File metaFile = new File(_mmapDir, dbusBuf.metaFileName()); // check that we don't have the files Assert.assertTrue(metaFile.exists()); validateFiles(metaFile, bufNum); validEntries.add(metaFile.getName()); DbusEventBufferMetaInfo mi = new DbusEventBufferMetaInfo(metaFile); mi.loadMetaInfo(); validEntries.add(mi.getSessionId()); } validEntries.add(goodFile); // Now we should be left with meta files, and session dirs and nothing else. entries = _mmapDir.listFiles(); for (File f : entries) { Assert.assertTrue(validEntries.contains(f.getName())); validEntries.remove(f.getName()); } Assert.assertTrue(validEntries.isEmpty()); // And everything else should have moved to the .BAK directory entries = _mmapBakDir.listFiles(); HashMap<String, File> fileHashMap = new HashMap<String, File>(entries.length); for (File f : entries) { fileHashMap.put(f.getName(), f); } Assert.assertTrue(fileHashMap.containsKey(badSes1.getName())); Assert.assertTrue(fileHashMap.get(badSes1.getName()).isDirectory()); Assert.assertEquals(fileHashMap.get(badSes1.getName()).listFiles().length, 1); Assert.assertEquals( fileHashMap.get(badSes1.getName()).listFiles()[0].getName(), junkFile.getName()); fileHashMap.remove(badSes1.getName()); Assert.assertTrue(fileHashMap.containsKey(badSes2.getName())); Assert.assertTrue(fileHashMap.get(badSes2.getName()).isDirectory()); Assert.assertEquals(fileHashMap.get(badSes2.getName()).listFiles().length, 0); fileHashMap.remove(badSes2.getName()); // We should have the renamed meta files in the hash now. for (File f : entries) { if (f.getName().startsWith(DbusEventBuffer.getMmapMetaInfoFileNamePrefix())) { Assert.assertTrue(fileHashMap.containsKey(f.getName())); Assert.assertTrue(f.isFile()); fileHashMap.remove(f.getName()); } } Assert.assertTrue(fileHashMap.isEmpty()); // One more test to make sure we create the BAK directory dynamically if it does not exist. FileUtils.deleteDirectory(_mmapBakDir); bufMult = createBufferMult(config); entries = _mmapDir.listFiles(); // Create an info file for one buffer. buf = bufMult.bufIterable().iterator().next(); buf.saveBufferMetaInfo(true); infoFile = new File(_mmapDir, buf.metaFileName() + ".info"); Assert.assertTrue(infoFile.exists()); bufMult.close(); entries = _mmapBakDir.listFiles(); fileHashMap = new HashMap<String, File>(entries.length); for (File f : entries) { fileHashMap.put(f.getName(), f); } Assert.assertTrue(fileHashMap.containsKey(infoFile.getName())); Assert.assertTrue(fileHashMap.get(infoFile.getName()).isFile()); }