forked from VoltDB/voltdb-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockVoltDB.cpp
More file actions
351 lines (311 loc) · 11.2 KB
/
Copy pathMockVoltDB.cpp
File metadata and controls
351 lines (311 loc) · 11.2 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
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
/* This file is part of VoltDB.
* Copyright (C) 2008-2015 VoltDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "MockVoltDB.h"
#include "Client.h"
#include "ClientImpl.h"
#include <arpa/inet.h>
#include "ByteBuffer.hpp"
#include "ByteBuffer.hpp"
#include <cstdio>
#include <event2/buffer.h>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
namespace voltdb {
class CxnContext {
/*
* Data associated with a specific connection
*/
public:
CxnContext(std::string name) : m_name(name), m_nextLength(4), m_lengthOrMessage(true), m_authenticated(false) {
}
const std::string m_name;
int32_t m_nextLength;
bool m_lengthOrMessage;
bool m_authenticated;
};
SharedByteBuffer fileAsByteBuffer(std::string filename) {
int ret = 0;
std::string path = "test_src/" + filename;
FILE *fp = fopen(path.c_str(), "r");
assert(fp);
ret = fseek(fp, 0L, SEEK_END);
assert(!ret);
size_t size = ftell(fp);
ret = fseek(fp, 0L, SEEK_SET);
assert(!ret);
assert(ftell(fp) == 0);
char *buffer = new char[size];
SharedByteBuffer b(buffer, (int)size);
size_t bytes_read = fread(buffer, 1, size, fp);
if (bytes_read != size) {
printf("Failed to read file at %s with size %d (read: %d)\n",
path.c_str(), (int)size, (int)bytes_read);
}
assert(bytes_read == size);
fclose(fp);
return b;
}
/**
type definition for the read or write callback.
The read callback is triggered when new data arrives in the input
buffer and the amount of readable data exceed the low watermark
which is 0 by default.
The write callback is triggered if the write buffer has been
exhausted or fell below its low watermark.
@param bev the bufferevent that triggered the callback
@param ctx the user specified context for this bufferevent
*/
static void readCallback(struct bufferevent *bev, void *ctx) {
MockVoltDB *impl = reinterpret_cast<MockVoltDB*>(ctx);
impl->readCallback(bev);
}
/*
* Only has to handle the case where there is an error or EOF
*/
static void eventCallback(struct bufferevent *bev, short events, void *ctx) {
MockVoltDB *impl = reinterpret_cast<MockVoltDB*>(ctx);
impl->eventCallback(bev, events);
}
/**
type definition for the read or write callback.
The read callback is triggered when new data arrives in the input
buffer and the amount of readable data exceed the low watermark
which is 0 by default.
The write callback is triggered if the write buffer has been
exhausted or fell below its low watermark.
@param bev the bufferevent that triggered the callback
@param ctx the user specified context for this bufferevent
*/
static void writeCallback(struct bufferevent *bev, void *ctx) {
MockVoltDB *impl = reinterpret_cast<MockVoltDB*>(ctx);
impl->writeCallback(bev);
}
static void acceptCallback(struct evconnlistener *listener,
evutil_socket_t fd, struct sockaddr *address, int socklen,
void *ctx) {
MockVoltDB *impl = reinterpret_cast<MockVoltDB*>(ctx);
impl->acceptCallback(listener, fd, address, socklen);
}
MockVoltDB::MockVoltDB(Client client) : m_base(client.m_impl->m_base), m_listener(NULL),
m_hangupOnRequestCounter(-1), m_dontRead(false), m_client(client) {
struct sockaddr_in sin;
::memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(0);
sin.sin_port = htons(21212);
m_listener =
evconnlistener_new_bind(
m_base,
voltdb::acceptCallback,
this,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
-1,
(struct sockaddr*)&sin,
sizeof(sin));
if (!m_listener) {
throw LibEventException();
}
}
MockVoltDB::~MockVoltDB() {
for (std::set<struct bufferevent*>::iterator i = m_connections.begin(); i != m_connections.end(); i++) {
bufferevent_free(*i);
}
m_connections.clear();
if (m_listener != NULL) {
evconnlistener_free(m_listener);
}
}
void MockVoltDB::mimicLargeReply(int64_t clientData, struct bufferevent *bev) {
char *storage = new char[32];
SharedByteBuffer *response = new SharedByteBuffer(storage, 32);
SharedByteBuffer tabledata = fileAsByteBuffer("large_serialized_table.bin");
// write a valid message header (length and protoVersion)
int32_t ttl_length = 18 + (51380323);
response->ensureCapacity(ttl_length);
errType err = errOk;
response->putInt32(err, ttl_length); // msg length prefix (not self-inclusive)
if (!isOk(err)) {
throw voltdb::Exception();
}
response->putInt8(err, 0); // protocol version
if (!isOk(err)) {
throw voltdb::Exception();
}
// write a valid invocation response header.
response->putInt64(err, clientData); // handle
if (!isOk(err)) {
throw voltdb::Exception();
}
response->putInt8(err, 0); // opt-out of "optional fields"
if (!isOk(err)) {
throw voltdb::Exception();
}
response->putInt8(err, 1); // status code:
if (!isOk(err)) {
throw voltdb::Exception();
}
response->putInt8(err, 0); // appstatus code
if (!isOk(err)) {
throw voltdb::Exception();
}
response->putInt32(err, 1); // client round trip time!
if (!isOk(err)) {
throw voltdb::Exception();
}
response->putInt16(err, 1); // result count.
if (!isOk(err)) {
throw voltdb::Exception();
}
// write a table, using the serialized table on disk.
// response->putInt32(51380323); // table size (via ls).
response->put(err, &tabledata);
if (!isOk(err)) {
throw voltdb::Exception();
}
response->flip();
struct evbuffer *evbuf = bufferevent_get_output(bev);
if (evbuffer_add(evbuf, response->bytes(), static_cast<size_t>(response->remaining()))) {
throw voltdb::LibEventException();
}
delete response;
}
void MockVoltDB::readCallback(struct bufferevent *bev) {
if (m_dontRead) {
return;
}
if (m_hangupOnRequestCounter == 0) {
bufferevent_free(bev);
m_contexts.erase(bev);
m_connections.erase(bev);
return;
}
boost::shared_ptr<CxnContext> ctx = m_contexts[bev];
if (!ctx->m_authenticated) {
if (m_hangupOnRequestCounter > 0) {
m_hangupOnRequestCounter--;
if (m_hangupOnRequestCounter == 0) {
bufferevent_free(bev);
m_contexts.erase(bev);
m_connections.erase(bev);
return;
}
}
SharedByteBuffer response = fileAsByteBuffer("authentication_response.msg");
struct evbuffer *evbuf = bufferevent_get_output(bev);
if (evbuffer_add(evbuf, response.bytes(), static_cast<size_t>(response.remaining()))) {
throw voltdb::LibEventException();
}
ctx->m_authenticated = true;
evbuf = bufferevent_get_input(bev);
char lengthBytes[4];
evbuffer_remove(evbuf, lengthBytes, 4);
ByteBuffer lengthBuffer(lengthBytes, 4);
errType err = errOk;
int32_t length = lengthBuffer.getInt32(err);
if (!isOk(err)) {
throw voltdb::Exception();
}
boost::scoped_array<char> message(new char[length]);
evbuffer_remove(evbuf, message.get(), length );
return;
}
struct evbuffer *evbuf = bufferevent_get_input(bev);
while (evbuffer_get_length(evbuf) > 0) {
if (m_hangupOnRequestCounter > 0) {
m_hangupOnRequestCounter--;
if (m_hangupOnRequestCounter == 0) {
bufferevent_flush(bev, EV_WRITE, BEV_FINISHED);
bufferevent_disable(bev, EV_READ);
bufferevent_enable(bev, EV_WRITE);
bufferevent_setwatermark( bev, EV_WRITE, 0, 262144);
return;
}
}
if (m_filenameForNextResponse == "") {
throw std::exception();
}
// message length
char lengthBytes[4];
evbuffer_remove(evbuf, lengthBytes, 4);
ByteBuffer lengthBuffer(lengthBytes, 4);
errType err = errOk;
int32_t length = lengthBuffer.getInt32(err);
if (!isOk(err)) {
throw voltdb::Exception();
}
// message
boost::scoped_array<char> message(new char[length]);
evbuffer_remove(evbuf, message.get(), length );
ByteBuffer messageBuffer(message.get(), length);
// ??
messageBuffer.getInt8(err);
if (!isOk(err)) {
throw voltdb::Exception();
}
bool wasNull;
messageBuffer.getString(err, wasNull);
if (!isOk(err)) {
throw voltdb::Exception();
}
int64_t clientData = messageBuffer.getInt64(err);
if (!isOk(err)) {
throw voltdb::Exception();
}
if (m_filenameForNextResponse == "mimicLargeReply") {
this->mimicLargeReply(clientData, bev);
}
else {
SharedByteBuffer response = fileAsByteBuffer(m_filenameForNextResponse);
response.putInt64(err, 5, clientData);
if (!isOk(err)) {
throw voltdb::Exception();
}
evbuf = bufferevent_get_output(bev);
if (evbuffer_add(evbuf, response.bytes(), static_cast<size_t>(response.remaining()))) {
throw voltdb::LibEventException();
}
}
evbuf = bufferevent_get_input(bev);
}
}
void MockVoltDB::eventCallback(struct bufferevent *bev, short events) {}
void MockVoltDB::writeCallback(struct bufferevent *bev) {
if (m_hangupOnRequestCounter == 0) {
bufferevent_free(bev);
m_contexts.erase(bev);
m_connections.erase(bev);
}
}
void MockVoltDB::acceptCallback(
struct evconnlistener *listener,
evutil_socket_t sock,
struct sockaddr *addr,
int len) {
struct bufferevent *bev = bufferevent_socket_new( m_base, sock, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb( bev, voltdb::readCallback, voltdb::writeCallback, voltdb::eventCallback, this);
bufferevent_setwatermark( bev, EV_READ, 0, 256);
bufferevent_enable(bev, EV_READ);
m_connections.insert(bev);
m_contexts[bev] = boost::shared_ptr<CxnContext>(new CxnContext(""));
}
}