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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#include <stdlib.h>
#include <stdarg.h>
#include <vector>
#include <MESA/swarmkv.h>
#include "shaper.h"
#include "shaper_maat.h"
#include "stub.h"
using namespace std;
struct profile_priority_queue_len {
uuid_t profile_uuid;
int priority_queue_len[SHAPING_PRIORITY_NUM_MAX][SHAPING_DIR_MAX];
UT_hash_handle hh;
};
struct profile_priority_queue_len *profile_priority_queue_len_hash = NULL;
extern struct stub_shaping_profile *profiles_hash;
void stub_set_token_bucket_avl_per_sec(const char *profile_uuid_str, unsigned int tokens, unsigned char direction, enum shaping_profile_limit_direction limit_direction)
{
uuid_t profile_uuid;
struct stub_shaping_profile *stub_profile = NULL;
unsigned token_bits;
uuid_parse(profile_uuid_str, profile_uuid);
HASH_FIND(hh, profiles_hash, profile_uuid, sizeof(uuid_t), stub_profile);
if (!stub_profile) {
stub_profile = (struct stub_shaping_profile*)calloc(1, sizeof(struct stub_shaping_profile));
uuid_copy(stub_profile->profile.uuid, profile_uuid);
HASH_ADD(hh, profiles_hash, profile.uuid, sizeof(uuid_t), stub_profile);
}
stub_profile->profile.limit_direction = limit_direction;
if (tokens == AVALIABLE_TOKEN_UNLIMITED) {
token_bits = tokens;
} else {
token_bits = tokens * 8;
}
if (limit_direction == PROFILE_LIMIT_DIRECTION_BIDIRECTION) {
stub_profile->profile.bidirection_limit_bandwidth = token_bits;
stub_profile->avaliable_token.bidirection_limit_bandwidth = token_bits;
} else {
if (direction == SHAPING_DIR_IN) {
stub_profile->profile.in_limit_bandwidth = token_bits;
stub_profile->avaliable_token.in_limit_bandwidth = token_bits;
} else {
stub_profile->profile.out_limit_bandwidth = token_bits;
stub_profile->avaliable_token.out_limit_bandwidth = token_bits;
}
}
return;
}
void stub_refresh_token_bucket(const char *profile_uuid_str)
{
uuid_t profile_uuid;
struct stub_shaping_profile *stub_profile = NULL;
uuid_parse(profile_uuid_str, profile_uuid);
HASH_FIND(hh, profiles_hash, profile_uuid, sizeof(uuid_t), stub_profile);
if (!stub_profile) {
return;
}
if (stub_profile->profile.limit_direction == PROFILE_LIMIT_DIRECTION_BIDIRECTION) {
stub_profile->avaliable_token.bidirection_limit_bandwidth = stub_profile->profile.bidirection_limit_bandwidth;
} else {
stub_profile->avaliable_token.in_limit_bandwidth = stub_profile->profile.in_limit_bandwidth;
stub_profile->avaliable_token.out_limit_bandwidth = stub_profile->profile.out_limit_bandwidth;
}
return;
}
void stub_swarmkv_clear_resource()
{
struct profile_priority_queue_len *node, *tmp = NULL;
HASH_ITER(hh, profile_priority_queue_len_hash, node, tmp) {
HASH_DEL(profile_priority_queue_len_hash, node);
free(node);
}
return;
}
/**************stub of swarmkv*****************/
void swarmkv_register_thread(struct swarmkv *db)
{
return;
}
struct swarmkv_options* swarmkv_options_new(void)
{
return NULL;
}
int swarmkv_options_set_logger(struct swarmkv_options *opts, void *logger)
{
return 0;
}
int swarmkv_options_set_bind_address(struct swarmkv_options *opts, const char* ip_addr)
{
return 0;
}
int swarmkv_options_set_cluster_port(struct swarmkv_options *opts, unsigned int cluster_port)
{
return 0;
}
int swarmkv_options_set_consul_port(struct swarmkv_options *opts, unsigned int consul_port)
{
return 0;
}
int swarmkv_options_set_consul_host(struct swarmkv_options *opts, const char* ip_addr)
{
return 0;
}
int swarmkv_options_set_cluster_announce_ip(struct swarmkv_options *opts, const char *ip_addr)
{
return 0;
}
int swarmkv_options_set_cluster_announce_port(struct swarmkv_options *opts, unsigned int cluster_announce_port)
{
return 0;
}
int swarmkv_options_set_health_check_port(struct swarmkv_options *opts, unsigned int health_check_port)
{
return 0;
}
int swarmkv_options_set_health_check_announce_port(struct swarmkv_options *opts, unsigned int health_check_announce_port)
{
return 0;
}
struct swarmkv *swarmkv_open(struct swarmkv_options *opts, const char * cluster_name, char **err)
{
struct swarmkv *db;
db = (struct swarmkv *)calloc(1, 1);
return db;
}
long long swarmkv_caller_get_pending_commands(struct swarmkv *db)
{
return 0;
}
int swarmkv_options_set_log_path(struct swarmkv_options *opts, const char *logpath)
{
return 0;
}
int swarmkv_options_set_log_level(struct swarmkv_options *opts, int loglevel)
{
return 0;
}
static void swarmkv_hincrby_cmd_func(char *cmd_str, swarmkv_on_reply_callback_t * cb, void *cb_arg)
{
uuid_t profile_uuid;
char uuid_str[UUID_STR_LEN] = {0};
int priority;
int value;
char direction[5] = {0};
enum shaping_packet_dir dir;
struct swarmkv_reply *reply = (struct swarmkv_reply*)calloc(1, sizeof(struct swarmkv_reply));
sscanf(cmd_str, "HINCRBY tsg-shaping-%s priority-%d-%s %d", uuid_str, &priority, direction, &value);
uuid_parse(uuid_str, profile_uuid);
if (strncmp(direction, "in", 2) == 0) {
dir = SHAPING_DIR_IN;
} else {
dir = SHAPING_DIR_OUT;
}
struct profile_priority_queue_len *node = NULL;
HASH_FIND(hh, profile_priority_queue_len_hash, profile_uuid, sizeof(uuid_t), node);
if (!node) {
node = (struct profile_priority_queue_len*)calloc(1, sizeof(struct profile_priority_queue_len));
uuid_copy(node->profile_uuid, profile_uuid);
HASH_ADD(hh, profile_priority_queue_len_hash, profile_uuid, sizeof(uuid_t), node);
}
node->priority_queue_len[priority][dir] += value;
reply->type = SWARMKV_REPLY_INTEGER;
cb(reply, cb_arg);
free(reply);
return;
}
static void swarmkv_hmget_cmd_func(char *cmd_str, swarmkv_on_reply_callback_t * cb, void *cb_arg)
{
uuid_t profile_uuid;
char uuid_str[UUID_STR_LEN] = {0};
int priority[10];
int ret;
int priority_num;
char direction[5] = {0};
enum shaping_packet_dir dir;
struct swarmkv_reply *reply = (struct swarmkv_reply*)calloc(1, sizeof(struct swarmkv_reply));
ret = sscanf(cmd_str, "HMGET tsg-shaping-%s priority-%d-%s priority-%d-%*s priority-%d-%*s priority-%d-%*s priority-%d-%*s priority-%d-%*s priority-%d-%*s priority-%d-%*s priority-%d-%*s",
uuid_str, &priority[0], direction, &priority[1], &priority[2], &priority[3], &priority[4], &priority[5], &priority[6], &priority[7], &priority[8]);
priority_num = ret - 1;
uuid_parse(uuid_str, profile_uuid);
struct profile_priority_queue_len *node = NULL;
HASH_FIND(hh, profile_priority_queue_len_hash, profile_uuid, sizeof(uuid_t), node);
if (!node) {
node = (struct profile_priority_queue_len*)calloc(1, sizeof(struct profile_priority_queue_len));
uuid_copy(node->profile_uuid, profile_uuid);
HASH_ADD(hh, profile_priority_queue_len_hash, profile_uuid, sizeof(uuid_t), node);
}
if (strncmp(direction, "in", 2) == 0) {
dir = SHAPING_DIR_IN;
} else {
dir = SHAPING_DIR_OUT;
}
reply->type = SWARMKV_REPLY_ARRAY;
reply->n_element = priority_num;
reply->elements = (struct swarmkv_reply**)calloc(priority_num, sizeof(struct swarmkv_reply*));
for (int i = 0; i < priority_num; i++) {
reply->elements[i] = (struct swarmkv_reply*)calloc(1, sizeof(struct swarmkv_reply));
reply->elements[i]->type = SWARMKV_REPLY_STRING;
char tmp_str[128] = {0};
sprintf(tmp_str, "%d", node->priority_queue_len[priority[i]][dir]);
reply->elements[i]->str = (char *)calloc(1, strlen(tmp_str));
memcpy(reply->elements[i]->str, tmp_str, strlen(tmp_str));
reply->elements[i]->len = strlen(tmp_str);
}
cb(reply, cb_arg);
for(unsigned int i = 0; i < reply->n_element; i++) {
if (reply->elements[i]) {
free(reply->elements[i]->str);
free(reply->elements[i]);
}
}
free(reply->elements);
free(reply);
}
void swarmkv_async_command(struct swarmkv *db, swarmkv_on_reply_callback_t * cb, void *cb_arg, const char *format, ...)
{
char *cmd_str = NULL;
char cmd_keyword[32] = {0};
va_list ap;
va_start(ap,format);
vasprintf(&cmd_str, format, ap);
va_end(ap);
sscanf(cmd_str, "%31s %*s", cmd_keyword);
if (strcmp(cmd_keyword, "HINCRBY") == 0) {
swarmkv_hincrby_cmd_func(cmd_str, cb, cb_arg);
} else if (strcmp(cmd_keyword, "HMGET") == 0) {
swarmkv_hmget_cmd_func(cmd_str, cb, cb_arg);
}
free(cmd_str);
return;
}
void swarmkv_tconsume(struct swarmkv * db, const char * key, size_t keylen, long long tokens, swarmkv_on_reply_callback_t *cb, void *cb_arg)
{
int actual_tokens;
struct swarmkv_reply reply;
char direction[16] = {0};
char uuid_str[UUID_STR_LEN] = {0};
uuid_t profile_uuid;
struct stub_shaping_profile *stub_profile = NULL;
sscanf(key, "tsg-shaping-%36s-%15s", uuid_str, direction);
uuid_parse(uuid_str, profile_uuid);
HASH_FIND(hh, profiles_hash, profile_uuid, sizeof(uuid_t), stub_profile);
if (!stub_profile) {
return;
}
if (strncmp("bidirectional", direction, sizeof(direction)) == 0) {
if (stub_profile->avaliable_token.bidirection_limit_bandwidth == AVALIABLE_TOKEN_UNLIMITED) {
actual_tokens = tokens;
} else {
actual_tokens = stub_profile->avaliable_token.bidirection_limit_bandwidth >= tokens ? tokens : 0;
stub_profile->avaliable_token.bidirection_limit_bandwidth -= actual_tokens;
}
} else if (strncmp("incoming", direction, sizeof(direction)) == 0) {
if (stub_profile->avaliable_token.in_limit_bandwidth == AVALIABLE_TOKEN_UNLIMITED) {
actual_tokens = tokens;
} else {
actual_tokens = stub_profile->avaliable_token.in_limit_bandwidth >= tokens ? tokens : 0;
stub_profile->avaliable_token.in_limit_bandwidth -= actual_tokens;
}
} else {
if (stub_profile->avaliable_token.out_limit_bandwidth == AVALIABLE_TOKEN_UNLIMITED) {
actual_tokens = tokens;
} else {
actual_tokens = stub_profile->avaliable_token.out_limit_bandwidth >= tokens ? tokens : 0;
stub_profile->avaliable_token.out_limit_bandwidth -= actual_tokens;
}
}
reply.integer = actual_tokens;
reply.type = SWARMKV_REPLY_INTEGER;
cb(&reply, cb_arg);
return;
}
void swarmkv_ftconsume(struct swarmkv * db, const char * key, size_t keylen, const char * member, size_t member_len, long long weight, long long tokens, swarmkv_on_reply_callback_t *cb, void *cb_arg)
{
swarmkv_tconsume(db, key, keylen, tokens, cb, cb_arg);
return;
}
void swarmkv_btconsume(struct swarmkv * db, const char * key, size_t keylen, const char * member, size_t member_len, long long tokens, swarmkv_on_reply_callback_t *cb, void *cb_arg)
{
return;
}
void swarmkv_close(struct swarmkv * db)
{
if (db) {
free(db);
}
return;
}
void swarmkv_caller_loop(struct swarmkv *db, int flags, struct timeval *tv)
{
return;
}
int swarmkv_options_set_caller_thread_number(struct swarmkv_options *opts, int nr_caller_threads)
{
return 0;
}
int swarmkv_options_set_worker_thread_number(struct swarmkv_options *opts, int nr_worker_threads)
{
return 0;
}
/**********************************************/
|