-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserprogram_knoss.cpp
More file actions
executable file
·209 lines (156 loc) · 5.39 KB
/
userprogram_knoss.cpp
File metadata and controls
executable file
·209 lines (156 loc) · 5.39 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
/* This program will create 3 clients and do a read and a write operation with each of them cuncurrently.
* Please set the varaiable servers before use
*/
#include "client.h"
#include "commons.h"
#include <cstdio>
#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
using namespace std;
typedef unsigned int uint;
//#define NUMBER_OF_CLIENTS 1
int num_secs = 1;
mutex file_lock;
// Define your server information here
static struct Server_info servers[] = {
{"127.0.0.1", 10000},{"127.0.0.1", 10001}, {"127.0.0.1", 10002}, {"127.0.0.1", 10003}, {"127.0.0.1", 10004}};
// static struct Server_info servers[] = {
// {"34.75.64.24", 10000},{"34.122.77.87", 10001},{"34.76.84.19",10002},{"104.155.208.169",10003},{"35.228.78.135",10004}};
// static struct Server_info servers[] = {
// {"34.75.64.24", 10000},{"34.122.77.87", 10001},{"34.76.84.19",10002}};
static char key[] = "123456"; // We only have one key in this userprogram
namespace Thread_helper{
void _put(const struct Client* c, const char* key, uint32_t key_size, const char* value, uint32_t value_size, uint64_t *latency){
ofstream myfile;
file_lock.lock();
myfile.open ("abd_file.edn",std::ios_base::app);
string str = string("{:process ") + to_string(c->id) + string(", :type :invoke, :f :write, :value ") + to_string(atoi(value))+string("}\n");
myfile<<str;
myfile.close();
file_lock.unlock();
cout<<str<<endl;
int status = put(c, key, key_size, value, value_size);
file_lock.lock();
myfile.open ("abd_file.edn",std::ios_base::app);
string str1 = "{:process " + to_string(c->id) + ", :type :ok, :f :write, :value " + to_string(atoi(value)) +"}\n";
myfile<<str1;
myfile.close();
file_lock.unlock();
cout<<str1<<endl;
if(status == 0){ // Success
return;
}
else{
exit(-1);
}
return;
}
void _get(const struct Client* c, const char* key, uint32_t key_size, char** value, uint32_t *value_size, uint64_t *latency){
ofstream myfile;
file_lock.lock();
myfile.open ("abd_file.edn",std::ios_base::app);
string str = "{:process " + to_string(c->id) + ", :type :invoke, :f :read, :value nil}\n";
myfile<<str;
myfile.close();
cout<<str<<endl;
file_lock.unlock();
int status = get(c, key, key_size, value, value_size);
string str1;
if(status == 0){ // Success
file_lock.lock();
myfile.open ("abd_file.edn",std::ios_base::app);
if(*value_size == 0) {
str1 = "{:process " + to_string(c->id) + ", :type :ok, :f :read, :value nil}\n";
} else {
str1 = "{:process " + to_string(c->id) + ", :type :ok, :f :read, :value " + to_string(atoi(*value)) +"}\n";
}
myfile<<str1;
myfile.close();
file_lock.unlock();
cout<<str1<<endl;
return;
}
else{
exit(-1);
}
return;
}
}
// if read_right_ratio is 10% then value is 1
void abd_latency_executer(int read_write_ratio) {
ofstream myfile;
myfile.open ("abd_file.edn");
struct Client* abd_clt[ABD_NUMBER_OF_CLIENTS];
uint64_t latency_value[num_secs*ABD_NUMBER_OF_CLIENTS]={0};
int counter = 0;
for(uint i = 0; i < ABD_NUMBER_OF_CLIENTS; i++){
abd_clt[i] = client_instance(i, "ABD", servers, sizeof(servers) / sizeof(struct Server_info));
if(abd_clt[i] == NULL){
fprintf(stderr, "%s\n", "Error occured in creating clients");
return;
}
}
std::vector<std::thread*> threads;
srand(time(0));
char* values[num_secs*ABD_NUMBER_OF_CLIENTS];
uint32_t value_sizes[num_secs*ABD_NUMBER_OF_CLIENTS];
for (int j = 0; j< num_secs;j++) {
for(int i = 0; i < ABD_NUMBER_OF_CLIENTS; i++) {
// build a random value
int prob = rand() % 10;
if (prob >= read_write_ratio) {
char value[SIZE_OF_VALUE+1] = {0};
for(int i = 0; i < SIZE_OF_VALUE; i++){
value[i] = '0' + rand() % 10;
}
//remove below line to reproduce false case
values[counter] = value;
threads.push_back(new std::thread(Thread_helper::_put, abd_clt[i], key,
sizeof(key), value, sizeof(value), &latency_value[counter]));
} else {
threads.push_back(new std::thread(Thread_helper::_get, abd_clt[i], key,
sizeof(key), &values[counter], &value_sizes[counter], &latency_value[counter]));
}
std::this_thread::sleep_for (std::chrono::milliseconds(10));
counter++;
}
}
for(int i = 0; i < num_secs*ABD_NUMBER_OF_CLIENTS; i++){
threads[i]->join();
}
threads.clear();
for(int i = 0; i < ABD_NUMBER_OF_CLIENTS; i++){
if(client_delete(abd_clt[i]) == -1){
fprintf(stderr, "%s\n", "Error occured in deleting clients");
return;
}
}
myfile.close();
}
int main(int argc, char* argv[]){
if(argc != 2){
fprintf(stderr, "%s%s%s\n", "Error\n"
"Usage: ", argv[0], "[ABD/CM]\n\n"
"Please note to run the servers first\n"
"This application is just for your testing purposes, "
"and the evaluation of your code will be done with another initiation of your Client libraries.");
return -1;
}
if(std::string(argv[1]) == "ABD"){
//abd_latency_executer(1); //10%
abd_latency_executer(5); //50%
//abd_latency_executer(9); //90%
}
else{
fprintf(stderr, "%s%s%s\n", "Error\n"
"Usage: ", argv[0], "[ABD/CM]\n\n"
"Please note to run the servers first\n"
"This application is just for your testing purposes, "
"and the evaluation of your code will be done with another initiation of your Client libraries.");
return -1;
}
return 0;
}