-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-engine.c
More file actions
3697 lines (3030 loc) · 96.7 KB
/
php-engine.c
File metadata and controls
3697 lines (3030 loc) · 96.7 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
$Author = Sargis Ananyan;
$Corp = RedM Inc;
$Web = https://redm.pro/;
$Lincese = BSD 3;
$Name = mLang;
$Description = A programming language for web programming.;
*/
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <signal.h>
#include <poll.h>
#include <netdb.h>
#include "crc32.h"
#include "kdb-data-common.h"
#include "net-events.h"
#include "net-buffers.h"
#include "server-functions.h"
#include "net-connections.h"
#include "net-memcache-server.h"
#include "net-memcache-client.h"
#include "net-crypto-aes.h"
#include "net-http-server.h"
#include "resolver.h"
#include "md5.h"
#include "net-rpc-server.h"
#include "net-rpc-client.h"
#include "net-rpc-common.h"
#include "rpc-const.h"
#include "net-mysql-client.h"
#include "php-engine-vars.h"
#include "php-master.h"
#include "php-runner.h"
#include "php-queries.h"
/** drinkless headers **/
#include "dl-utils-lite.h"
static void turn_sigterm_on (void);
#define MAX_TIMEOUT 30
#define RPC_DEFAULT_PING_INTERVAL 30.0
double rpc_ping_interval = RPC_DEFAULT_PING_INTERVAL;
#define RPC_STOP_INTERVAL (2 * rpc_ping_interval)
#define RPC_FAIL_INTERVAL (20 * rpc_ping_interval)
#define RPC_CONNECT_TIMEOUT 3
/***
DB-client
***/
#define RESPONSE_FAIL_TIMEOUT 30.0
int proxy_client_execute (struct connection *c, int op);
int sqlp_authorized (struct connection *c);
int sqlp_becomes_ready (struct connection *c);
int sqlp_check_ready (struct connection *c);
struct mysql_client_functions db_client_outbound = {
.execute = proxy_client_execute,
.sql_authorized = sqlp_authorized,
.sql_becomes_ready = sqlp_becomes_ready,
.check_ready = sqlp_check_ready,
.sql_flush_packet = sqlc_flush_packet,
.sql_check_perm = sqlc_default_check_perm,
.sql_init_crypto = sqlc_init_crypto,
};
char *sql_username = "boxed";
char *sql_password = "password";
char *sql_database = "boxed_base";
long long cur_request_id = -1;
/***
MC-client
***/
int memcache_client_execute (struct connection *c, int op);
int memcache_client_check_ready (struct connection *c);
int memcache_connected (struct connection *c);
struct memcache_client_functions memcache_client_outbound = {
.execute = memcache_client_execute,
.check_ready = memcache_client_check_ready,
.connected = memcache_connected,
.flush_query = mcc_flush_query,
.mc_check_perm = mcc_default_check_perm,
.mc_init_crypto = mcc_init_crypto,
.mc_start_crypto = mcc_start_crypto
};
/***
RPC-client
***/
int rpcc_execute (struct connection *c, int op, int len);
int rpcc_send_query (struct connection *c);
int rpcc_check_ready (struct connection *c);
void prepare_rpc_query (struct connection *c, int *q, int qn);
void send_rpc_query (struct connection *c, int op, long long id, int *q, int qn);
struct rpc_client_functions rpc_client_outbound = {
.execute = rpcc_execute, //replaced
.check_ready = rpcc_check_ready, //replaced
.flush_packet = rpcc_flush_packet,
.rpc_check_perm = rpcc_default_check_perm,
.rpc_init_crypto = rpcc_init_crypto,
.rpc_start_crypto = rpcc_start_crypto,
.rpc_ready = rpcc_send_query //replaced
};
/***
OUTBOUND CONNECTIONS
***/
struct conn_target default_ct = {
.min_connections = 2,
.max_connections = 3,
.type = &ct_memcache_client,
.extra = &memcache_client_outbound,
.reconnect_timeout = 1
};
struct conn_target db_ct = {
.min_connections = 2,
.max_connections = 3,
.type = &ct_mysql_client,
.extra = &db_client_outbound,
.reconnect_timeout = 1,
.port = 3306
};
struct conn_target rpc_ct = {
.min_connections = 2,
.max_connections = 3,
.type = &ct_rpc_client,
.extra = (void *)&rpc_client_outbound,
.port = 11210,
.reconnect_timeout = 1
};
struct in_addr settings_addr;
struct connection *get_target_connection (struct conn_target *S, int force_flag) {
struct connection *c, *d = NULL;
int u = 10000;
if (!S) {
return NULL;
}
for (c = S->first_conn; c != (struct connection *)S; c = c->next) {
int r = S->type->check_ready (c);
if (r == cr_ok || (force_flag && r == cr_notyet)) {
return c;
} else if (r == cr_stopped && c->unreliability < u) {
u = c->unreliability;
d = c;
}
}
return d;
}
struct connection *get_target_connection_force (struct conn_target *S) {
struct connection *res = get_target_connection (S, 0);
if (res == NULL) {
create_new_connections (S);
res = get_target_connection (S, 1);
}
return res;
}
int get_target_impl (struct conn_target *ct) {
//TODO: fix ref_cnt overflow
struct conn_target *res = create_target (ct, NULL);
int res_id = (int)(res - Targets);
return res_id;
}
int get_target_by_pid (int ip, int port, struct conn_target *ct) {
ct->target.s_addr = htonl (ip);
ct->port = port;
return get_target_impl (ct);
}
int cur_lease_target_ip = -1;
int cur_lease_target_port = -1;
struct conn_target *cur_lease_target_ct = NULL;
int cur_lease_target = -1;
int get_lease_target_by_pid (int ip, int port, struct conn_target *ct) {
if (ip == cur_lease_target_ip && port == cur_lease_target_port && ct == cur_lease_target_ct) {
return cur_lease_target;
}
if (cur_lease_target != -1) {
struct conn_target *old_target = &Targets[cur_lease_target];
destroy_target (old_target);
}
cur_lease_target_ip = ip;
cur_lease_target_port = port;
cur_lease_target_ct = ct;
cur_lease_target = get_target_by_pid (ip, port, ct);
return cur_lease_target;
}
int get_target (const char *host, int port, struct conn_target *ct) {
if (!(0 <= port && port < 0x10000)) {
vkprintf (0, "bad port %d\n", port);
return -1;
}
struct hostent *h;
if (!(h = kdb_gethostbyname (host)) || h->h_addrtype != AF_INET || h->h_length != 4 || !h->h_addr_list || !h->h_addr) {
vkprintf (0, "can't resolve host\n");
return -1;
}
ct->target = *(struct in_addr *) h->h_addr;
ct->port = port;
return get_target_impl (ct);
}
double fix_timeout (double timeout) {
if (timeout < 0) {
return 0;
}
if (timeout > MAX_TIMEOUT) {
return MAX_TIMEOUT;
}
return timeout;
}
/***
HTTP INTERFACE
***/
void http_return (struct connection *c, const char *str, int len);
int delete_pending_query (struct conn_query *q) {
vkprintf (1, "delete_pending_query(%p,%p)\n", q, q->requester);
delete_conn_query (q);
zfree (q, sizeof (*q));
return 0;
}
struct conn_query_functions pending_cq_func = {
.magic = (int)CQUERY_FUNC_MAGIC,
.title = "pending-query",
.wakeup = delete_pending_query,
.close = delete_pending_query,
.complete = delete_pending_query
};
/** delayed httq queries queue **/
struct connection pending_http_queue;
int php_worker_run_flag;
/** dummy request queue **/
struct connection dummy_request_queue;
/** net write command **/
/** do write delayed write to connection **/
typedef struct {
command_t base;
unsigned char *data;
int len;
long long extra;
} command_net_write_t;
void command_net_write_run_sql (command_t *base_command, void *data) {
//fprintf (stderr, "command_net_write [ptr=%p]\n", base_command);
command_net_write_t *command = (command_net_write_t *)base_command;
assert (command->data != NULL);
struct connection *d = (struct connection *)data;
assert (d->status == conn_ready);
/*
{
fprintf (stderr, "packet_len = %d\n", command->len);
int s = command->len;
fprintf (stderr, "got prefix [len = %d]\n", s);
unsigned char *packet = command->data;
if (s > 5) {
int len = (packet[0]) + (packet[1] << 8) + (packet[2] << 16);
int packet_number = packet[3];
int command_type = packet[4];
fprintf (stderr, "[len = %d], [num = %d], [type = %d], [%.*s]\n", len, packet_number, command_type, s - 5, packet + 5);
}
}
*/
assert (write_out (&(d)->Out, command->data, command->len) == command->len);
SQLC_FUNC (d)->sql_flush_packet (d, command->len - 4);
flush_connection_output (d);
d->last_query_sent_time = precise_now;
d->status = conn_wait_answer;
SQLC_DATA(d)->response_state = resp_first;
free (command->data);
command->data = NULL;
command->len = 0;
}
void command_net_write_run_rpc (command_t *base_command, void *data) {
command_net_write_t *command = (command_net_write_t *)base_command;
//fprintf (stderr, "command_net_write [ptr=%p] [len = %d]\n", base_command, command->len);
assert (command->data != NULL);
struct connection *d = (struct connection *)data;
//assert (d->status == conn_ready);
send_rpc_query (d, RPC_INVOKE_REQ, command->extra, (int *)command->data, command->len);
flush_connection_output (d);
d->last_query_sent_time = precise_now;
free (command->data);
command->data = NULL;
command->len = 0;
}
void command_net_write_free (command_t *base_command) {
command_net_write_t *command = (command_net_write_t *)base_command;
if (command->data != NULL) {
free (command->data);
command->data = NULL;
command->len = 0;
}
free (command);
}
command_t command_net_write_sql_base = {
.run = command_net_write_run_sql,
.free = command_net_write_free
};
command_t command_net_write_rpc_base = {
.run = command_net_write_run_rpc,
.free = command_net_write_free
};
command_t *create_command_net_writer (const char *data, int data_len, command_t *base, long long extra) {
command_net_write_t *command = malloc (sizeof (command_net_write_t));
command->base.run = base->run;
command->base.free = base->free;
command->data = malloc ((size_t)data_len);
memcpy (command->data, data, (size_t)data_len);
command->len = data_len;
command->extra = extra;
return (command_t *)command;
}
/** php-script **/
int rpc_main_target = -1;
int rpc_lease_target = -1;
double rpc_lease_timeout = -1;
npid_t lease_pid;
double lease_stats_start_time;
double lease_stats_time;
long long lease_stats_cnt;
typedef enum {
lst_off,
lst_start,
lst_on,
lst_finish
} lease_state_t;
lease_state_t lease_state = lst_off;
int lease_ready_flag = 0;
void lease_change_state (lease_state_t new_state) {
if (lease_state != new_state) {
lease_state = new_state;
lease_ready_flag = 0;
}
}
#define run_once_count 1
const int queries_to_recreate_script = 100;
void *php_script;
typedef enum {http_worker, rpc_worker, once_worker} php_worker_mode_t;
typedef struct {
struct connection *conn;
php_query_data *data;
int paused;
int terminate_flag;
const char *error_message;
double init_time;
double start_time;
double finish_time;
enum {phpq_try_start, phpq_init_script, phpq_run, phpq_free_script, phpq_finish} state;
php_worker_mode_t mode;
long long req_id;
int target_fd;
} php_worker;
php_worker *php_worker_create (php_worker_mode_t mode, struct connection *c, http_query_data *http_data, rpc_query_data *rpc_data, double timeout, long long req_id) {
php_worker *worker = dl_malloc (sizeof (php_worker));
worker->data = php_query_data_create (http_data, rpc_data);
worker->conn = c;
assert (c != NULL);
worker->state = phpq_try_start;
worker->mode = mode;
worker->init_time = precise_now;
worker->finish_time = precise_now + timeout;
worker->paused = 0;
worker->terminate_flag = 0;
worker->error_message = "no error";
worker->req_id = req_id;
if (worker->conn->target) {
worker->target_fd = worker->conn->target - Targets;
} else {
worker->target_fd = -1;
}
vkprintf (2, "create php script [req_id = %016llx]\n", req_id);
return worker;
}
void php_worker_free (php_worker *worker) {
if (worker == NULL) {
return;
}
php_query_data_free (worker->data);
worker->data = NULL;
dl_free (worker, sizeof (php_worker));
}
int has_pending_scripts() {
return php_worker_run_flag || pending_http_queue.first_query != (struct conn_query *)&pending_http_queue;
}
/** trying to start query **/
void php_worker_try_start (php_worker *worker) {
if (worker->terminate_flag) {
worker->state = phpq_finish;
return;
}
if (php_worker_run_flag) { // put connection into pending_http_query
vkprintf (2, "php script [req_id = %016llx] is waiting\n", worker->req_id);
struct conn_query *pending_q = zmalloc (sizeof (struct conn_query));
pending_q->custom_type = 0;
pending_q->outbound = (struct connection *)&pending_http_queue;
assert (worker->conn != NULL);
pending_q->requester = worker->conn;
pending_q->cq_type = &pending_cq_func;
pending_q->timer.wakeup_time = worker->finish_time;
insert_conn_query (pending_q);
worker->conn->status = conn_wait_net;
worker->paused = 1;
return;
}
php_worker_run_flag = 1;
worker->state = phpq_init_script;
}
void php_worker_init_script (php_worker *worker) {
double timeout = worker->finish_time - precise_now - 0.01;
if (worker->terminate_flag || timeout < 0.2) {
worker->state = phpq_finish;
return;
}
/*if (http_sfd != -1) {*/
/*epoll_remove (http_sfd);*/
/*}*/
get_utime_monotonic();
worker->start_time = precise_now;
vkprintf (1, "START php script [req_id = %016llx]\n", worker->req_id);
immediate_stats.is_running = 1;
immediate_stats.is_wait_net = 0;
//init memory allocator for queries
qmem_init();
qresults_clean();
script_t *script = get_script ("#0");
dl_assert (script != NULL, "failed to get script");
if (php_script == NULL) {
php_script = php_script_create ((size_t)max_memory, (size_t)(8 << 20));
}
php_script_init (php_script, script, worker->data);
php_script_set_timeout (timeout);
worker->state = phpq_run;
}
void php_worker_terminate (php_worker *worker, int flag, const char *error_message) {
worker->terminate_flag = 1;
worker->error_message = error_message;
if (flag) {
vkprintf (0, "php_worker_terminate\n");
worker->conn = NULL;
}
}
void php_worker_run_query_x2 (php_worker *worker, php_query_x2_t *query) {
php_script_query_readed (php_script);
// worker->conn->status = conn_wait_net;
// worker->conn->pending_queries = 1;
static php_query_x2_answer_t res;
res.x2 = query->val * query->val;
query->base.ans = &res;
php_script_query_answered (php_script);
}
void php_worker_run_query_connect (php_worker *worker, php_query_connect_t *query) {
php_script_query_readed (php_script);
static php_query_connect_answer_t res;
switch (query->protocol) {
case p_memcached:
res.connection_id = get_target (query->host, query->port, &default_ct);
break;
case p_sql:
res.connection_id = sql_target_id;
break;
case p_rpc:
res.connection_id = get_target (query->host, query->port, &rpc_ct);
break;
default:
assert ("unknown protocol" && 0);
}
query->base.ans = &res;
php_script_query_answered (php_script);
}
extern conn_query_type_t mc_cq_func;
extern conn_query_type_t sql_cq_func;
struct conn_query *create_pnet_query (struct connection *http_conn, struct connection *mc_conn, net_ansgen_t *gen, double finish_time);
void create_pnet_delayed_query (struct connection *http_conn, struct conn_target *t, net_ansgen_t *gen, double finish_time);
int pnet_query_timeout (struct conn_query *q);
void net_error (net_ansgen_t *ansgen, php_query_base_t *query, const char *err) {
ansgen->func->error (ansgen, err);
query->ans = ansgen->ans;
ansgen->func->free (ansgen);
php_script_query_answered (php_script);
}
void php_worker_run_mc_query_packet (php_worker *worker, php_net_query_packet_t *query) {
query_stats.desc = "MC";
query_stats.query = query->data;
php_script_query_readed (php_script);
mc_ansgen_t *ansgen = mc_ansgen_packet_create();
ansgen->func->set_query_type (ansgen, query->extra_type);
net_ansgen_t *net_ansgen = (net_ansgen_t *)ansgen;
int connection_id = query->connection_id;
if (connection_id < 0 || connection_id >= MAX_TARGETS) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (1)");
return;
}
struct conn_target *target = &Targets[connection_id];
if (target == NULL) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (2)");
return;
}
net_ansgen->func->set_desc (net_ansgen, qmem_pstr ("[%s:%d]", inet_ntoa (target->target), target->port));
query_stats.port = target->port;
struct connection *conn = get_target_connection_force (target);
if (conn == NULL) {
net_error (net_ansgen, (php_query_base_t *)query, "Failed to establish connection [probably reconnect timeout is not expired]");
return;
}
if (conn->status != conn_connecting) {
write_out (&conn->Out, query->data, query->data_len);
MCC_FUNC (conn)->flush_query (conn);
} else {
if (conn->Tmp == NULL) {
conn->Tmp = alloc_head_buffer();
}
write_out (conn->Tmp, query->data, query->data_len);
}
double timeout = fix_timeout (query->timeout) + precise_now;
struct conn_query *cq = create_pnet_query (worker->conn, conn, (net_ansgen_t *)ansgen, timeout);
if (query->extra_type & PNETF_IMMEDIATE) {
pnet_query_timeout (cq);
} else if (worker->conn != NULL) {
worker->conn->status = conn_wait_net;
}
return;
}
void php_worker_run_sql_query_packet (php_worker *worker, php_net_query_packet_t *query) {
query_stats.desc = "SQL";
int connection_id = query->connection_id;
php_script_query_readed (php_script);
sql_ansgen_t *ansgen = sql_ansgen_packet_create();
net_ansgen_t *net_ansgen = (net_ansgen_t *)ansgen;
if (connection_id != sql_target_id) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (sql connection expected)");
return;
}
if (connection_id < 0 || connection_id >= MAX_TARGETS) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (1)");
return;
}
struct conn_target *target = &Targets[connection_id];
if (target == NULL) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (2)");
return;
}
net_ansgen->func->set_desc (net_ansgen, qmem_pstr ("[%s:%d]", inet_ntoa (target->target), target->port));
struct connection *conn = get_target_connection (target, 0);
double timeout = fix_timeout (query->timeout) + precise_now;
if (conn != NULL && conn->status == conn_ready) {
// assert (conn->type->check_ready (conn) == cr_ok);
write_out (&conn->Out, query->data, query->data_len);
SQLC_FUNC (conn)->sql_flush_packet (conn, query->data_len - 4);
flush_connection_output (conn);
conn->last_query_sent_time = precise_now;
conn->status = conn_wait_answer;
SQLC_DATA(conn)->response_state = resp_first;
ansgen->func->set_writer (ansgen, NULL);
ansgen->func->ready (ansgen, NULL);
create_pnet_query (worker->conn, conn, net_ansgen, timeout);
} else {
int new_conn_cnt = create_new_connections (target);
if (new_conn_cnt <= 0 && get_target_connection (target, 1) == NULL) {
net_error (net_ansgen, (php_query_base_t *)query, "Failed to establish connection [probably reconnect timeout is not expired]");
return;
}
ansgen->func->set_writer (ansgen, create_command_net_writer (query->data, query->data_len, &command_net_write_sql_base, -1));
create_pnet_delayed_query (worker->conn, target, net_ansgen, timeout);
}
if (worker->conn != NULL) {
worker->conn->status = conn_wait_net;
}
}
void php_worker_run_rpc_query_packet (php_worker *worker, php_net_query_packet_t *query) {
query_stats.desc = "RPC SEND";
int connection_id = query->connection_id;
php_script_query_readed (php_script);
//fprintf (stderr, "[len = %d]\n", query->data_len);
net_send_ansgen_t *ansgen = net_send_ansgen_create();
net_ansgen_t *net_ansgen = (net_ansgen_t *)ansgen;
vkprintf (4, "php_worker_run_rpc_query_packet [connection_id = %d]\n", connection_id);
if (connection_id < 0 || connection_id >= MAX_TARGETS) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (1)");
return;
}
struct conn_target *target = &Targets[connection_id];
vkprintf (4, "[target = %p]\n", target);
if (target == NULL) {
net_error (net_ansgen, (php_query_base_t *)query, "Invalid connection_id (2)");
return;
}
net_ansgen->func->set_desc (net_ansgen, qmem_pstr ("[%s:%d]", inet_ntoa (target->target), target->port));
query_stats.port = target->port;
struct connection *conn = get_target_connection (target, 0);
long long qres_id = create_qres_ans();
assert (qres_id != -1);
ansgen->qres_id = qres_id;
vkprintf (4, "php_worker_run_rpc_query_packet: create_rpc_query [conn = %p] [conn_status = %d] [data_len = %d] [rpc_id = <%lld>]\n",
conn, conn != NULL ? conn->status : -1, query->data_len, ansgen->qres_id);
qres_t *qres = get_qres (qres_id, qr_ans);
assert (qres != NULL);
double timeout = fix_timeout (query->timeout) + precise_now;
qres_set_timeout (qres, timeout);
if (conn != NULL) {
assert (conn->type->check_ready (conn) == cr_ok);
vkprintf (4, "php_worker_run_rpc_query_packet: send rpc query\n");
send_rpc_query (conn, RPC_INVOKE_REQ, qres_id, (int *)query->data, query->data_len);
flush_connection_output (conn);
conn->last_query_sent_time = precise_now;
ansgen->func->set_writer (ansgen, NULL);
ansgen->func->send_and_finish (ansgen, NULL);
query->base.ans = net_ansgen->ans;
net_ansgen->func->free (net_ansgen);
php_script_query_answered (php_script);
return;
} else {
int new_conn_cnt = create_new_connections (target);
vkprintf (4, "php_worker_run_rpc_query_packet: [new_conn_cnt = %d]\n", new_conn_cnt);
if (new_conn_cnt <= 0 && get_target_connection (target, 1) == NULL) {
net_error (net_ansgen, (php_query_base_t *)query, "Failed to establish connection [probably reconnect timeout is not expired]");
return;
}
ansgen->func->set_writer (ansgen, create_command_net_writer (query->data, query->data_len, &command_net_write_rpc_base, qres_id));
create_pnet_delayed_query (worker->conn, target, net_ansgen, timeout);
}
if (worker->conn != NULL) {
worker->conn->status = conn_wait_net;
}
}
void php_worker_run_get_query_packet (php_worker *worker, php_net_query_packet_t *query) {
php_script_query_readed (php_script);
if (query->protocol == p_get) {
query_stats.desc = "RPC GET";
} else if (query->protocol == p_get_id) {
query_stats.desc = "RPC GET ID";
}
//fprintf (stderr, "[len = %d]\n", query->data_len);
net_get_ansgen_t *ansgen = net_get_ansgen_create();
net_ansgen_t *net_ansgen = (net_ansgen_t *)ansgen;
//TODO: fix this hack
assert (query->data_len == sizeof (long long));
long long request_id = *(long long *)query->data;
ansgen->func->set_id (ansgen, request_id);
double timeout = ansgen->func->try_wait (ansgen, precise_now);
if (timeout <= 1) {
query->base.ans = net_ansgen->ans;
net_ansgen->func->free (net_ansgen);
php_script_query_answered (php_script);
return;
}
query_stats.timeout = timeout - precise_now;
create_pnet_query (worker->conn, &dummy_request_queue, net_ansgen, timeout);
cur_request_id = request_id;
//TODO: try to remove if
if (worker->conn != NULL) {
worker->conn->status = conn_wait_net;
}
}
void php_worker_run_net_query_packet (php_worker *worker, php_net_query_packet_t *query) {
switch (query->protocol) {
case p_memcached:
php_worker_run_mc_query_packet (worker, query);
break;
case p_sql:
php_worker_run_sql_query_packet (worker, query);
break;
case p_rpc:
php_worker_run_rpc_query_packet (worker, query);
break;
case p_get:
case p_get_id:
php_worker_run_get_query_packet (worker, query);
break;
default:
assert (0);
}
}
void php_worker_run_net_query (php_worker *worker, php_query_base_t *q_base) {
switch (q_base->type & 0xFFFF) {
case NETQ_PACKET:
php_worker_run_net_query_packet (worker, (php_net_query_packet_t *)q_base);
break;
default:
assert ("unknown net_query type"&& 0);
}
}
void prepare_rpc_query_raw (int packet_id, int *q, int qn) {
assert (sizeof (int) == 4);
q[0] = qn;
assert ((qn & 3) == 0);
qn >>= 2;
assert (qn >= 5);
q[1] = packet_id;
q[qn - 1] = (int)compute_crc32 (q, q[0] - 4);
}
void prepare_rpc_query (struct connection *c, int *q, int qn) {
prepare_rpc_query_raw (RPCS_DATA(c)->out_packet_num++, q, qn);
}
void send_rpc_query (struct connection *c, int op, long long id, int *q, int qn) {
q[2] = op;
if (id != -1) {
*(long long *)(q + 3) = id;
}
prepare_rpc_query (c, q, qn);
vkprintf (4, "send_rpc_query: [len = %d] [op = %08x] [rpc_id = <%lld>]\n", q[0], op, id);
assert (write_out (&c->Out, q, q[0]) == q[0]);
RPCS_FUNC(c)->flush_packet (c);
}
void php_worker_run_rpc_send_message (php_worker *worker, php_query_rpc_message *ans) {
if (worker->mode == rpc_worker) {
struct connection *c = worker->conn;
int *q = (int *)ans->data;
int qn = ans->data_len;
vkprintf (2, "going to send %d bytes to session [%016llx:%016llx]\n", qn, ans->auth_key_id, ans->session_id);
send_rpc_query (c, RPC_SEND_SESSION_MSG, ans->auth_key_id, q, qn);
}
php_script_query_readed (php_script);
php_script_query_answered (php_script);
}
void php_worker_run_rpc_answer_query (php_worker *worker, php_query_rpc_answer *ans) {
if (worker->mode == rpc_worker) {
struct connection *c = worker->conn;
int *q = (int *)ans->data;
int qn = ans->data_len;
vkprintf (2, "going to send %d bytes as an answer [req_id = %016llx]\n", qn, worker->req_id);
send_rpc_query (c, q[2] == 0 ? RPC_REQ_RESULT : RPC_REQ_ERROR, worker->req_id, q, qn);
}
php_script_query_readed (php_script);
php_script_query_answered (php_script);
}
void php_worker_create_queue (php_worker *worker, php_query_create_queue_t *query) {
php_script_query_readed (php_script);
static php_query_create_queue_answer_t res;
long long queue_id = create_qres_watchcat (query->request_ids, query->request_ids_len);
res.queue_id = queue_id;
query->base.ans = &res;
php_script_query_answered (php_script);
}
int php_worker_http_load_post_impl (php_worker *worker, char *buf, int min_len, int max_len) {
assert (worker != NULL);
struct connection *c = worker->conn;
double precise_now = get_utime_monotonic();
// fprintf (stderr, "Trying to load data of len [%d;%d] at %.6lf\n", min_len, max_len, precise_now - worker->start_time);
if (worker->finish_time < precise_now + 0.01) {
return -1;
}
if (c == NULL || c->error) {
return -1;
}
assert (!c->crypto);
assert (c->basic_type != ct_pipe);
assert (min_len <= max_len);
int read = 0;
int have_bytes = get_total_ready_bytes (&c->In);
if (have_bytes > 0) {
if (have_bytes > max_len) {
have_bytes = max_len;
}
assert (read_in (&c->In, buf, have_bytes) == have_bytes);
read += have_bytes;
}
struct pollfd poll_fds;
poll_fds.fd = c->fd;
poll_fds.events = POLLIN | POLLPRI;
while (read < min_len) {
precise_now = get_utime_monotonic();
double left_time = worker->finish_time - precise_now;
assert (left_time < 2000000.0);
if (left_time < 0.01) {
return -1;
}
int r = poll (&poll_fds, 1, (int)(left_time * 1000 + 1));
int err = errno;
if (r > 0) {
assert (r == 1);
r = recv (c->fd, buf + read, max_len - read, 0);
err = errno;
/*
if (r < 0) {
fprintf (stderr, "Error recv: %m\n");
} else {
fprintf (stderr, "Received %d bytes at %.6lf\n", r, precise_now - worker->start_time);
}
*/
if (r > 0) {
read += r;