View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.fail;
25  
26  import java.io.IOException;
27  import java.util.Random;
28  import java.util.concurrent.CountDownLatch;
29  
30  import org.apache.directory.mavibot.btree.BTree;
31  import org.apache.directory.mavibot.btree.Tuple;
32  import org.apache.directory.mavibot.btree.TupleCursor;
33  import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
34  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
35  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
36  import org.junit.AfterClass;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  
40  
41  /**
42   * A class to test multi-threaded operations on the btree
43   *  
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class MultiThreadedInMemoryBtreeTest
47  {
48      /** The btree we use */
49      private static BTree<Long, String> btree;
50  
51  
52      /**
53       * Create the btree once
54       * @throws IOException If the creation failed
55       */
56      @BeforeClass
57      public static void setup() throws IOException
58      {
59          btree = BTreeFactory.createInMemoryBTree( "test", new LongSerializer(), new StringSerializer() );
60      }
61  
62  
63      /**
64       * Close the btree
65       */
66      @AfterClass
67      public static void shutdown() throws IOException
68      {
69          btree.close();
70      }
71  
72  
73      /**
74       * Create a btree with 50 000 elements in it
75       * @throws IOException If the creation failed
76       */
77      private void create50KBTree() throws IOException
78      {
79          Random random = new Random( System.nanoTime() );
80  
81          int nbElems = 50000;
82  
83          // Create a BTree with 50 000 entries
84          btree.setPageSize( 32 );
85  
86          for ( int i = 0; i < nbElems; i++ )
87          {
88              Long key = ( long ) random.nextLong();
89              String value = Long.toString( key );
90  
91              try
92              {
93                  btree.insert( key, value );
94  
95                  if ( i % 10000 == 0 )
96                  {
97                      System.out.println( "Written " + i + " elements" );
98                  }
99              }
100             catch ( Exception e )
101             {
102                 e.printStackTrace();
103                 System.out.println( btree );
104                 System.out.println( "Error while adding " + value );
105                 return;
106             }
107         }
108     }
109 
110 
111     /**
112      * Browse the btree in its current revision, reading all of its elements
113      * @return The number of read elements 
114      * @throws IOException If the browse failed
115      */
116     private int testBrowse() throws IOException
117     {
118         TupleCursor<Long, String> cursor = btree.browse();
119 
120         int nb = 0;
121         long elem = Long.MIN_VALUE;
122 
123         while ( cursor.hasNext() )
124         {
125             Tuple<Long, String> res = cursor.next();
126 
127             if ( res.getKey() > elem )
128             {
129                 elem = res.getKey();
130                 nb++;
131             }
132         }
133 
134         cursor.close();
135 
136         return nb;
137     }
138 
139 
140     /**
141      * Check that we can read the btree while it is being modified. We will start
142      * 100 readers for one writer.
143      * 
144      * @throws InterruptedException If the btree access failed.
145      */
146     @Test
147     public void testBrowseMultiThreads() throws InterruptedException
148     {
149         int nbThreads = 100;
150         final CountDownLatch latch = new CountDownLatch( nbThreads );
151 
152         Thread writer = new Thread()
153         {
154             public void run()
155             {
156                 try
157                 {
158                     create50KBTree();
159                 }
160                 catch ( Exception e )
161                 {
162                 }
163             }
164         };
165 
166         long t0 = System.currentTimeMillis();
167 
168         // Start the writer
169         writer.start();
170 
171         for ( int i = 0; i < nbThreads; i++ )
172         {
173             Thread test = new Thread()
174             {
175                 public void run()
176                 {
177                     try
178                     {
179                         int res = 0;
180                         int previous = -1;
181 
182                         while ( previous < res )
183                         {
184                             previous = res;
185                             res = testBrowse();
186                             Thread.sleep( 500 );
187                         }
188 
189                         latch.countDown();
190                     }
191                     catch ( Exception e )
192                     {
193                     }
194                 }
195             };
196 
197             // Start each reader
198             test.start();
199         }
200 
201         // Wait for all the readers to be done
202         latch.await();
203 
204         long t1 = System.currentTimeMillis();
205 
206         System.out.println( " Time to create 50K entries and to have " + nbThreads + " threads reading them : "
207             + ( ( t1 - t0 ) / 1000 ) + " seconds" );
208     }
209 
210 
211     /**
212      * Test that we can use many threads inserting data in a BTree
213      * @throws InterruptedException
214      */
215     @Test
216     public void testInsertMultiThreads() throws InterruptedException, IOException
217     {
218         int nbThreads = 100;
219         final CountDownLatch latch = new CountDownLatch( nbThreads );
220 
221         //Thread.sleep( 60000L );
222 
223         long t0 = System.currentTimeMillis();
224 
225         for ( int i = 0; i < nbThreads; i++ )
226         {
227             final long prefix = i;
228             Thread test = new Thread()
229             {
230                 public void run()
231                 {
232                     try
233                     {
234                         // Inject 1000 elements
235                         for ( int j = 0; j < 1000; j++ )
236                         {
237                             long value = prefix * 1000 + j;
238                             btree.insert( value, Long.toString( value ) );
239 
240                             /*
241                             if ( j % 10000 == 0 )
242                             {
243                                 System.out.println( "Thread " + Thread.currentThread().getName() + " flushed " + j
244                                     + " elements" );
245                             }
246                             */
247                         }
248 
249                         latch.countDown();
250                     }
251                     catch ( Exception e )
252                     {
253                     }
254                 }
255             };
256 
257             // Start each reader
258             test.start();
259         }
260 
261         // Wait for all the readers to be done
262         latch.await();
263 
264         long t1 = System.currentTimeMillis();
265 
266         // Check that the tree contains all the values
267         try
268         {
269             for ( long i = 0L; i < 10000L; i++ )
270             {
271                 assertEquals( Long.toString( i ), btree.get( i ) );
272             }
273         }
274         catch ( KeyNotFoundException knfe )
275         {
276             fail();
277         }
278 
279         System.out.println( " Time to create 1M entries : "
280             + ( ( t1 - t0 ) / 1000 ) + " seconds" );
281     }
282 }