You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2012/05/08 17:04:51 UTC

svn commit: r1335563 - in /qpid/trunk/qpid/cpp/src/qpid: broker/Bridge.cpp broker/Bridge.h broker/Link.cpp broker/SessionHandler.cpp broker/SessionHandler.h ha/Primary.cpp

Author: aconway
Date: Tue May  8 15:04:51 2012
New Revision: 1335563

URL: http://svn.apache.org/viewvc?rev=1335563&view=rev
Log:
QPID-3603: Add ErrorListener to Bridge to handle session errors.

Will be used in HA code.

Added:
    qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp   (with props)
Modified:
    qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.cpp
    qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.h
    qpid/trunk/qpid/cpp/src/qpid/broker/Link.cpp
    qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.cpp
    qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h

Modified: qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.cpp?rev=1335563&r1=1335562&r2=1335563&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.cpp Tue May  8 15:04:51 2012
@@ -90,8 +90,7 @@ void Bridge::create(Connection& c)
     FieldTable options;
     if (args.i_sync) options.setInt("qpid.sync_frequency", args.i_sync);
     SessionHandler& sessionHandler = c.getChannel(channel);
-    sessionHandler.setDetachedCallback(
-        boost::bind(&Bridge::sessionDetached, shared_from_this()));
+    sessionHandler.setErrorListener(shared_from_this());
     if (args.i_srcIsLocal) {
         if (args.i_dynamic)
             throw Exception("Dynamic routing not supported for push routes");
@@ -377,8 +376,29 @@ const string& Bridge::getLocalTag() cons
 {
     return link->getBroker()->getFederationTag();
 }
-void Bridge::sessionDetached() {
+
+// SessionHandler::ErrorListener methods.
+void Bridge::connectionException(
+    framing::connection::CloseCode code, const std::string& msg)
+{
+    if (errorListener) errorListener->connectionException(code, msg);
+}
+
+void Bridge::channelException(
+    framing::session::DetachCode code, const std::string& msg)
+{
+    if (errorListener) errorListener->channelException(code, msg);
+}
+
+void Bridge::executionException(
+    framing::execution::ErrorCode code, const std::string& msg)
+{
+    if (errorListener) errorListener->executionException(code, msg);
+}
+
+void Bridge::detach() {
     detached = true;
+    if (errorListener) errorListener->detach();
 }
 
 std::string Bridge::createName(const std::string& linkName,

Modified: qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.h?rev=1335563&r1=1335562&r2=1335563&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/broker/Bridge.h Tue May  8 15:04:51 2012
@@ -29,6 +29,7 @@
 #include "qpid/framing/FieldTable.h"
 #include "qpid/management/Manageable.h"
 #include "qpid/broker/Exchange.h"
+#include "qpid/broker/SessionHandler.h"
 #include "qmf/org/apache/qpid/broker/ArgsLinkBridge.h"
 #include "qmf/org/apache/qpid/broker/Bridge.h"
 
@@ -43,14 +44,14 @@ class Connection;
 class ConnectionState;
 class Link;
 class LinkRegistry;
-class SessionHandler;
 
 class Bridge : public PersistableConfig,
                public management::Manageable,
                public Exchange::DynamicBridge,
+               public SessionHandler::ErrorListener,
                public boost::enable_shared_from_this<Bridge>
 {
-public:
+  public:
     typedef boost::shared_ptr<Bridge> shared_ptr;
     typedef boost::function<void(Bridge*)> CancellationListener;
     typedef boost::function<void(Bridge&, SessionHandler&)> InitializeCallback;
@@ -104,10 +105,14 @@ public:
                                   const std::string& dest,
                                   const std::string& key);
 
-private:
-    // Callback when the bridge's session is detached.
-    void sessionDetached();
+    // SessionHandler::ErrorListener methods.
+    void connectionException(framing::connection::CloseCode code, const std::string& msg);
+    void channelException(framing::session::DetachCode, const std::string& msg);
+    void executionException(framing::execution::ErrorCode, const std::string& msg);
+    void detach();
 
+    void setErrorListener(boost::shared_ptr<ErrorListener> e) { errorListener = e; }
+  private:
     struct PushHandler : framing::FrameHandler {
         PushHandler(Connection* c) { conn = c; }
         void handle(framing::AMQFrame& frame);
@@ -138,6 +143,7 @@ private:
     void cancel(Connection& c);
     void closed();
     friend class Link; // to call create, cancel, closed()
+    boost::shared_ptr<ErrorListener> errorListener;
 };
 
 

Modified: qpid/trunk/qpid/cpp/src/qpid/broker/Link.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/broker/Link.cpp?rev=1335563&r1=1335562&r2=1335563&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/broker/Link.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/broker/Link.cpp Tue May  8 15:04:51 2012
@@ -267,13 +267,20 @@ void Link::setUrl(const Url& u) {
 
 
 namespace {
-    /** invoked when session used to subscribe to remote's amq.failover exchange detaches */
-    void sessionDetached(Link *link) {
-        QPID_LOG(notice, "detached from 'amq.failover' for link: " << link->getName());
-    }
+class DetachedCallback : public SessionHandler::ErrorListener {
+  public:
+    DetachedCallback(const Link& link) : name(link.getName()) {}
+    void connectionException(framing::connection::CloseCode, const std::string&) {}
+    void channelException(framing::session::DetachCode, const std::string&) {}
+    void executionException(framing::execution::ErrorCode, const std::string&) {}
+    void detach()  {
+        QPID_LOG(notice, "detached from 'amq.failover' for link: " << name);
+    }
+  private:
+    const std::string name;
+};
 }
 
-
 void Link::opened() {
     Mutex::ScopedLock mutex(lock);
     if (!connection) return;
@@ -301,7 +308,8 @@ void Link::opened() {
     failoverChannel = nextChannel();
 
     SessionHandler& sessionHandler = connection->getChannel(failoverChannel);
-    sessionHandler.setDetachedCallback( boost::bind(&sessionDetached, this) );
+    sessionHandler.setErrorListener(
+        boost::shared_ptr<SessionHandler::ErrorListener>(new DetachedCallback(*this)));
     failoverSession = queueName;
     sessionHandler.attachAs(failoverSession);
 

Modified: qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.cpp?rev=1335563&r1=1335562&r2=1335563&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.cpp Tue May  8 15:04:51 2012
@@ -35,23 +35,39 @@ SessionHandler::SessionHandler(Connectio
     : amqp_0_10::SessionHandler(&c.getOutput(), ch),
       connection(c),
       proxy(out),
-      clusterOrderProxy(c.getClusterOrderOutput() ? new SetChannelProxy(ch, c.getClusterOrderOutput()) : 0)
+      clusterOrderProxy(c.getClusterOrderOutput() ?
+                        new SetChannelProxy(ch, c.getClusterOrderOutput()) : 0)
 {}
 
 SessionHandler::~SessionHandler() {}
 
-void SessionHandler::connectionException(framing::connection::CloseCode code, const std::string& msg) {
+void SessionHandler::connectionException(
+    framing::connection::CloseCode code, const std::string& msg)
+{
     // NOTE: must tell the error listener _before_ calling connection.close()
-    if (connection.getErrorListener()) connection.getErrorListener()->connectionError(msg);
+    if (connection.getErrorListener())
+        connection.getErrorListener()->connectionError(msg);
+    if (errorListener)
+        errorListener->connectionException(code, msg);
     connection.close(code, msg);
 }
 
-void SessionHandler::channelException(framing::session::DetachCode, const std::string& msg) {
-    if (connection.getErrorListener()) connection.getErrorListener()->sessionError(getChannel(), msg);
+void SessionHandler::channelException(
+    framing::session::DetachCode code, const std::string& msg)
+{
+    if (connection.getErrorListener())
+        connection.getErrorListener()->sessionError(getChannel(), msg);
+    if (errorListener)
+        errorListener->channelException(code, msg);
 }
 
-void SessionHandler::executionException(framing::execution::ErrorCode, const std::string& msg) {
-    if (connection.getErrorListener()) connection.getErrorListener()->sessionError(getChannel(), msg);
+void SessionHandler::executionException(
+    framing::execution::ErrorCode code, const std::string& msg)
+{
+    if (connection.getErrorListener())
+        connection.getErrorListener()->sessionError(getChannel(), msg);
+    if (errorListener)
+        errorListener->executionException(code, msg);
 }
 
 ConnectionState& SessionHandler::getConnection() { return connection; }
@@ -64,7 +80,7 @@ void SessionHandler::handleDetach() {
     if (session.get())
         connection.getBroker().getSessionManager().detach(session);
     assert(!session.get());
-    if (detachedCallback) detachedCallback();
+    if (errorListener) errorListener->detach();
     connection.closeChannel(channel.get());
 }
 
@@ -118,8 +134,4 @@ void SessionHandler::attached(const std:
     }
 }
 
-void SessionHandler::setDetachedCallback(boost::function<void()> cb) {
-    detachedCallback = cb;
-}
-
 }} // namespace qpid::broker

Modified: qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h?rev=1335563&r1=1335562&r2=1335563&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h Tue May  8 15:04:51 2012
@@ -25,7 +25,7 @@
 #include "qpid/amqp_0_10/SessionHandler.h"
 #include "qpid/broker/SessionHandler.h"
 #include "qpid/framing/AMQP_ClientProxy.h"
-#include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
 
 namespace qpid {
 class SessionState;
@@ -43,6 +43,21 @@ class SessionState;
  */
 class SessionHandler : public amqp_0_10::SessionHandler {
   public:
+    class ErrorListener {
+      public:
+        virtual ~ErrorListener() {}
+        virtual void connectionException(
+            framing::connection::CloseCode code, const std::string& msg) = 0;
+        virtual void channelException(
+            framing::session::DetachCode, const std::string& msg) = 0;
+        virtual void executionException(
+            framing::execution::ErrorCode, const std::string& msg) = 0;
+        /** Called when it is safe to delete the ErrorListener. */
+        virtual void detach() = 0;
+    };
+
+    /**
+     *@param e must not be deleted until ErrorListener::detach has been called */
     SessionHandler(Connection&, framing::ChannelId);
     ~SessionHandler();
 
@@ -71,7 +86,7 @@ class SessionHandler : public amqp_0_10:
     void attached(const std::string& name);//used by 'pushing' inter-broker bridges
     void attachAs(const std::string& name);//used by 'pulling' inter-broker bridges
 
-    void setDetachedCallback(boost::function<void()> cb);
+    void setErrorListener(boost::shared_ptr<ErrorListener> e) { errorListener = e; }
 
   protected:
     virtual void setState(const std::string& sessionName, bool force);
@@ -94,7 +109,7 @@ class SessionHandler : public amqp_0_10:
     framing::AMQP_ClientProxy proxy;
     std::auto_ptr<SessionState> session;
     std::auto_ptr<SetChannelProxy> clusterOrderProxy;
-    boost::function<void ()> detachedCallback;
+    boost::shared_ptr<ErrorListener> errorListener;
 };
 
 }} // namespace qpid::broker

Added: qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp?rev=1335563&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp (added)
+++ qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp Tue May  8 15:04:51 2012
@@ -0,0 +1,86 @@
+/*
+ *
+ * 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 "Backup.h"
+#include "ConnectionExcluder.h"
+#include "HaBroker.h"
+#include "Primary.h"
+#include "qpid/broker/Broker.h"
+#include "qpid/broker/Queue.h"
+#include "qpid/framing/FieldTable.h"
+#include <boost/bind.hpp>
+
+namespace qpid {
+namespace ha {
+
+Primary* Primary::instance = 0;
+
+Primary::Primary(HaBroker& b) :
+    haBroker(b), logPrefix(b),
+    expected(b.getSettings().expectedBackups),
+    unready(0), activated(false)
+{
+    instance = this;            // Let queue replicators find us.
+    if (expected == 0)          // No need for ready check
+        activate(*(sys::Mutex::ScopedLock*)0); // fake lock, ok in ctor.
+    else {
+        // Set up the primary-ready check: ready when all queues have
+        // expected number of backups. Note clients are excluded at this point
+        // so dont't have to worry about changes to the set of queues.
+        HaBroker::QueueNames names = haBroker.getActiveBackups();
+        for (HaBroker::QueueNames::const_iterator i = names.begin(); i != names.end(); ++i)
+        {
+            queues[*i] = 0;
+            ++unready;
+            QPID_LOG(debug, logPrefix << "Need backup of " << *i
+                     << ", " << unready << " unready queues");
+        }
+        QPID_LOG(critical, "FIXME Primary " << queues.size() << " queues");
+        if (queues.empty())
+            activate(*(sys::Mutex::ScopedLock*)0); // fake lock, ok in ctor.
+        else {
+            QPID_LOG(debug, logPrefix << "Waiting for  " << expected
+                     << " backups on " << queues.size() << " queues");
+            // Allow backups to connect.
+            haBroker.getExcluder()->setBackupAllowed(true);
+        }
+    }
+}
+
+void Primary::addReplica(const std::string& q) {
+    sys::Mutex::ScopedLock l(lock);
+    if (!activated) {
+        QueueCounts::iterator i = queues.find(q);
+        if (i != queues.end()) {
+            ++i->second;
+            if (i->second == expected) --unready;
+            QPID_LOG(debug, logPrefix << i->second << " backups caught up on " << q
+                     << ", " << unready << " unready queues");
+            if (unready == 0) activate(l);
+        }
+    }
+}
+
+void Primary::activate(sys::Mutex::ScopedLock&) {
+    activated = true;
+    haBroker.activate();
+}
+
+}} // namespace qpid::ha

Propchange: qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: qpid/trunk/qpid/cpp/src/qpid/ha/Primary.cpp
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org