forked from naver/arcus-memcached
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitems.c
8255 lines (7542 loc) · 296 KB
/
items.c
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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* arcus-memcached - Arcus memory cache server
* Copyright 2010-2014 NAVER Corp.
* Copyright 2014-2015 JaM2in Co., Ltd.
*
* Licensed 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 "config.h"
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <inttypes.h>
#include <sys/time.h> /* gettimeofday() */
#include "default_engine.h"
#define ENABLE_DETACH_REF_ITEM_FROM_LRU 1
/* item unlink cause */
enum item_unlink_cause {
ITEM_UNLINK_NORMAL = 1, /* unlink by normal request */
ITEM_UNLINK_EVICT, /* unlink by eviction */
ITEM_UNLINK_INVALID, /* unlink by invalidation such like expiration/flush */
ITEM_UNLINK_REPLACE, /* unlink by replacement of set/replace command,
* simple kv type only
*/
ITEM_UNLINK_ABORT, /* unlink by abortion of creating a collection
* collection type only
*/
ITEM_UNLINK_EMPTY, /* unlink by empty collection
* collection type only
*/
ITEM_UNLINK_STALE /* unlink by staleness */
};
/* element delete cause */
enum elem_delete_cause {
ELEM_DELETE_NORMAL = 1, /* delete by normal request */
ELEM_DELETE_COLL, /* delete by collection deletion */
ELEM_DELETE_TRIM /* delete by overflow trim */
};
/* Forward Declarations */
static void item_link_q(struct default_engine *engine, hash_item *it);
static void item_unlink_q(struct default_engine *engine, hash_item *it);
static ENGINE_ERROR_CODE do_item_link(struct default_engine *engine, hash_item *it);
static void do_item_unlink(struct default_engine *engine, hash_item *it, enum item_unlink_cause cause);
static void do_coll_all_elem_delete(struct default_engine *engine, hash_item *it);
#ifdef JHPARK_KEY_DUMP
static void do_item_dump_stop(struct default_engine *engine);
#endif
extern int genhash_string_hash(const void* p, size_t nkey);
/*
* We only reposition items in the LRU queue if they haven't been repositioned
* in this many seconds. That saves us from churning on frequently-accessed
* items.
*/
#define ITEM_UPDATE_INTERVAL 60
/* LRU id of small memory items */
#define LRU_CLSID_FOR_SMALL 0
/* item type checking */
#define IS_LIST_ITEM(it) (((it)->iflag & ITEM_IFLAG_LIST) != 0)
#define IS_SET_ITEM(it) (((it)->iflag & ITEM_IFLAG_SET) != 0)
#define IS_BTREE_ITEM(it) (((it)->iflag & ITEM_IFLAG_BTREE) != 0)
#define IS_COLL_ITEM(it) (((it)->iflag & ITEM_IFLAG_COLL) != 0)
/* btree item status */
#define BTREE_ITEM_STATUS_USED 2
#define BTREE_ITEM_STATUS_UNLINK 1
#define BTREE_ITEM_STATUS_FREE 0
/* btree scan direction */
#define BTREE_DIRECTION_PREV 2
#define BTREE_DIRECTION_NEXT 1
#define BTREE_DIRECTION_NONE 0
/* bkey type */
#define BKEY_TYPE_UNKNOWN 0
#define BKEY_TYPE_UINT64 1
#define BKEY_TYPE_BINARY 2
/* binary bkey min & max length */
#define BKEY_MIN_BINARY_LENG 1
#define BKEY_MAX_BINARY_LENG MAX_BKEY_LENG
/* uint64 bkey min & max value */
#define BTREE_UINT64_MIN_BKEY 0
#define BTREE_UINT64_MAX_BKEY (uint64_t)((int64_t)-1) /* need check */
/* btree element item or btree node item */
#define BTREE_GET_ELEM_ITEM(node, indx) ((btree_elem_item *)((node)->item[indx]))
#define BTREE_GET_NODE_ITEM(node, indx) ((btree_indx_node *)((node)->item[indx]))
/* get bkey real size */
#define BTREE_REAL_NBKEY(nbkey) ((nbkey)==0 ? sizeof(uint64_t) : (nbkey))
/* overflow type */
#define OVFL_TYPE_NONE 0
#define OVFL_TYPE_COUNT 1
#define OVFL_TYPE_RANGE 2
/* bkey range type */
#define BKEY_RANGE_TYPE_SIN 1 /* single bkey */
#define BKEY_RANGE_TYPE_ASC 2 /* ascending bkey range */
#define BKEY_RANGE_TYPE_DSC 3 /* descending bkey range */
/* special address for representing unlinked status */
#define ADDR_MEANS_UNLINKED 1
/* collection meta flag */
#define COLL_META_FLAG_READABLE 2
#define COLL_META_FLAG_STICKY 4
#define COLL_META_FLAG_TRIMMED 8
/** How long an object can reasonably be assumed to be locked before
* harvesting it on a low memory condition. */
#define TAIL_REPAIR_TIME (3 * 3600)
/* btree position debugging */
static bool btree_position_debug = false;
/* config: evict items to free memory */
static bool item_evict_to_free = true;
/* min & max bkey constants */
static uint64_t btree_uint64_min_bkey = BTREE_UINT64_MIN_BKEY;
static uint64_t btree_uint64_max_bkey = BTREE_UINT64_MAX_BKEY;
static unsigned char btree_binary_min_bkey[BKEY_MIN_BINARY_LENG] = { 0x00 };
static unsigned char btree_binary_max_bkey[BKEY_MAX_BINARY_LENG] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
/* maximum collection size */
static int32_t coll_size_limit = 1000000;
static int32_t max_list_size = 50000;
static int32_t max_set_size = 50000;
static int32_t max_btree_size = 50000;
/* default collection size */
static int32_t default_list_size = 4000;
static int32_t default_set_size = 4000;
static int32_t default_btree_size = 4000;
static EXTENSION_LOGGER_DESCRIPTOR *logger;
/*
* Static functions
*/
#define ITEM_REFCOUNT_FULL 65535
#define ITEM_REFCOUNT_MOVE 32768
static inline void ITEM_REFCOUNT_INCR(hash_item *it)
{
it->refcount++;
if (it->refcount == ITEM_REFCOUNT_FULL) {
it->refchunk += 1;
it->refcount -= ITEM_REFCOUNT_MOVE;
assert(it->refchunk != 0); /* overflow */
}
}
static inline void ITEM_REFCOUNT_DECR(hash_item *it)
{
it->refcount--;
if (it->refcount == 0 && it->refchunk > 0) {
it->refchunk -= 1;
it->refcount = ITEM_REFCOUNT_MOVE;
}
}
/* warning: don't use these macros with a function, as it evals its arg twice */
static inline size_t ITEM_ntotal(struct default_engine *engine, const hash_item *item)
{
size_t ret;
if (IS_COLL_ITEM(item)) {
ret = sizeof(*item) + META_OFFSET_IN_ITEM(item->nkey, item->nbytes);
if (IS_LIST_ITEM(item)) ret += sizeof(list_meta_info);
else if (IS_SET_ITEM(item)) ret += sizeof(set_meta_info);
else /* BTREE_ITEM */ ret += sizeof(btree_meta_info);
} else {
ret = sizeof(*item) + item->nkey + item->nbytes;
}
if (engine->config.use_cas) {
ret += sizeof(uint64_t);
}
return ret;
}
static inline size_t ITEM_stotal(struct default_engine *engine, const hash_item *item)
{
size_t ntotal = ITEM_ntotal(engine, item);
size_t stotal = slabs_space_size(engine, ntotal);
if (IS_COLL_ITEM(item)) {
coll_meta_info *info = (coll_meta_info *)item_get_meta(item);
stotal += info->stotal;
}
return stotal;
}
/* Get the next CAS id for a new item. */
static uint64_t get_cas_id(void)
{
static uint64_t cas_id = 0;
return ++cas_id;
}
/* Enable this for reference-count debugging. */
#if 0
# define DEBUG_REFCNT(it,op) \
fprintf(stderr, "item %x refcnt(%c) %d %c\n", \
it, op, it->refcount, \
(it->it_flags & ITEM_LINKED) ? 'L' : ' ')
#else
# define DEBUG_REFCNT(it,op) while(0)
#endif
static void increase_collection_space(struct default_engine *engine, ENGINE_ITEM_TYPE item_type,
coll_meta_info *info, const size_t inc_space)
{
info->stotal += inc_space;
/* Currently, stats.lock is useless since global cache lock is held. */
//pthread_mutex_lock(&engine->stats.lock);
#ifdef ENABLE_STICKY_ITEM
if ((info->mflags & COLL_META_FLAG_STICKY) != 0)
engine->stats.sticky_bytes += inc_space;
#endif
assoc_prefix_update_size(info->prefix, item_type, inc_space, true);
engine->stats.curr_bytes += inc_space;
//pthread_mutex_unlock(&engine->stats.lock);
}
static void decrease_collection_space(struct default_engine *engine, ENGINE_ITEM_TYPE item_type,
coll_meta_info *info, const size_t dec_space)
{
assert(info->stotal >= dec_space);
info->stotal -= dec_space;
/* Currently, stats.lock is useless since global cache lock is held. */
//pthread_mutex_lock(&engine->stats.lock);
#ifdef ENABLE_STICKY_ITEM
if ((info->mflags & COLL_META_FLAG_STICKY) != 0)
engine->stats.sticky_bytes -= dec_space;
#endif
assoc_prefix_update_size(info->prefix, item_type, dec_space, false);
engine->stats.curr_bytes -= dec_space;
//pthread_mutex_unlock(&engine->stats.lock);
}
/*
* Collection Delete Queue Management
*/
static void push_coll_del_queue(struct default_engine *engine, hash_item *it)
{
/* push the item into the tail of delete queue */
it->next = NULL;
pthread_mutex_lock(&engine->coll_del_lock);
if (engine->coll_del_queue.tail == NULL) {
engine->coll_del_queue.head = it;
} else {
engine->coll_del_queue.tail->next = it;
}
engine->coll_del_queue.tail = it;
engine->coll_del_queue.size++;
if (engine->coll_del_sleep == true) {
/* wake up collection delete thead */
pthread_cond_signal(&engine->coll_del_cond);
}
pthread_mutex_unlock(&engine->coll_del_lock);
}
static hash_item *pop_coll_del_queue(struct default_engine *engine)
{
/* pop an item from the head of delete queue */
hash_item *it = NULL;
pthread_mutex_lock(&engine->coll_del_lock);
if (engine->coll_del_queue.head != NULL) {
it = engine->coll_del_queue.head;
engine->coll_del_queue.head = it->next;
if (engine->coll_del_queue.head == NULL) {
engine->coll_del_queue.tail = NULL;
}
engine->coll_del_queue.size--;
}
pthread_mutex_unlock(&engine->coll_del_lock);
return it;
}
static bool do_item_isvalid(struct default_engine *engine, hash_item *it, rel_time_t current_time)
{
/* check if it's expired */
#ifdef ENABLE_STICKY_ITEM
/* The sticky item has an exptime((rel_tiem_t)(-1)) larger than current_item.
* So, it cannot be expired.
**/
#endif
if (it->exptime != 0 && it->exptime <= current_time) {
return false; /* expired */
}
/* check flushed items as well as expired items */
if (engine->config.oldest_live != 0) {
if (engine->config.oldest_live <= current_time && it->time <= engine->config.oldest_live)
return false; /* flushed by flush_all */
}
/* check if prefix is valid */
if (assoc_prefix_isvalid(engine, it) == false) {
return false;
}
return true; /* Yes, it's a valid item */
}
static hash_item *do_item_reclaim(struct default_engine *engine, hash_item *it,
const size_t ntotal, const unsigned int clsid,
const unsigned int lruid)
{
/* increment # of reclaimed */
pthread_mutex_lock(&engine->stats.lock);
engine->stats.reclaimed++;
pthread_mutex_unlock(&engine->stats.lock);
engine->items.itemstats[lruid].reclaimed++;
/* it->refcount == 0 */
#ifdef USE_SINGLE_LRU_LIST
#else
if (lruid != LRU_CLSID_FOR_SMALL) {
it->refcount = 1;
slabs_adjust_mem_requested(engine, it->slabs_clsid, ITEM_ntotal(engine,it), ntotal);
do_item_unlink(engine, it, ITEM_UNLINK_INVALID);
/* Initialize the item block: */
it->slabs_clsid = 0;
it->refcount = 0;
return it;
}
/* collection item or small-sized kv item */
#endif
if (IS_COLL_ITEM(it))
do_coll_all_elem_delete(engine, it);
do_item_unlink(engine, it, ITEM_UNLINK_INVALID);
/* allocate from slab allocator */
it = slabs_alloc(engine, ntotal, clsid);
return it;
}
static void do_item_evict(struct default_engine *engine, hash_item *it,
const unsigned int lruid,
rel_time_t current_time, const void *cookie)
{
/* increment # of evicted */
engine->items.itemstats[lruid].evicted++;
engine->items.itemstats[lruid].evicted_time = current_time - it->time;
if (it->exptime != 0) {
engine->items.itemstats[lruid].evicted_nonzero++;
}
pthread_mutex_lock(&engine->stats.lock);
engine->stats.evictions++;
pthread_mutex_unlock(&engine->stats.lock);
if (cookie != NULL) {
engine->server.stat->evicting(cookie, item_get_key(it), it->nkey);
}
/* unlink the item */
if (IS_COLL_ITEM(it))
do_coll_all_elem_delete(engine, it);
do_item_unlink(engine, it, ITEM_UNLINK_EVICT);
}
static void do_item_repair(struct default_engine *engine, hash_item *it,
const unsigned int lruid)
{
/* increment # of repaired */
engine->items.itemstats[lruid].tailrepairs++;
it->refcount = 0;
it->refchunk = 0;
/* unlink the item */
if (IS_COLL_ITEM(it))
do_coll_all_elem_delete(engine, it);
do_item_unlink(engine, it, ITEM_UNLINK_EVICT);
}
static void do_item_invalidate(struct default_engine *engine, hash_item *it,
const unsigned int lruid, bool immediate)
{
/* increment # of reclaimed */
pthread_mutex_lock(&engine->stats.lock);
engine->stats.reclaimed++;
pthread_mutex_unlock(&engine->stats.lock);
engine->items.itemstats[lruid].reclaimed++;
/* it->refcount == 0 */
if (immediate) {
if (IS_COLL_ITEM(it)) {
do_coll_all_elem_delete(engine, it);
}
}
do_item_unlink(engine, it, ITEM_UNLINK_INVALID);
}
static void *do_item_alloc_internal(struct default_engine *engine,
const size_t ntotal, const unsigned int clsid,
const void *cookie)
{
hash_item *it = NULL;
/* do a quick check if we have any expired items in the tail.. */
int tries;
hash_item *search;
hash_item *previt = NULL;
rel_time_t current_time = engine->server.core->get_current_time();
#ifdef USE_SINGLE_LRU_LIST
unsigned int id = 1;
unsigned int clsid_based_on_ntotal = 1;
if ((it = slabs_alloc(engine, ntotal, clsid_based_on_ntotal)) != NULL) {
it->slabs_clsid = 0;
return (void*)it;
}
#else
unsigned int id;
unsigned int clsid_based_on_ntotal;
if (clsid == LRU_CLSID_FOR_SMALL) {
clsid_based_on_ntotal = slabs_clsid(engine, ntotal);
id = clsid;
} else {
clsid_based_on_ntotal = clsid;
if (ntotal <= MAX_SM_VALUE_SIZE) {
id = LRU_CLSID_FOR_SMALL;
} else {
id = clsid;
}
}
#endif
int space_shortage_level = slabs_space_shortage_level();
if (space_shortage_level > 0 && id == LRU_CLSID_FOR_SMALL
&& item_evict_to_free == true)
{
tries = space_shortage_level;
search = engine->items.tails[id];
while (search != NULL) {
#ifdef ENABLE_DETACH_REF_ITEM_FROM_LRU
if (search->nkey > 0) {
previt = search->prev;
if (search->refcount == 0) {
if (do_item_isvalid(engine, search, current_time) == false) {
do_item_invalidate(engine, search, id, true);
} else {
do_item_evict(engine, search, id, current_time, cookie);
}
} else { /* search->refcount > 0 */
/* just unlink the item from LRU list. */
item_unlink_q(engine, search);
}
search = previt;
} else { /* search->nkey == 0: scrub cursor item */
search = search->prev; /* ignore it */
}
#else
if (search->refcount == 0 && search->nkey > 0) {
previt = search->prev;
if (do_item_isvalid(engine, search, current_time) == false) {
do_item_invalidate(engine, search, id, true);
} else {
do_item_evict(engine, search, id, current_time, cookie);
}
search = previt;
} else { /* search->nkey == 0: scrub cursor item */
search = search->prev; /* ignore it */
}
#endif
if ((--tries) == 0) break;
}
}
#ifdef ENABLE_STICKY_ITEM
/* reclaim the flushed sticky items */
if (engine->items.sticky_curMK[id] != NULL) {
tries = 20;
while (engine->items.sticky_curMK[id] != NULL) {
search = engine->items.sticky_curMK[id];
engine->items.sticky_curMK[id] = search->prev;
if (search->nkey > 0 && search->refcount == 0 &&
do_item_isvalid(engine, search, current_time) == false) {
it = do_item_reclaim(engine, search, ntotal, clsid_based_on_ntotal, id);
if (it != NULL) break; /* allocated */
}
if ((--tries) == 0) break;
}
if (it != NULL) {
/* try one more invalidation */
search = engine->items.sticky_curMK[id];
if (search != NULL && search->nkey > 0 && search->refcount == 0 &&
do_item_isvalid(engine, search, current_time) == false) {
do_item_invalidate(engine, search, id, false);
}
it->slabs_clsid = 0;
return (void*)it;
}
}
#endif
if (engine->items.curMK[id] != NULL) {
assert(engine->items.lowMK[id] != NULL);
/* step 1) reclaim items from lowMK position */
tries = 20;
search = engine->items.lowMK[id];
while (search != NULL && search != engine->items.curMK[id]) {
if (search->nkey > 0 && search->refcount == 0 &&
do_item_isvalid(engine, search, current_time) == false) {
previt = search->prev;
it = do_item_reclaim(engine, search, ntotal, clsid_based_on_ntotal, id);
if (it != NULL) break; /* allocated */
search = previt;
} else {
if (search->exptime == 0 && search == engine->items.lowMK[id]) {
/* The scrub cursor item also corresponds to this case. */
engine->items.lowMK[id] = search->prev; /* move lowMK position upward */
}
search = search->prev;
}
if ((--tries) == 0) break;
}
if (it != NULL) {
/* try one more invalidation */
if (previt != NULL && previt->nkey > 0 && previt->refcount == 0 &&
do_item_isvalid(engine, previt, current_time) == false) {
do_item_invalidate(engine, previt, id, false);
}
it->slabs_clsid = 0;
return (void *)it;
}
/* step 2) reclaim items from curMK position */
tries += 20;
while (engine->items.curMK[id] != NULL) {
search = engine->items.curMK[id];
engine->items.curMK[id] = search->prev;
if (search->nkey > 0 && search->refcount == 0 &&
do_item_isvalid(engine, search, current_time) == false) {
it = do_item_reclaim(engine, search, ntotal, clsid_based_on_ntotal, id);
if (it != NULL) break; /* allocated */
}
if ((--tries) == 0) break;
}
if (engine->items.curMK[id] == NULL) {
engine->items.curMK[id] = engine->items.lowMK[id];
}
if (it != NULL) {
/* try one more invalidation */
search = engine->items.curMK[id];
if (search != NULL && search->nkey > 0 && search->refcount == 0 &&
do_item_isvalid(engine, search, current_time) == false) {
do_item_invalidate(engine, search, id, false);
}
it->slabs_clsid = 0;
return (void *)it;
}
}
it = slabs_alloc(engine, ntotal, clsid_based_on_ntotal);
if (it == NULL) {
/*
** Could not find an expired item at the tail, and memory allocation
** failed. Try to evict some items!
*/
/* If requested to not push old items out of cache when memory runs out,
* we're out of luck at this point...
*/
if (item_evict_to_free != true) {
engine->items.itemstats[clsid_based_on_ntotal].outofmemory++;
return NULL;
}
/*
* try to get one off the right LRU
* don't necessariuly unlink the tail because it may be locked: refcount>0
* search up from tail an item with refcount==0 and unlink it; give up after 50
* tries
*/
tries = 200;
search = engine->items.tails[id];
while (search != NULL) {
#ifdef ENABLE_DETACH_REF_ITEM_FROM_LRU
if (search->nkey > 0) {
previt = search->prev;
if (search->refcount == 0) {
if (do_item_isvalid(engine, search, current_time) == false) {
it = do_item_reclaim(engine, search, ntotal, clsid_based_on_ntotal, id);
} else {
do_item_evict(engine, search, id, current_time, cookie);
it = slabs_alloc(engine, ntotal, clsid_based_on_ntotal);
}
if (it != NULL) break; /* allocated */
} else { /* search->refcount > 0 */
/* just unlink the item from LRU list. */
item_unlink_q(engine, search);
}
search = previt;
} else { /* search->nkey == 0: scrub cursor item */
search = search->prev; /* ignore it */
}
#else
if (search->refcount == 0 && search->nkey > 0) {
previt = search->prev;
if (do_item_isvalid(engine, search, current_time) == false) {
it = do_item_reclaim(engine, search, ntotal, clsid_based_on_ntotal, id);
} else {
do_item_evict(engine, search, id, current_time, cookie);
it = slabs_alloc(engine, ntotal, clsid_based_on_ntotal);
}
if (it != NULL) break; /* allocated */
search = previt;
} else { /* search->nkey == 0: scrub cursor item */
search = search->prev; /* ignore it */
}
#endif
if ((--tries) == 0) break;
}
}
if (it == NULL) {
engine->items.itemstats[id].outofmemory++;
/* Last ditch effort. There is a very rare bug which causes
* refcount leaks. We've fixed most of them, but it still happens,
* and it may happen in the future.
* We can reasonably assume no item can stay locked for more than
* three hours, so if we find one in the tail which is that old,
* free it anyway.
*/
if (id <= POWER_LARGEST) {
tries = 50;
search = engine->items.tails[id];
while (search != NULL) {
if (search->nkey > 0 && search->refcount != 0 &&
search->time + TAIL_REPAIR_TIME < current_time) {
previt = search->prev;
do_item_repair(engine, search, id);
it = slabs_alloc(engine, ntotal, clsid_based_on_ntotal);
if (it != NULL) break; /* allocated */
search = previt;
} else {
search = search->prev;
}
if ((--tries) == 0) break;
}
}
}
if (it != NULL) {
it->slabs_clsid = 0;
}
return (void *)it;
}
/*@null@*/
static hash_item *do_item_alloc(struct default_engine *engine,
const void *key, const size_t nkey,
const int flags, const rel_time_t exptime,
const int nbytes, const void *cookie)
{
hash_item *it = NULL;
size_t ntotal = sizeof(hash_item) + nkey + nbytes;
if (engine->config.use_cas) {
ntotal += sizeof(uint64_t);
}
unsigned int id = slabs_clsid(engine, ntotal);
if (id == 0) {
return NULL;
}
#ifdef ENABLE_STICKY_ITEM
/* sticky memory limit check */
if (exptime == (rel_time_t)(-1)) { /* sticky item */
if (engine->stats.sticky_bytes >= engine->config.sticky_limit)
return NULL;
}
#endif
it = do_item_alloc_internal(engine, ntotal, id, cookie);
if (it == NULL) {
return NULL;
}
assert(it->slabs_clsid == 0);
it->slabs_clsid = id;
assert(it != engine->items.heads[it->slabs_clsid]);
#ifdef ENABLE_DETACH_REF_ITEM_FROM_LRU
it->next = it->prev = it;
it->h_next = 0;
#else
it->next = it->prev = it->h_next = 0;
#endif
it->refcount = 1; /* the caller will have a reference */
it->refchunk = 0;
DEBUG_REFCNT(it, '*');
it->iflag = engine->config.use_cas ? ITEM_WITH_CAS : 0;
it->nkey = nkey;
it->nbytes = nbytes;
it->flags = flags;
memcpy((void*)item_get_key(it), key, nkey);
it->exptime = exptime;
it->nprefix = 0;
return it;
}
static void do_item_free(struct default_engine *engine, hash_item *it)
{
size_t ntotal = ITEM_ntotal(engine, it);
unsigned int clsid;
assert((it->iflag & ITEM_LINKED) == 0);
assert(it != engine->items.heads[it->slabs_clsid]);
assert(it != engine->items.tails[it->slabs_clsid]);
assert(it->refcount == 0);
if (IS_COLL_ITEM(it)) {
coll_meta_info *info = (coll_meta_info *)item_get_meta(it);
if (info->ccnt > 0) { /* NOT empty collection (list or set) */
push_coll_del_queue(engine, it);
return;
}
}
/* so slab size changer can tell later if item is already free or not */
clsid = it->slabs_clsid;
it->slabs_clsid = 0;
DEBUG_REFCNT(it, 'F');
slabs_free(engine, it, ntotal, clsid);
}
static void item_link_q(struct default_engine *engine, hash_item *it)
{
hash_item **head, **tail;
assert(it->slabs_clsid <= POWER_LARGEST);
#ifdef USE_SINGLE_LRU_LIST
int clsid = 1;
#else
int clsid = it->slabs_clsid;
if (IS_COLL_ITEM(it) || ITEM_ntotal(engine, it) <= MAX_SM_VALUE_SIZE) {
clsid = LRU_CLSID_FOR_SMALL;
}
#endif
#ifdef ENABLE_STICKY_ITEM
if (it->exptime == (rel_time_t)(-1)) {
head = &engine->items.sticky_heads[clsid];
tail = &engine->items.sticky_tails[clsid];
engine->items.sticky_sizes[clsid]++;
} else {
#endif
head = &engine->items.heads[clsid];
tail = &engine->items.tails[clsid];
engine->items.sizes[clsid]++;
if (it->exptime > 0) { /* expirable item */
if (engine->items.lowMK[clsid] == NULL) {
/* set lowMK and curMK pointer in LRU */
engine->items.lowMK[clsid] = it;
engine->items.curMK[clsid] = it;
}
}
#ifdef ENABLE_STICKY_ITEM
}
#endif
assert(it != *head);
assert((*head && *tail) || (*head == 0 && *tail == 0));
it->prev = 0;
it->next = *head;
if (it->next) it->next->prev = it;
*head = it;
if (*tail == 0) *tail = it;
return;
}
static void item_unlink_q(struct default_engine *engine, hash_item *it)
{
hash_item **head, **tail;
assert(it->slabs_clsid <= POWER_LARGEST);
#ifdef USE_SINGLE_LRU_LIST
int clsid = 1;
#else
int clsid = it->slabs_clsid;
if (IS_COLL_ITEM(it) || ITEM_ntotal(engine, it) <= MAX_SM_VALUE_SIZE) {
clsid = LRU_CLSID_FOR_SMALL;
}
#endif
#ifdef ENABLE_DETACH_REF_ITEM_FROM_LRU
if (it->prev == it && it->next == it) { /* special meaning: unlinked from LRU */
return; /* Already unlinked from LRU list */
}
#endif
#ifdef ENABLE_STICKY_ITEM
if (it->exptime == (rel_time_t)(-1)) {
head = &engine->items.sticky_heads[clsid];
tail = &engine->items.sticky_tails[clsid];
engine->items.sticky_sizes[clsid]--;
/* move curMK, srcub pointer in LRU */
if (engine->items.sticky_curMK[clsid] == it)
engine->items.sticky_curMK[clsid] = it->prev;
if (engine->items.sticky_scrub[clsid] == it)
engine->items.sticky_scrub[clsid] = it->next; /* move forward */
} else {
#endif
head = &engine->items.heads[clsid];
tail = &engine->items.tails[clsid];
engine->items.sizes[clsid]--;
/* move lowMK, curMK, srcub pointer in LRU */
if (engine->items.lowMK[clsid] == it)
engine->items.lowMK[clsid] = it->prev;
if (engine->items.curMK[clsid] == it) {
engine->items.curMK[clsid] = it->prev;
if (engine->items.curMK[clsid] == NULL)
engine->items.curMK[clsid] = engine->items.lowMK[clsid];
}
if (engine->items.scrub[clsid] == it)
engine->items.scrub[clsid] = it->next; /* move forward */
#ifdef ENABLE_STICKY_ITEM
}
#endif
if (*head == it) {
assert(it->prev == 0);
*head = it->next;
}
if (*tail == it) {
assert(it->next == 0);
*tail = it->prev;
}
assert(it->next != it);
assert(it->prev != it);
if (it->next) it->next->prev = it->prev;
if (it->prev) it->prev->next = it->next;
#ifdef ENABLE_DETACH_REF_ITEM_FROM_LRU
it->prev = it->next = it; /* special meaning: unlinked from LRU */
#endif
return;
}
static ENGINE_ERROR_CODE do_item_link(struct default_engine *engine, hash_item *it)
{
const char *key = item_get_key(it);
size_t stotal;
assert((it->iflag & ITEM_LINKED) == 0);
assert(it->nbytes < (1024 * 1024)); /* 1MB max size */
MEMCACHED_ITEM_LINK(key, it->nkey, it->nbytes);
/* Allocate a new CAS ID on link. */
item_set_cas(it, get_cas_id());
/* link the item to prefix info */
stotal = ITEM_stotal(engine, it);
prefix_t *pt;
ENGINE_ERROR_CODE ret = assoc_prefix_link(engine, it, stotal, &pt);
if (ret != ENGINE_SUCCESS) {
return ret;
}
if (IS_COLL_ITEM(it)) {
coll_meta_info *info = (coll_meta_info *)item_get_meta(it);
info->prefix = (void*)pt;
assert(info->stotal == 0); /* Only empty collection can be linked */
}
/* link the item to the hash table */
it->iflag |= ITEM_LINKED;
it->time = engine->server.core->get_current_time();
assoc_insert(engine, engine->server.core->hash(key, it->nkey, 0), it);
/* link the item to LRU list */
item_link_q(engine, it);
/* update item statistics */
pthread_mutex_lock(&engine->stats.lock);
#ifdef ENABLE_STICKY_ITEM
if (it->exptime == (rel_time_t)(-1)) { /* sticky item */
engine->stats.sticky_bytes += stotal;
engine->stats.sticky_items += 1;
}
#endif
engine->stats.curr_bytes += stotal;
engine->stats.curr_items += 1;
engine->stats.total_items += 1;
pthread_mutex_unlock(&engine->stats.lock);
return ENGINE_SUCCESS;
}
static void do_item_unlink(struct default_engine *engine, hash_item *it,
enum item_unlink_cause cause)
{
/* cause: item unlink cause will be used, later
*/
const char *key = item_get_key(it);
size_t stotal;
MEMCACHED_ITEM_UNLINK(key, it->nkey, it->nbytes);
if ((it->iflag & ITEM_LINKED) != 0) {
/* unlink the item from LUR list */
item_unlink_q(engine, it);
/* unlink the item from hash table */
assoc_delete(engine, engine->server.core->hash(key, it->nkey, 0),
key, it->nkey);
it->iflag &= ~ITEM_LINKED;
/* unlink the item from prefix info */
stotal = ITEM_stotal(engine, it);
assoc_prefix_unlink(engine, it, stotal);
if (IS_COLL_ITEM(it)) {
coll_meta_info *info = (coll_meta_info *)item_get_meta(it);
info->prefix = NULL;
info->stotal = 0; /* Don't need to decrease space statistics any more */
}
/* update item statistics */
pthread_mutex_lock(&engine->stats.lock);
#ifdef ENABLE_STICKY_ITEM
if (it->exptime == (rel_time_t)(-1)) { /* sticky item */
engine->stats.sticky_bytes -= stotal;
engine->stats.sticky_items -= 1;
}
#endif
engine->stats.curr_bytes -= stotal;
engine->stats.curr_items -= 1;
pthread_mutex_unlock(&engine->stats.lock);
/* free the item if no one reference it */
if (it->refcount == 0) {
do_item_free(engine, it);
}
}
}
static void do_item_release(struct default_engine *engine, hash_item *it)
{
MEMCACHED_ITEM_REMOVE(item_get_key(it), it->nkey, it->nbytes);
if (it->refcount != 0) {
ITEM_REFCOUNT_DECR(it);
DEBUG_REFCNT(it, '-');
}
#ifdef ENABLE_DETACH_REF_ITEM_FROM_LRU
if (it->refcount == 0) {
if ((it->iflag & ITEM_LINKED) == 0) {
do_item_free(engine, it);
}
else if (it->prev == it && it->next == it) {
/* link the item into the LRU list */
rel_time_t current_time = engine->server.core->get_current_time();
if (do_item_isvalid(engine, it, current_time)) {
it->time = current_time;
item_link_q(engine, it);
} else {
do_item_unlink(engine, it, ITEM_UNLINK_INVALID);
}
}
}
#else
if (it->refcount == 0 && (it->iflag & ITEM_LINKED) == 0) {
do_item_free(engine, it);
}
#endif
}
static void do_item_update(struct default_engine *engine, hash_item *it)
{
rel_time_t current_time = engine->server.core->get_current_time();
MEMCACHED_ITEM_UPDATE(item_get_key(it), it->nkey, it->nbytes);
if (it->time < current_time - ITEM_UPDATE_INTERVAL) {
if ((it->iflag & ITEM_LINKED) != 0) {
item_unlink_q(engine, it);
it->time = current_time;
item_link_q(engine, it);
}
}
}