-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_old.cc
317 lines (271 loc) · 8.66 KB
/
server_old.cc
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
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <stdio.h>
#include <fcntl.h>
#include <sstream>
#include "unistd.h"
#include <grpc/support/log.h>
#include <grpcpp/grpcpp.h>
#include "keyvalue.grpc.pb.h"
#include "storage.hpp"
int THREADPOOL_SIZE = 4;
using grpc::Server;
using grpc::ServerAsyncResponseWriter;
using grpc::ServerBuilder;
using grpc::ServerCompletionQueue;
using grpc::ServerContext;
using grpc::Status;
using keyvalue::KeyRequest;
using keyvalue::KeyValueReply;
using keyvalue::KeyValueRequest;
using keyvalue::KVStore;
class ServerImpl final
{
public:
~ServerImpl()
{
delete &storage_;
server_->Shutdown();
for (int i = 0; i < THREADPOOL_SIZE; i++)
cq_[i]->Shutdown();
}
void Run()
{
ifstream f_config;
f_config.open("config.txt");
string portno;
getline(f_config, portno);
string server_address("0.0.0.0:" + portno);
string line;
getline(f_config, line);
getline(f_config, line);
getline(f_config, line);
stringstream ss(line);
ss >> THREADPOOL_SIZE;
cout << "THREADPOOL_SIZE " << THREADPOOL_SIZE << endl;
cout << "port no. " << portno << endl;
cq_ = new unique_ptr<ServerCompletionQueue>[THREADPOOL_SIZE];
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service_);
for (int i = 0; i < THREADPOOL_SIZE; i++)
cq_[i] = builder.AddCompletionQueue();
server_ = builder.BuildAndStart();
cout << "Server listening on " << server_address << endl;
vector<thread> worker_threads;
for (int i = 0; i < THREADPOOL_SIZE; ++i)
worker_threads.push_back(thread(&ServerImpl::HandleRpcs, this, i));
for (auto i{0}; i < worker_threads.size(); i++)
worker_threads[i].join();
}
private:
class CallData
{
public:
virtual void Proceed() = 0;
};
class GetKeyFunc final : public CallData
{
public:
GetKeyFunc(KVStore::AsyncService *service, Storage *storage, ServerCompletionQueue *cq, int thread_id_)
: service_(service), storage_(storage), cq_(cq), responder_(&ctx_), status_(CREATE), thread_id(thread_id_)
{
Proceed();
}
void Proceed()
{
if (status_ == CREATE)
{
status_ = PROCESS;
service_->RequestGetKey(&ctx_, &request_, &responder_, cq_, cq_, this);
}
else if (status_ == PROCESS)
{
//new client
new GetKeyFunc(service_, storage_, cq_, thread_id);
// The actual processing.
storage_->reader_lock();
string result = storage_->handle_get(request_.key());
storage_->reader_unlock();
if (!result.compare("ERROR"))
{
reply_.set_message("KEY DOES NOT EXISTS");
reply_.set_status(400);
}
else
{
reply_.set_status(200);
reply_.set_value(result);
reply_.set_key(request_.key());
}
reply_.set_timestamp(request_.timestamp());
status_ = FINISH;
responder_.Finish(reply_, Status::OK, this);
}
else
{
GPR_ASSERT(status_ == FINISH);
delete this;
}
}
private:
KVStore::AsyncService *service_;
ServerCompletionQueue *cq_;
ServerContext ctx_;
Storage *storage_;
KeyRequest request_;
KeyValueReply reply_;
int thread_id;
ServerAsyncResponseWriter<KeyValueReply> responder_;
enum CallStatus
{
CREATE,
PROCESS,
FINISH
};
CallStatus status_;
};
class PutKeyFunc final : public CallData
{
public:
PutKeyFunc(KVStore::AsyncService *service, Storage *storage, ServerCompletionQueue *cq, int thread_id_)
: service_(service), storage_(storage), cq_(cq), responder_(&ctx_), status_(CREATE), thread_id(thread_id_)
{
Proceed();
}
void Proceed()
{
if (status_ == CREATE)
{
status_ = PROCESS;
service_->RequestPutKey(&ctx_, &request_, &responder_, cq_, cq_, this);
}
else if (status_ == PROCESS)
{
//new client
new PutKeyFunc(service_, storage_, cq_, thread_id);
// The actual processing.
storage_->writer_lock();
storage_->handle_put(request_.key(), request_.value());
storage_->writer_unlock();
reply_.set_status(200);
reply_.set_key(request_.key());
reply_.set_value(request_.value());
reply_.set_timestamp(request_.timestamp());
status_ = FINISH;
responder_.Finish(reply_, Status::OK, this);
}
else
{
GPR_ASSERT(status_ == FINISH);
delete this;
}
}
private:
KVStore::AsyncService *service_;
ServerCompletionQueue *cq_;
ServerContext ctx_;
Storage *storage_;
KeyValueRequest request_;
KeyValueReply reply_;
int thread_id;
ServerAsyncResponseWriter<KeyValueReply> responder_;
enum CallStatus
{
CREATE,
PROCESS,
FINISH
};
CallStatus status_;
};
class DeleteKeyFunc final : public CallData
{
public:
DeleteKeyFunc(KVStore::AsyncService *service, Storage *storage, ServerCompletionQueue *cq, int thread_id_)
: service_(service), storage_(storage), cq_(cq), responder_(&ctx_), status_(CREATE), thread_id(thread_id_)
{
Proceed();
}
void Proceed()
{
if (status_ == CREATE)
{
status_ = PROCESS;
service_->RequestDeleteKey(&ctx_, &request_, &responder_, cq_, cq_, this);
}
else if (status_ == PROCESS)
{
//new client
new DeleteKeyFunc(service_, storage_, cq_, thread_id);
// The actual processing.
storage_->writer_lock();
string result = storage_->handle_delete(request_.key());
storage_->writer_unlock();
if (!result.compare("ERROR"))
{
reply_.set_message("KEY DOES NOT EXISTS");
reply_.set_status(400);
}
else
{
reply_.set_status(200);
reply_.set_key(request_.key());
}
reply_.set_timestamp(request_.timestamp());
status_ = FINISH;
responder_.Finish(reply_, Status::OK, this);
}
else
{
GPR_ASSERT(status_ == FINISH);
delete this;
}
}
private:
KVStore::AsyncService *service_;
ServerCompletionQueue *cq_;
ServerContext ctx_;
Storage *storage_;
KeyRequest request_;
KeyValueReply reply_;
int thread_id;
ServerAsyncResponseWriter<KeyValueReply> responder_;
enum CallStatus
{
CREATE,
PROCESS,
FINISH
};
CallStatus status_;
};
// This can be run in multiple threads if needed.
void HandleRpcs(int thread_id)
{
new GetKeyFunc(&service_, &storage_, cq_[thread_id].get(), thread_id);
new PutKeyFunc(&service_, &storage_, cq_[thread_id].get(), thread_id);
new DeleteKeyFunc(&service_, &storage_, cq_[thread_id].get(), thread_id);
void *tag;
bool ok;
while (true)
{
// cout << "In Handle " << thread_id << endl;
GPR_ASSERT(cq_[thread_id]->Next(&tag, &ok));
GPR_ASSERT(ok);
static_cast<CallData *>(tag)->Proceed();
}
}
unique_ptr<ServerCompletionQueue> *cq_;
KVStore::AsyncService service_;
unique_ptr<Server> server_;
Storage storage_;
};
int main(int argc, char **argv)
{
ServerImpl server;
int file = open("log.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
dup2(file, fileno(stderr));
server.Run();
return 0;
}