Skip to content
This repository
tree: 2ededd34bd
Fetching contributors…

Cannot retrieve contributors at this time

file 279 lines (239 sloc) 9.469 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/* -*- 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 <voldemort/BootstrapFailureException.h>
#include <voldemort/SocketStoreClientFactory.h>
#include "SocketStore.h"
#include "RoutedStore.h"
#include "RequestFormat.h"
#include "DefaultStoreClient.h"
#include "Cluster.h"
#include "InconsistencyResolvingStore.h"
#include "TimeBasedInconsistencyResolver.h"
#include "VectorClockInconsistencyResolver.h"
#include "RoundRobinRoutingStrategy.h"

#include <iostream>
#include <exception>
#include <ctype.h>

namespace Voldemort {

using namespace boost;
using namespace std;

static const string METADATA_STORE_NAME("metadata");
static const string CLUSTER_KEY("cluster.xml");
static const string STORES_KEY("stores.xml");
static const string ROLLBACK_CLUSTER_KEY("rollback.cluster.xml");

class SocketStoreClientFactoryImpl {
public:
    SocketStoreClientFactoryImpl(ClientConfig& conf);
    ~SocketStoreClientFactoryImpl();

    /**
* Get a raw socket store object
*/
    Store* getStore(const string& storeName,
                    const string& host,
                    int port,
                    RequestFormat::RequestFormatType type);
    
    /**
* Retrieve the given metadata key using the bootstrap list
*/
    VersionedValue bootstrapMetadata(const string& key);

    Store* getStore(const string& storeName,
                    const string& host,
                    int port,
                    RequestFormat::RequestFormatType type,
                    bool shouldReroute);

    shared_ptr<ClientConfig> config;
    shared_ptr<ConnectionPool> connPool;
    RequestFormat::RequestFormatType requestFormatType;
};

SocketStoreClientFactoryImpl::SocketStoreClientFactoryImpl(ClientConfig& conf)
    : config(new ClientConfig(conf)), connPool(new ConnectionPool(config)),
      requestFormatType(RequestFormat::PROTOCOL_BUFFERS)
{

}

SocketStoreClientFactoryImpl::~SocketStoreClientFactoryImpl() {

}

Store* SocketStoreClientFactoryImpl::getStore(const string& storeName,
                                              const string& host,
                                              int port,
                                              RequestFormat::RequestFormatType type,
                                              bool shouldReroute) {
    return new SocketStore(storeName,
                           host,
                           port,
                           config,
                           connPool,
                           type,
                           shouldReroute);
}

#define THROW_BOOTSTRAP \
throw BootstrapFailureException("Invalid bootstrap URL " + url + \
": Expected tcp://host:port");
#define EXPECT_C(char, next) \
if (tolower(*it) != char) \
THROW_BOOTSTRAP; \
state = next;

static void parseBootstrapUrl(const string& url, string& host, int& port) {
    static const int STATE_T = 0;
    static const int STATE_C = 1;
    static const int STATE_P = 2;
    static const int STATE_COL1 = 3;
    static const int STATE_SL1 = 4;
    static const int STATE_SL2 = 5;
    static const int STATE_HOSTSTRING = 6;
    static const int STATE_PORTSTRING = 7;

    stringstream hostStr, portStr;
    int state = STATE_T;

    string::const_iterator it;
    for (it = url.begin(); it != url.end(); ++it) {
        switch(state) {
        case STATE_T:
            EXPECT_C('t', STATE_C);
            break;
        case STATE_C:
            EXPECT_C('c', STATE_P);
            break;
        case STATE_P:
            EXPECT_C('p', STATE_COL1);
            break;
        case STATE_COL1:
            EXPECT_C(':', STATE_SL1);
            break;
        case STATE_SL1:
            EXPECT_C('/', STATE_SL2);
            break;
        case STATE_SL2:
            EXPECT_C('/', STATE_HOSTSTRING);
            break;
        case STATE_HOSTSTRING:
            if (isalnum(*it) || *it == '.' || *it == '-')
                hostStr << *it;
            else if (*it == ':')
                state = STATE_PORTSTRING;
            else
                THROW_BOOTSTRAP;
            break;
        case STATE_PORTSTRING:
            if (isdigit(*it))
                portStr << *it;
            else
                THROW_BOOTSTRAP;
            break;
        }
    }

    if (hostStr.str().length() == 0)
        THROW_BOOTSTRAP;
    if (portStr.str().length() == 0)
        THROW_BOOTSTRAP;

    host = hostStr.str();
    portStr >> port;
    if (port == 0)
        THROW_BOOTSTRAP;
}

VersionedValue SocketStoreClientFactoryImpl::bootstrapMetadata(const string& key) {
    std::list<std::string>* boots = config->getBootstrapUrls();
    std::list<std::string>::const_iterator it;
    for (it = boots->begin(); it != boots->end(); ++it) {
        try {
            string host;
            int port;
            parseBootstrapUrl(*it, host, port);

            auto_ptr<Store> store(getStore(METADATA_STORE_NAME,
                                           host,
                                           port,
                                           requestFormatType,
                                           false));

            auto_ptr<std::list<VersionedValue> > vvs(store->get(key));
            if (vvs->size() == 1) {
                return vvs->front();
            }
        } catch (std::exception& e) {
            /* XXX - TODO Need a real logging mechanism */
            cerr << "Warning: Could not bootstrap '" << *it << "': "
                 << e.what() << endl;
        }
    }
    throw BootstrapFailureException("No available bootstrap servers found!");
}

SocketStoreClientFactory::SocketStoreClientFactory(ClientConfig& conf) {
    pimpl_ = new SocketStoreClientFactoryImpl(conf);

}

SocketStoreClientFactory::~SocketStoreClientFactory() {
    if (pimpl_)
        delete pimpl_;
}

StoreClient* SocketStoreClientFactory::getStoreClient(const std::string& storeName) {
    shared_ptr<InconsistencyResolver> nullResolver;
    return getStoreClient(storeName, nullResolver);
}

StoreClient* SocketStoreClientFactory::
getStoreClient(const std::string& storeName,
               shared_ptr<InconsistencyResolver>& resolver) {
    shared_ptr<Store> store(getRawStore(storeName, resolver));
    return new DefaultStoreClient(store,
                                  resolver,
                                  pimpl_->config,
                                  this);

}

Store* SocketStoreClientFactory::getRawStore(const std::string& storeName,
                                             shared_ptr<InconsistencyResolver>& resolver) {
    VersionedValue clustervv = pimpl_->bootstrapMetadata(CLUSTER_KEY);
    const std::string* clusterXml = clustervv.getValue();
    shared_ptr<Cluster> cluster(new Cluster(*clusterXml));

    shared_ptr<std::map<int, shared_ptr<Store> > >
        clusterMap(new std::map<int, shared_ptr<Store> >());

    const std::map<int, boost::shared_ptr<Node> >* nodeMap = cluster->getNodeMap();
    std::map<int, boost::shared_ptr<Node> >::const_iterator it;
    for (it = nodeMap->begin(); it != nodeMap->end(); ++it) {
        shared_ptr<Store> store(pimpl_->getStore(storeName,
                                                 it->second->getHost(),
                                                 it->second->getSocketPort(),
                                                 pimpl_->requestFormatType,
                                                 true));
        (*clusterMap)[it->second->getId()] = store;
    }

    //VersionedValue storevv = pimpl_->bootstrapMetadata(STORES_KEY);
    //const std::string* storesXml = storevv.getValue();

    // Routed store
    shared_ptr<RoutingStrategy>
        routingStrategy(new RoundRobinRoutingStrategy(pimpl_->config,
                                                      cluster));
    shared_ptr<Store> routedStore(new RoutedStore(storeName,
                                                  pimpl_->config,
                                                  cluster,
                                                  clusterMap,
                                                  routingStrategy));


    InconsistencyResolvingStore* conStore = new InconsistencyResolvingStore(routedStore);
    try {
        shared_ptr<InconsistencyResolver>
            vcResolver(new VectorClockInconsistencyResolver());
        conStore->addResolver(vcResolver);
        
        if (resolver.get()) {
            conStore->addResolver(resolver);
        } else {
            shared_ptr<InconsistencyResolver>
                tbResolver(new TimeBasedInconsistencyResolver());
            conStore->addResolver(tbResolver);
        }
    } catch (...) {
        if (conStore) delete conStore;
        throw;
    }

    return conStore;
}

} /* namespace Voldemort */
Something went wrong with that request. Please try again.