aboutsummaryrefslogtreecommitdiffstats
path: root/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp
blob: 5e09073f8a26d48571e59e3ff56f21497de6748f (plain) (blame)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>

#include "zkfacade.h"

#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <cassert>
#include <cstdio>
#include <sstream>
#include <thread>
#include <boost/throw_exception.hpp>
#include <boost/function_output_iterator.hpp>

#include <zookeeper/zookeeper.h>
#include <vespa/filedistribution/common/logfwd.h>
#include <vespa/defaults.h>
#include <vespa/vespalib/util/sync.h>

typedef std::unique_lock<std::mutex> UniqueLock;

using filedistribution::ZKFacade;
using filedistribution::Buffer;
using filedistribution::ZKGenericException;
using filedistribution::ZKException;
using filedistribution::ZKLogging;

typedef ZKFacade::Path Path;

namespace {

std::string
toErrorMsg(int zkStatus) {
    switch(zkStatus) {
      //System errors
      case ZRUNTIMEINCONSISTENCY:
        return "Zookeeper: A runtime inconsistency was found(ZRUNTIMEINCONSISTENCY)";
      case ZDATAINCONSISTENCY:
        return "Zookeeper: A data inconsistency was found(ZDATAINCONSISTENCY)";
      case ZCONNECTIONLOSS:
        return "Zookeeper: Connection to the server has been lost(ZCONNECTIONLOSS)";
      case ZMARSHALLINGERROR:
        return "Zookeeper: Error while marshalling or unmarshalling data(ZMARSHALLINGERROR)";
      case ZUNIMPLEMENTED:
        return "Zookeeper: Operation is unimplemented(ZUNIMPLEMENTED)";
      case ZOPERATIONTIMEOUT:
        return "Zookeeper: Operation timeout(ZOPERATIONTIMEOUT)";
      case ZBADARGUMENTS:
        return "Zookeeper: Invalid arguments(ZBADARGUMENTS)";
      case ZINVALIDSTATE:
        return "Zookeeper: The connection with the zookeeper servers timed out(ZINVALIDSTATE).";

      //API errors
      case ZNONODE:
        return "Zookeeper: Node does not exist(ZNONODE)";
      case ZNOAUTH:
        return "Zookeeper: Not authenticated(ZNOAUTH)";
      case ZBADVERSION:
        return "Zookeeper: Version conflict(ZBADVERSION)";
      case ZNOCHILDRENFOREPHEMERALS:
        return "Zookeeper: Ephemeral nodes may not have children(ZNOCHILDRENFOREPHEMERALS)";
      case ZNODEEXISTS:
        return "Zookeeper: The node already exists(ZNODEEXISTS)";
      case ZNOTEMPTY:
        return "Zookeeper: The node has children(ZNOTEMPTY)";
      case ZSESSIONEXPIRED:
        return "Zookeeper: The session has been expired by the server(ZSESSIONEXPIRED)";
      case ZINVALIDCALLBACK:
        return "Zookeeper: Invalid callback specified(ZINVALIDCALLBACK)";
      case ZINVALIDACL:
        return "Zookeeper: Invalid ACL specified(ZINVALIDACL)";
      case ZAUTHFAILED:
        return "Zookeeper: Client authentication failed(ZAUTHFAILED)";
      case ZCLOSING:
        return "Zookeeper: ZooKeeper is closing(ZCLOSING)";
      case ZNOTHING:
        return "Zookeeper: No server responses to process(ZNOTHING)";
      default:
        std::cerr <<"In ZKGenericException::what(): Invalid error code " << zkStatus <<std::endl;
        return "Zookeeper: Invalid error code.";
    }
}

class RetryController {
    unsigned int _retryCount;
    ZKFacade& _zkFacade;

    static const unsigned int _maxRetries = 10;
public:
    int _lastStatus;

    RetryController(ZKFacade* zkFacade)
    :_retryCount(0),
     _zkFacade(*zkFacade),
     _lastStatus(0)
    {}

    void operator()(int status) {
        _lastStatus = status;
    }

    bool shouldRetry() {
        ++_retryCount;

        return  _zkFacade.retriesEnabled() &&
                _lastStatus != ZOK &&
                _retryCount < _maxRetries &&
                isNonLastingError(_lastStatus) &&
                pause();
    }

    bool isNonLastingError(int error) {
        return error == ZCONNECTIONLOSS ||
            error == ZOPERATIONTIMEOUT;
    }

    bool pause() {
        unsigned int sleepInSeconds = 1;
        sleep(sleepInSeconds);
        LOGFWD(info, "Retrying zookeeper operation.");
        return true;
    }

    void throwIfError(const Path & path) {
        namespace fd = filedistribution;

        switch (_lastStatus) {
          case ZSESSIONEXPIRED:
            throw fd::ZKSessionExpired(path.string(), VESPA_STRLOC);
          case ZNONODE:
            throw fd::ZKNodeDoesNotExistsException(path.string(), VESPA_STRLOC);
          case ZNODEEXISTS:
            throw fd::ZKNodeExistsException(path.string(), VESPA_STRLOC);
          case ZCONNECTIONLOSS:
            throw fd::ZKConnectionLossException(path.string(), VESPA_STRLOC);
          default:
            if (_lastStatus != ZOK) {
                throw fd::ZKGenericException(_lastStatus, toErrorMsg(_lastStatus) + " : " + path.string(), VESPA_STRLOC);
            }
        }
    }
};

class DeallocateZKStringVectorGuard {
    String_vector& _strings;
public:
    DeallocateZKStringVectorGuard(String_vector& strings)
        :_strings(strings)
    {}

    ~DeallocateZKStringVectorGuard() {
        deallocate_String_vector(&_strings);
    }
};

const Path
setDataForNewFile(ZKFacade& zk, const Path& path, const char* buffer, int length, zhandle_t* zhandle, int createFlags)  {

    RetryController retryController(&zk);
    const int maxPath = 1024;
    char createdPath[maxPath];
    do {
        retryController( zoo_create(zhandle, path.string().c_str(), buffer, length, &ZOO_OPEN_ACL_UNSAFE, createFlags, createdPath, maxPath));
    } while (retryController.shouldRetry());
    Path newPath(createdPath);
    retryController.throwIfError(newPath);
    return newPath;
}

void
setDataForExistingFile(ZKFacade& zk, const Path& path, const char* buffer, int length, zhandle_t* zhandle)  {
    RetryController retryController(&zk);

    const int ignoreVersion = -1;
    do {
        retryController(zoo_set(zhandle, path.string().c_str(), buffer, length, ignoreVersion));
    } while (retryController.shouldRetry());

    retryController.throwIfError(path);
}

} //anonymous namespace

namespace filedistribution {

VESPA_IMPLEMENT_EXCEPTION(ZKNodeDoesNotExistsException, ZKException);
VESPA_IMPLEMENT_EXCEPTION(ZKConnectionLossException, ZKException);
VESPA_IMPLEMENT_EXCEPTION(ZKNodeExistsException, ZKException);
VESPA_IMPLEMENT_EXCEPTION(ZKFailedConnecting, ZKException);
VESPA_IMPLEMENT_EXCEPTION(ZKSessionExpired, ZKException);
VESPA_IMPLEMENT_EXCEPTION_SPINE(ZKGenericException);

}

/********** Active watchers *******************************************/
struct ZKFacade::ZKWatcher {
    const std::weak_ptr<ZKFacade> _owner;
    const NodeChangedWatcherSP _nodeChangedWatcher;

    ZKWatcher(
        const std::shared_ptr<ZKFacade> &owner,
        const NodeChangedWatcherSP& nodeChangedWatcher )
    :_owner(owner),
    _nodeChangedWatcher(nodeChangedWatcher)
    {}

    static void watcherFn(zhandle_t *zh, int type,
        int state, const char *path,void *watcherContext) {

        (void)zh;
        (void)state;
        (void)path;

        if (type == ZOO_SESSION_EVENT) {
            //The session events do not cause unregistering of the watcher
            //inside zookeeper, so don't unregister it here.
            LOGFWD(debug, "ZKWatcher recieved session event with state '%d'. Ignoring", state);
            return;
        }

        LOGFWD(debug, "ZKWatcher: Begin watcher called for path '%s' with type %d.", path, type);

        ZKWatcher* self = static_cast<ZKWatcher*>(watcherContext);

        //WARNING: Since we're creating a shared_ptr to ZKFacade here, this might cause
        //destruction of the ZKFacade in a zookeeper thread.
        //Since zookeeper_close blocks until all watcher threads are finished, and we're inside a watcher thread,
        //this will cause infinite waiting.
        //To avoid this, a custom shared_ptr deleter using a separate deleter thread must be used.

        if (std::shared_ptr<ZKFacade> zk = self->_owner.lock()) {
            zk->invokeWatcher(watcherContext);
        }

        LOGFWD(debug, "ZKWatcher: End watcher called for path '%s' with type %d.", path, type);
    }
};

void
ZKFacade::stateWatchingFun(zhandle_t*, int type, int state, const char* path, void* context) {
    (void)path;
    (void)context;

    //The ZKFacade won't expire before zookeeper_close has finished.
    if (type == ZOO_SESSION_EVENT) {
        LOGFWD(debug, "Zookeeper session event: %d", state);
        if (state == ZOO_EXPIRED_SESSION_STATE) {
            throw ZKSessionExpired(path, VESPA_STRLOC);
        } else if (state == ZOO_AUTH_FAILED_STATE) {
            throw ZKGenericException(ZNOAUTH, path, VESPA_STRLOC);
        }
    } else {
        LOGFWD(info, "State watching function: Unexpected event: '%d' -- '%d' ",  type, state);
    }
}


void* /* watcherContext */
ZKFacade::registerWatcher(const NodeChangedWatcherSP& watcher) {

    UniqueLock lock(_watchersMutex);
    std::shared_ptr<ZKWatcher> zkWatcher(new ZKWatcher(shared_from_this(), watcher));
    _watchers[zkWatcher.get()] = zkWatcher;
    return zkWatcher.get();
}


std::shared_ptr<ZKFacade::ZKWatcher>
ZKFacade::unregisterWatcher(void* watcherContext) {
    UniqueLock lock(_watchersMutex);

    WatchersMap::iterator i = _watchers.find(watcherContext);
    if (i == _watchers.end()) {
        return std::shared_ptr<ZKWatcher>();
    } else {
        std::shared_ptr<ZKWatcher> result = i->second;
        _watchers.erase(i);
        return result;
    }
}

void
ZKFacade::invokeWatcher(void* watcherContext) {
    std::shared_ptr<ZKWatcher> watcher = unregisterWatcher(watcherContext);

    if (!_watchersEnabled)
        return;

    if (watcher) {
        (*watcher->_nodeChangedWatcher)();
    } else {
        LOGFWD(error, "Invoke called on expired watcher.");
    }
}

/********** End live watchers ***************************************/


ZKFacade::ZKFacade(const std::string& zkservers)
    :_retriesEnabled(true),
     _watchersEnabled(true),
     _zhandle(zookeeper_init(zkservers.c_str(),
                             &ZKFacade::stateWatchingFun,
                             _zkSessionTimeOut,
                             0, //clientid,
                             this, //context,
                             0)) //flags
{
    if (!_zhandle) {
        throw ZKFailedConnecting("No zhandle", VESPA_STRLOC);
    }
}

ZKFacade::~ZKFacade() {
    disableRetries();
    _watchersEnabled = false;
    vespalib::Gate done;
    std::thread closer([&done, zhandle=_zhandle] () { zookeeper_close(zhandle); done.countDown(); });
    if ( done.await(50*1000) ) {
        LOGFWD(debug, "Zookeeper connection closed successfully.");
    } else {
        LOGFWD(error, "Not able to close down zookeeper. Dumping core so you can figure out what is wrong");
        abort();
    }
    closer.join();
}

const std::string
ZKFacade::getString(const Path& path) {
    Buffer buffer(getData(path));
    return std::string(buffer.begin(), buffer.end());
}

Buffer
ZKFacade::getData(const Path& path) {
    RetryController retryController(this);
    Buffer buffer(_maxDataSize);
    int bufferSize = _maxDataSize;

    const int watchIsOff = 0;
    do {
        Stat stat;
        bufferSize = _maxDataSize;

        retryController( zoo_get(_zhandle, path.string().c_str(), watchIsOff, &*buffer.begin(), &bufferSize, &stat));
    } while(retryController.shouldRetry());

    retryController.throwIfError(path);
    buffer.resize(bufferSize);
    return buffer;
}

Buffer
ZKFacade::getData(const Path& path, const NodeChangedWatcherSP& watcher) {
    RegistrationGuard unregisterGuard(*this, watcher);
    void* watcherContext = unregisterGuard.get();
    RetryController retryController(this);

    Buffer buffer(_maxDataSize);
    int bufferSize = _maxDataSize;

    do {
        Stat stat;
        bufferSize = _maxDataSize;

        retryController( zoo_wget(_zhandle, path.string().c_str(), &ZKWatcher::watcherFn, watcherContext, &*buffer.begin(), &bufferSize, &stat));
    } while (retryController.shouldRetry());

    retryController.throwIfError(path);

    buffer.resize(bufferSize);
    unregisterGuard.release();
    return buffer;
}

void
ZKFacade::setData(const Path& path, const Buffer& buffer, bool mustExist) {
    return setData(path, &*buffer.begin(), buffer.size(), mustExist);
}

void
ZKFacade::setData(const Path& path, const char* buffer, size_t length, bool mustExist) {
    assert (length < _maxDataSize);

    if (mustExist || hasNode(path)) {
        setDataForExistingFile(*this, path, buffer, length, _zhandle);
    } else {
        setDataForNewFile(*this, path, buffer, length, _zhandle, 0);
    }
}

const Path
ZKFacade::createSequenceNode(const Path& path, const char* buffer, size_t length) {
    assert (length < _maxDataSize);

    int createFlags = ZOO_SEQUENCE;
    return setDataForNewFile(*this, path, buffer, length, _zhandle, createFlags);
}

bool
ZKFacade::hasNode(const Path& path) {
    RetryController retryController(this);
    do {
        Stat stat;
        const int noWatch = 0;
        retryController( zoo_exists(_zhandle, path.string().c_str(), noWatch, &stat));
    } while(retryController.shouldRetry());

    switch(retryController._lastStatus) {
      case ZNONODE:
        return false;
      case ZOK:
        return true;
      default:
        retryController.throwIfError(path);
        //this should never happen:
        assert(false);
        return false;
    }
}

bool
ZKFacade::hasNode(const Path& path, const NodeChangedWatcherSP& watcher) {
    RegistrationGuard unregisterGuard(*this, watcher);
    void* watcherContext = unregisterGuard.get();
    RetryController retryController(this);
    do {
        Stat stat;
        retryController(zoo_wexists(_zhandle, path.string().c_str(), &ZKWatcher::watcherFn, watcherContext, &stat));
    } while (retryController.shouldRetry());

    bool retval(false);
    switch(retryController._lastStatus) {
      case ZNONODE:
        retval = false;
        break;
      case ZOK:
        retval = true;
        break;
      default:
        retryController.throwIfError(path);
        //this should never happen:
        assert(false);
        retval =  false;
        break;
    }
    unregisterGuard.release();
    return retval;
}

void
ZKFacade::addEphemeralNode(const Path& path) {
    try {
        setDataForNewFile(*this, path, "", 0, _zhandle, ZOO_EPHEMERAL);
    } catch(const ZKNodeExistsException& e) {
        remove(path);
        addEphemeralNode(path);
    }
}

void
ZKFacade::remove(const Path& path) {
    std::vector< std::string > children = getChildren(path);
    if (!children.empty()) {
        std::for_each(children.begin(), children.end(), [&](const std::string & s){ remove(path / s); });
    }

    RetryController retryController(this);
    do {
        int ignoreVersion = -1;
        retryController( zoo_delete(_zhandle, path.string().c_str(), ignoreVersion));
    } while (retryController.shouldRetry());

    if (retryController._lastStatus != ZNONODE) {
        retryController.throwIfError(path);
    }
}

void
ZKFacade::removeIfExists(const Path& path) {
    try {
        if (hasNode(path)) {
            remove(path);
        }
    } catch (const ZKNodeDoesNotExistsException& e) {
        //someone else removed it concurrently, not a problem.
    }
}

void
ZKFacade::retainOnly(const Path& path, const std::vector<std::string>& childrenToPreserve) {
    typedef std::vector<std::string> Children;

    Children current = getChildren(path);
    std::sort(current.begin(), current.end());

    Children toPreserveSorted(childrenToPreserve);
    std::sort(toPreserveSorted.begin(), toPreserveSorted.end());

    std::set_difference(current.begin(), current.end(),
                        toPreserveSorted.begin(), toPreserveSorted.end(),
                        boost::make_function_output_iterator([&](const std::string & s){ remove(path / s); }));
}

std::vector< std::string >
ZKFacade::getChildren(const Path& path) {
    RetryController retryController(this);
    String_vector children;
    do {
        const bool watch = false;
        retryController( zoo_get_children(_zhandle, path.string().c_str(), watch, &children));
    } while (retryController.shouldRetry());

    retryController.throwIfError(path);

    DeallocateZKStringVectorGuard deallocateGuard(children);

    typedef std::vector<std::string> ResultType;
    ResultType result;
    result.reserve(children.count);

    std::copy(children.data, children.data + children.count, std::back_inserter(result));

    return result;
}

std::vector< std::string >
ZKFacade::getChildren(const Path& path, const NodeChangedWatcherSP& watcher) {
    RegistrationGuard unregisterGuard(*this, watcher);
    void* watcherContext = unregisterGuard.get();

    RetryController retryController(this);
    String_vector children;
    do {
        retryController( zoo_wget_children(_zhandle, path.string().c_str(), &ZKWatcher::watcherFn, watcherContext, &children));
    } while (retryController.shouldRetry());

    retryController.throwIfError(path);

    DeallocateZKStringVectorGuard deallocateGuard(children);

    typedef std::vector<std::string> ResultType;
    ResultType result;
    result.reserve(children.count);

    std::copy(children.data, children.data + children.count, std::back_inserter(result));

    unregisterGuard.release();
    return result;
}


void
ZKFacade::disableRetries() {
    _retriesEnabled = false;
}

ZKLogging::ZKLogging() :
    _file(nullptr)
{
    std::string filename(vespa::Defaults::vespaHome());
    filename.append("/tmp/zookeeper.log");
    _file = std::fopen(filename.c_str(), "w");
    if (_file == nullptr) {
         std::cerr <<"Could not open file " <<filename << std::endl;
    } else {
         zoo_set_log_stream(_file);
    }

    zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR);
}

ZKLogging::~ZKLogging()
{
    zoo_set_log_stream(nullptr);
    if (_file != nullptr) {
        std::fclose(_file);
        _file = nullptr;
    }
}