summaryrefslogtreecommitdiffstats
path: root/filedistribution/src/tests/lib/mock-zookeeper.cpp
blob: 4d39e41786abc9b2df4597ad5999ec7fd4de6b33 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <zookeeper/zookeeper.h>

#include <string>
#include <map>
#include <cassert>
#include <cstring>
#include <vector>

#include <thread>
#include <atomic>
#include <boost/lexical_cast.hpp>

#include <iostream>

#include <vespa/filedistribution/common/concurrentqueue.h>

using std::map;
using std::string;
using std::vector;
using std::pair;
using std::make_pair;
using filedistribution::ConcurrentQueue;

namespace {
std::pair<string, string> parentPathAndChildName(const string& childPath)
{
    if (childPath.empty()) {
        return std::make_pair("", "");
    } else {
        assert (childPath[0] == '/');

        size_t index = childPath.find_last_of("/");
        return std::make_pair(childPath.substr(0, index), childPath.substr(index + 1));
    }
}

struct Node {
    typedef map<string, Node> Children;
    Children children;
    bool exists;
    bool ephemeral;
    vector<char> buffer;
    vector<pair<watcher_fn, void*> > watchers;

    Node()
        :exists(false),
         ephemeral(false)
    {}

    void addWatcher(watcher_fn fn, void* context) {
        if (fn)
            watchers.push_back(make_pair(fn, context));
    }

    void triggerWatches(zhandle_t* zh, const std::string& path);
};

std::shared_ptr<Node> sharedRoot;

void doNothing() { }

struct ZHandle {
    struct Worker {
        ZHandle& zhandle;

        Worker(ZHandle* parent) : zhandle(*parent) {}

        void operator()();
    };

    int sequence;

    std::shared_ptr<Node> root;
    std::atomic<bool> _closed;
    std::thread _watchersThread;
    vector<string> ephemeralNodes;

    typedef std::function<void (void)> InvokeWatcherFun;
    ConcurrentQueue<InvokeWatcherFun> watcherInvocations;

    Node& getNode(const string& path);

    Node& getParent(const string& path);

    void ephemeralNode(const string&path) {
        ephemeralNodes.push_back(path);
    }

    ZHandle() : sequence(0), _closed(false), _watchersThread(Worker(this)) {
        if (!sharedRoot)
            sharedRoot.reset(new Node());

        root = sharedRoot;
    }

    ~ZHandle() {
        std::for_each(ephemeralNodes.begin(), ephemeralNodes.end(),
                      [this] (const string & s) { zoo_delete((zhandle_t*)this, s.c_str(), 0); });
        close();
        _watchersThread.join();
    }
    void close() {
        _closed.store(true);
        watcherInvocations.push(std::ref(doNothing));
    }
};

void
ZHandle::Worker::operator()()
{
    while (! zhandle._closed.load()) {
        InvokeWatcherFun fun = zhandle.watcherInvocations.pop();
        fun();
    }
}

Node& ZHandle::getNode(const string& path)  {
    auto splittedPath = parentPathAndChildName(path);
    if (splittedPath.second.empty()) {
        return *root;
    } else {
        return getNode(splittedPath.first).children[splittedPath.second];
    }
}

Node&
ZHandle::getParent(const string& childPath)
{
    auto splittedPath = parentPathAndChildName(childPath);
    if (splittedPath.second.empty()) {
        throw "Can't get parent of root.";
    } else {
        return getNode(splittedPath.first);
    }
}

void
Node::triggerWatches(zhandle_t* zh, const std::string& path) {
    for (auto i = watchers.begin(); i != watchers.end(); ++i) {
        ((ZHandle*)zh)->watcherInvocations.push([zh, i, path] () { i->first(zh, 0, 0, path.c_str(), i->second); });
    }
    watchers.clear();
}

} //anonymous namespace

extern "C" {

ZOOAPI void zoo_set_debug_level(ZooLogLevel) {}
ZOOAPI zhandle_t *zookeeper_init(const char * host, watcher_fn fn,
                                 int recv_timeout, const clientid_t *clientid, void *context, int flags)
{
    (void)host;
    (void)fn;
    (void)recv_timeout;
    (void)clientid;
    (void)context;
    (void)flags;

    return (zhandle_t*)new ZHandle;
}

ZOOAPI int zookeeper_close(zhandle_t *zh)
{
    delete (ZHandle*)zh;
    return 0;
}

ZOOAPI int zoo_create(zhandle_t *zh, const char *pathOrPrefix, const char *value,
                      int valuelen, const struct ACL_vector *, int flags,
                      char *path_buffer, int path_buffer_len)
{
    std::string path = pathOrPrefix;
    if (flags & ZOO_SEQUENCE)
        path += boost::lexical_cast<std::string>(((ZHandle*)zh)->sequence++);

    strncpy(path_buffer, path.c_str(), path_buffer_len);
    Node& node = ((ZHandle*)zh)->getNode(path);
    node.exists = true;

    if (flags & ZOO_EPHEMERAL)
        ((ZHandle*)zh)->ephemeralNode(path);

    node.buffer.resize(valuelen);
    std::copy(value, value + valuelen, node.buffer.begin());


    node.triggerWatches(zh, path);
    ((ZHandle*)zh)->getParent(path).triggerWatches(zh,
            parentPathAndChildName(path).first);

    return 0;
}


ZOOAPI int zoo_set(zhandle_t *zh, const char *path, const char *buffer,
                   int buflen, int version) {
    (void)version;

    Node& node = ((ZHandle*)zh)->getNode(path);
    if (!node.exists)
        return ZNONODE;


    node.buffer.resize(buflen);
    std::copy(buffer, buffer + buflen, node.buffer.begin());

    node.triggerWatches(zh, path);
    return 0;
}



ZOOAPI int zoo_get_children(zhandle_t *zh, const char *path, int watch,
                            struct String_vector *strings)
{
    (void)watch;
    return zoo_wget_children(zh, path,
                             0, 0,
                             strings);
}

ZOOAPI int zoo_wget_children(zhandle_t *zh, const char *path,
                             watcher_fn watcher, void* watcherCtx,
                             struct String_vector *strings)
{
    Node& node = ((ZHandle*)zh)->getNode(path);
    strings->count = node.children.size();
    strings->data = new char*[strings->count];

    int index = 0;
    for (auto i = node.children.begin(); i != node.children.end(); ++i) {
        strings->data[index] = new char[i->first.length() + 1];
        std::strcpy(strings->data[index], &*i->first.begin());
        ++index;
    }

    node.addWatcher(watcher, watcherCtx);

    return 0;
}




ZOOAPI int zoo_delete(zhandle_t *zh, const char *path, int version)
{
    (void)version;

    std::string pathStr = path;
    int index = pathStr.find_last_of("/");

    if (pathStr.length() == 1)
        throw "Can't delete root";

    Node& parent = ((ZHandle*)zh)->getNode(pathStr.substr(0, index));
    parent.children.erase(pathStr.substr(index + 1));

    ((ZHandle*)zh)->getParent(path).triggerWatches(zh,
            parentPathAndChildName(path).first);

    return 0;
}

void zoo_set_log_stream(FILE*) {}

int deallocate_String_vector(struct String_vector *v) {
    for (int i=0; i< v->count; ++i) {
        delete[] v->data[i];
    }
    delete[] v->data;
    return 0;
}


ZOOAPI int zoo_get(zhandle_t *zh, const char *path, int watch, char *buffer,
                   int* buffer_len, struct Stat *stat)
{
    (void)watch;

    return zoo_wget(zh, path,
                    0, 0,
                    buffer, buffer_len, stat);

}

ZOOAPI int zoo_wget(zhandle_t *zh, const char *path,
                    watcher_fn watcher, void* watcherCtx,
                    char *buffer, int* buffer_len, struct Stat *)
{
    Node& node = ((ZHandle*)zh)->getNode(path);
    std::copy(node.buffer.begin(), node.buffer.end(), buffer);
    *buffer_len = node.buffer.size();

    node.addWatcher(watcher, watcherCtx);
    return 0;
}

ZOOAPI int zoo_wexists(zhandle_t *zh, const char *path,
                       watcher_fn watcher, void* watcherCtx, struct Stat *)
{
    Node& node = ((ZHandle*)zh)->getNode(path);

    node.addWatcher(watcher, watcherCtx);
    return node.exists ? ZOK : ZNONODE;
}

ZOOAPI int zoo_exists(zhandle_t *zh, const char *path, int watch, struct Stat *stat)
{
    (void)watch;
    return zoo_wexists(zh, path,
                       0, 0,
                       stat);
}




ZOOAPI ACL_vector ZOO_OPEN_ACL_UNSAFE;

ZOOAPI const int ZOO_SEQUENCE = 1;
ZOOAPI const int ZOO_EPHEMERAL = 2;
ZOOAPI const int ZOO_SESSION_EVENT = 3;
ZOOAPI const int ZOO_EXPIRED_SESSION_STATE = 4;
ZOOAPI const int ZOO_AUTH_FAILED_STATE = 5;
}