KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > LogBufferPoolTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LogBufferPoolTest.java,v 1.59 2006/10/30 21:14:47 bostic Exp $
7  */

8
9 package com.sleepycat.je.log;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.RandomAccessFile JavaDoc;
14 import java.nio.ByteBuffer JavaDoc;
15 import java.nio.channels.FileChannel JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.List JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import com.sleepycat.je.Database;
23 import com.sleepycat.je.DatabaseConfig;
24 import com.sleepycat.je.DatabaseEntry;
25 import com.sleepycat.je.DatabaseException;
26 import com.sleepycat.je.DbInternal;
27 import com.sleepycat.je.Environment;
28 import com.sleepycat.je.EnvironmentConfig;
29 import com.sleepycat.je.config.EnvironmentParams;
30 import com.sleepycat.je.dbi.EnvironmentImpl;
31 import com.sleepycat.je.dbi.MemoryBudget;
32 import com.sleepycat.je.utilint.DbLsn;
33 import com.sleepycat.je.util.TestUtils;
34
35 public class LogBufferPoolTest extends TestCase {
36
37     Environment env;
38     Database db;
39     EnvironmentImpl environment;
40     FileManager fileManager;
41     File JavaDoc envHome;
42     LogBufferPool bufPool;
43
44     public LogBufferPoolTest() {
45         super();
46         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
47     }
48
49     protected void setUp()
50         throws Exception JavaDoc {
51
52         /* Remove files to start with a clean slate. */
53         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
54     }
55
56     protected void tearDown()
57         throws Exception JavaDoc {
58
59         bufPool = null;
60     if (fileManager != null) {
61         fileManager.clear();
62         fileManager.close();
63     }
64         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
65     }
66
67     /**
68      * Make sure that we'll add more buffers as needed.
69      */

70     public void testGrowBuffers()
71         throws Throwable JavaDoc {
72
73         try {
74
75             setupEnv(true);
76
77             /*
78              * Each buffer can only hold 2 items. Put enough test items in to
79              * get seven buffers.
80              */

81             List JavaDoc lsns = new ArrayList JavaDoc();
82             for (int i = 0; i < 14; i++) {
83                 long lsn = insertData(bufPool, (byte)(i + 1));
84                 lsns.add(new Long JavaDoc(lsn));
85             }
86
87             /*
88              * Check that the bufPool knows where each LSN lives and that the
89              * fetched buffer does hold this item.
90              */

91             LogBuffer logBuf;
92             ByteBuffer JavaDoc b;
93             for (int i = 0; i < 14; i++) {
94
95                 /*
96                  * For each test LSN, ask the bufpool for the logbuffer that
97                  * houses it.
98                  */

99                 long testLsn = DbLsn.longToLsn((Long JavaDoc) lsns.get(i));
100                 logBuf = bufPool.getReadBuffer(testLsn);
101                 assertNotNull(logBuf);
102
103                 /* Here's the expected data. */
104                 byte[] expected = new byte[10];
105                 Arrays.fill(expected, (byte)(i+1));
106
107                 /* Here's the data in the log buffer. */
108                 byte[] logData = new byte[10];
109                 b = logBuf.getDataBuffer();
110                 long firstLsnInBuf = logBuf.getFirstLsn();
111                 b.position((int) (DbLsn.getFileOffset(testLsn) -
112                   DbLsn.getFileOffset(firstLsnInBuf)));
113                 logBuf.getDataBuffer().get(logData);
114
115                 /* They'd better be equal. */
116                 assertTrue(Arrays.equals(logData, expected));
117                 logBuf.release();
118             }
119
120             /*
121              * This LSN shouldn't be in the buffers, it's less than any
122              * buffered item.
123              */

124             assertNull(bufPool.getReadBuffer(DbLsn.makeLsn(0,10)));
125
126             /*
127              * This LSN is illegal to ask for, it's greater than any registered
128              * LSN.
129              */

130             assertNull("LSN too big",
131                        bufPool.getReadBuffer(DbLsn.makeLsn(10, 141)));
132         } catch (Throwable JavaDoc t) {
133             t.printStackTrace();
134             throw t;
135         }
136     }
137
138     /**
139      * Helper to insert fake data.
140      * @return LSN registered for this fake data
141      */

142     private long insertData(LogBufferPool bufPool,
143                 byte value)
144     throws IOException JavaDoc, DatabaseException {
145
146         byte[] data = new byte[10];
147         Arrays.fill(data, value);
148         boolean flippedFile = fileManager.bumpLsn(data.length);
149         LogBuffer logBuf = bufPool.getWriteBuffer(data.length, flippedFile);
150         logBuf.getDataBuffer().put(data);
151         long lsn = fileManager.getLastUsedLsn();
152         bufPool.writeCompleted(fileManager.getLastUsedLsn(), false);
153         return lsn;
154     }
155
156     /**
157      * Test buffer flushes.
158      */

159     public void testBufferFlush()
160         throws Throwable JavaDoc {
161
162         try {
163             setupEnv(false);
164             assertFalse("There should be no files", fileManager.filesExist());
165
166             /*
167          * Each buffer can only hold 2 items. Put enough test items in to
168          * get five buffers.
169          */

170         /* CWL: What is lsnList used for? */
171             List JavaDoc lsnList = new ArrayList JavaDoc();
172             for (int i = 0; i < 9; i++) {
173                 long lsn = insertData(bufPool, (byte)(i+1));
174                 lsnList.add(new Long JavaDoc(lsn));
175             }
176             bufPool.writeBufferToFile(0);
177             fileManager.syncLogEnd();
178         
179             /* We should see two files exist. */
180             String JavaDoc[] fileNames =
181         fileManager.listFiles(FileManager.JE_SUFFIXES);
182             assertEquals("Should be 2 files", 2, fileNames.length);
183
184             /* Read the files. */
185             if (false) {
186             ByteBuffer JavaDoc dataBuffer = ByteBuffer.allocate(100);
187             FileHandle file0 = fileManager.getFileHandle(0L);
188             RandomAccessFile JavaDoc file = file0.getFile();
189             FileChannel JavaDoc channel = file.getChannel();
190             int bytesRead = channel.read(dataBuffer,
191                                          FileManager.firstLogEntryOffset());
192             dataBuffer.flip();
193             assertEquals("Check bytes read", 50, bytesRead);
194             assertEquals("Check size of file", 50, dataBuffer.limit());
195             file.close();
196             FileHandle file1 = fileManager.getFileHandle(1L);
197             file = file1.getFile();
198             channel = file.getChannel();
199             bytesRead = channel.read(dataBuffer,
200                                      FileManager.firstLogEntryOffset());
201             dataBuffer.flip();
202             assertEquals("Check bytes read", 40, bytesRead);
203             assertEquals("Check size of file", 40, dataBuffer.limit());
204             file0.release();
205             file1.release();
206             }
207         } catch (Throwable JavaDoc e) {
208             e.printStackTrace();
209             throw e;
210         }
211     }
212
213     public void testTemporaryBuffers()
214     throws Exception JavaDoc {
215
216     final int KEY_SIZE = 10;
217     final int DATA_SIZE = 1000000;
218
219     tempBufferInitEnvInternal
220         ("0", MemoryBudget.MIN_MAX_MEMORY_SIZE_STRING);
221     DatabaseEntry key = new DatabaseEntry(new byte[KEY_SIZE]);
222     DatabaseEntry data = new DatabaseEntry(new byte[DATA_SIZE]);
223     db.put(null, key, data);
224     db.close();
225     env.close();
226     }
227
228     private void tempBufferInitEnvInternal(String JavaDoc buffSize, String JavaDoc cacheSize)
229     throws DatabaseException {
230
231         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
232         envConfig.setTransactional(true);
233         envConfig.setAllowCreate(true);
234     if (!buffSize.equals("0")) {
235         envConfig.setConfigParam("je.log.totalBufferBytes", buffSize);
236     }
237
238     if (!cacheSize.equals("0")) {
239         envConfig.setConfigParam("je.maxMemory", cacheSize);
240     }
241         env = new Environment(envHome, envConfig);
242
243         DatabaseConfig dbConfig = new DatabaseConfig();
244         dbConfig.setAllowCreate(true);
245         dbConfig.setSortedDuplicates(true);
246     dbConfig.setTransactional(true);
247         db = env.openDatabase(null, "InsertAndDelete", dbConfig);
248     }
249
250     private void setupEnv(boolean inMemory)
251         throws Exception JavaDoc {
252
253         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
254    
255     DbInternal.disableParameterValidation(envConfig);
256         envConfig.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(),
257                                  EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
258     envConfig.setConfigParam
259         (EnvironmentParams.LOG_FILE_MAX.getName(), "90");
260     envConfig.setConfigParam
261         (EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
262     envConfig.setAllowCreate(true);
263         if (inMemory) {
264             /* Make the bufPool grow some buffers. Disable writing. */
265             envConfig.setConfigParam
266         (EnvironmentParams.LOG_MEMORY_ONLY.getName(), "true");
267         }
268         environment = new EnvironmentImpl(envHome, envConfig);
269
270         /* Make a standalone file manager for this test. */
271         environment.close();
272         environment.open(); /* Just sets state to OPEN. */
273         fileManager = new FileManager(environment, envHome, false);
274         bufPool = new LogBufferPool(fileManager, environment);
275
276         /*
277          * Remove any files after the environment is created again! We want to
278          * remove the files made by recovery, so we can test the file manager
279          * in controlled cases.
280          */

281         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
282     }
283 }
284
Popular Tags