-
Notifications
You must be signed in to change notification settings - Fork 3
/
BTreeClu.java
278 lines (243 loc) · 8.47 KB
/
BTreeClu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Sequential B*-Tree implementation for the
* Concurrent Search Tree Project for
* Parallel Computing I
*
* Author: David C. Larsen <[email protected]>
* Date: April. 12, 2011
*/
import edu.rit.mp.*;
import edu.rit.mp.buf.*;
import edu.rit.pj.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.concurrent.LinkedBlockingQueue;
/**
* A cluster implementation of a B+-Tree.
*/
public class BTreeClu
{
/** The underlying BTree that all of the nodes will have. */
private static BTree<Integer, Integer> bTree = new BTreeSeq<Integer, Integer>();
/* Environmental data */
private static Comm world;
private static int rank;
private static int size;
/** Used for sending commands to the worker nodes. */
private static CharacterBuf command = new CharacterItemBuf();
/** Used for sending keys to worker nodes. */
private static IntegerBuf key = new IntegerItemBuf();
/** Used for sending values to worker nodes. */
private static IntegerBuf value = new IntegerItemBuf();
/** Used to keep track of which node was last assigned work. */
private static volatile int lastNodeUsed = 0;
/** Threads responsible for making sure that messages get transmitted
* properly to the backend node. */
private static BTreeCluWorkerThread[] slaves;
/** Communication status indicators for each of the backend workers. */
private static CommRequest[] workers;
/** A queue of the operations that need to be performed on the tree. */
private static LinkedBlockingQueue<BTreeOperation<Integer,Integer>> opQ = null;
/** Used to determine if the load generator should keep working. */
private static boolean running = true;
/** Command-line arguments. */
private static String[] arguments;
/**
* Set the operation queue that will be used to issue commands with.
*/
public static void setOperationQueue( LinkedBlockingQueue<BTreeOperation<Integer, Integer>> Q )
{
if( rank == 0 )
{
opQ = Q;
}
}
/**
* Starts running the BTree cluster.
*/
public static void run(String[] args) throws Exception
{
long startTime = System.currentTimeMillis();
Comm.init(args);
world = Comm.world();
rank = world.rank();
size = world.size();
arguments = args;
if( rank == 0 )
{
slaves = new BTreeCluWorkerThread[size];
workers = new CommRequest[size];
if( opQ == null )
{
opQ = new LinkedBlockingQueue<BTreeOperation<Integer,Integer>>();
}
new Thread("Load generator"){
public void run()
{
int maxIter = 1000;
if( arguments.length > 0 )
{
try{
maxIter = Integer.parseInt( arguments[0] );
} catch( NumberFormatException nfe )
{
System.err.println( nfe );
}
}
for( int i=0; i < maxIter; i++ )
{
put( i, i*10 );
}
try{
opQ.put( new BTreeOperation<Integer,Integer>( 'q' ) );
} catch( InterruptedException ie )
{
terminate();
}
while( running || !opQ.isEmpty() )
{
try{
BTreeOperation<Integer, Integer> nextOp = opQ.take();
if( running )
{
if( nextOp.operation == 'q' )
{
terminate();
}
else
{
dispatch( world, lastNodeUsed, nextOp );
lastNodeUsed = (lastNodeUsed + 1) % size;
// System.out.println( nextOp.key );
}
}
} catch( InterruptedException ie )
{
// Quit
System.err.println("exiting");
}
}
// Tell all of the workers to shut down -- there's no more work.
for( int i = size-1; i >= 0; i-- )
{
try{
world.send( i, CharacterBuf.buffer('q') );
} catch( IOException ioe )
{
System.err.println(ioe);
}
}
}
private BTreeCluWorkerThread
dispatch( Comm world, int workerNode, BTreeOperation op )
{
BTreeCluWorkerThread thread =
new BTreeCluWorkerThread( world,
workerNode,
op );
thread.start();
return thread;
}
private void put( int key, int value )
{
try{
opQ.put( new BTreeOperation<Integer,Integer>( 'p',
key,
value ));
} catch( InterruptedException ie )
{
System.out.println(ie);
}
}
private void get( int key )
{
try{
opQ.put( new BTreeOperation<Integer, Integer>( 'g',
key ));
} catch( InterruptedException ie )
{
System.out.println(ie);
}
}
}.start();
}
// Listen for commands, execute them.
while( true )
{
Integer got = null;
world.receive(0, command);
switch( command.get(0) )
{
case 'g':
world.receive( 0, key );
got = get( new Integer(key.get( 0 )) );
break;
case 'p':
world.receive( 0, key );
world.receive( 0, value );
got = put( new Integer(key.get( 0 )),
new Integer(value.get( 0 )) );
break;
case 'q':
System.out.println( rank + " says goodbye" );
return;
default:
System.err.print( rank + " got an un-recognized command: ");
System.err.println( command.get( 0 ) );
return;
}
}
}
/** {@inheritDoc} */
public static void clear()
{
bTree.clear();
}
/** {@inheritDoc} */
public static boolean containsKey( Integer key )
{
return bTree.containsKey( key );
}
/** {@inheritDoc} */
public static boolean containsValue( Integer value )
{
assert(false);
return false;
}
/** {@inheritDoc} */
public static Integer get( Integer key )
{
return bTree.get( key );
}
/** {@inheritDoc} */
public static boolean isEmpty()
{
return bTree.isEmpty();
}
/** {@inheritDoc} */
public static Integer put( Integer key, Integer value )
{
return bTree.put( key, value );
}
/** {@inheritDoc} */
public static Integer remove( Integer key )
{
return bTree.remove( key );
}
/** {@inheritDoc} */
public static int size()
{
return bTree.size();
}
/** {@inheritDoc} */
public static Node<Integer,Integer> getRoot() {
return bTree.getRoot();
}
/**
* Terminate the main thread of execution.
*/
public static void terminate()
{
running = false;
}
}