FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rtpengine/daemon/redis.c at master · Robin-Rhee/rtpengine · GitHub
Robin-Rhee
rtpengine
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rtpengine
/
daemon
/
redis.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2958 lines (2470 loc) · 84.6 KB
Breadcrumbs
rtpengine
/
daemon
/
redis.c
Copy path
File metadata and controls
2958 lines (2470 loc) · 84.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include
"redis.h"
#include
<stdio.h>
#include
<hiredis/hiredis.h>
#include
<sys/types.h>
#include
<sys/time.h>
#include
<unistd.h>
#include
<glib.h>
#include
<stdarg.h>
#include
<ctype.h>
#include
<glib.h>
#include
<hiredis/hiredis.h>
#include
<hiredis/async.h>
#include
<hiredis/adapters/libevent.h>
#include
<event2/thread.h>
#include
<stdlib.h>
#include
<glib-object.h>
#include
<json-glib/json-glib.h>
#include
<inttypes.h>
#include
<stdbool.h>
#include
"compat.h"
#include
"helpers.h"
#include
"call.h"
#include
"log.h"
#include
"log_funcs.h"
#include
"str.h"
#include
"crypto.h"
#include
"dtls.h"
#include
"recording.h"
#include
"rtplib.h"
#include
"str.h"
#include
"ssrc.h"
#include
"main.h"
#include
"codec.h"
#include
"sdp.h"
typedef
union
{
GQueue
*
q
;
stream_fd_q
*
sfds_q
;
medias_arr
*
ma
;
sfd_intf_list_q
*
siq
;
packet_stream_q
*
psq
;
endpoint_map_q
*
emq
;
}
callback_arg_t
__attribute__ ((
__transparent_union__
));
struct
redis
*
rtpe_redis
;
struct
redis
*
rtpe_redis_write
;
struct
redis
*
rtpe_redis_write_disabled
;
struct
redis
*
rtpe_redis_notify
;
static
__thread
const
ng_parser_t
*
redis_parser
=
&
ng_parser_json
;
static
const
ng_parser_t
*
const
redis_format_parsers
[
__REDIS_FORMAT_MAX
]
=
{
&
ng_parser_native
,
&
ng_parser_json
,
};
INLINE
redisReply
*
redis_expect
(
int
type
,
redisReply
*
r
) {
if
(!
r
)
return
NULL
;
if
(
r
->
type
!=
type
) {
freeReplyObject
(
r
);
return
NULL
;
}
return
r
;
}
#if
__YCM
/* format checking in YCM editor */
INLINE
void
redis_pipe
(
struct
redis
*
r
,
const
char
*
fmt
, ...)
__attribute__((
format
(
printf
,
2
,
3
)));
INLINE
redisReply
*
redis_get
(
struct
redis
*
r
,
int
type
,
const
char
*
fmt
, ...)
__attribute__((
format
(
printf
,
3
,
4
)));
static
int
redisCommandNR
(
redisContext
*
r
,
const
char
*
fmt
, ...)
__attribute__((
format
(
printf
,
2
,
3
)));
#define
PB
"%.*s"
#define
PBSTR
(
x
) (int) (x)->len, (x)->s
#define
STR_R
(
x
) (int) (x)->len, (x)->str
#define
S_LEN
(
s
,
l
) (int) (l), (s)
#else
#define
PB
"%b"
#define
PBSTR
(
x
) (x)->s, (size_t) (x)->len
#define
STR_R
(
x
) (x)->str, (size_t) (x)->len
#define
S_LEN
(
s
,
l
) (s), (size_t) (l)
#endif
#define
REDIS_FMT
(
x
) (int) (x)->len, (x)->str
#define
rlog
(
l
,
x
...) ilog(l | LOG_FLAG_RESTORE, x)
// To protect against a restore race condition: Keyspace notifications are set up
// before existing calls are restored (restore_thread). Therefore the following
// scenario is possible:
// NOTIF THREAD: receives SET, creates call
// RESTORE THREAD: executes KEYS *
// NOTIF THREAD: receives another SET:
// NOTIF THREAD: does call_destroy(), which:
// adds ports to late-release list
// RESTORE THREAD: comes across call ID, does GET
// RESTORE THREAD: creates new call
// RESTORE THREAD: wants to allocate ports, but they're still in use
// NOTIF THREAD: now does release_closed_sockets()
static
mutex_t
redis_ports_release_lock
=
MUTEX_STATIC_INIT
;
static
cond_t
redis_ports_release_cond
=
COND_STATIC_INIT
;
static
int
redis_ports_release_balance
=
0
;
// negative = releasers, positive = allocators
static
int
redis_check_conn
(
struct
redis
*
r
);
static
void
json_restore_call
(
struct
redis
*
r
,
const
str
*
id
,
bool
foreign
);
static
int
redis_connect
(
struct
redis
*
r
,
int
wait
,
bool
resolve
);
static
int
json_build_ssrc
(
struct
call_media
*
,
parser_arg
arg
);
static
void
redis_do_delete
(
call_t
*
c
,
struct
redis
*
r
);
// mutually exclusive multi-A multi-B lock
// careful with deadlocks against redis->lock
static
void
redis_ports_release_push
(
bool
inc
) {
LOCK
(
&
redis_ports_release_lock
);
if
(
inc
) {
while
(
redis_ports_release_balance
<
0
)
cond_wait
(
&
redis_ports_release_cond
,
&
redis_ports_release_lock
);
}
else
{
while
(
redis_ports_release_balance
>
0
)
cond_wait
(
&
redis_ports_release_cond
,
&
redis_ports_release_lock
);
}
redis_ports_release_balance
+=
(
inc
?
1
:
-1
);
}
static
void
redis_ports_release_pop
(
bool
inc
) {
LOCK
(
&
redis_ports_release_lock
);
redis_ports_release_balance
-=
(
inc
?
1
:
-1
);
if
(
redis_ports_release_balance
==
0
)
cond_broadcast
(
&
redis_ports_release_cond
);
}
static
void
redis_pipe
(
struct
redis
*
r
,
const
char
*
fmt
, ...) {
va_list
ap
;
if
(!
r
->
ctx
) {
ilog
(
LOG_ERROR
,
"Unable to pipe redis command. No redis context"
);
return
;
}
va_start
(
ap
,
fmt
);
redisvAppendCommand
(
r
->
ctx
,
fmt
,
ap
);
va_end
(
ap
);
r
->
pipeline
++
;
}
static
redisReply
*
redis_get
(
struct
redis
*
r
,
int
type
,
const
char
*
fmt
, ...) {
va_list
ap
;
redisReply
*
ret
;
if
(!
r
->
ctx
) {
ilog
(
LOG_ERROR
,
"Unable to get redis reply. No redis context"
);
return
NULL
;
}
va_start
(
ap
,
fmt
);
ret
=
redis_expect
(
type
,
redisvCommand
(
r
->
ctx
,
fmt
,
ap
));
va_end
(
ap
);
return
ret
;
}
static
int
redisCommandNR
(
redisContext
*
r
,
const
char
*
fmt
, ...) {
va_list
ap
;
redisReply
*
ret
;
int
i
=
0
;
if
(!
r
) {
ilog
(
LOG_ERROR
,
"Unable to send redis command. No redis context"
);
return
-1
;
}
va_start
(
ap
,
fmt
);
ret
=
redisvCommand
(
r
,
fmt
,
ap
);
va_end
(
ap
);
if
(!
ret
)
return
-1
;
if
(
ret
->
type
==
REDIS_REPLY_ERROR
) {
i
=
-1
;
ilog
(
LOG_WARNING
,
"Redis returned error to command '%s': %s"
,
fmt
,
ret
->
str
);
}
freeReplyObject
(
ret
);
return
i
;
}
/* called with r->lock held */
static
int
redis_check_type
(
struct
redis
*
r
,
char
*
key
,
char
*
suffix
,
char
*
type
) {
redisReply
*
rp
;
if
(!
r
->
ctx
) {
ilog
(
LOG_ERROR
,
"Unable to check redis reply type. No redis context"
);
return
-1
;
}
rp
=
redisCommand
(
r
->
ctx
,
"TYPE %s%s"
,
key
,
suffix
? :
""
);
if
(!
rp
)
return
-1
;
if
(
rp
->
type
!=
REDIS_REPLY_STATUS
) {
freeReplyObject
(
rp
);
return
-1
;
}
if
(
strcmp
(
rp
->
str
,
type
)
&&
strcmp
(
rp
->
str
,
"none"
))
redisCommandNR
(
r
->
ctx
,
"DEL %s%s"
,
key
,
suffix
? :
""
);
freeReplyObject
(
rp
);
return
0
;
}
/* called with r->lock held */
static
void
redis_consume
(
struct
redis
*
r
) {
redisReply
*
rp
;
if
(!
r
->
ctx
) {
ilog
(
LOG_ERROR
,
"Unable to consume pipelined replies. No redis context"
);
r
->
pipeline
=
0
;
return
;
}
while
(
r
->
pipeline
) {
if
(
redisGetReply
(
r
->
ctx
, (
void
*
*
)
&
rp
)
==
REDIS_OK
)
freeReplyObject
(
rp
);
r
->
pipeline
--
;
}
}
int
redis_set_timeout
(
struct
redis
*
r
,
int64_t
timeout
) {
struct
timeval
tv_cmd
;
if
(!
timeout
)
return
0
;
tv_cmd
=
timeval_from_us
(
timeout
);
if
(
redisSetTimeout
(
r
->
ctx
,
tv_cmd
))
return
-1
;
ilog
(
LOG_INFO
,
"Setting timeout for Redis commands to %"
PRId64
" milliseconds"
,
timeout
);
return
0
;
}
int
redis_reconnect
(
struct
redis
*
r
) {
int
rval
;
LOCK
(
&
r
->
lock
);
rval
=
redis_connect
(
r
,
1
,
r
->
update_resolve
);
if
(
rval
)
r
->
state
=
REDIS_STATE_DISCONNECTED
;
return
rval
;
}
// struct must be locked or single thread
static
int
redis_select_db
(
struct
redis
*
r
,
int
db
) {
if
(
db
==
r
->
current_db
)
return
0
;
if
(
redisCommandNR
(
r
->
ctx
,
"SELECT %i"
,
db
))
return
-1
;
r
->
current_db
=
db
;
return
0
;
}
void
redis_set_keepalive
(
int
fd
) {
if
(
rtpe_config
.
redis_tcp_keepalive_time
>
0
) {
int
keepalive_en
=
1
;
setsockopt
(
fd
,
SOL_SOCKET
,
SO_KEEPALIVE
,
&
keepalive_en
,
sizeof
(
keepalive_en
));
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_KEEPIDLE
,
&
rtpe_config
.
redis_tcp_keepalive_time
,
sizeof
(
rtpe_config
.
redis_tcp_keepalive_time
));
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_KEEPINTVL
,
&
rtpe_config
.
redis_tcp_keepalive_intvl
,
sizeof
(
rtpe_config
.
redis_tcp_keepalive_intvl
));
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_KEEPCNT
,
&
rtpe_config
.
redis_tcp_keepalive_probes
,
sizeof
(
rtpe_config
.
redis_tcp_keepalive_probes
));
}
}
/* called with r->lock held if necessary */
static
int
redis_connect
(
struct
redis
*
r
,
int
wait
,
bool
resolve
) {
struct
timeval
tv
;
redisReply
*
rp
;
char
*
s
;
int64_t
cmd_timeout
,
connect_timeout
;
sockaddr_t
a
;
if
(
r
->
ctx
)
redisFree
(
r
->
ctx
);
r
->
ctx
=
NULL
;
r
->
current_db
=
-1
;
connect_timeout
=
atomic_get_na
(
&
rtpe_config
.
redis_connect_timeout
)
*
1000LL
;
cmd_timeout
=
atomic_get_na
(
&
rtpe_config
.
redis_cmd_timeout
)
*
1000LL
;
tv
=
timeval_from_us
(
connect_timeout
);
/* re-resolve if asked */
if
(
resolve
&&
r
->
hostname
) {
if
(!
sockaddr_getaddrinfo
(
&
a
,
r
->
hostname
))
ilog
(
LOG_WARN
,
"Failed to re-resolve remote server hostname: '%s'. Just use older one: '%s'."
,
r
->
hostname
,
r
->
host
);
else
{
sockaddr_print
(
&
a
,
r
->
host
,
sizeof
(
r
->
host
));
r
->
endpoint
.
address
=
a
;
}
}
r
->
ctx
=
redisConnectWithTimeout
(
r
->
host
,
r
->
endpoint
.
port
,
tv
);
if
(!
r
->
ctx
)
goto
err
;
if
(
r
->
ctx
->
err
)
goto
err2
;
redis_set_keepalive
(
r
->
ctx
->
fd
);
if
(
redis_set_timeout
(
r
,
cmd_timeout
))
goto
err2
;
if
(
r
->
auth
) {
if
(
redisCommandNR
(
r
->
ctx
,
"AUTH %s"
,
r
->
auth
))
goto
err2
;
}
else
{
if
(
redisCommandNR
(
r
->
ctx
,
"PING"
))
goto
err2
;
}
if
(
redis_select_db
(
r
,
r
->
db
))
goto
err2
;
while
(
wait
--
>=
0
) {
ilog
(
LOG_INFO
,
"Asking Redis whether it's master or slave..."
);
rp
=
redisCommand
(
r
->
ctx
,
"INFO"
);
if
(!
rp
) {
goto
err2
;
}
s
=
strstr
(
rp
->
str
,
"role:"
);
if
(!
s
) {
goto
err3
;
}
if
(!
memcmp
(
s
,
"role:master"
,
11
)
||
!
memcmp
(
s
,
"role:active-replica"
,
19
)) {
if
(
r
->
role
==
MASTER_REDIS_ROLE
||
r
->
role
==
ANY_REDIS_ROLE
) {
ilog
(
LOG_INFO
,
"Connected to Redis %s in master mode"
,
endpoint_print_buf
(
&
r
->
endpoint
));
goto
done
;
}
else
if
(
r
->
role
==
SLAVE_REDIS_ROLE
) {
ilog
(
LOG_INFO
,
"Connected to Redis %s in master mode, but wanted mode is slave; retrying..."
,
endpoint_print_buf
(
&
r
->
endpoint
));
goto
next
;
}
}
else
if
(!
memcmp
(
s
,
"role:slave"
,
10
)) {
if
(
r
->
role
==
SLAVE_REDIS_ROLE
||
r
->
role
==
ANY_REDIS_ROLE
) {
ilog
(
LOG_INFO
,
"Connected to Redis %s in slave mode"
,
endpoint_print_buf
(
&
r
->
endpoint
));
goto
done
;
}
else
if
(
r
->
role
==
MASTER_REDIS_ROLE
) {
ilog
(
LOG_INFO
,
"Connected to Redis %s in slave mode, but wanted mode is master; retrying..."
,
endpoint_print_buf
(
&
r
->
endpoint
));
goto
next
;
}
}
else
{
goto
err3
;
}
next
:
freeReplyObject
(
rp
);
usleep
(
1000000
);
}
goto
err2
;
done
:
freeReplyObject
(
rp
);
redis_check_type
(
r
,
"calls"
,
NULL
,
"set"
);
return
0
;
err3
:
freeReplyObject
(
rp
);
err2
:
if
(
r
->
ctx
->
err
) {
rlog
(
LOG_ERR
,
"Failed to connect to Redis %s, error: %s"
,
endpoint_print_buf
(
&
r
->
endpoint
),
r
->
ctx
->
errstr
);
return
-1
;
}
redisFree
(
r
->
ctx
);
r
->
ctx
=
NULL
;
err
:
rlog
(
LOG_ERR
,
"Failed to connect to Redis %s"
,
endpoint_print_buf
(
&
r
->
endpoint
));
return
-1
;
}
void
on_redis_notification
(
redisAsyncContext
*
actx
,
void
*
reply
,
void
*
privdata
) {
struct
redis
*
r
=
0
;
call_t
*
c
=
NULL
;
str
callid
;
str
keyspace_id
;
if
(!
rtpe_redis_notify
) {
rlog
(
LOG_ERROR
,
"A redis notification has been received but no redis_notify database found"
);
return
;
}
r
=
rtpe_redis_notify
;
mutex_lock
(
&
r
->
lock
);
redisReply
*
rr
=
(
redisReply
*
)
reply
;
if
(
reply
==
NULL
||
rr
->
type
!=
REDIS_REPLY_ARRAY
)
goto
err
;
for
(
int
j
=
0
;
j
<
rr
->
elements
;
j
++
) {
rlog
(
LOG_DEBUG
,
"Redis-Notify: %u) %s%s%s\n"
,
j
,
FMT_M
(
rr
->
element
[
j
]
->
str
));
}
if
(
rr
->
elements
!=
4
)
goto
err
;
// format: __keyspace@<db>__:<key>
keyspace_id
=
STR_LEN
(
rr
->
element
[
2
]
->
str
,
rr
->
element
[
2
]
->
len
);
if
(
str_shift_cmp
(
&
keyspace_id
,
"__keyspace@"
))
goto
err
;
// extract <db>
char
*
endp
;
r
->
db
=
strtoul
(
keyspace_id
.
s
,
&
endp
,
10
);
if
(
endp
==
keyspace_id
.
s
||
*
endp
!=
'_'
)
goto
err
;
if
(
str_shift
(
&
keyspace_id
,
endp
-
keyspace_id
.
s
+
3
))
goto
err
;
if
(
keyspace_id
.
s
[
-1
]
!=
':'
)
goto
err
;
// now at <key>
callid
=
keyspace_id
;
if
(
redis_check_conn
(
r
)
==
REDIS_STATE_DISCONNECTED
)
goto
err
;
// select the right db for restoring the call
if
(
redis_select_db
(
r
,
r
->
db
)) {
if
(
r
->
ctx
&&
r
->
ctx
->
err
)
rlog
(
LOG_ERROR
,
"Redis error: %s"
,
r
->
ctx
->
errstr
);
redisFree
(
r
->
ctx
);
r
->
ctx
=
NULL
;
goto
err
;
}
if
(
strncmp
(
rr
->
element
[
3
]
->
str
,
"set"
,
3
)
==
0
) {
c
=
call_get
(
&
callid
);
if
(
c
) {
rwlock_unlock_w
(
&
c
->
master_lock
);
if
(
IS_FOREIGN_CALL
(
c
)) {
c
->
redis_hosted_db
=
rtpe_redis_write
->
db
;
// don't delete from foreign DB
// redis_notify->lock is held
redis_ports_release_push
(true);
redis_do_delete
(
c
,
rtpe_redis_write
);
call_destroy
(
c
);
release_closed_sockets
();
redis_ports_release_pop
(true);
}
else
{
rlog
(
LOG_WARN
,
"Redis-Notifier: Ignoring SET received for OWN call: "
STR_FORMAT
"\n"
,
STR_FMT
(
&
callid
));
goto
err
;
}
}
redis_select_db
(
r
,
r
->
db
);
mutex_unlock
(
&
r
->
lock
);
// unlock before restoring calls to avoid deadlock in case err happens
json_restore_call
(
r
,
&
callid
, true);
mutex_lock
(
&
r
->
lock
);
}
if
(
strncmp
(
rr
->
element
[
3
]
->
str
,
"del"
,
3
)
==
0
) {
c
=
call_get
(
&
callid
);
if
(!
c
) {
rlog
(
LOG_NOTICE
,
"Redis-Notifier: DEL did not find call with callid: "
STR_FORMAT
"\n"
,
STR_FMT
(
&
callid
));
goto
err
;
}
rwlock_unlock_w
(
&
c
->
master_lock
);
if
(!
IS_FOREIGN_CALL
(
c
)) {
rlog
(
LOG_WARN
,
"Redis-Notifier: Ignoring DEL received for an OWN call: "
STR_FORMAT
"\n"
,
STR_FMT
(
&
callid
));
goto
err
;
}
// redis_notify->lock is held
redis_ports_release_push
(true);
call_destroy
(
c
);
release_closed_sockets
();
redis_ports_release_pop
(true);
}
err
:
if
(
c
)
// because of call_get(..)
obj_put
(
c
);
mutex_unlock
(
&
r
->
lock
);
release_closed_sockets
();
log_info_reset
();
}
void
redis_delete_async_context_connect
(
const
redisAsyncContext
*
redis_delete_async_context
,
int
status
) {
if
(
status
==
REDIS_ERR
) {
rtpe_redis_write
->
async_ctx
=
NULL
;
if
(
redis_delete_async_context
->
errstr
) {
rlog
(
LOG_ERROR
,
"redis_delete_async_context_connect error %d: %s"
,
redis_delete_async_context
->
err
,
redis_delete_async_context
->
errstr
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_delete_async_context_connect error %d: no errstr"
,
redis_delete_async_context
->
err
);
}
}
else
if
(
status
==
REDIS_OK
) {
rlog
(
LOG_NOTICE
,
"redis_delete_async_context_connect initiated by user"
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_delete_async_context_connect invalid status code %d"
,
status
);
}
}
void
redis_delete_async_context_disconnect
(
const
redisAsyncContext
*
redis_delete_async_context
,
int
status
) {
rtpe_redis_write
->
async_ctx
=
NULL
;
if
(
status
==
REDIS_ERR
) {
if
(
redis_delete_async_context
->
errstr
) {
rlog
(
LOG_ERROR
,
"redis_delete_async_context_disconnect error %d: %s"
,
redis_delete_async_context
->
err
,
redis_delete_async_context
->
errstr
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_delete_async_context_disconnect error %d: no errstr"
,
redis_delete_async_context
->
err
);
}
}
else
if
(
status
==
REDIS_OK
) {
rlog
(
LOG_NOTICE
,
"redis_delete_async_context_disconnect initiated by user"
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_delete_async_context_disconnect invalid status code %d"
,
status
);
}
}
void
redis_notify_async_context_disconnect
(
const
redisAsyncContext
*
redis_notify_async_context
,
int
status
) {
if
(
status
==
REDIS_ERR
) {
if
(
redis_notify_async_context
->
errstr
) {
rlog
(
LOG_ERROR
,
"redis_notify_async_context_disconnect error %d on context free: %s"
,
redis_notify_async_context
->
err
,
redis_notify_async_context
->
errstr
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_notify_async_context_disconnect error %d on context free: no errstr"
,
redis_notify_async_context
->
err
);
}
}
else
if
(
status
==
REDIS_OK
) {
rlog
(
LOG_NOTICE
,
"redis_notify_async_context_disconnect initiated by user"
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_notify_async_context_disconnect invalid status code %d"
,
status
);
}
}
// connect_cb = connect callback, disconnect_cb = disconnect callback
int
redis_async_context_alloc
(
struct
redis
*
r
,
void
*
connect_cb
,
void
*
disconnect_cb
) {
// sanity checks
if
(!
r
) {
rlog
(
LOG_ERROR
,
"redis_async_context_alloc: NULL r"
);
return
-1
;
}
else
{
rlog
(
LOG_DEBUG
,
"redis_async_context_alloc: Use Redis %s"
,
endpoint_print_buf
(
&
r
->
endpoint
));
}
// alloc async context
r
->
async_ctx
=
redisAsyncConnect
(
r
->
host
,
r
->
endpoint
.
port
);
if
(!
r
->
async_ctx
) {
rlog
(
LOG_ERROR
,
"redis_async_context_alloc: can't create new"
);
return
-1
;
}
if
(
r
->
async_ctx
->
err
) {
rlog
(
LOG_ERROR
,
"redis_async_context_alloc: can't create new error: %s"
,
r
->
async_ctx
->
errstr
);
return
-1
;
}
redis_set_keepalive
(
r
->
async_ctx
->
c
.
fd
);
// callbacks async context
if
(
redisAsyncSetConnectCallback
(
r
->
async_ctx
,
connect_cb
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"redis_async_context_alloc: can't set connect callback"
);
return
-1
;
}
if
(
redisAsyncSetDisconnectCallback
(
r
->
async_ctx
,
disconnect_cb
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"redis_async_context_alloc: can't set disconnect callback"
);
return
-1
;
}
rlog
(
LOG_DEBUG
,
"redis_async_context_alloc: Success"
);
return
0
;
}
int
redis_async_event_base_action
(
struct
redis
*
r
,
enum
event_base_action
action
) {
// sanity checks
if
(!
r
) {
rlog
(
LOG_ERR
,
"redis_async_event_base_action: NULL r"
);
return
-1
;
}
else
{
rlog
(
LOG_DEBUG
,
"redis_async_event_base_action: Use Redis %s"
,
endpoint_print_buf
(
&
r
->
endpoint
));
}
if
(!
r
->
async_ev
&&
action
!=
EVENT_BASE_ALLOC
) {
rlog
(
LOG_NOTICE
,
"redis_async_event_base_action: async_ev is NULL on event base action %d"
,
action
);
return
-1
;
}
// exec event base action
switch
(
action
) {
case
EVENT_BASE_ALLOC
:
r
->
async_ev
=
event_base_new
();
if
(!
r
->
async_ev
) {
rlog
(
LOG_ERROR
,
"redis_async_event_base_action: Fail alloc async_ev"
);
return
-1
;
}
else
{
rlog
(
LOG_DEBUG
,
"redis_async_event_base_action: Success alloc async_ev"
);
}
break
;
case
EVENT_BASE_FREE
:
event_base_free
(
r
->
async_ev
);
rlog
(
LOG_DEBUG
,
"redis_async_event_base_action: Success free async_ev"
);
break
;
case
EVENT_BASE_LOOPBREAK
:
if
(
event_base_loopbreak
(
r
->
async_ev
)) {
rlog
(
LOG_ERROR
,
"redis_async_event_base_action: Fail loopbreak async_ev"
);
return
-1
;
}
else
{
rlog
(
LOG_DEBUG
,
"redis_async_event_base_action: Success loopbreak async_ev"
);
}
break
;
default
:
rlog
(
LOG_ERROR
,
"redis_async_event_base_action: No event base action found: %d"
,
action
);
return
-1
;
}
return
0
;
}
int
redis_notify_subscribe_action
(
struct
redis
*
r
,
enum
subscribe_action
action
,
int
keyspace
) {
if
(!
r
->
async_ctx
) {
rlog
(
LOG_ERROR
,
"redis_notify_async_context is NULL on subscribe action"
);
return
-1
;
}
if
(
r
->
async_ctx
->
err
) {
rlog
(
LOG_ERROR
,
"redis_notify_async_context error on subscribe action: %s"
,
r
->
async_ctx
->
errstr
);
return
-1
;
}
switch
(
action
) {
case
SUBSCRIBE_KEYSPACE
:
if
(
redisAsyncCommand
(
r
->
async_ctx
,
on_redis_notification
,
NULL
,
"psubscribe __keyspace@%i__:*"
,
keyspace
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"Fail redisAsyncCommand on JSON SUBSCRIBE_KEYSPACE"
);
return
-1
;
}
break
;
case
UNSUBSCRIBE_KEYSPACE
:
if
(
redisAsyncCommand
(
r
->
async_ctx
,
on_redis_notification
,
NULL
,
"punsubscribe __keyspace@%i__:*"
,
keyspace
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"Fail redisAsyncCommand on JSON UNSUBSCRIBE_KEYSPACE"
);
return
-1
;
}
break
;
case
UNSUBSCRIBE_ALL
:
if
(
redisAsyncCommand
(
r
->
async_ctx
,
on_redis_notification
,
NULL
,
"punsubscribe"
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"Fail redisAsyncCommand on JSON UNSUBSCRIBE_ALL"
);
return
-1
;
}
break
;
default
:
rlog
(
LOG_ERROR
,
"No subscribe action found: %d"
,
action
);
return
-1
;
}
return
0
;
}
static
int
redis_delete_async
(
struct
redis
*
r
) {
// sanity checks
if
(!
r
) {
rlog
(
LOG_ERROR
,
"redis_delete_async: Don't use Redis async deletions because no redis/redis_write."
);
return
-1
;
}
// alloc new redis async context
if
(
r
->
async_ctx
==
NULL
&&
redis_async_context_alloc
(
r
,
redis_delete_async_context_connect
,
redis_delete_async_context_disconnect
)
<
0
) {
r
->
async_ctx
=
NULL
;
rlog
(
LOG_ERROR
,
"redis_delete_async: Failed to alloc async_ctx"
);
return
-1
;
}
// attach event base
if
(
redisLibeventAttach
(
r
->
async_ctx
,
r
->
async_ev
)
==
REDIS_ERR
) {
if
(
r
->
async_ctx
->
err
) {
rlog
(
LOG_ERROR
,
"redis_delete_async: redis_delete_async_context can't attach event base error: %s"
,
r
->
async_ctx
->
errstr
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_delete_async: redis_delete_async_context can't attach event base"
);
}
return
-1
;
}
// commands
if
(
r
->
auth
) {
if
(
redisAsyncCommand
(
r
->
async_ctx
,
NULL
,
NULL
,
"AUTH %s"
,
r
->
auth
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"redis_delete_async: Fail redisAsyncCommand on AUTH"
);
return
-1
;
}
}
else
{
if
(
redisAsyncCommand
(
r
->
async_ctx
,
NULL
,
NULL
,
"PING"
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"redis_delete_async: Fail redisAsyncCommand on PING"
);
return
-1
;
}
}
// delete commands
gchar
*
redis_command
;
gint
redis_command_total
=
0
;
mutex_lock
(
&
r
->
async_lock
);
while
(!
g_queue_is_empty
(
&
r
->
async_queue
)) {
redis_command_total
++
;
redis_command
=
g_queue_pop_head
(
&
r
->
async_queue
);
if
(
redisAsyncCommand
(
r
->
async_ctx
,
NULL
,
NULL
,
redis_command
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"redis_delete_async: Fail redisAsyncCommand on DELETE"
);
}
g_free
(
redis_command
);
}
mutex_unlock
(
&
r
->
async_lock
);
rlog
(
LOG_NOTICE
,
"redis_delete_async: Queued DELETE redisAsyncCommand total: %d"
,
redis_command_total
);
// dispatch event base => thread blocks here
if
(
event_base_dispatch
(
r
->
async_ev
)
<
0
) {
rlog
(
LOG_ERROR
,
"redis_delete_async: Fail event_base_dispatch()"
);
return
-1
;
}
// loopbreak
redisAsyncDisconnect
(
r
->
async_ctx
);
r
->
async_ctx
=
NULL
;
return
0
;
}
static
int
redis_notify
(
struct
redis
*
r
) {
GList
*
l
;
if
(!
r
) {
rlog
(
LOG_ERROR
,
"redis_notify database is NULL on redis_notify()"
);
return
-1
;
}
if
(!
r
->
async_ctx
) {
rlog
(
LOG_ERROR
,
"redis_notify_async_context is NULL on redis_notify()"
);
return
-1
;
}
if
(!
r
->
async_ev
) {
rlog
(
LOG_ERROR
,
"redis_notify_event_base is NULL on redis_notify()"
);
return
-1
;
}
// get redis_notify database
rlog
(
LOG_INFO
,
"Use Redis %s to subscribe to notifications"
,
endpoint_print_buf
(
&
r
->
endpoint
));
// attach event base
if
(
redisLibeventAttach
(
r
->
async_ctx
,
r
->
async_ev
)
==
REDIS_ERR
) {
if
(
r
->
async_ctx
->
err
) {
rlog
(
LOG_ERROR
,
"redis_notify_async_context can't attach event base error: %s"
,
r
->
async_ctx
->
errstr
);
}
else
{
rlog
(
LOG_ERROR
,
"redis_notify_async_context can't attach event base"
);
}
return
-1
;
}
if
(
r
->
auth
) {
if
(
redisAsyncCommand
(
r
->
async_ctx
,
on_redis_notification
,
NULL
,
"AUTH %s"
,
r
->
auth
)
!=
REDIS_OK
) {
rlog
(
LOG_ERROR
,
"Fail redisAsyncCommand on AUTH"
);
return
-1
;
}
}
// subscribe to the values in the configured keyspaces
rwlock_lock_r
(
&
rtpe_config
.
keyspaces_lock
);
for
(
l
=
rtpe_config
.
redis_subscribed_keyspaces
.
head
;
l
;
l
=
l
->
next
) {
int
id
=
GPOINTER_TO_INT
(
l
->
data
);
if
(
id
<
0
)
continue
;
redis_notify_subscribe_action
(
r
,
SUBSCRIBE_KEYSPACE
,
id
);
}
rwlock_unlock_r
(
&
rtpe_config
.
keyspaces_lock
);
// dispatch event base => thread blocks here
if
(
event_base_dispatch
(
r
->
async_ev
)
<
0
) {
rlog
(
LOG_ERROR
,
"Fail event_base_dispatch()"
);
return
-1
;
}
return
0
;
}
void
redis_delete_async_loop
(
void
*
d
) {
struct
redis
*
r
=
NULL
;
// sanity checks
r
=
rtpe_redis_write
;
if
(!
r
) {
rlog
(
LOG_ERROR
,
"redis_delete_async_loop: Don't use Redis async deletions because no redis/redis_write."
);
return
;
}
r
->
async_last
=
rtpe_now
;
// init libevent for pthread usage
if
(
evthread_use_pthreads
()
<
0
) {
ilog
(
LOG_ERROR
,
"redis_delete_async_loop: evthread_use_pthreads failed."
);
return
;
}
// alloc libevent base
if
(
redis_async_event_base_action
(
r
,
EVENT_BASE_ALLOC
)
<
0
) {
rlog
(
LOG_ERROR
,
"redis_delete_async_loop: Failed to EVENT_BASE_ALLOC."
);
return
;
}
// loop (almost) forever
while
(!
rtpe_shutdown
) {
redis_delete_async
(
r
);
sleep
(
1
);
}
}
void
redis_notify_loop
(
void
*
d
) {
int
redis_notify_return
=
0
;
const
int64_t
microseconds
=
1000000L
;
int64_t
next_run
=
rtpe_now
;
struct
redis
*
r
;
r
=
rtpe_redis_notify
;
if
(!
r
) {
rlog
(
LOG_ERROR
,
"Don't use Redis notifications. See --redis-notifications parameter."
);
return
;
}
// init libevent for pthread usage
if
(
evthread_use_pthreads
()
<
0
) {
ilog
(
LOG_ERROR
,
"evthread_use_pthreads failed"
);
return
;
}
// alloc redis async context
if
(
redis_async_context_alloc
(
r
,
NULL
,
redis_notify_async_context_disconnect
)
<
0
) {
return
;
}
// alloc event base
if
(
redis_async_event_base_action
(
r
,
EVENT_BASE_ALLOC
)
<
0
) {
return
;
}
// initial redis_notify
if
(
redis_check_conn
(
r
)
==
REDIS_STATE_CONNECTED
) {
redis_notify_return
=
redis_notify
(
r
);
}
// loop redis_notify => in case of lost connection
while
(!
rtpe_shutdown
) {
rtpe_now
=
now_us
();
if
(
rtpe_now
<
next_run
) {
usleep
(
100000
);
continue
;
}
next_run
=
rtpe_now
+
microseconds
;
if
(
redis_check_conn
(
r
)
==
REDIS_STATE_CONNECTED
||
redis_notify_return
<
0
) {
r
->
async_ctx
=
NULL
;
// alloc new redis async context upon redis breakdown
if
(
redis_async_context_alloc
(
r
,
NULL
,
redis_notify_async_context_disconnect
)
<
0
) {
continue
;
}
// prepare notifications
redis_notify_return
=
redis_notify
(
r
);
}
}
if
(
r
->
state
==
REDIS_STATE_CONNECTED
) {
// unsubscribe notifications
redis_notify_subscribe_action
(
r
,
UNSUBSCRIBE_ALL
,
0
);
// free async context
redisAsyncDisconnect
(
r
->
async_ctx
);
r
->
async_ctx
=
NULL
;
}
}
struct
redis
*
redis_new
(
const
endpoint_t
*
ep
,
int
db
,
const
char
*
hostname
,
const
char
*
auth
,
enum
redis_role
role
,
int
no_redis_required
,
bool
update_resolve
) {
struct
redis
*
r
;
r
=
g_new0
(
struct
redis
,
1
);
r
->
endpoint
=
*
ep
;
sockaddr_print
(
&
ep
->
address
,
r
->
host
,
sizeof
(
r
->
host
));
r
->
db
=
db
;
r
->
auth
=
auth
;
r
->
hostname
=
hostname
;
r
->
role
=
role
;
r
->
state
=
REDIS_STATE_DISCONNECTED
;
r
->
no_redis_required
=
no_redis_required
;
r
->
restore_tick_us
=
0
;
r
->
consecutive_errors
=
0
;
r
->
update_resolve
=
update_resolve
;
mutex_init
(
&
r
->
lock
);
if
(
redis_connect
(
r
,
10
, false)) {
if
(
r
->
no_redis_required
) {
rlog
(
LOG_WARN
,
"Starting with no initial connection to Redis %s !"
,
endpoint_print_buf
(
&
r
->
endpoint
));
return
r
;
}
goto
err
;
}
// redis is connected
rlog
(
LOG_INFO
,
"Established initial connection to Redis %s"
,
endpoint_print_buf
(
&
r
->
endpoint
));
r
->
state
=
REDIS_STATE_CONNECTED
;
return
r
;
err
:
mutex_destroy
(
&
r
->
lock
);
g_free
(
r
);
return
NULL
;
}
struct
redis
*
redis_dup
(
const
struct
redis
*
r
,
int
db
) {
return
redis_new
(
&
r
->
endpoint
,
(
db
>=
0
?
db
:
r
->
db
),
r
->
hostname
,
r
->
auth
,
r
->
role
,
r
->
no_redis_required
,
r
->
update_resolve
);
}
void
redis_close
(
struct
redis
*
r
) {
if
(!
r
)
return
;
if
(
r
->
ctx
)
redisFree
(
r
->
ctx
);
r
->
ctx
=
NULL
;
mutex_destroy
(
&
r
->
lock
);
g_free
(
r
);
}
static
void
redis_count_err_and_disable
(
struct
redis
*
r
)
{
int
allowed_errors
;
int64_t
disable_time_us
;
allowed_errors
=
atomic_get_na
(
&
rtpe_config
.
redis_allowed_errors
);
disable_time_us
=
atomic_get_na
(
&
rtpe_config
.
redis_disable_time_us
);
if
(
allowed_errors
<
0
) {
return
;
}
r
->
consecutive_errors
++
;
if
(
r
->
consecutive_errors
>
allowed_errors
) {
r
->
restore_tick_us
=
rtpe_now
+
disable_time_us
;
ilog
(
LOG_WARNING
,
"Redis server %s disabled for %"
PRId64
" seconds"
,
endpoint_print_buf
(
&
r
->
endpoint
),
disable_time_us
/
1000000L
);
}
}
/* must be called with r->lock held */
static
int
redis_check_conn
(
struct
redis
*
r
) {
rtpe_now
=
now_us
();
// XXX this needed here?
if
((
r
->
state
==
REDIS_STATE_DISCONNECTED
)
&&
r
->
restore_tick_us
>
rtpe_now
) {
ilog
(
LOG_WARNING
,
"Redis server '%s' is disabled. Don't try RE-Establishing for %"
PRId64
" more seconds"
,
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL