Date: Wednesday, January 25, 2006 @ 23:13:31
  Author: gilles
    Path: /cvsroot/carob/carob

Modified: include/ControllerConnectPolicy.hpp (1.3 -> 1.4)
          src/ControllerConnectPolicy.cpp (1.4 -> 1.5)

moved suspected controller list in .cpp (not visible from the rest of the world)
changed it into a map<ControllerInfo, socket> to prepare ping code (not yet 
implemented)
added number of failures static int that orders suspected controller list


-------------------------------------+
 include/ControllerConnectPolicy.hpp |   28 ++++++++----
 src/ControllerConnectPolicy.cpp     |   79 ++++++++++++++++++++++++----------
 2 files changed, 75 insertions(+), 32 deletions(-)


Index: carob/include/ControllerConnectPolicy.hpp
diff -u carob/include/ControllerConnectPolicy.hpp:1.3 
carob/include/ControllerConnectPolicy.hpp:1.4
--- carob/include/ControllerConnectPolicy.hpp:1.3       Tue Jan 24 17:20:53 2006
+++ carob/include/ControllerConnectPolicy.hpp   Wed Jan 25 23:13:30 2006
@@ -21,7 +21,6 @@
 #ifndef CONTROLLERCONNECTPOLICY_H_
 #define CONTROLLERCONNECTPOLICY_H_
 
-
 #include "CarobException.hpp"
 #include "CriticalSection.hpp"
 
@@ -29,11 +28,14 @@
 
 namespace CarobNS
 {
+
 class ControllerInfo;
 
 /**
  * Abstract class to define the policy used by the driver to choose a 
controller
  * to connect to.
+ * Contains a library-wide static list of controllers suspected of failure (not
+ * visible from outside this class)
  */
 class AbstractControllerConnectPolicy
 {
@@ -73,6 +75,12 @@
   std::vector<ControllerInfo> getControllerList() const
                                   { return controller_list; }
   /**
+   * Tries to connect to each suspected controller with a non-blocking connect
+   * and checks the connection state. In case of connection success, send the
+   * ping command and remove the controller from the suspect list.
+   */
+  static void                 updateSuspectList();
+  /**
    * Returns true if the specified controller is suspected of failure.
    * @param controllerInfo the controller to check
    * @return true if the controller is in the suspect list
@@ -80,27 +88,29 @@
   static bool                 isSuspectedOfFailure(
                                   const ControllerInfo& controllerInfo);
   /**
-   * Adds the given controller to the list of suspects.
+   * Adds the given controller to the list of suspects. Note that this function
+   * modifies the given controller info by setting the failure_number field !
    * @param controllerInfo the controller suspected of failure
    */
   void                        suspectControllerOfFailure(
-                                  const ControllerInfo& controllerInfo);
+                                  ControllerInfo& controllerInfo);
   /**
    * Removes the specified controller from the list of suspect controllers
    * @param controller the controller to remove from the list
    */
   void                        removeControllerFromSuspectList(
                                   const ControllerInfo& controller);
+  /**
+   * Gives the number of controller failures since this process started
+   */
+  static int                  getNumberOfFailures();
 protected:
   /** list of valid controllers */
   std::vector<ControllerInfo> controller_list;
-  /** list of suspected controllers */
-  // Could have been a hash_set as in java but hash_set class is gnu specific
-  static std::vector<ControllerInfo> suspected_controllers;
-  /** class wide mutex */
+  /** class-wide mutex */
   CriticalSection             policy_CS;
-  /** mutex on supected controllers */
-  CriticalSection             suspected_controllers_CS;
+  /** mutex on suspected controllers */
+  static CriticalSection      suspected_controllers_CS;
 private:
 };
 
Index: carob/src/ControllerConnectPolicy.cpp
diff -u carob/src/ControllerConnectPolicy.cpp:1.4 
carob/src/ControllerConnectPolicy.cpp:1.5
--- carob/src/ControllerConnectPolicy.cpp:1.4   Tue Jan 24 17:20:53 2006
+++ carob/src/ControllerConnectPolicy.cpp       Wed Jan 25 23:13:30 2006
@@ -21,15 +21,37 @@
 
 #include "ControllerConnectPolicy.hpp"
 
+#include "JavaSocket.hpp"
+
 #include "ConnectionParameters.hpp"
 #include "Common.hpp"
 
+#include <map>
+
 using std::wstring;
 
 using namespace CarobNS;
 
+CriticalSection AbstractControllerConnectPolicy::suspected_controllers_CS;
+
+// ControllerInfo comparison function
+struct SortByFailureNumber
+{
+  bool operator()(const ControllerInfo& ci1, const ControllerInfo& ci2) const
+  {
+    return (ci1.getFailureNumber() < ci2.getFailureNumber());
+  }
+};
+
+typedef std::map<ControllerInfo, JavaSocket, SortByFailureNumber> 
SuspectedList;
 // Static list of suspected controllers
-std::vector<ControllerInfo> 
AbstractControllerConnectPolicy::suspected_controllers;
+namespace
+{
+  /** list of controllers suspected of failure */
+  SuspectedList suspected_controllers;
+  /** # of controllers that have fail since this driver has been created */
+  int controller_failure_number = 0;
+};
 
 AbstractControllerConnectPolicy::AbstractControllerConnectPolicy(
     const std::vector<ControllerInfo>& controllerList, long retryIntervalInMs)
@@ -39,7 +61,6 @@
     throw DriverException(
         L"Invalid empty controller list in connect policy constructor");
   controller_list = controllerList;
-  suspected_controllers.reserve(controller_list.size());
 }
 
 AbstractControllerConnectPolicy::~AbstractControllerConnectPolicy()
@@ -47,21 +68,29 @@
   controller_list.clear();
 }
 
-bool AbstractControllerConnectPolicy::isSuspectedOfFailure(
-    const ControllerInfo& controllerInfo)
+void AbstractControllerConnectPolicy::updateSuspectList()
 {
-  for (std::vector<ControllerInfo>::const_iterator iter = 
suspected_controllers.begin();
+  LockScope scLs(&suspected_controllers_CS);
+  for (SuspectedList::const_iterator iter = suspected_controllers.begin();
       iter != suspected_controllers.end(); iter++)
   {
-    if (*iter == controllerInfo)
-      return true;
+    //TODO:
+    //connect
+    //select
+    //foreach writable (in select result) => remove from list
   }
-  return false;
 }
 
-void AbstractControllerConnectPolicy::suspectControllerOfFailure(
+bool AbstractControllerConnectPolicy::isSuspectedOfFailure(
     const ControllerInfo& controllerInfo)
 {
+  SuspectedList::const_iterator iter = 
suspected_controllers.find(controllerInfo);
+  return iter != suspected_controllers.end();
+}
+
+void AbstractControllerConnectPolicy::suspectControllerOfFailure(
+    ControllerInfo& controllerInfo)
+{
   wstring 
fctName(L"AbstractControllerConnectPolicy::suspectControllerOfFailure");
 
   // First check that the controller is not already in the list of suspects
@@ -74,11 +103,18 @@
     ControllerInfo controller = controller_list[i];
     if (controller == controllerInfo)
     {
+      // prepare the ping operation to this controller
+      JavaSocket newSocket;
+      newSocket.create(false); //create non blocking
       LockScope scLs(&suspected_controllers_CS);
-      suspected_controllers.push_back(controllerInfo);
+      // assign a failure number to this controller
+      controller_failure_number++;
+      controllerInfo.setFailureNumber(controller_failure_number);
+      //add the controller and its prepared socket to the list
+      suspected_controllers[controllerInfo] = newSocket;
       if (isDebugEnabled())
         logDebug(fctName, L"Controller " + (wstring)controllerInfo
-            + L" is now suspected of failure");
+            + L" is now suspected of failure (#");
       return;
     }
   }
@@ -89,22 +125,19 @@
 {
   wstring 
fctName(L"AbstractControllerConnectPolicy::removeControllerFromSuspectList");
   LockScope scLs(&suspected_controllers_CS);
-  for (std::vector<ControllerInfo>::iterator iter = 
suspected_controllers.begin();
-      iter != suspected_controllers.end(); iter++)
+  suspected_controllers.erase(controller);
+  if (isDebugEnabled())
   {
-    if (*iter == controller)
-    {
-      suspected_controllers.erase(iter);
-      if (isDebugEnabled())
-      {
-        logDebug(fctName, L"Controller " + (wstring)controller
-            + L" is removed from suspect list");
-      }
-      break;
-    }
+    logDebug(fctName, L"Controller " + (wstring)controller
+        + L" is removed from suspect list");
   }
 }
 
+int AbstractControllerConnectPolicy::getNumberOfFailures()
+{
+  return controller_failure_number;
+}
+
 
////////////////////////////////////////////////////////////////////////////////
 // ROUND ROBIN POLICY
 
////////////////////////////////////////////////////////////////////////////////

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to