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.Cursor;
32  import org.apache.directory.mavibot.btree.Tuple;
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:labs@labs.apache.org">Mavibot labs Project</a>
45   */
46  public class MultiThreadedBtreeTest
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 = new BTree<Long, String>( "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 500 000 elements in it
75       * @throws IOException If the creation failed
76       */
77      private void create500KBTree() throws IOException
78      {
79          Random random = new Random( System.nanoTime() );
80  
81          int nbElems = 500000;
82  
83          // Create a BTree with 500 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 % 100000 == 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         Cursor<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         long revision = cursor.getRevision();
135 
136         cursor.close();
137 
138         //System.out.println( Thread.currentThread().getName() + " Nb elements read : " + nb + " on revision : "
139         //    + revision );
140 
141         return nb;
142     }
143 
144 
145     /**
146      * Chack that we can read the btree while it is being modified. We will start
147      * 100 readers for one writer.
148      * 
149      * @throws InterruptedException If the btree access failed.
150      */
151     @Test
152     public void testBrowseMultiThreads() throws InterruptedException
153     {
154         int nbThreads = 100;
155         final CountDownLatch latch = new CountDownLatch( nbThreads );
156 
157         Thread writer = new Thread()
158         {
159             public void run()
160             {
161                 try
162                 {
163                     create500KBTree();
164                 }
165                 catch ( Exception e )
166                 {
167                 }
168             }
169         };
170 
171         long t0 = System.currentTimeMillis();
172 
173         // Start the writer
174         writer.start();
175 
176         for ( int i = 0; i < nbThreads; i++ )
177         {
178             Thread test = new Thread()
179             {
180                 public void run()
181                 {
182                     try
183                     {
184                         int res = 0;
185                         int previous = -1;
186 
187                         while ( previous < res )
188                         {
189                             previous = res;
190                             res = testBrowse();
191                             Thread.sleep( 500 );
192                         }
193 
194                         latch.countDown();
195                     }
196                     catch ( Exception e )
197                     {
198                     }
199                 }
200             };
201 
202             // Start each reader
203             test.start();
204         }
205 
206         // Wait for all the readers to be done
207         latch.await();
208 
209         long t1 = System.currentTimeMillis();
210 
211         System.out.println( " Time to create 500K entries and to have " + nbThreads + " threads reading them : "
212             + ( ( t1 - t0 ) / 1000 ) + " seconds" );
213     }
214 
215 
216     /**
217      * Test that we can use many threads inserting data in a BTree
218      * @throws InterruptedException
219      */
220     @Test
221     public void testInsertMultiThreads() throws InterruptedException, IOException
222     {
223         int nbThreads = 100;
224         final CountDownLatch latch = new CountDownLatch( nbThreads );
225 
226         //Thread.sleep( 60000L );
227 
228         long t0 = System.currentTimeMillis();
229 
230         for ( int i = 0; i < nbThreads; i++ )
231         {
232             final long prefix = i;
233             Thread test = new Thread()
234             {
235                 public void run()
236                 {
237                     try
238                     {
239                         // Inject 10000 elements
240                         for ( int j = 0; j < 10000; j++ )
241                         {
242                             long value = prefix * 10000 + j;
243                             btree.insert( value, Long.toString( value ) );
244 
245                             /*
246                             if ( j % 10000 == 0 )
247                             {
248                                 System.out.println( "Thread " + Thread.currentThread().getName() + " flushed " + j
249                                     + " elements" );
250                             }
251                             */
252                         }
253 
254                         latch.countDown();
255                     }
256                     catch ( Exception e )
257                     {
258                     }
259                 }
260             };
261 
262             // Start each reader
263             test.start();
264         }
265 
266         // Wait for all the readers to be done
267         latch.await();
268 
269         long t1 = System.currentTimeMillis();
270 
271         // Check that the tree contains all the values
272         try
273         {
274             for ( long i = 0L; i < 100000L; i++ )
275             {
276                 assertEquals( Long.toString( i ), btree.get( i ) );
277             }
278         }
279         catch ( KeyNotFoundException knfe )
280         {
281             fail();
282         }
283 
284         System.out.println( " Time to create 1M entries : "
285             + ( ( t1 - t0 ) / 1000 ) + " seconds" );
286     }
287 }