Permalink
Browse files

Implement basic round robin routing strategy for client as well as so…

…me additional cleanups
  • Loading branch information...
1 parent 7abc530 commit e647ee14dfc7d994453af704d925254c988b2c74 @readams readams committed Jun 12, 2009
@@ -96,6 +96,14 @@ class ClientConfig
* @return the value
*/
int getMaxThreads();
+
+ /**
+ * Get the number of milliseconds a node will be banned before we
+ * try again to connect to it following a failure.
+ *
+ * @return the value
+ */
+ int getNodeBannageMs();
private:
/** Internal implementation details for ClientConfig */
@@ -25,29 +25,27 @@ class ClientConfigImpl {
public:
int maxConnectionsPerNode;
int maxTotalConnections;
- int maxThreads;
- int maxQueuedRequests;
- long threadIdleMs;
+ // int maxThreads;
+ // int maxQueuedRequests;
+ // long threadIdleMs;
long connectionTimeoutMs;
long socketTimeoutMs;
long routingTimeoutMs;
- long defaultNodeBannageMs;
- int socketBufferSize;
+ long nodeBannageMs;
std::list<std::string>* bootstrapUrls;
};
ClientConfig::ClientConfig() {
pimpl_ = new ClientConfigImpl();
pimpl_->maxConnectionsPerNode = 0;
pimpl_->maxTotalConnections = 500;
- pimpl_->maxThreads = 5;
- pimpl_->maxQueuedRequests = 500;
- pimpl_->threadIdleMs = 100000;
+ // pimpl_->maxThreads = 5;
+ // pimpl_->maxQueuedRequests = 500;
+ // pimpl_->threadIdleMs = 100000;
pimpl_->connectionTimeoutMs = 500;
pimpl_->socketTimeoutMs = 5000;
pimpl_->routingTimeoutMs = 15000;
- pimpl_->defaultNodeBannageMs = 30000;
- pimpl_->socketBufferSize = 64 * 1024;
+ pimpl_->nodeBannageMs = 30000;
pimpl_->bootstrapUrls = NULL;
}
@@ -84,8 +82,14 @@ long ClientConfig::getSocketTimeoutMs() {
return pimpl_->socketTimeoutMs;
}
+#if 0
int ClientConfig::getMaxThreads() {
return pimpl_->maxThreads;
}
+#endif
+
+int ClientConfig::getNodeBannageMs() {
+ return pimpl_->nodeBannageMs;
+}
} /* namespace Voldemort */
@@ -39,22 +39,6 @@ enum STATE {
STATE_PARTITIONS
};
-Node::Node(int id,
- std::string& host,
- int httpPort,
- int socketPort,
- int adminPort,
- shared_ptr<std::list<int> >& partitions)
- : id_(id), host_(host), httpPort_(httpPort), socketPort_(socketPort),
- adminPort_(adminPort), partitions_(partitions) {
-
-}
-
-Node::Node()
- : id_(-1), httpPort_(0), socketPort_(0),
- adminPort_(0), partitions_(new std::list<int>) {
-}
-
void Cluster::startElement(void* data, const XML_Char*el, const XML_Char **attr) {
Cluster* cl = (Cluster*) data;
@@ -202,4 +186,15 @@ boost::shared_ptr<Node>& Cluster::getNodeById(int nodeId) {
throw VoldemortException("Invalid node ID: " + nodeId);
}
+std::ostream& operator<<(std::ostream& output, const Cluster& cluster) {
+ output << "Cluster('" << cluster.name << "', [";
+ std::map<int, boost::shared_ptr<Node> >::const_iterator it;
+ for (it = cluster.nodesById.begin(); it != cluster.nodesById.end(); ++it) {
+ if (it != cluster.nodesById.begin()) output << ", ";
+ output << *(it->second);
+ }
+ output << "])";
+ return output;
+}
+
} /* namespace Voldemort */
@@ -30,11 +30,14 @@ libvoldemort_la_SOURCES = \
include/ProtocolBuffersRequestFormat.h \
include/Connection.h \
include/ConnectionPool.h \
+ include/Node.h \
include/Cluster.h \
include/RoutedStore.h \
include/InconsistencyResolvingStore.h \
include/VectorClockInconsistencyResolver.h \
include/TimeBasedInconsistencyResolver.h \
+ include/RoutingStrategy.h \
+ include/RoundRobinRoutingStrategy.h \
DefaultStoreClient.cpp \
VersionedValue.cpp \
VectorClock.cpp \
@@ -46,11 +49,13 @@ libvoldemort_la_SOURCES = \
ProtocolBuffersRequestFormat.cpp \
Connection.cpp \
ConnectionPool.cpp \
+ Node.cpp \
Cluster.cpp \
RoutedStore.cpp \
InconsistencyResolvingStore.cpp \
VectorClockInconsistencyResolver.cpp \
TimeBasedInconsistencyResolver.cpp \
+ RoundRobinRoutingStrategy.cpp \
voldemort-client.pb.cc \
voldemort-client.pb.h
View
@@ -1,6 +1,6 @@
/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
- * Implementation for Cluster class.
+ * Implementation for Node class.
*
* Copyright (c) 2009 Webroot Software, Inc.
*
@@ -17,10 +17,8 @@
* the License.
*/
-#include "Cluster.h"
-#include <voldemort/VoldemortException.h>
-#include <iostream>
-#include <string.h>
+#include "Node.h"
+#include <sys/time.h>
namespace Voldemort {
@@ -33,13 +31,40 @@ Node::Node(int id,
int adminPort,
shared_ptr<std::list<int> >& partitions)
: id_(id), host_(host), httpPort_(httpPort), socketPort_(socketPort),
- adminPort_(adminPort), partitions_(partitions) {
+ adminPort_(adminPort), partitions_(partitions),
+ isAvailable_(true), lastChecked_(0) {
}
Node::Node()
: id_(-1), httpPort_(0), socketPort_(0),
- adminPort_(0), partitions_(new std::list<int>) {
+ adminPort_(0), partitions_(new std::list<int>),
+ isAvailable_(true), lastChecked_(0) {
+}
+
+void Node::setAvailable(bool avail) {
+ isAvailable_ = true;
+
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ lastChecked_ = (uint64_t)tv.tv_sec*1000 + (uint64_t)tv.tv_usec/1000;
+}
+
+uint64_t Node::getMsSinceLastChecked() {
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ uint64_t time = (uint64_t)tv.tv_sec*1000 + (uint64_t)tv.tv_usec/1000;
+ return (time - lastChecked_);
+}
+
+bool Node::isAvailable(uint64_t timeout) {
+ return (isAvailable_ ||
+ getMsSinceLastChecked() > timeout);
+}
+
+std::ostream& operator<<(std::ostream& output, const Node& node) {
+ output << "Node" << node.id_;
+ return output;
}
} /* namespace Voldemort */
@@ -72,9 +72,9 @@ static VectorClock* readVectorClock(const voldemort::VectorClock* vvc) {
return new VectorClock(&entries, (uint64_t)vvc->timestamp());
}
+/* TODO map error types to various exception objects derived
+ from VoldemortException */
static void throwException(const voldemort::Error& error) {
- /* XXX - TODO map error types to various exception objects derived
- from VoldemortException */
switch(error.error_code()) {
case 2:
throw InsufficientOperationalNodesException(error.error_message());
@@ -0,0 +1,49 @@
+/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
+/*
+ * Implementation for SocketStoreClientFactory class.
+ *
+ * Copyright (c) 2009 Webroot Software, Inc.
+ *
+ * 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.
+ */
+
+#include "RoundRobinRoutingStrategy.h"
+
+namespace Voldemort {
+
+shared_ptr<std::list<shared_ptr<Node> > >
+RoundRobinRoutingStrategy::routeRequest(const std::string& key) {
+ shared_ptr<std::list<shared_ptr<Node> > >
+ prefList(new std::list<shared_ptr<Node> >());
+
+ int clusterSize = cluster->getNodeMap()->size();
+ int i = 0;
+ while (i < clusterSize) {
+ if (startIterator == cluster->getNodeMap()->end()) {
+ startIterator = cluster->getNodeMap()->begin();
+ }
+
+ Node* node = startIterator->second.get();
+ if (node->isAvailable(clientConfig->getNodeBannageMs())) {
+ prefList->push_back(startIterator->second);
+ ++startIterator;
+ break;
+ }
+ ++startIterator;
+ }
+ return prefList;
+}
+
+using namespace boost;
+
+} /* namespace Voldemort */
Oops, something went wrong.

0 comments on commit e647ee1

Please sign in to comment.