summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/ha/PrimaryTxObserver.cpp
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2013-08-01 20:27:26 +0000
committerAlan Conway <aconway@apache.org>2013-08-01 20:27:26 +0000
commite6598e9f95d55b80f96dbcb1e12bc1fc38c66af1 (patch)
tree7179cb6fa40a59d1390f295a613de64cc242814a /cpp/src/qpid/ha/PrimaryTxObserver.cpp
parent0ffcd71ac9c9f3742aae6e251eafe031068bda31 (diff)
downloadqpid-python-e6598e9f95d55b80f96dbcb1e12bc1fc38c66af1.tar.gz
QPID-4327: HA TX transactions: basic replication.
On primary a PrimaryTxObserver observes a transaction's TxBuffer and generates transaction events on a tx-replication-queue. On the backup a TxReplicator receives the events and constructs a TxBuffer equivalent to the one in the primary. Unfinished: - Primary does not wait for backups to prepare() before committing. - All connected backups are assumed to be in the transaction, there are race conditions around brokers joining/leavinv where this assumption is invalid. - Need more tests. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1509423 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/ha/PrimaryTxObserver.cpp')
-rw-r--r--cpp/src/qpid/ha/PrimaryTxObserver.cpp104
1 files changed, 104 insertions, 0 deletions
diff --git a/cpp/src/qpid/ha/PrimaryTxObserver.cpp b/cpp/src/qpid/ha/PrimaryTxObserver.cpp
new file mode 100644
index 0000000000..8a8364ac22
--- /dev/null
+++ b/cpp/src/qpid/ha/PrimaryTxObserver.cpp
@@ -0,0 +1,104 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include "Event.h"
+#include "HaBroker.h"
+#include "Primary.h"
+#include "PrimaryTxObserver.h"
+#include "QueueGuard.h"
+#include "RemoteBackup.h"
+#include "ReplicatingSubscription.h"
+
+#include "qpid/broker/Broker.h"
+#include "qpid/broker/Queue.h"
+#include <boost/lexical_cast.hpp>
+
+namespace qpid {
+namespace ha {
+
+using namespace std;
+using namespace boost;
+using namespace broker;
+
+PrimaryTxObserver::PrimaryTxObserver(HaBroker& hb) :
+ haBroker(hb), broker(hb.getBroker()),
+ id(true) // FIXME aconway 2013-07-11: is UUID an appropriate TX ID?
+{
+ logPrefix = "Primary transaction "+id.str().substr(0,8)+": ";
+ QPID_LOG(trace, logPrefix << "started");
+ pair<shared_ptr<Queue>, bool> result =
+ broker.getQueues().declare(
+ TRANSACTION_REPLICATOR_PREFIX+id.str(),
+ QueueSettings(/*durable*/false, /*autodelete*/true));
+ assert(result.second);
+ txQueue = result.first;
+}
+
+void PrimaryTxObserver::enqueue(const QueuePtr& q, const broker::Message& m)
+{
+ QPID_LOG(trace, logPrefix << "enqueue: " << LogMessageId(*q, m));
+ enqueues[q] += m.getReplicationId();
+ txQueue->deliver(TxEnqueueEvent(q->getName(), m.getReplicationId()).message());
+ txQueue->deliver(m);
+}
+
+void PrimaryTxObserver::dequeue(
+ const QueuePtr& q, QueuePosition pos, ReplicationId id)
+{
+ QPID_LOG(trace, logPrefix << "dequeue: " << LogMessageId(*q, pos, id));
+ txQueue->deliver(TxDequeueEvent(q->getName(), id).message());
+}
+
+void PrimaryTxObserver::deduplicate() {
+ shared_ptr<Primary> primary(boost::dynamic_pointer_cast<Primary>(haBroker.getRole()));
+ assert(primary);
+ // FIXME aconway 2013-07-29: need to verify which backups are *in* the transaction.
+ // Use cluster membership for now
+ BrokerInfo::Set brokers = haBroker.getMembership().getBrokers();
+ types::Uuid selfId = haBroker.getMembership().getSelf();
+ // Tell replicating subscriptions to skip IDs in the transaction.
+ for (BrokerInfo::Set::iterator b = brokers.begin(); b != brokers.end(); ++b) {
+ if (b->getSystemId() == selfId) continue;
+ for (QueueIdsMap::iterator q = enqueues.begin(); q != enqueues.end(); ++q)
+ primary->skip(b->getSystemId(), q->first, q->second);
+ }
+}
+
+bool PrimaryTxObserver::prepare() {
+ // FIXME aconway 2013-07-23: need to delay completion of prepare till all
+ // backups have prepared.
+ QPID_LOG(trace, logPrefix << "prepare");
+ deduplicate();
+ txQueue->deliver(TxPrepareEvent().message());
+ return true;
+}
+
+void PrimaryTxObserver::commit() {
+ QPID_LOG(trace, logPrefix << "commit");
+ txQueue->deliver(TxCommitEvent().message());
+}
+
+void PrimaryTxObserver::rollback() {
+ QPID_LOG(trace, logPrefix << "rollback");
+ txQueue->deliver(TxRollbackEvent().message());
+}
+
+}} // namespace qpid::ha