Repository navigation
Expand file tree
/
Copy pathcl.cpp
More file actions
277 lines (252 loc) · 10.2 KB
/
Copy pathcl.cpp
File metadata and controls
277 lines (252 loc) · 10.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
#include <algorithm>
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <cstdint>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
#include "network/socket_utils.hpp"
#include "network/message.hpp"
std::vector<uint8_t> parse_value(const std::string& val_str) {
std::vector<uint8_t> result;
std::stringstream ss(val_str);
std::string item;
while (std::getline(ss, item, ',')) {
try {
int num = std::stoi(item);
result.push_back(static_cast<uint8_t>(num));
} catch (...) {
continue;
}
}
return result;
}
bool parse_u64(const char* text, uint64_t maximum, uint64_t& result) {
try {
size_t parsed = 0;
const std::string input(text);
if (input.empty() || input.front() == '-') return false;
const unsigned long long value = std::stoull(input, &parsed);
if (parsed != input.size() || value > maximum) return false;
result = static_cast<uint64_t>(value);
return true;
} catch (...) {
return false;
}
}
std::vector<uint8_t> deterministic_value(uint64_t record_index,
size_t value_size) {
std::vector<uint8_t> value(value_size);
for (size_t byte_index = 0; byte_index < value.size(); ++byte_index) {
const uint64_t pattern =
record_index * 131U + static_cast<uint64_t>(byte_index) * 17U;
value[byte_index] = static_cast<uint8_t>(pattern % 251U);
}
return value;
}
int main(int argc, char** argv) {
std::signal(SIGPIPE, SIG_IGN);
if (argc < 3) {
std::cout << "Usage:\n";
std::cout << " PUT: ./cl put <port> <key> <value_csv>\n";
std::cout << " GET: ./cl get <port> <key>\n";
std::cout << " LOCAL DIAGNOSTIC: ./cl get-local <port> <key>\n";
std::cout << " HEALTH: ./cl health <port>\n";
std::cout << " LOAD: ./cl load <port> <key_prefix> <count> <value_size>\n";
std::cout << " HOTLOAD: ./cl hotload <port> <key> <count> "
"<value_size> <seed>\n";
std::cout << " VERIFY: ./cl verify <port> <key_prefix> <count> "
"<value_size> <term>\n";
std::cout << " LOCAL DIAGNOSTIC VERIFY: ./cl verify-local <port> <key_prefix> <count> <value_size> <term>\n";
std::cout << "Set DISKEYV_HOST to connect to a non-local server.\n";
std::cout << "Example: ./cl put 5000 mykey 1,2,3\n";
return 1;
}
std::string op = argv[1];
const bool verifying = op == "verify" || op == "verify-local";
const bool local_read = op == "get-local" || op == "verify-local";
int port = std::stoi(argv[2]);
const bool valid_arguments =
(op == "put" && argc >= 5) || ((op == "get" || op == "get-local") && argc >= 4) ||
(op == "health" && argc >= 3) || (op == "load" && argc >= 6) ||
(op == "hotload" && argc >= 7) ||
(verifying && argc >= 7);
if (!valid_arguments) {
std::cerr << "Invalid or incomplete operation: " << op << "\n";
return 1;
}
if (local_read) std::cerr << "Diagnostic local read: may be stale or uncommitted\n";
const char* configured_host = std::getenv("DISKEYV_HOST");
const std::string host = configured_host == nullptr
? "127.0.0.1"
: configured_host;
int sock = connect_to(host, port);
if (sock < 0) {
std::cerr << "Failed to connect to " << host << ':' << port << "\n";
return 1;
}
NetMessage msg;
if (op == "put") {
std::string key = argv[3];
std::string value_str = argv[4];
std::vector<uint8_t> value = parse_value(value_str);
msg.type = MsgType::CLIENT_PUT;
msg.key = key;
msg.value = value;
if (!send_message(sock, msg)) {
std::cerr << "Failed to send PUT request\n";
close(sock);
return 1;
}
NetMessage reply;
if (!recv_message(sock, reply)) {
std::cerr << "Failed to receive PUT reply\n";
close(sock);
return 1;
}
if (reply.type != MsgType::CLIENT_PUT_REPLY ||
reply.status != OperationStatus::OK) {
std::cerr << "PUT failed: " << status_name(reply.status) << "\n";
close(sock);
return 2;
}
std::cout << "PUT completed successfully\n";
} else if (op == "get" || op == "get-local") {
std::string key = argv[3];
msg.type = local_read ? MsgType::CLIENT_GET_LOCAL : MsgType::CLIENT_GET;
msg.key = key;
if (!send_message(sock, msg)) {
std::cerr << "Failed to send GET request\n";
close(sock);
return 1;
}
NetMessage reply;
if (!recv_message(sock, reply)) {
std::cerr << "Failed to receive GET reply\n";
close(sock);
return 1;
}
if (reply.type != MsgType::CLIENT_GET_REPLY ||
reply.status != OperationStatus::OK) {
std::cerr << "GET failed: " << status_name(reply.status) << "\n";
close(sock);
return reply.status == OperationStatus::NOT_FOUND ? 3 : 2;
}
std::cout << "GET reply:\n";
std::cout << "Term: " << reply.term << "\n";
std::cout << "Seq : " << reply.seq << "\n";
std::cout << "Inc : " << reply.incarnation << "\n";
std::cout << "Value size: " << reply.value.size() << "\n";
if (!reply.value.empty()) {
std::cout << "Value: ";
for (size_t i = 0; i < reply.value.size(); ++i) {
if (i > 0) std::cout << ",";
std::cout << static_cast<int>(reply.value[i]);
}
std::cout << "\n";
}
} else if (op == "load" || op == "hotload" || verifying) {
const std::string prefix = argv[3];
uint64_t count = 0;
uint64_t value_size = 0;
uint64_t expected_term = 0;
uint64_t value_seed = 0;
constexpr uint64_t kMaxBulkRecords = 1000000;
if (!parse_u64(argv[4], kMaxBulkRecords, count) || count == 0 ||
!parse_u64(argv[5], kMaxWireValueSize, value_size) ||
(verifying &&
(!parse_u64(argv[6], std::numeric_limits<uint64_t>::max(),
expected_term) ||
expected_term == 0)) ||
(op == "hotload" &&
!parse_u64(argv[6], std::numeric_limits<uint64_t>::max(), value_seed))) {
std::cerr << "Invalid bulk count, value size, or term\n";
close(sock);
return 1;
}
constexpr uint64_t kPipelineWindow = 32;
for (uint64_t first = 0; first < count;) {
const uint64_t in_window = std::min(kPipelineWindow, count - first);
for (uint64_t offset = 0; offset < in_window; ++offset) {
const uint64_t record_index = first + offset;
const std::string key = op == "hotload" ? prefix : prefix + std::to_string(record_index);
if (key.empty() || key.size() > kMaxWireKeySize) {
std::cerr << "Generated key exceeds wire limit at record "
<< record_index << "\n";
close(sock);
return 1;
}
NetMessage request;
request.type = verifying ? (local_read ? MsgType::CLIENT_GET_LOCAL : MsgType::CLIENT_GET)
: MsgType::CLIENT_PUT;
request.key = key;
if (!verifying) {
request.value = deterministic_value(
value_seed + record_index,
static_cast<size_t>(value_size)
);
}
if (!send_message(sock, request)) {
std::cerr << "Bulk send failed at record " << record_index << "\n";
close(sock);
return 2;
}
}
for (uint64_t offset = 0; offset < in_window; ++offset) {
const uint64_t record_index = first + offset;
NetMessage reply;
if (!recv_message(sock, reply)) {
std::cerr << "Bulk receive failed at record "<< record_index << "\n";
close(sock);
return 2;
}
if (!verifying) {
if (reply.type != MsgType::CLIENT_PUT_REPLY ||
reply.status != OperationStatus::OK) {
std::cerr << "Bulk PUT failed at record "
<< record_index << ": "
<< status_name(reply.status) << "\n";
close(sock);
return 2;
}
} else {
const std::vector<uint8_t> expected = deterministic_value(
record_index, static_cast<size_t>(value_size)
);
if (reply.type != MsgType::CLIENT_GET_REPLY ||
reply.status != OperationStatus::OK ||
reply.term != expected_term ||
reply.value != expected) {
std::cerr << "Bulk verification failed at record "
<< record_index << "\n";
close(sock);
return 2;
}
}
}
first += in_window;
}
std::cout << (verifying ? "Verified " : "Loaded ") << count
<< " records with prefix " << prefix << "\n";
} else if (op == "health") {
msg.type = MsgType::CLIENT_HEALTH;
if (!send_message(sock, msg)) {
std::cerr << "Failed to send health request\n";
close(sock);
return 1;
}
NetMessage reply;
if (!recv_message(sock, reply) ||
reply.type != MsgType::CLIENT_HEALTH_REPLY ||
reply.status != OperationStatus::OK) {
std::cerr << "Health check failed\n";
close(sock);
return 2;
}
std::cout << "healthy\n";
}
close(sock);
return 0;
}