/
HStoreMessenger.java
821 lines (714 loc) · 37.1 KB
/
HStoreMessenger.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
package edu.mit.hstore;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.voltdb.ClientResponseImpl;
import org.voltdb.VoltTable;
import org.voltdb.catalog.Database;
import org.voltdb.catalog.Host;
import org.voltdb.catalog.Partition;
import org.voltdb.catalog.Site;
import org.voltdb.messaging.FastDeserializer;
import org.voltdb.messaging.FastSerializer;
import org.voltdb.messaging.FragmentTaskMessage;
import org.voltdb.messaging.TransactionInfoBaseMessage;
import org.voltdb.messaging.VoltMessage;
import org.voltdb.utils.DBBPool;
import org.voltdb.utils.Pair;
import org.voltdb.utils.DBBPool.BBContainer;
import ca.evanjones.protorpc.NIOEventLoop;
import ca.evanjones.protorpc.ProtoRpcChannel;
import ca.evanjones.protorpc.ProtoRpcController;
import ca.evanjones.protorpc.ProtoServer;
import com.google.protobuf.ByteString;
import com.google.protobuf.GeneratedMessage;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcController;
import edu.brown.catalog.CatalogUtil;
import edu.brown.hstore.Hstore;
import edu.brown.hstore.Hstore.*;
import edu.brown.hstore.Hstore.TransactionWorkRequest.PartitionFragment;
import edu.brown.utils.LoggerUtil;
import edu.brown.utils.ProfileMeasurement;
import edu.brown.utils.ThreadUtil;
import edu.brown.utils.LoggerUtil.LoggerBoolean;
import edu.mit.hstore.callbacks.TransactionPrepareCallback;
import edu.mit.hstore.callbacks.TransactionRedirectResponseCallback;
import edu.mit.hstore.callbacks.TransactionWorkCallback;
import edu.mit.hstore.dtxn.LocalTransaction;
import edu.mit.hstore.interfaces.Shutdownable;
/**
*
* @author pavlo
*/
public class HStoreMessenger implements Shutdownable {
public static final Logger LOG = Logger.getLogger(HStoreMessenger.class);
private final static LoggerBoolean debug = new LoggerBoolean(LOG.isDebugEnabled());
private final static LoggerBoolean trace = new LoggerBoolean(LOG.isTraceEnabled());
static {
LoggerUtil.attachObserver(LOG, debug, trace);
}
private static final DBBPool buffer_pool = new DBBPool(true, false);
private static final ByteString ok = ByteString.copyFrom("OK".getBytes());
private final HStoreSite hstore_site;
private final Site catalog_site;
private final int local_site_id;
private final Set<Integer> local_partitions;
private final NIOEventLoop eventLoop = new NIOEventLoop();
private final int num_sites;
private final LinkedBlockingDeque<Pair<byte[], TransactionRedirectResponseCallback>> forwardQueue = new LinkedBlockingDeque<Pair<byte[], TransactionRedirectResponseCallback>>();
/** SiteId -> HStoreServer */
private final Map<Integer, HStoreService> channels = new HashMap<Integer, HStoreService>();
private final Thread listener_thread;
private final ProtoServer listener;
private final Handler handler = new Handler();
private final ForwardTxnDispatcher forwardDispatcher = new ForwardTxnDispatcher();
private final Thread forward_thread;
private boolean shutting_down = false;
private Shutdownable.ShutdownState state = ShutdownState.INITIALIZED;
/**
*
*/
private class MessengerListener implements Runnable {
@Override
public void run() {
if (hstore_site.getHStoreConf().site.cpu_affinity)
hstore_site.getThreadManager().registerProcessingThread();
Throwable error = null;
try {
HStoreMessenger.this.eventLoop.run();
} catch (RuntimeException ex) {
error = ex;
} catch (AssertionError ex) {
error = ex;
} catch (Exception ex) {
error = ex;
}
if (error != null) {
if (hstore_site.isShuttingDown() == false) {
LOG.error("HStoreMessenger.Listener stopped!", error);
}
Throwable cause = null;
if (error instanceof RuntimeException && error.getCause() != null) {
if (error.getCause().getMessage() != null && error.getCause().getMessage().isEmpty() == false) {
cause = error.getCause();
}
}
if (cause == null) cause = error;
// These errors are ok if we're actually stopping...
if (HStoreMessenger.this.state == ShutdownState.SHUTDOWN ||
HStoreMessenger.this.state == ShutdownState.PREPARE_SHUTDOWN ||
HStoreMessenger.this.hstore_site.isShuttingDown()) {
// IGNORE
} else {
LOG.fatal("Unexpected error in messenger listener thread", cause);
HStoreMessenger.this.shutdownCluster(error);
}
}
if (trace.get()) LOG.trace("Messenger Thread for Site #" + catalog_site.getId() + " has stopped!");
}
}
/**
*
*/
private class ForwardTxnDispatcher implements Runnable {
final ProfileMeasurement idleTime = new ProfileMeasurement("IDLE");
@Override
public void run() {
if (hstore_site.getHStoreConf().site.cpu_affinity)
hstore_site.getThreadManager().registerProcessingThread();
Pair<byte[], TransactionRedirectResponseCallback> p = null;
while (HStoreMessenger.this.shutting_down == false) {
try {
idleTime.start();
p = HStoreMessenger.this.forwardQueue.take();
idleTime.stop();
} catch (InterruptedException ex) {
break;
}
try {
HStoreMessenger.this.hstore_site.procedureInvocation(p.getFirst(), p.getSecond());
} catch (Throwable ex) {
LOG.warn("Failed to invoke forwarded transaction", ex);
continue;
}
} // WHILE
}
}
/**
* Constructor
* @param site
*/
public HStoreMessenger(HStoreSite site) {
this.hstore_site = site;
this.catalog_site = site.getSite();
this.local_site_id = this.catalog_site.getId();
this.num_sites = CatalogUtil.getNumberOfSites(this.catalog_site);
Set<Integer> partitions = new HashSet<Integer>();
for (Partition catalog_part : this.catalog_site.getPartitions()) {
partitions.add(catalog_part.getId());
} // FOR
this.local_partitions = Collections.unmodifiableSet(partitions);
if (debug.get()) LOG.debug("Local Partitions for Site #" + site.getSiteId() + ": " + this.local_partitions);
// This listener thread will process incoming messages
this.listener = new ProtoServer(this.eventLoop);
// Special thread to handle forward requests
if (this.hstore_site.getHStoreConf().site.messenger_redirect_thread) {
this.forward_thread = new Thread(forwardDispatcher, HStoreSite.getThreadName(this.hstore_site, "frwd"));
this.forward_thread.setDaemon(true);
} else {
this.forward_thread = null;
}
// Wrap the listener in a daemon thread
this.listener_thread = new Thread(new MessengerListener(), HStoreSite.getThreadName(this.hstore_site, "msg"));
this.listener_thread.setDaemon(true);
this.eventLoop.setExitOnSigInt(true);
}
/**
* Start the messenger. This is a blocking call that will initialize the connections
* and start the listener thread!
*/
public synchronized void start() {
assert(this.state == ShutdownState.INITIALIZED) : "Invalid MessengerState " + this.state;
this.state = ShutdownState.STARTED;
if (debug.get()) LOG.debug("Initializing connections");
this.initConnections();
if (this.forward_thread != null) {
if (debug.get()) LOG.debug("Starting ForwardTxn dispatcher thread");
this.forward_thread.start();
}
if (debug.get()) LOG.debug("Starting listener thread");
this.listener_thread.start();
}
/**
* Returns true if the messenger has started
* @return
*/
public boolean isStarted() {
return (this.state == ShutdownState.STARTED ||
this.state == ShutdownState.PREPARE_SHUTDOWN);
}
/**
* Internal call for testing to hide errors
*/
@Override
public void prepareShutdown() {
if (this.state != ShutdownState.PREPARE_SHUTDOWN) {
assert(this.state == ShutdownState.STARTED) : "Invalid MessengerState " + this.state;
this.state = ShutdownState.PREPARE_SHUTDOWN;
}
}
/**
* Stop this messenger. This kills the ProtoRpc event loop
*/
@Override
public synchronized void shutdown() {
assert(this.state == ShutdownState.STARTED || this.state == ShutdownState.PREPARE_SHUTDOWN) : "Invalid MessengerState " + this.state;
this.state = ShutdownState.SHUTDOWN;
try {
if (trace.get()) LOG.trace("Stopping eventLoop for Site #" + this.getLocalSiteId());
this.eventLoop.exitLoop();
if (trace.get()) LOG.trace("Stopping listener thread for Site #" + this.getLocalSiteId());
this.listener_thread.interrupt();
if (trace.get()) LOG.trace("Joining on listener thread for Site #" + this.getLocalSiteId());
this.listener_thread.join();
} catch (InterruptedException ex) {
// IGNORE
} catch (Throwable ex) {
LOG.error("Unexpected error when trying to stop messenger for Site #" + this.getLocalSiteId(), ex);
} finally {
if (trace.get()) LOG.trace("Closing listener socket for Site #" + this.getLocalSiteId());
this.listener.close();
}
assert(this.isShuttingDown());
}
/**
* Returns true if the messenger has stopped
* @return
*/
@Override
public boolean isShuttingDown() {
return (this.state == ShutdownState.SHUTDOWN);
}
protected int getLocalSiteId() {
return (this.local_site_id);
}
protected int getLocalMessengerPort() {
return (this.hstore_site.getSite().getMessenger_port());
}
protected final Thread getListenerThread() {
return (this.listener_thread);
}
private VoltTable copyVoltTable(VoltTable vt) throws Exception {
FastSerializer fs = new FastSerializer(buffer_pool);
fs.writeObject(vt);
BBContainer bc = fs.getBBContainer();
assert(bc.b.hasArray());
ByteString bs = ByteString.copyFrom(bc.b);
FastDeserializer fds = new FastDeserializer(bs.asReadOnlyByteBuffer());
VoltTable copy = fds.readObject(VoltTable.class);
return (copy);
}
/**
* Initialize all the network connections to remote
*/
private void initConnections() {
Database catalog_db = CatalogUtil.getDatabase(this.catalog_site);
// Find all the destinations we need to connect to
if (debug.get()) LOG.debug("Configuring outbound network connections for Site #" + this.catalog_site.getId());
Map<Host, Set<Site>> host_partitions = CatalogUtil.getSitesPerHost(catalog_db);
Integer local_port = this.catalog_site.getMessenger_port();
ArrayList<Integer> site_ids = new ArrayList<Integer>();
ArrayList<InetSocketAddress> destinations = new ArrayList<InetSocketAddress>();
for (Entry<Host, Set<Site>> e : host_partitions.entrySet()) {
String host = e.getKey().getIpaddr();
for (Site catalog_site : e.getValue()) {
int site_id = catalog_site.getId();
int port = catalog_site.getMessenger_port();
if (site_id != this.catalog_site.getId()) {
if (debug.get()) LOG.debug("Creating RpcChannel to " + host + ":" + port + " for site #" + site_id);
destinations.add(new InetSocketAddress(host, port));
site_ids.add(site_id);
} // FOR
} // FOR
} // FOR
// Initialize inbound channel
assert(local_port != null);
if (debug.get()) LOG.debug("Binding listener to port " + local_port + " for Site #" + this.catalog_site.getId());
this.listener.register(this.handler);
this.listener.bind(local_port);
// Make the outbound connections
if (destinations.isEmpty()) {
if (debug.get()) LOG.debug("There are no remote sites so we are skipping creating connections");
} else {
if (debug.get()) LOG.debug("Connecting to " + destinations.size() + " remote site messengers");
ProtoRpcChannel[] channels = null;
try {
channels = ProtoRpcChannel.connectParallel(this.eventLoop, destinations.toArray(new InetSocketAddress[]{}), 15000);
} catch (RuntimeException ex) {
LOG.warn("Failed to connect to remote sites. Going to try again...");
// Try again???
try {
channels = ProtoRpcChannel.connectParallel(this.eventLoop, destinations.toArray(new InetSocketAddress[]{}));
} catch (Exception ex2) {
LOG.fatal("Site #" + this.getLocalSiteId() + " failed to connect to remote sites");
this.listener.close();
throw ex;
}
}
assert channels.length == site_ids.size();
for (int i = 0; i < channels.length; i++) {
this.channels.put(site_ids.get(i), HStoreService.newStub(channels[i]));
} // FOR
if (debug.get()) LOG.debug("Site #" + this.getLocalSiteId() + " is fully connected to all sites");
}
}
// ----------------------------------------------------------------------------
// MESSAGE ROUTERS
// ----------------------------------------------------------------------------
private abstract class MessageRouter<T extends GeneratedMessage, U extends GeneratedMessage> {
public void sendMessages(LocalTransaction ts, T msg, RpcCallback<U> callback, Collection<Integer> partitions) {
// If this flag is true, then we'll invoke the local method
// We want to do this *after* we send out all the messages to the remote sites
// so that we don't have to wait as long for the responses to come back over the network
Collection<Integer> local_partitions = null;
boolean site_sent[] = new boolean[HStoreMessenger.this.num_sites];
int ctr = 0;
for (Integer p : partitions) {
int dest_site_id = hstore_site.getSiteIdForPartitionId(p).intValue();
// Skip this HStoreSite if we're already sent it a message
if (site_sent[dest_site_id]) continue;
if (debug.get())
LOG.debug(String.format("Sending %s message to %s for %s",
msg.getClass().getSimpleName(), HStoreSite.formatSiteName(dest_site_id), ts));
// Local Partition
if (HStoreMessenger.this.local_site_id == dest_site_id) {
if (local_partitions == null) local_partitions = new HashSet<Integer>(); // XXX: ObjectPool?
local_partitions.add(p);
// Remote Partition
} else {
HStoreService channel = HStoreMessenger.this.channels.get(dest_site_id);
assert(channel != null) : "Invalid partition id '" + p + "' at " + HStoreMessenger.this.hstore_site.getSiteName();
ProtoRpcController controller = this.getProtoRpcController(ts, dest_site_id);
assert(controller != null) : "Invalid " + msg.getClass().getSimpleName() + " ProtoRpcController for site #" + dest_site_id;
this.sendRemote(channel, controller, msg, callback);
}
site_sent[dest_site_id] = true;
ctr++;
} // FOR
// Optimization: We'll invoke sendLocal() after we have sent out
// all of the mesages to remote sites
if (local_partitions.size() > 0) this.sendLocal(ts.getTransactionId(), msg, local_partitions);
if (debug.get())
LOG.debug(String.format("Sent %d %s to %d partitions for txn #%d ",
ts.getTransactionId(), msg.getClass().getSimpleName(), partitions.size()));
}
/**
*
* @param channel
* @param msg
* @param callback
*/
protected abstract void sendRemote(HStoreService channel, ProtoRpcController controller, T msg, RpcCallback<U> callback);
protected abstract void sendLocal(long txn_id, T msg, Collection<Integer> partitions);
protected abstract ProtoRpcController getProtoRpcController(LocalTransaction ts, int site_id);
}
// TransactionInit
private final MessageRouter<TransactionInitRequest, TransactionInitResponse> router_transactionInit = new MessageRouter<TransactionInitRequest, TransactionInitResponse>() {
protected void sendLocal(long txn_id, TransactionInitRequest msg, Collection<Integer> partitions) {
}
protected void sendRemote(HStoreService channel, ProtoRpcController controller, TransactionInitRequest msg, RpcCallback<TransactionInitResponse> callback) {
channel.transactionInit(controller, msg, callback);
}
protected ProtoRpcController getProtoRpcController(LocalTransaction ts, int site_id) {
return ts.getTransactionInitController(site_id);
}
};
// TransactionPrepare
private final MessageRouter<TransactionPrepareRequest, TransactionPrepareResponse> router_transactionPrepare = new MessageRouter<TransactionPrepareRequest, TransactionPrepareResponse>() {
protected void sendLocal(long txn_id, TransactionPrepareRequest msg, Collection<Integer> partitions) {
// We don't care whether we actually updated anybody locally, so we don't need to
// pass in a set to get the partitions that were updated here.
hstore_site.transactionPrepare(txn_id, partitions, null);
}
protected void sendRemote(HStoreService channel, ProtoRpcController controller, TransactionPrepareRequest msg, RpcCallback<TransactionPrepareResponse> callback) {
channel.transactionPrepare(controller, msg, callback);
}
protected ProtoRpcController getProtoRpcController(LocalTransaction ts, int site_id) {
return ts.getTransactionPrepareController(site_id);
}
};
// TransactionFinish
private final MessageRouter<TransactionFinishRequest, TransactionFinishResponse> router_transactionFinish = new MessageRouter<TransactionFinishRequest, TransactionFinishResponse>() {
protected void sendLocal(long txn_id, TransactionFinishRequest msg, Collection<Integer> partitions) {
hstore_site.transactionFinish(txn_id, msg.getStatus(), partitions);
}
protected void sendRemote(HStoreService channel, ProtoRpcController controller, TransactionFinishRequest msg, RpcCallback<TransactionFinishResponse> callback) {
channel.transactionFinish(controller, msg, callback);
}
protected ProtoRpcController getProtoRpcController(LocalTransaction ts, int site_id) {
return ts.getTransactionFinishController(site_id);
}
};
// Shutdown
private final MessageRouter<ShutdownRequest, ShutdownResponse> router_shutdown = new MessageRouter<ShutdownRequest, ShutdownResponse>() {
protected void sendLocal(long txn_id, ShutdownRequest msg, Collection<Integer> partitions) {
}
protected void sendRemote(HStoreService channel, ProtoRpcController controller, ShutdownRequest msg, RpcCallback<ShutdownResponse> callback) {
channel.shutdown(controller, msg, callback);
}
protected ProtoRpcController getProtoRpcController(LocalTransaction ts, int site_id) {
return new ProtoRpcController();
}
};
// ----------------------------------------------------------------------------
// HSTORE RPC SERVICE METHODS
// ----------------------------------------------------------------------------
/**
* We want to make this a private inner class so that we do not expose
* the RPC methods to other parts of the code.
*/
private class Handler extends HStoreService {
@Override
public void transactionInit(RpcController controller, TransactionInitRequest request,
RpcCallback<TransactionInitResponse> done) {
// TODO Auto-generated method stub
}
@Override
public void transactionWork(RpcController controller, TransactionWorkRequest request,
RpcCallback<TransactionWorkResponse> done) {
assert(request.hasTransactionId()) : "Got Hstore." + request.getClass().getSimpleName() + " without a txn id!";
long txn_id = request.getTransactionId();
// This is work from a transaction executing at another node
// Any other message can just be sent along to the ExecutionSite without sending
// back anything right away. The ExecutionSite will use our wrapped callback handle
// to send back whatever response it needs to, but we won't actually send it
// until we get back results from all of the partitions
TransactionWorkCallback callback = null;
try {
callback = (TransactionWorkCallback)HStoreObjectPools.POOL_TXNWORK.borrowObject();
callback.init(txn_id, request.getFragmentsCount(), done);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
FragmentTaskMessage ftask = null;
for (PartitionFragment partition_task : request.getFragmentsList()) {
// Decode the inner VoltMessage
try {
ftask = (FragmentTaskMessage)VoltMessage.createMessageFromBuffer(partition_task.getWork().asReadOnlyByteBuffer(), false);
} catch (Exception e) {
throw new RuntimeException(e);
}
assert(txn_id == ftask.getTxnId());
hstore_site.transactionWork(txn_id, ftask, callback);
} // FOR
// We don't need to send back a response right here.
// TransactionWorkCallback will wait until it has results from all of the partitions
// the tasks were sent to and then send back everything in a single response message
}
@Override
public void transactionPrepare(RpcController controller, TransactionPrepareRequest request,
RpcCallback<TransactionPrepareResponse> done) {
assert(request.hasTransactionId()) : "Got Hstore." + request.getClass().getSimpleName() + " without a txn id!";
long txn_id = request.getTransactionId();
Collection<Integer> updated = new HashSet<Integer>();
hstore_site.transactionPrepare(txn_id, request.getPartitionsList(), updated);
assert(updated.isEmpty() == false);
Hstore.TransactionPrepareResponse response = Hstore.TransactionPrepareResponse.newBuilder()
.setTransactionId(txn_id)
.addAllPartitions(updated)
.setStatus(Hstore.Status.OK)
.build();
done.run(response);
}
@Override
public void transactionFinish(RpcController controller, TransactionFinishRequest request,
RpcCallback<TransactionFinishResponse> done) {
assert(request.hasTransactionId()) : "Got Hstore." + request.getClass().getSimpleName() + " without a txn id!";
long txn_id = request.getTransactionId();
hstore_site.transactionFinish(txn_id, request.getStatus(), request.getPartitionsList());
// Send back a FinishResponse to let them know we're cool with everything...
Hstore.TransactionFinishResponse response = Hstore.TransactionFinishResponse.newBuilder()
.setTransactionId(txn_id)
.build();
done.run(response);
// Always tell the HStoreSite to clean-up any state for this txn
hstore_site.completeTransaction(txn_id, request.getStatus());
}
@Override
public void transactionRedirect(RpcController controller, TransactionRedirectRequest request,
RpcCallback<TransactionRedirectResponse> done) {
// We need to create a wrapper callback so that we can get the output that
// HStoreSite wants to send to the client and forward
// it back to whomever told us about this txn
if (trace.get())
LOG.trace(String.format("Recieved redirected transaction request from HStoreSite %s", HStoreSite.formatSiteName(request.getSenderId())));
byte serializedRequest[] = request.getWork().toByteArray(); // XXX Copy!
TransactionRedirectResponseCallback callback = null;
try {
callback = (TransactionRedirectResponseCallback)HStoreObjectPools.POOL_FORWARDTXN_RESPONSE.borrowObject();
callback.init(local_site_id, request.getSenderId(), done);
} catch (Exception ex) {
throw new RuntimeException("Failed to get ForwardTxnResponseCallback", ex);
}
if (HStoreMessenger.this.forward_thread != null) {
HStoreMessenger.this.forwardQueue.add(Pair.of(serializedRequest, callback));
} else {
hstore_site.procedureInvocation(serializedRequest, callback);
}
}
@Override
public void shutdown(RpcController controller, ShutdownRequest request,
RpcCallback<ShutdownResponse> done) {
LOG.info(String.format("Got shutdown request from HStoreSite %s", HStoreSite.formatSiteName(request.getSenderId())));
HStoreMessenger.this.shutting_down = true;
// Tell the HStoreSite to shutdown
hstore_site.shutdown();
// Then send back the acknowledgment
Hstore.ShutdownResponse response = Hstore.ShutdownResponse.newBuilder()
.setSenderId(local_site_id)
.build();
// Send this now!
done.run(response);
LOG.info(String.format("Shutting down %s [status=%d]", hstore_site.getSiteName(), request.getExitStatus()));
if (debug.get())
LOG.info(String.format("ForwardDispatcher Queue Idle Time: %.2fms",
forwardDispatcher.idleTime.getTotalThinkTimeMS()));
ThreadUtil.sleep(1000); // HACK
LogManager.shutdown();
System.exit(request.getExitStatus());
}
} // END CLASS
// ----------------------------------------------------------------------------
// TRANSACTION METHODS
// ----------------------------------------------------------------------------
/**
*
* @param ts
* @param callback
*/
public void transactionInit(LocalTransaction ts, RpcCallback<Hstore.TransactionInitResponse> callback) {
Hstore.TransactionInitRequest request = Hstore.TransactionInitRequest.newBuilder()
.setTransactionId(ts.getTransactionId())
.addAllPartitions(ts.getPredictTouchedPartitions())
.build();
this.router_transactionInit.sendMessages(ts, request, callback, request.getPartitionsList());
}
/**
*
* @param builders
* @param callback
*/
public void transactionWork(LocalTransaction ts, Map<Integer, Hstore.TransactionWorkRequest.Builder> builders, RpcCallback<Hstore.TransactionWorkResponse> callback) {
for (Entry<Integer, Hstore.TransactionWorkRequest.Builder> e : builders.entrySet()) {
int site_id = e.getKey().intValue();
assert(e.getValue().getFragmentsCount() > 0);
// We should never get work for our local partitions
assert(site_id != this.local_site_id);
this.channels.get(site_id).transactionWork(ts.getTransactionWorkController(site_id), e.getValue().build(), callback);
} // FOR
}
/**
* Notify the given partitions that this transaction is finished with them
* This can also be used for the "early prepare" optimization.
* @param ts
* @param callback
* @param partitions
*/
public void transactionPrepare(LocalTransaction ts, RpcCallback<Hstore.TransactionPrepareResponse> callback, Collection<Integer> partitions) {
Hstore.TransactionPrepareRequest request = Hstore.TransactionPrepareRequest.newBuilder()
.setTransactionId(ts.getTransactionId())
.addAllPartitions(ts.getDonePartitions())
.build();
this.router_transactionPrepare.sendMessages(ts, request, callback, partitions);
}
/**
* Notify all remote HStoreSites that the distributed transaction is done with data
* at the given partitions and that they need to commit/abort the results.
* @param ts
* @param status
* @param callback
*/
public void transactionFinish(LocalTransaction ts, Hstore.Status status, RpcCallback<Hstore.TransactionFinishResponse> callback) {
Hstore.TransactionFinishRequest request = Hstore.TransactionFinishRequest.newBuilder()
.setTransactionId(ts.getTransactionId())
.setStatus(status)
.addAllPartitions(ts.getDonePartitions())
.build();
this.router_transactionFinish.sendMessages(ts,
request,
callback,
ts.getTouchedPartitions().values());
}
/**
* Forward a StoredProcedureInvocation request to a remote site for execution
* @param serializedRequest
* @param callback
* @param partition
*/
public void transactionRedirect(byte[] serializedRequest, RpcCallback<Hstore.TransactionRedirectResponse> callback, int partition) {
int dest_site_id = hstore_site.getSiteIdForPartitionId(partition);
if (debug.get()) LOG.debug("Redirecting transaction request to partition #" + partition + " on " + HStoreSite.formatSiteName(dest_site_id));
ByteString bs = ByteString.copyFrom(serializedRequest);
Hstore.TransactionRedirectRequest mr = Hstore.TransactionRedirectRequest.newBuilder()
.setSenderId(this.local_site_id)
.setWork(bs)
.build();
this.channels.get(dest_site_id).transactionRedirect(new ProtoRpcController(), mr, callback);
}
// ----------------------------------------------------------------------------
// SHUTDOWN METHODS
// ----------------------------------------------------------------------------
/**
* Take down the cluster. If the blocking flag is true, then this call will never return
* @param ex
* @param blocking
*/
public void shutdownCluster(final Throwable ex, final boolean blocking) {
if (blocking) {
this.shutdownCluster(null);
} else {
// Make this a thread so that we don't block and can continue cleaning up other things
Thread shutdownThread = new Thread() {
@Override
public void run() {
HStoreMessenger.this.shutdownCluster(ex); // Never returns!
}
};
shutdownThread.setDaemon(true);
shutdownThread.start();
}
return;
}
/**
* Tell all of the other sites to shutdown and then knock ourselves out...
* This is will never return
*/
public void shutdownCluster() {
this.shutdownCluster(null);
}
/**
* Shutdown the cluster. If the given Exception is not null, then all the nodes will
* exit with a non-zero status. This is will never return
* Will not return.
* @param ex
*/
public synchronized void shutdownCluster(Throwable ex) {
final int num_sites = this.channels.size();
if (this.shutting_down) return;
this.shutting_down = true;
this.hstore_site.prepareShutdown();
LOG.info("Shutting down cluster" + (ex != null ? ": " + ex.getMessage() : ""));
final int exit_status = (ex == null ? 0 : 1);
final CountDownLatch latch = new CountDownLatch(num_sites);
if (num_sites > 0) {
RpcCallback<Hstore.ShutdownResponse> callback = new RpcCallback<Hstore.ShutdownResponse>() {
private final Set<Integer> siteids = new HashSet<Integer>();
@Override
public void run(Hstore.ShutdownResponse parameter) {
int siteid = parameter.getSenderId();
assert(this.siteids.contains(siteid) == false) : "Duplicate response from " + hstore_site.getSiteName();
this.siteids.add(siteid);
if (trace.get()) LOG.trace("Received " + this.siteids.size() + "/" + num_sites + " shutdown acknowledgements");
latch.countDown();
}
};
if (debug.get()) LOG.debug("Sending shutdown request to " + num_sites + " remote sites");
for (Entry<Integer, HStoreService> e: this.channels.entrySet()) {
Hstore.ShutdownRequest sm = Hstore.ShutdownRequest.newBuilder()
.setSenderId(catalog_site.getId())
.setExitStatus(exit_status)
.build();
e.getValue().shutdown(new ProtoRpcController(), sm, callback);
if (trace.get()) LOG.trace("Sent SHUTDOWN to " + HStoreSite.formatSiteName(e.getKey()));
} // FOR
}
// Tell ourselves to shutdown while we wait
if (debug.get()) LOG.debug("Telling local site to shutdown");
this.hstore_site.shutdown();
// Block until the latch releases us
if (num_sites > 0) {
LOG.info(String.format("Waiting for %d sites to finish shutting down", latch.getCount()));
try {
latch.await(5, TimeUnit.SECONDS);
} catch (Exception ex2) {
// IGNORE!
}
}
LOG.info(String.format("Shutting down [site=%d, status=%d]", catalog_site.getId(), exit_status));
LogManager.shutdown();
System.exit(exit_status);
}
// ----------------------------------------------------------------------------
// UTILITY METHODS
// ----------------------------------------------------------------------------
/**
* Returns an HStoreService handle that is connected to the given site
* @param catalog_site
* @return
*/
protected static HStoreService getHStoreService(Site catalog_site) {
NIOEventLoop eventLoop = new NIOEventLoop();
InetSocketAddress addresses[] = new InetSocketAddress[] {
new InetSocketAddress(catalog_site.getHost().getIpaddr(), catalog_site.getMessenger_port())
};
ProtoRpcChannel[] channels = null;
try {
channels = ProtoRpcChannel.connectParallel(eventLoop, addresses);
} catch (Exception ex) {
}
HStoreService channel = HStoreService.newStub(channels[0]);
return (channel);
}
}