Date: Thursday, January 5, 2006 @ 16:11:43
Author: gilles
Path: /cvsroot/carob/carob
Added: include/ControllerConnectPolicy.hpp (1.1)
src/ControllerConnectPolicy.cpp (1.1)
Modified: Makefile (1.30 -> 1.31)
(new class)
ControllerConnectPolicy defines one abstract class and several implementations
that precise the way driver chooses a controller to connect back to (after a
controller crash or network failure)
-------------------------------------+
Makefile | 3
include/ControllerConnectPolicy.hpp | 130 ++++++++++++++++++++++++++++++++++
src/ControllerConnectPolicy.cpp | 97 +++++++++++++++++++++++++
3 files changed, 229 insertions(+), 1 deletion(-)
Index: carob/Makefile
diff -u carob/Makefile:1.30 carob/Makefile:1.31
--- carob/Makefile:1.30 Wed Dec 28 18:14:57 2005
+++ carob/Makefile Thu Jan 5 16:11:43 2006
@@ -42,6 +42,7 @@
${SRCDIR}/DriverSocket.o\
${SRCDIR}/CarobException.o\
${SRCDIR}/ConnectionParameters.o\
+ ${SRCDIR}/ControllerConnectPolicy.o\
${SRCDIR}/Connection.o\
${SRCDIR}/Request.o\
${SRCDIR}/RequestWithResultSetParameters.o\
@@ -52,7 +53,7 @@
${SRCDIR}/ParameterStatement.o\
${SRCDIR}/BigDecimal.o\
${SRCDIR}/SQLDataSerialization.o\
- ${SRCDIR}/StringCodecs.o\
+ ${SRCDIR}/StringCodecs.o
# _GLIBCXX_GTHREAD_USE_WEAK
CXXFLAGS = -g3 -Wall -I${INCDIR} -O0
LDFLAGS = -fPIC -shared -lpthread
Index: carob/include/ControllerConnectPolicy.hpp
diff -u /dev/null carob/include/ControllerConnectPolicy.hpp:1.1
--- /dev/null Thu Jan 5 16:11:43 2006
+++ carob/include/ControllerConnectPolicy.hpp Thu Jan 5 16:11:43 2006
@@ -0,0 +1,130 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005 Emic Networks
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed 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.
+ *
+ * Initial developer(s): Gilles Rayrat
+ * Contributor(s): Marc Herbert, Zsolt Simon
+ */
+#ifndef CONTROLLERCONNECTPOLICY_H_
+#define CONTROLLERCONNECTPOLICY_H_
+
+#include <vector>
+
+#include "ConnectionParameters.hpp"
+#include "CriticalSection.hpp"
+
+namespace CarobNS
+{
+/**
+ * Abstract class to define the policy used by the driver to choose a
controller
+ * to connect to.
+ */
+class AbstractControllerConnectPolicy
+{
+public:
+ /**
+ * Creates a new <code>AbstractControllerConnectPolicy</code> object
+ *
+ * @param controllerList the controller list on which the policy applies
+ * @param retryIntervalInMs Interval in milliseconds before retrying to
+ * re-connect to a controller that has failed
+ */
+ AbstractControllerConnectPolicy(const std::vector<ControllerInfo>&
controllerList,
+ long retryIntervalInMs) throw (DriverException, UnexpectedException);
+ /**
+ * Kills the controller ping thread and clear lists
+ */
+ virtual ~AbstractControllerConnectPolicy();
+ /**
+ * Gets a controller using the implementation specific policy
+ * @return <code>ControllerInfo</code> of the selected controller
+ * @throws NoMoreControllerException if no controller in the controller list
+ * is reachable
+ */
+ virtual ControllerInfo getController() throw (NoMoreControllerException,
+ UnexpectedException) = 0;
+ /**
+ * Sets the controller list
+ * @param controllerList The controllerList to set.
+ */
+ void setControllerList(
+ const std::vector<ControllerInfo>&
controllerList)
+ { controller_list = controllerList; }
+ /**
+ * Gets the list of controllers supposed valid
+ * @return the list of controllers
+ */
+ std::vector<ControllerInfo> getControllerList() const
+ { return controller_list; }
+ /**
+ * Gets the list of suspected controllers
+ * @return the list of suspected controllers
+ */
+ std::vector<ControllerInfo> getSuspectedControllers() const
+ { return suspected_controllers; }
+
+ /**
+ * 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
+ */
+ bool isSuspectedOfFailure(
+ const ControllerInfo& controllerInfo);
+ /**
+ * Adds the given controller to the list of suspects.
+ * @param controllerInfo the controller suspected of failure
+ */
+ void suspectControllerOfFailure(
+ const 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);
+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
+ std::vector<ControllerInfo> suspected_controllers;
+ /** class wide mutex */
+ CriticalSection policy_CS;
+ /** mutex on supected controllers */
+ CriticalSection suspected_controllers_CS;
+private:
+ /** Interval between two connection attempts */
+ long retry_interval_in_ms;
+};
+
+/**
+ * Defines a controller selection policy that iterates through the controller
+ * list, one after the other, starting with the first one in the list
+ */
+class RoundRobinConnectPolicy : public AbstractControllerConnectPolicy
+{
+public:
+ RoundRobinConnectPolicy(const std::vector<ControllerInfo>& controllerList,
+ long retryIntervalInMs) throw (DriverException, UnexpectedException);
+ ControllerInfo getController() throw (NoMoreControllerException,
+ UnexpectedException);
+private:
+ int index;
+};
+
+} //namespace CarobNS
+
+#endif /*CONTROLLERCONNECTPOLICY_H_*/
Index: carob/src/ControllerConnectPolicy.cpp
diff -u /dev/null carob/src/ControllerConnectPolicy.cpp:1.1
--- /dev/null Thu Jan 5 16:11:43 2006
+++ carob/src/ControllerConnectPolicy.cpp Thu Jan 5 16:11:43 2006
@@ -0,0 +1,97 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005 Emic Networks
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed 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.
+ *
+ * Initial developer(s): Gilles Rayrat
+ * Contributor(s):
+ */
+
+#include "ControllerConnectPolicy.hpp"
+
+using std::wstring;
+
+using namespace CarobNS;
+
+AbstractControllerConnectPolicy::AbstractControllerConnectPolicy(
+ const std::vector<ControllerInfo>& controllerList, long retryIntervalInMs)
+ throw (DriverException, UnexpectedException)
+{
+ if (controllerList.size() == 0)
+ throw DriverException(
+ L"Invalid empty controller list in connect policy constructor");
+ controller_list = controllerList;
+ // TODO: do we really need to reserve this space ?
+ // suspected_controllers.reserve(controller_list.length());
+ retry_interval_in_ms = retryIntervalInMs;
+}
+
+AbstractControllerConnectPolicy::~AbstractControllerConnectPolicy()
+{
+ controller_list.clear();
+ suspected_controllers.clear();
+ /*
+ // Kill controller ping thread
+ if (controllerPingThread != null)
+ synchronized (controllerPingThread)
+ {
+ controllerPingThread.notify();
+ }
+ */
+}
+
+bool AbstractControllerConnectPolicy::isSuspectedOfFailure(
+ const ControllerInfo& controllerInfo)
+{
+ for (std::vector<ControllerInfo>::const_iterator iter =
suspected_controllers.begin();
+ iter != suspected_controllers.end(); iter++)
+ {
+ if (*iter == controllerInfo)
+ return true;
+ }
+ return false;
+}
+
+RoundRobinConnectPolicy::RoundRobinConnectPolicy(
+ const std::vector<ControllerInfo>& controllerList, long retryIntervalInMs)
+ throw (DriverException, UnexpectedException) :
+AbstractControllerConnectPolicy(controllerList, retryIntervalInMs),
+index(-1)
+{
+}
+
+ControllerInfo RoundRobinConnectPolicy::getController()
+ throw (NoMoreControllerException, UnexpectedException)
+{
+ LockScope ls(&policy_CS);
+ {
+ LockScope scLs(&suspected_controllers_CS);
+ if (suspected_controllers.size() == controller_list.size())
+ throw NoMoreControllerException(L"All "
+ + toWString(suspected_controllers.size())
+ + L" controllers down");
+ do
+ {
+ index = (index + 1) % controller_list.size();
+ }
+ while (isSuspectedOfFailure(controller_list[index]));
+ }
+/*TODO: if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG)
+ System.out.println("Selected controller[" + index + "]:"
+ + controllerList[index]);
+*/
+ return controller_list[index];
+}
+
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits