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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <apr_atomic.h>
#include <apr_thread_mutex.h>
#include <apr_thread_cond.h>
#include <apr_strings.h>
#include <apr_time.h>
#include <httpd.h>
#include <http_core.h>
#include <http_log.h>
#include <mpm_common.h>
#include "mod_http2.h"
#include "h2.h"
#include "h2_private.h"
#include "h2_bucket_beam.h"
#include "h2_config.h"
#include "h2_conn.h"
#include "h2_ctx.h"
#include "h2_h2.h"
#include "h2_mplx.h"
#include "h2_request.h"
#include "h2_stream.h"
#include "h2_session.h"
#include "h2_task.h"
#include "h2_workers.h"
#include "h2_util.h"
/* utility for iterating over ihash stream sets */
typedef struct {
h2_mplx *m;
h2_stream *stream;
apr_time_t now;
apr_size_t count;
} stream_iter_ctx;
/**
* Naming convention for static functions:
* - m_*: function only called from the master connection
* - s_*: function only called from a secondary connection
* - t_*: function only called from a h2_task holder
* - mst_*: function called from everyone
*/
static apr_status_t s_mplx_be_happy(h2_mplx *m, h2_task *task);
static apr_status_t m_be_annoyed(h2_mplx *m);
apr_status_t h2_mplx_m_child_init(apr_pool_t *pool, server_rec *s)
{
return APR_SUCCESS;
}
#define H2_MPLX_ENTER(m) \
do { apr_status_t rv; if ((rv = apr_thread_mutex_lock(m->lock)) != APR_SUCCESS) {\
return rv;\
} } while(0)
#define H2_MPLX_LEAVE(m) \
apr_thread_mutex_unlock(m->lock)
#define H2_MPLX_ENTER_ALWAYS(m) \
apr_thread_mutex_lock(m->lock)
#define H2_MPLX_ENTER_MAYBE(m, dolock) \
if (dolock) apr_thread_mutex_lock(m->lock)
#define H2_MPLX_LEAVE_MAYBE(m, dolock) \
if (dolock) apr_thread_mutex_unlock(m->lock)
static void mst_check_data_for(h2_mplx *m, int stream_id, int mplx_is_locked);
static void mst_stream_input_ev(void *ctx, h2_bucket_beam *beam)
{
h2_stream *stream = ctx;
h2_mplx *m = stream->session->mplx;
apr_atomic_set32(&m->event_pending, 1);
}
static void m_stream_input_consumed(void *ctx, h2_bucket_beam *beam, apr_off_t length)
{
h2_stream_in_consumed(ctx, length);
}
static void ms_stream_joined(h2_mplx *m, h2_stream *stream)
{
ap_assert(!h2_task_has_started(stream->task) || stream->task->worker_done);
h2_ififo_remove(m->readyq, stream->id);
h2_ihash_remove(m->shold, stream->id);
h2_ihash_add(m->spurge, stream);
}
static void m_stream_cleanup(h2_mplx *m, h2_stream *stream)
{
ap_assert(stream->state == H2_SS_CLEANUP);
if (stream->input) {
h2_beam_on_consumed(stream->input, NULL, NULL, NULL);
h2_beam_abort(stream->input);
}
if (stream->output) {
h2_beam_on_produced(stream->output, NULL, NULL);
h2_beam_leave(stream->output);
}
h2_stream_cleanup(stream);
h2_ihash_remove(m->streams, stream->id);
h2_iq_remove(m->q, stream->id);
if (!h2_task_has_started(stream->task) || stream->task->done_done) {
ms_stream_joined(m, stream);
}
else {
h2_ififo_remove(m->readyq, stream->id);
h2_ihash_add(m->shold, stream);
if (stream->task) {
stream->task->c->aborted = 1;
}
}
}
/**
* A h2_mplx needs to be thread-safe *and* if will be called by
* the h2_session thread *and* the h2_worker threads. Therefore:
* - calls are protected by a mutex lock, m->lock
* - the pool needs its own allocator, since apr_allocator_t are
* not re-entrant. The separate allocator works without a
* separate lock since we already protect h2_mplx itself.
* Since HTTP/2 connections can be expected to live longer than
* their HTTP/1 cousins, the separate allocator seems to work better
* than protecting a shared h2_session one with an own lock.
*/
h2_mplx *h2_mplx_m_create(conn_rec *c, server_rec *s, apr_pool_t *parent,
h2_workers *workers)
{
apr_status_t status = APR_SUCCESS;
apr_allocator_t *allocator;
apr_thread_mutex_t *mutex;
h2_mplx *m;
m = apr_pcalloc(parent, sizeof(h2_mplx));
if (m) {
m->id = c->id;
m->c = c;
m->s = s;
/* We create a pool with its own allocator to be used for
* processing secondary connections. This is the only way to have the
* processing independent of its parent pool in the sense that it
* can work in another thread. Also, the new allocator needs its own
* mutex to synchronize sub-pools.
*/
status = apr_allocator_create(&allocator);
if (status != APR_SUCCESS) {
return NULL;
}
apr_allocator_max_free_set(allocator, ap_max_mem_free);
apr_pool_create_ex(&m->pool, parent, NULL, allocator);
if (!m->pool) {
apr_allocator_destroy(allocator);
return NULL;
}
apr_pool_tag(m->pool, "h2_mplx");
apr_allocator_owner_set(allocator, m->pool);
status = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT,
m->pool);
if (status != APR_SUCCESS) {
apr_pool_destroy(m->pool);
return NULL;
}
apr_allocator_mutex_set(allocator, mutex);
status = apr_thread_mutex_create(&m->lock, APR_THREAD_MUTEX_DEFAULT,
m->pool);
if (status != APR_SUCCESS) {
apr_pool_destroy(m->pool);
return NULL;
}
m->max_streams = h2_config_sgeti(s, H2_CONF_MAX_STREAMS);
m->stream_max_mem = h2_config_sgeti(s, H2_CONF_STREAM_MAX_MEM);
m->streams = h2_ihash_create(m->pool, offsetof(h2_stream,id));
m->shold = h2_ihash_create(m->pool, offsetof(h2_stream,id));
m->spurge = h2_ihash_create(m->pool, offsetof(h2_stream,id));
m->q = h2_iq_create(m->pool, m->max_streams);
status = h2_ififo_set_create(&m->readyq, m->pool, m->max_streams);
if (status != APR_SUCCESS) {
apr_pool_destroy(m->pool);
return NULL;
}
m->workers = workers;
m->max_active = workers->max_workers;
m->limit_active = 6; /* the original h1 max parallel connections */
m->last_mood_change = apr_time_now();
m->mood_update_interval = apr_time_from_msec(100);
m->spare_secondary = apr_array_make(m->pool, 10, sizeof(conn_rec*));
}
return m;
}
int h2_mplx_m_shutdown(h2_mplx *m)
{
int max_stream_started = 0;
H2_MPLX_ENTER(m);
max_stream_started = m->max_stream_started;
/* Clear schedule queue, disabling existing streams from starting */
h2_iq_clear(m->q);
H2_MPLX_LEAVE(m);
return max_stream_started;
}
static int m_input_consumed_signal(h2_mplx *m, h2_stream *stream)
{
if (stream->input) {
return h2_beam_report_consumption(stream->input);
}
return 0;
}
static int m_report_consumption_iter(void *ctx, void *val)
{
h2_stream *stream = val;
h2_mplx *m = ctx;
m_input_consumed_signal(m, stream);
if (stream->state == H2_SS_CLOSED_L
&& (!stream->task || stream->task->worker_done)) {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, m->c,
H2_STRM_LOG(APLOGNO(10026), stream, "remote close missing"));
nghttp2_submit_rst_stream(stream->session->ngh2, NGHTTP2_FLAG_NONE,
stream->id, NGHTTP2_NO_ERROR);
}
return 1;
}
static int s_output_consumed_signal(h2_mplx *m, h2_task *task)
{
if (task->output.beam) {
return h2_beam_report_consumption(task->output.beam);
}
return 0;
}
static int m_stream_destroy_iter(void *ctx, void *val)
{
h2_mplx *m = ctx;
h2_stream *stream = val;
h2_ihash_remove(m->spurge, stream->id);
ap_assert(stream->state == H2_SS_CLEANUP);
if (stream->input) {
/* Process outstanding events before destruction */
m_input_consumed_signal(m, stream);
h2_beam_log(stream->input, m->c, APLOG_TRACE2, "stream_destroy");
h2_beam_destroy(stream->input);
stream->input = NULL;
}
if (stream->task) {
h2_task *task = stream->task;
conn_rec *secondary;
int reuse_secondary = 0;
stream->task = NULL;
secondary = task->c;
if (secondary) {
if (m->s->keep_alive_max == 0 || secondary->keepalives < m->s->keep_alive_max) {
reuse_secondary = ((m->spare_secondary->nelts < (m->limit_active * 3 / 2))
&& !task->rst_error);
}
if (reuse_secondary) {
h2_beam_log(task->output.beam, m->c, APLOG_DEBUG,
APLOGNO(03385) "h2_task_destroy, reuse secondary");
h2_task_destroy(task);
APR_ARRAY_PUSH(m->spare_secondary, conn_rec*) = secondary;
}
else {
h2_beam_log(task->output.beam, m->c, APLOG_TRACE1,
"h2_task_destroy, destroy secondary");
h2_secondary_destroy(secondary);
}
}
}
h2_stream_destroy(stream);
return 0;
}
static void m_purge_streams(h2_mplx *m, int lock)
{
if (!h2_ihash_empty(m->spurge)) {
H2_MPLX_ENTER_MAYBE(m, lock);
while (!h2_ihash_iter(m->spurge, m_stream_destroy_iter, m)) {
/* repeat until empty */
}
H2_MPLX_LEAVE_MAYBE(m, lock);
}
}
typedef struct {
h2_mplx_stream_cb *cb;
void *ctx;
} stream_iter_ctx_t;
static int m_stream_iter_wrap(void *ctx, void *stream)
{
stream_iter_ctx_t *x = ctx;
return x->cb(stream, x->ctx);
}
apr_status_t h2_mplx_m_stream_do(h2_mplx *m, h2_mplx_stream_cb *cb, void *ctx)
{
stream_iter_ctx_t x;
H2_MPLX_ENTER(m);
x.cb = cb;
x.ctx = ctx;
h2_ihash_iter(m->streams, m_stream_iter_wrap, &x);
H2_MPLX_LEAVE(m);
return APR_SUCCESS;
}
static int m_report_stream_iter(void *ctx, void *val) {
h2_mplx *m = ctx;
h2_stream *stream = val;
h2_task *task = stream->task;
if (APLOGctrace1(m->c)) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
H2_STRM_MSG(stream, "started=%d, scheduled=%d, ready=%d, out_buffer=%ld"),
!!stream->task, stream->scheduled, h2_stream_is_ready(stream),
(long)h2_beam_get_buffered(stream->output));
}
if (task) {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, m->c, /* NO APLOGNO */
H2_STRM_MSG(stream, "->03198: %s %s %s"
"[started=%d/done=%d]"),
task->request->method, task->request->authority,
task->request->path, task->worker_started,
task->worker_done);
}
else {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, m->c, /* NO APLOGNO */
H2_STRM_MSG(stream, "->03198: no task"));
}
return 1;
}
static int m_unexpected_stream_iter(void *ctx, void *val) {
h2_mplx *m = ctx;
h2_stream *stream = val;
ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, m->c, /* NO APLOGNO */
H2_STRM_MSG(stream, "unexpected, started=%d, scheduled=%d, ready=%d"),
!!stream->task, stream->scheduled, h2_stream_is_ready(stream));
return 1;
}
static int m_stream_cancel_iter(void *ctx, void *val) {
h2_mplx *m = ctx;
h2_stream *stream = val;
/* disabled input consumed reporting */
if (stream->input) {
h2_beam_on_consumed(stream->input, NULL, NULL, NULL);
}
/* take over event monitoring */
h2_stream_set_monitor(stream, NULL);
/* Reset, should transit to CLOSED state */
h2_stream_rst(stream, H2_ERR_NO_ERROR);
/* All connection data has been sent, simulate cleanup */
h2_stream_dispatch(stream, H2_SEV_EOS_SENT);
m_stream_cleanup(m, stream);
return 0;
}
void h2_mplx_m_release_and_join(h2_mplx *m, apr_thread_cond_t *wait)
{
apr_status_t status;
int i, wait_secs = 60, old_aborted;
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
"h2_mplx(%ld): start release", m->id);
/* How to shut down a h2 connection:
* 0. abort and tell the workers that no more tasks will come from us */
m->aborted = 1;
h2_workers_unregister(m->workers, m);
H2_MPLX_ENTER_ALWAYS(m);
/* While really terminating any secondary connections, treat the master
* connection as aborted. It's not as if we could send any more data
* at this point. */
old_aborted = m->c->aborted;
m->c->aborted = 1;
/* How to shut down a h2 connection:
* 1. cancel all streams still active */
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): release, %d/%d/%d streams (total/hold/purge), %d active tasks",
m->id, (int)h2_ihash_count(m->streams),
(int)h2_ihash_count(m->shold), (int)h2_ihash_count(m->spurge), m->tasks_active);
while (!h2_ihash_iter(m->streams, m_stream_cancel_iter, m)) {
/* until empty */
}
/* 2. no more streams should be scheduled or in the active set */
ap_assert(h2_ihash_empty(m->streams));
ap_assert(h2_iq_empty(m->q));
/* 3. while workers are busy on this connection, meaning they
* are processing tasks from this connection, wait on them finishing
* in order to wake us and let us check again.
* Eventually, this has to succeed. */
m->join_wait = wait;
for (i = 0; h2_ihash_count(m->shold) > 0; ++i) {
status = apr_thread_cond_timedwait(wait, m->lock, apr_time_from_sec(wait_secs));
if (APR_STATUS_IS_TIMEUP(status)) {
/* This can happen if we have very long running requests
* that do not time out on IO. */
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, m->c, APLOGNO(03198)
"h2_mplx(%ld): waited %d sec for %d tasks",
m->id, i*wait_secs, (int)h2_ihash_count(m->shold));
h2_ihash_iter(m->shold, m_report_stream_iter, m);
}
}
m->join_wait = NULL;
/* 4. With all workers done, all streams should be in spurge */
ap_assert(m->tasks_active == 0);
if (!h2_ihash_empty(m->shold)) {
ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, m->c, APLOGNO(03516)
"h2_mplx(%ld): unexpected %d streams in hold",
m->id, (int)h2_ihash_count(m->shold));
h2_ihash_iter(m->shold, m_unexpected_stream_iter, m);
}
m->c->aborted = old_aborted;
H2_MPLX_LEAVE(m);
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c, "h2_mplx(%ld): released", m->id);
}
apr_status_t h2_mplx_m_stream_cleanup(h2_mplx *m, h2_stream *stream)
{
H2_MPLX_ENTER(m);
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
H2_STRM_MSG(stream, "cleanup"));
m_stream_cleanup(m, stream);
H2_MPLX_LEAVE(m);
return APR_SUCCESS;
}
h2_stream *h2_mplx_t_stream_get(h2_mplx *m, h2_task *task)
{
h2_stream *s = NULL;
H2_MPLX_ENTER_ALWAYS(m);
s = h2_ihash_get(m->streams, task->stream_id);
H2_MPLX_LEAVE(m);
return s;
}
static void mst_output_produced(void *ctx, h2_bucket_beam *beam, apr_off_t bytes)
{
h2_stream *stream = ctx;
h2_mplx *m = stream->session->mplx;
mst_check_data_for(m, stream->id, 0);
}
static apr_status_t t_out_open(h2_mplx *m, int stream_id, h2_bucket_beam *beam)
{
h2_stream *stream = h2_ihash_get(m->streams, stream_id);
if (!stream || !stream->task || m->aborted) {
return APR_ECONNABORTED;
}
ap_assert(stream->output == NULL);
stream->output = beam;
if (APLOGctrace2(m->c)) {
h2_beam_log(beam, stream->task->c, APLOG_TRACE2, "out_open");
}
else {
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, stream->task->c,
"h2_mplx(%s): out open", stream->task->id);
}
h2_beam_on_produced(stream->output, mst_output_produced, stream);
if (stream->task->output.copy_files) {
h2_beam_on_file_beam(stream->output, h2_beam_no_files, NULL);
}
/* we might see some file buckets in the output, see
* if we have enough handles reserved. */
mst_check_data_for(m, stream->id, 1);
return APR_SUCCESS;
}
apr_status_t h2_mplx_t_out_open(h2_mplx *m, int stream_id, h2_bucket_beam *beam)
{
apr_status_t status;
H2_MPLX_ENTER(m);
if (m->aborted) {
status = APR_ECONNABORTED;
}
else {
status = t_out_open(m, stream_id, beam);
}
H2_MPLX_LEAVE(m);
return status;
}
static apr_status_t s_out_close(h2_mplx *m, h2_task *task)
{
apr_status_t status = APR_SUCCESS;
if (!task) {
return APR_ECONNABORTED;
}
if (task->c) {
++task->c->keepalives;
}
if (!h2_ihash_get(m->streams, task->stream_id)) {
return APR_ECONNABORTED;
}
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, status, task->c,
"h2_mplx(%s): close", task->id);
status = h2_beam_close(task->output.beam);
h2_beam_log(task->output.beam, task->c, APLOG_TRACE2, "out_close");
s_output_consumed_signal(m, task);
mst_check_data_for(m, task->stream_id, 1);
return status;
}
apr_status_t h2_mplx_m_out_trywait(h2_mplx *m, apr_interval_time_t timeout,
apr_thread_cond_t *iowait)
{
apr_status_t status;
H2_MPLX_ENTER(m);
if (m->aborted) {
status = APR_ECONNABORTED;
}
else if (h2_mplx_m_has_master_events(m)) {
status = APR_SUCCESS;
}
else {
m_purge_streams(m, 0);
h2_ihash_iter(m->streams, m_report_consumption_iter, m);
m->added_output = iowait;
status = apr_thread_cond_timedwait(m->added_output, m->lock, timeout);
if (APLOGctrace2(m->c)) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
"h2_mplx(%ld): trywait on data for %f ms)",
m->id, timeout/1000.0);
}
m->added_output = NULL;
}
H2_MPLX_LEAVE(m);
return status;
}
static void mst_check_data_for(h2_mplx *m, int stream_id, int mplx_is_locked)
{
/* If m->lock is already held, we must release during h2_ififo_push()
* which can wait on its not_full condition, causing a deadlock because
* no one would then be able to acquire m->lock to empty the fifo.
*/
H2_MPLX_LEAVE_MAYBE(m, mplx_is_locked);
if (h2_ififo_push(m->readyq, stream_id) == APR_SUCCESS) {
H2_MPLX_ENTER_ALWAYS(m);
apr_atomic_set32(&m->event_pending, 1);
if (m->added_output) {
apr_thread_cond_signal(m->added_output);
}
H2_MPLX_LEAVE_MAYBE(m, !mplx_is_locked);
}
else {
H2_MPLX_ENTER_MAYBE(m, mplx_is_locked);
}
}
apr_status_t h2_mplx_m_reprioritize(h2_mplx *m, h2_stream_pri_cmp *cmp, void *ctx)
{
apr_status_t status;
H2_MPLX_ENTER(m);
if (m->aborted) {
status = APR_ECONNABORTED;
}
else {
h2_iq_sort(m->q, cmp, ctx);
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): reprioritize tasks", m->id);
status = APR_SUCCESS;
}
H2_MPLX_LEAVE(m);
return status;
}
static void ms_register_if_needed(h2_mplx *m, int from_master)
{
if (!m->aborted && !m->is_registered && !h2_iq_empty(m->q)) {
apr_status_t status = h2_workers_register(m->workers, m);
if (status == APR_SUCCESS) {
m->is_registered = 1;
}
else if (from_master) {
ap_log_cerror(APLOG_MARK, APLOG_ERR, status, m->c, APLOGNO(10021)
"h2_mplx(%ld): register at workers", m->id);
}
}
}
apr_status_t h2_mplx_m_process(h2_mplx *m, struct h2_stream *stream,
h2_stream_pri_cmp *cmp, void *ctx)
{
apr_status_t status;
H2_MPLX_ENTER(m);
if (m->aborted) {
status = APR_ECONNABORTED;
}
else {
status = APR_SUCCESS;
h2_ihash_add(m->streams, stream);
if (h2_stream_is_ready(stream)) {
/* already have a response */
mst_check_data_for(m, stream->id, 1);
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
H2_STRM_MSG(stream, "process, add to readyq"));
}
else {
h2_iq_add(m->q, stream->id, cmp, ctx);
ms_register_if_needed(m, 1);
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
H2_STRM_MSG(stream, "process, added to q"));
}
}
H2_MPLX_LEAVE(m);
return status;
}
static h2_task *s_next_stream_task(h2_mplx *m)
{
h2_stream *stream;
int sid;
while (!m->aborted && (m->tasks_active < m->limit_active)
&& (sid = h2_iq_shift(m->q)) > 0) {
stream = h2_ihash_get(m->streams, sid);
if (stream) {
conn_rec *secondary, **psecondary;
psecondary = (conn_rec **)apr_array_pop(m->spare_secondary);
if (psecondary) {
secondary = *psecondary;
secondary->aborted = 0;
}
else {
secondary = h2_secondary_create(m->c, stream->id, m->pool);
}
if (!stream->task) {
if (sid > m->max_stream_started) {
m->max_stream_started = sid;
}
if (stream->input) {
h2_beam_on_consumed(stream->input, mst_stream_input_ev,
m_stream_input_consumed, stream);
}
stream->task = h2_task_create(secondary, stream->id,
stream->request, m, stream->input,
stream->session->s->timeout,
m->stream_max_mem);
if (!stream->task) {
ap_log_cerror(APLOG_MARK, APLOG_ERR, APR_ENOMEM, secondary,
H2_STRM_LOG(APLOGNO(02941), stream,
"create task"));
return NULL;
}
}
stream->task->started_at = apr_time_now();
++m->tasks_active;
return stream->task;
}
}
if (m->tasks_active >= m->limit_active && !h2_iq_empty(m->q)) {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, m->c,
"h2_session(%ld): delaying request processing. "
"Current limit is %d and %d workers are in use.",
m->id, m->limit_active, m->tasks_active);
}
return NULL;
}
apr_status_t h2_mplx_s_pop_task(h2_mplx *m, h2_task **ptask)
{
apr_status_t rv = APR_EOF;
*ptask = NULL;
ap_assert(m);
ap_assert(m->lock);
if (APR_SUCCESS != (rv = apr_thread_mutex_lock(m->lock))) {
return rv;
}
if (m->aborted) {
rv = APR_EOF;
}
else {
*ptask = s_next_stream_task(m);
rv = (*ptask != NULL && !h2_iq_empty(m->q))? APR_EAGAIN : APR_SUCCESS;
}
if (APR_EAGAIN != rv) {
m->is_registered = 0; /* h2_workers will discard this mplx */
}
H2_MPLX_LEAVE(m);
return rv;
}
static void s_task_done(h2_mplx *m, h2_task *task)
{
h2_stream *stream;
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, task->c,
"h2_mplx(%ld): task(%s) done", m->id, task->id);
s_out_close(m, task);
task->worker_done = 1;
task->done_at = apr_time_now();
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, task->c,
"h2_mplx(%s): request done, %f ms elapsed", task->id,
(task->done_at - task->started_at) / 1000.0);
if (task->c && !task->c->aborted && task->started_at > m->last_mood_change) {
s_mplx_be_happy(m, task);
}
ap_assert(task->done_done == 0);
stream = h2_ihash_get(m->streams, task->stream_id);
if (stream) {
/* stream not done yet. */
if (!m->aborted && task->redo) {
/* reset and schedule again */
h2_task_redo(task);
h2_iq_add(m->q, stream->id, NULL, NULL);
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, task->c,
H2_STRM_MSG(stream, "redo, added to q"));
}
else {
/* stream not cleaned up, stay around */
task->done_done = 1;
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, task->c,
H2_STRM_MSG(stream, "task_done, stream open"));
if (stream->input) {
h2_beam_leave(stream->input);
}
/* more data will not arrive, resume the stream */
mst_check_data_for(m, stream->id, 1);
}
}
else if ((stream = h2_ihash_get(m->shold, task->stream_id)) != NULL) {
/* stream is done, was just waiting for this. */
task->done_done = 1;
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, task->c,
H2_STRM_MSG(stream, "task_done, in hold"));
if (stream->input) {
h2_beam_leave(stream->input);
}
ms_stream_joined(m, stream);
}
else if ((stream = h2_ihash_get(m->spurge, task->stream_id)) != NULL) {
ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, task->c,
H2_STRM_LOG(APLOGNO(03517), stream, "already in spurge"));
ap_assert("stream should not be in spurge" == NULL);
}
else {
ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, task->c, APLOGNO(03518)
"h2_mplx(%s): task_done, stream not found",
task->id);
ap_assert("stream should still be available" == NULL);
}
}
void h2_mplx_s_task_done(h2_mplx *m, h2_task *task, h2_task **ptask)
{
H2_MPLX_ENTER_ALWAYS(m);
--m->tasks_active;
s_task_done(m, task);
if (m->join_wait) {
apr_thread_cond_signal(m->join_wait);
}
if (ptask) {
/* caller wants another task */
*ptask = s_next_stream_task(m);
}
ms_register_if_needed(m, 0);
H2_MPLX_LEAVE(m);
}
/*******************************************************************************
* h2_mplx DoS protection
******************************************************************************/
static int m_timed_out_busy_iter(void *data, void *val)
{
stream_iter_ctx *ctx = data;
h2_stream *stream = val;
if (h2_task_has_started(stream->task) && !stream->task->worker_done
&& (ctx->now - stream->task->started_at) > stream->task->timeout) {
/* timed out stream occupying a worker, found */
ctx->stream = stream;
return 0;
}
return 1;
}
static h2_stream *m_get_timed_out_busy_stream(h2_mplx *m)
{
stream_iter_ctx ctx;
ctx.m = m;
ctx.stream = NULL;
ctx.now = apr_time_now();
h2_ihash_iter(m->streams, m_timed_out_busy_iter, &ctx);
return ctx.stream;
}
static int m_latest_repeatable_unsubmitted_iter(void *data, void *val)
{
stream_iter_ctx *ctx = data;
h2_stream *stream = val;
if (!stream->task) goto leave;
if (!h2_task_has_started(stream->task) || stream->task->worker_done) goto leave;
if (h2_stream_is_ready(stream)) goto leave;
if (stream->task->redo) {
++ctx->count;
goto leave;
}
if (h2_task_can_redo(stream->task)) {
/* this task occupies a worker, the response has not been submitted
* yet, not been cancelled and it is a repeatable request
* -> we could redo it later */
if (!ctx->stream
|| (ctx->stream->task->started_at < stream->task->started_at)) {
/* we did not have one or this one was started later */
ctx->stream = stream;
}
}
leave:
return 1;
}
static apr_status_t m_assess_task_to_throttle(h2_task **ptask, h2_mplx *m)
{
stream_iter_ctx ctx;
/* count the running tasks already marked for redo and get one that could
* be throttled */
*ptask = NULL;
ctx.m = m;
ctx.stream = NULL;
ctx.count = 0;
h2_ihash_iter(m->streams, m_latest_repeatable_unsubmitted_iter, &ctx);
if (m->tasks_active - ctx.count > m->limit_active) {
/* we are above the limit of running tasks, accounting for the ones
* already throttled. */
if (ctx.stream && ctx.stream->task) {
*ptask = ctx.stream->task;
return APR_EAGAIN;
}
/* above limit, be seeing no candidate for easy throttling */
if (m_get_timed_out_busy_stream(m)) {
/* Too many busy workers, unable to cancel enough streams
* and with a busy, timed out stream, we tell the client
* to go away... */
return APR_TIMEUP;
}
}
return APR_SUCCESS;
}
static apr_status_t m_unschedule_slow_tasks(h2_mplx *m)
{
h2_task *task;
apr_status_t rv;
/* Try to get rid of streams that occupy workers. Look for safe requests
* that are repeatable. If none found, fail the connection.
*/
while (APR_EAGAIN == (rv = m_assess_task_to_throttle(&task, m))) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
"h2_mplx(%s): unschedule, resetting task for redo later",
task->id);
task->redo = 1;
h2_task_rst(task, H2_ERR_CANCEL);
}
return rv;
}
static apr_status_t s_mplx_be_happy(h2_mplx *m, h2_task *task)
{
apr_time_t now;
--m->irritations_since;
now = apr_time_now();
if (m->limit_active < m->max_active
&& (now - m->last_mood_change >= m->mood_update_interval
|| m->irritations_since < -m->limit_active)) {
m->limit_active = H2MIN(m->limit_active * 2, m->max_active);
m->last_mood_change = now;
m->irritations_since = 0;
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, task->c,
"h2_mplx(%ld): mood update, increasing worker limit to %d",
m->id, m->limit_active);
}
return APR_SUCCESS;
}
static apr_status_t m_be_annoyed(h2_mplx *m)
{
apr_status_t status = APR_SUCCESS;
apr_time_t now;
++m->irritations_since;
now = apr_time_now();
if (m->limit_active > 2 &&
((now - m->last_mood_change >= m->mood_update_interval)
|| (m->irritations_since >= m->limit_active))) {
if (m->limit_active > 16) {
m->limit_active = 16;
}
else if (m->limit_active > 8) {
m->limit_active = 8;
}
else if (m->limit_active > 4) {
m->limit_active = 4;
}
else if (m->limit_active > 2) {
m->limit_active = 2;
}
m->last_mood_change = now;
m->irritations_since = 0;
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): mood update, decreasing worker limit to %d",
m->id, m->limit_active);
}
if (m->tasks_active > m->limit_active) {
status = m_unschedule_slow_tasks(m);
}
return status;
}
apr_status_t h2_mplx_m_idle(h2_mplx *m)
{
apr_status_t status = APR_SUCCESS;
apr_size_t scount;
H2_MPLX_ENTER(m);
scount = h2_ihash_count(m->streams);
if (scount > 0) {
if (m->tasks_active) {
/* If we have streams in connection state 'IDLE', meaning
* all streams are ready to sent data out, but lack
* WINDOW_UPDATEs.
*
* This is ok, unless we have streams that still occupy
* h2 workers. As worker threads are a scarce resource,
* we need to take measures that we do not get DoSed.
*
* This is what we call an 'idle block'. Limit the amount
* of busy workers we allow for this connection until it
* well behaves.
*/
status = m_be_annoyed(m);
}
else if (!h2_iq_empty(m->q)) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): idle, but %d streams to process",
m->id, (int)h2_iq_count(m->q));
status = APR_EAGAIN;
}
else {
/* idle, have streams, but no tasks active. what are we waiting for?
* WINDOW_UPDATEs from client? */
h2_stream *stream = NULL;
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): idle, no tasks ongoing, %d streams",
m->id, (int)h2_ihash_count(m->streams));
h2_ihash_shift(m->streams, (void**)&stream, 1);
if (stream) {
h2_ihash_add(m->streams, stream);
if (stream->output && !stream->out_checked) {
/* FIXME: this looks like a race between the session thinking
* it is idle and the EOF on a stream not being sent.
* Signal to caller to leave IDLE state.
*/
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
H2_STRM_MSG(stream, "output closed=%d, mplx idle"
", out has %ld bytes buffered"),
h2_beam_is_closed(stream->output),
(long)h2_beam_get_buffered(stream->output));
h2_ihash_add(m->streams, stream);
mst_check_data_for(m, stream->id, 1);
stream->out_checked = 1;
status = APR_EAGAIN;
}
}
}
}
ms_register_if_needed(m, 1);
H2_MPLX_LEAVE(m);
return status;
}
/*******************************************************************************
* mplx master events dispatching
******************************************************************************/
int h2_mplx_m_has_master_events(h2_mplx *m)
{
return apr_atomic_read32(&m->event_pending) > 0;
}
apr_status_t h2_mplx_m_dispatch_master_events(h2_mplx *m, stream_ev_callback *on_resume,
void *on_ctx)
{
h2_stream *stream;
int n, id;
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
"h2_mplx(%ld): dispatch events", m->id);
apr_atomic_set32(&m->event_pending, 0);
/* update input windows for streams */
h2_ihash_iter(m->streams, m_report_consumption_iter, m);
m_purge_streams(m, 1);
n = h2_ififo_count(m->readyq);
while (n > 0
&& (h2_ififo_try_pull(m->readyq, &id) == APR_SUCCESS)) {
--n;
stream = h2_ihash_get(m->streams, id);
if (stream) {
on_resume(on_ctx, stream);
}
}
return APR_SUCCESS;
}
apr_status_t h2_mplx_m_keep_active(h2_mplx *m, h2_stream *stream)
{
mst_check_data_for(m, stream->id, 0);
return APR_SUCCESS;
}
int h2_mplx_m_awaits_data(h2_mplx *m)
{
int waiting = 1;
H2_MPLX_ENTER_ALWAYS(m);
if (h2_ihash_empty(m->streams)) {
waiting = 0;
}
else if (!m->tasks_active && !h2_ififo_count(m->readyq) && h2_iq_empty(m->q)) {
waiting = 0;
}
H2_MPLX_LEAVE(m);
return waiting;
}
static int reset_is_acceptable(h2_stream *stream)
{
/* client may terminate a stream via H2 RST_STREAM message at any time.
* This is annyoing when we have committed resources (e.g. worker threads)
* to it, so our mood (e.g. willingness to commit resources on this
* connection in the future) goes down.
*
* This is a DoS protection. We do not want to make it too easy for
* a client to eat up server resources.
*
* However: there are cases where a RST_STREAM is the only way to end
* a request. This includes websockets and server-side-event streams (SSEs).
* The responses to such requests continue forever otherwise.
*
*/
if (!stream->task) return 1; /* have not started or already ended for us. acceptable. */
if (!(stream->id & 0x01)) return 1; /* stream initiated by us. acceptable. */
if (!stream->has_response) return 0; /* no response headers produced yet. bad. */
if (!stream->out_data_frames) return 0; /* no response body data sent yet. bad. */
return 1; /* otherwise, be forgiving */
}
apr_status_t h2_mplx_m_client_rst(h2_mplx *m, int stream_id)
{
h2_stream *stream;
apr_status_t status = APR_SUCCESS;
H2_MPLX_ENTER_ALWAYS(m);
stream = h2_ihash_get(m->streams, stream_id);
if (stream && !reset_is_acceptable(stream)) {
status = m_be_annoyed(m);
}
H2_MPLX_LEAVE(m);
return status;
}
|