2 * Index, mapping scores to log positions.
4 * The index is made up of some number of index sections, each of
5 * which is typically stored on a different disk. The blocks in all the
6 * index sections are logically numbered, with each index section
7 * responsible for a range of blocks. Blocks are typically 8kB.
9 * The N index blocks are treated as a giant hash table. The top 32 bits
10 * of score are used as the key for a lookup. Each index block holds
11 * one hash bucket, which is responsible for ceil(2^32 / N) of the key space.
13 * The index is sized so that a particular bucket is extraordinarily
14 * unlikely to overflow: assuming compressed data blocks are 4kB
15 * on disk, and assuming each block has a 40 byte index entry,
16 * the index data will be 1% of the total data. Since scores are essentially
17 * random, all buckets should be about the same fullness.
18 * A factor of 5 gives us a wide comfort boundary to account for
19 * random variation. So the index disk space should be 5% of the arena disk space.
26 static int initindex1(Index*);
27 static ISect *initisect1(ISect *is);
29 #define KEY(k,d) ((d) ? (k)>>(32-(d)) : 0)
31 static char IndexMagic[] = "venti index configuration";
34 initindex(char *name, ISect **sects, int n)
39 u32int last, blocksize, tabsize;
44 seterr(EOk, "no index sections to initialize index");
49 fprint(2, "no mem\n");
50 seterr(EOk, "can't initialize index: out of memory");
55 tabsize = sects[0]->tabsize;
56 if(partifile(&f, sects[0]->part, sects[0]->tabbase, tabsize) < 0)
58 if(parseindex(&f, ix) < 0){
64 if(namecmp(ix->name, name) != 0){
65 seterr(ECorrupt, "mismatched index name: found %s expected %s", ix->name, name);
69 seterr(ECorrupt, "mismatched number index sections: found %d expected %d", n, ix->nsects);
75 blocksize = ix->blocksize;
76 for(i = 0; i < ix->nsects; i++){
78 if(namecmp(is->index, ix->name) != 0) {
79 seterr(ECorrupt, "%s: index name is %s, not %s",
80 sects[i]->part->name, is->index, ix->name);
85 if(is->blocksize != blocksize) {
86 seterr(ECorrupt, "%s: blocksize is %d, not %d",
87 sects[i]->part->name, (int)is->blocksize, (int)blocksize);
90 if(is->tabsize != tabsize) {
91 seterr(ECorrupt, "%s: tabsize is %d, not %d",
92 sects[i]->part->name, (int)is->tabsize, (int)tabsize);
95 if(namecmp(is->name, ix->smap[i].name) != 0) {
96 seterr(ECorrupt, "%s: name is %s, not %s",
97 sects[i]->part->name, is->name, ix->smap[i].name);
100 if(is->start != ix->smap[i].start || is->stop != ix->smap[i].stop) {
101 seterr(ECorrupt, "%s: range is %lld,%lld, not %lld,%lld",
102 sects[i]->part->name, is->start, is->stop,
103 ix->smap[i].start, ix->smap[i].stop);
106 if(is->start > is->stop) {
107 seterr(ECorrupt, "%s: invalid range %lld,%lld",
108 sects[i]->part->name, is->start, is->stop);
111 if(is->start != last || is->start > is->stop) {
112 seterr(ECorrupt, "%s: range %lld-%lld, but last section ended at %lld",
113 sects[i]->part->name, is->start, is->stop, last);
118 ix->tabsize = tabsize;
121 if(initindex1(ix) < 0){
126 ix->arenas = MKNZ(Arena*, ix->narenas);
127 if(maparenas(ix->amap, ix->arenas, ix->narenas, ix->name) < 0){
136 initindex1(Index *ix)
140 ix->div = (((u64int)1 << 32) + ix->buckets - 1) / ix->buckets;
141 buckets = (((u64int)1 << 32) - 1) / ix->div + 1;
142 if(buckets != ix->buckets){
143 seterr(ECorrupt, "inconsistent math for divisor and buckets in %s", ix->name);
158 seterr(EOk, "no sections in index %s", ix->name);
161 b = alloczblock(ix->tabsize, 1, ix->blocksize);
163 seterr(EOk, "can't write index configuration: out of memory");
167 if(outputindex(&f, ix) < 0){
168 seterr(EOk, "can't make index configuration: table storage too small %d", ix->tabsize);
172 for(i = 0; i < ix->nsects; i++){
173 if(writepart(ix->sects[i]->part, ix->sects[i]->tabbase, b->data, ix->tabsize) < 0
174 || flushpart(ix->sects[i]->part) < 0){
175 seterr(EOk, "can't write index: %r");
182 for(i = 0; i < ix->nsects; i++)
183 if(wbisect(ix->sects[i]) < 0)
190 * index: IndexMagic '\n' version '\n' name '\n' blocksize '\n' [V2: bitblocks '\n'] sections arenas
191 * version, blocksize: u32int
192 * name: max. ANameSize string
193 * sections, arenas: AMap
196 outputindex(Fmt *f, Index *ix)
198 if(fmtprint(f, "%s\n%ud\n%s\n%ud\n", IndexMagic, ix->version, ix->name, ix->blocksize) < 0
199 || outputamap(f, ix->smap, ix->nsects) < 0
200 || outputamap(f, ix->amap, ix->narenas) < 0)
206 parseindex(IFile *f, Index *ix)
216 if(s == nil || strcmp(s, IndexMagic) != 0){
217 seterr(ECorrupt, "bad index magic for %s", f->name);
224 if(ifileu32int(f, &v) < 0){
225 seterr(ECorrupt, "syntax error: bad version number in %s", f->name);
229 if(ix->version != IndexVersion){
230 seterr(ECorrupt, "bad version number in %s", f->name);
237 if(ifilename(f, ix->name) < 0){
238 seterr(ECorrupt, "syntax error: bad index name in %s", f->name);
245 if(ifileu32int(f, &v) < 0){
246 seterr(ECorrupt, "syntax error: bad block size number in %s", f->name);
251 if(parseamap(f, &amn) < 0)
256 if(parseamap(f, &amn) < 0)
265 * initialize an entirely new index
268 newindex(char *name, ISect **sects, int n)
273 u32int div, ub, xb, start, stop, blocksize, tabsize;
277 seterr(EOk, "creating index with no index sections");
282 * compute the total buckets available in the index,
283 * and the total buckets which are used.
286 blocksize = sects[0]->blocksize;
287 tabsize = sects[0]->tabsize;
288 for(i = 0; i < n; i++){
290 * allow index, start, and stop to be set if index is correct
291 * and start and stop are what we would have picked.
292 * this allows calling fmtindex to reformat the index after
293 * replacing a bad index section with a freshly formatted one.
294 * start and stop are checked below.
296 if(sects[i]->index[0] != '\0' && strcmp(sects[i]->index, name) != 0){
297 seterr(EOk, "creating new index using non-empty section %s", sects[i]->name);
300 if(blocksize != sects[i]->blocksize){
301 seterr(EOk, "%s has block size %d, but %s has %d",
302 sects[0]->part->name, (int)blocksize,
303 sects[i]->part->name, (int)sects[i]->blocksize);
306 if(tabsize != sects[i]->tabsize){
307 seterr(EOk, "%s has table size %d, but %s has %d",
308 sects[0]->part->name, (int)tabsize,
309 sects[i]->part->name, (int)sects[i]->tabsize);
312 nb += sects[i]->blocks;
316 * check for duplicate names
318 for(i = 0; i < n; i++){
319 for(j = i + 1; j < n; j++){
320 if(namecmp(sects[i]->name, sects[j]->name) == 0){
321 seterr(EOk, "%s and %s both have section name %s",
322 sects[i]->part->name,
323 sects[j]->part->name,
330 if(nb >= ((u64int)1 << 32)){
331 fprint(2, "%s: index is 2^32 blocks or more; ignoring some of it\n",
333 nb = ((u64int)1 << 32) - 1;
336 div = (((u64int)1 << 32) + nb - 1) / nb;
338 fprint(2, "%s: index divisor %d too coarse; "
339 "index larger than needed, ignoring some of it\n",
342 nb = (((u64int)1 << 32) - 1) / (100 - 1);
344 ub = (((u64int)1 << 32) - 1) / div + 1;
346 seterr(EBug, "index initialization math wrong");
352 * initialize each of the index sections
353 * and the section map table
355 smap = MKNZ(AMap, n);
357 seterr(EOk, "can't create new index: out of memory");
361 for(i = 0; i < n; i++){
362 stop = start + sects[i]->blocks - xb / n;
366 if(sects[i]->start != 0 || sects[i]->stop != 0)
367 if(sects[i]->start != start || sects[i]->stop != stop){
368 seterr(EOk, "creating new index using non-empty section %s", sects[i]->name);
372 sects[i]->start = start;
373 sects[i]->stop = stop;
374 namecp(sects[i]->index, name);
376 smap[i].start = start;
378 namecp(smap[i].name, sects[i]->name);
383 * initialize the index itself
387 seterr(EOk, "can't create new index: out of memory");
391 ix->version = IndexVersion;
392 namecp(ix->name, name);
396 ix->blocksize = blocksize;
398 ix->tabsize = tabsize;
401 if(initindex1(ix) < 0){
410 initisect(Part *part)
416 b = alloczblock(HeadSize, 0, 0);
417 if(b == nil || readpart(part, PartBlank, b->data, HeadSize) < 0){
418 seterr(EAdmin, "can't read index section header: %r");
428 ok = unpackisect(is, b->data);
431 seterr(ECorrupt, "corrupted index section header: %r");
436 if(is->version != ISectVersion1 && is->version != ISectVersion2){
437 seterr(EAdmin, "unknown index section version %d", is->version);
442 return initisect1(is);
446 newisect(Part *part, u32int vers, char *name, u32int blocksize, u32int tabsize)
455 namecp(is->name, name);
458 is->blocksize = blocksize;
461 tabbase = (PartBlank + HeadSize + blocksize - 1) & ~(blocksize - 1);
462 is->blockbase = (tabbase + tabsize + blocksize - 1) & ~(blocksize - 1);
463 is->blocks = is->part->size / blocksize - is->blockbase / blocksize;
465 if(is->version == ISectVersion2){
467 is->bucketmagic = fastrand();
468 }while(is->bucketmagic==0);
478 * initialize the computed parameters for an index
481 initisect1(ISect *is)
485 is->buckmax = (is->blocksize - IBucketSize) / IEntrySize;
486 is->blocklog = u64log2(is->blocksize);
487 if(is->blocksize != (1 << is->blocklog)){
488 seterr(ECorrupt, "illegal non-power-of-2 bucket size %d\n", is->blocksize);
492 partblocksize(is->part, is->blocksize);
493 is->tabbase = (PartBlank + HeadSize + is->blocksize - 1) & ~(is->blocksize - 1);
494 if(is->tabbase >= is->blockbase){
495 seterr(ECorrupt, "index section config table overlaps bucket storage");
499 is->tabsize = is->blockbase - is->tabbase;
500 v = is->part->size & ~(u64int)(is->blocksize - 1);
501 if(is->blockbase + (u64int)is->blocks * is->blocksize != v){
502 seterr(ECorrupt, "invalid blocks in index section %s", is->name);
509 if(is->stop - is->start > is->blocks){
510 seterr(ECorrupt, "index section overflows available space");
514 if(is->start > is->stop){
515 seterr(ECorrupt, "invalid index section range");
528 b = alloczblock(HeadSize, 1, 0);
534 if(packisect(is, b->data) < 0){
535 seterr(ECorrupt, "can't make index section header: %r");
539 if(writepart(is->part, PartBlank, b->data, HeadSize) < 0 || flushpart(is->part) < 0){
540 seterr(EAdmin, "can't write index section header: %r");
567 for(i = 0; i < ix->nsects; i++)
568 freeisect(ix->sects[i]);
575 * write a clump to an available arena in the index
576 * and return the address of the clump within the index.
577 ZZZ question: should this distinguish between an arena
578 filling up and real errors writing the clump?
581 writeiclump(Index *ix, Clump *c, u8int *clbuf)
588 trace(TraceLump, "writeiclump enter");
590 for(i = ix->mapalloc; i < ix->narenas; i++){
591 a = writeaclump(ix->arenas[i], c, clbuf);
594 ia.addr = ix->amap[i].start + a;
595 ia.type = c->info.type;
596 ia.size = c->info.uncsize;
597 ia.blocks = (c->info.size + ClumpSize + (1<<ABlockLog) - 1) >> ABlockLog;
598 as.arena = ix->arenas[i];
600 as.stats = as.arena->memstats;
601 insertscore(c->info.score, &ia, IEDirty, &as);
602 qunlock(&ix->writing);
603 trace(TraceLump, "writeiclump exit");
607 qunlock(&ix->writing);
609 seterr(EAdmin, "no space left in arenas");
610 trace(TraceLump, "writeiclump failed");
615 * convert an arena index to an relative arena address
618 amapitoa(Index *ix, u64int a, u64int *aa)
626 if(ix->amap[m].start <= a)
633 if(a > ix->amap[l].stop){
634 for(i=0; i<ix->narenas; i++)
635 print("arena %d: %llux - %llux\n", i, ix->amap[i].start, ix->amap[i].stop);
636 print("want arena %d for %llux\n", l, a);
637 seterr(ECrash, "unmapped address passed to amapitoa");
641 if(ix->arenas[l] == nil){
642 seterr(ECrash, "unmapped arena selected in amapitoa");
645 *aa = a - ix->amap[l].start;
646 return ix->arenas[l];
650 * convert an arena index to the bounds of the containing arena group.
653 amapitoag(Index *ix, u64int a, u64int *gstart, u64int *glimit, int *g)
658 arena = amapitoa(ix, a, &aa);
661 if(arenatog(arena, aa, gstart, glimit, g) < 0)
669 iaddrcmp(IAddr *ia1, IAddr *ia2)
671 return ia1->type != ia2->type
672 || ia1->size != ia2->size
673 || ia1->blocks != ia2->blocks
674 || ia1->addr != ia2->addr;
678 * lookup the score in the partition
680 * nothing needs to be explicitly locked:
681 * only static parts of ix are used, and
682 * the bucket is locked by the DBlock lock.
685 loadientry(Index *ix, u8int *score, int type, IEntry *ie)
695 trace(TraceLump, "loadientry enter");
700 qunlock(&stats.lock);
703 if(!inbloomfilter(mainindex->bloom, score)){
704 trace(TraceLump, "loadientry bloomhit");
708 trace(TraceLump, "loadientry loadibucket");
709 b = loadibucket(ix, score, &is, &buck, &ib);
710 trace(TraceLump, "loadientry loadedibucket");
714 if(okibucket(&ib, is) < 0){
715 trace(TraceLump, "loadientry badbucket");
719 h = bucklook(score, type, ib.data, ib.n);
722 trace(TraceLump, "loadientry found");
723 unpackientry(ie, &ib.data[h]);
727 trace(TraceLump, "loadientry notfound");
728 addstat(StatBloomFalseMiss, 1);
731 trace(TraceLump, "loadientry exit");
736 okibucket(IBucket *ib, ISect *is)
738 if(ib->n <= is->buckmax)
741 seterr(EICorrupt, "corrupted disk index bucket: n=%ud max=%ud, range=[%lud,%lud)",
742 ib->n, is->buckmax, is->start, is->stop);
747 * look for score within data;
748 * return 1 | byte index of matching index,
749 * or 0 | index of least element > score
752 bucklook(u8int *score, int otype, u8int *data, int n)
754 int i, r, l, m, h, c, cc, type;
759 type = vttodisktype(otype);
765 for(i = 0; i < VtScoreSize; i++){
776 cc = data[h + IEntryTypeOff];
777 if(type != cc && type != -1){
788 return l * IEntrySize;
792 * compare two IEntries; consistent with bucklook
795 ientrycmp(const void *vie1, const void *vie2)
802 for(i = 0; i < VtScoreSize; i++){
811 v1 = ie1[IEntryTypeOff];
812 v2 = ie2[IEntryTypeOff];
822 * find the number of the index section holding bucket #buck
825 indexsect0(Index *ix, u32int buck)
833 if(ix->sects[m]->start <= buck)
842 * load the index block at bucket #buck
845 loadibucket0(Index *ix, u32int buck, ISect **pis, u32int *pbuck, IBucket *ib, int mode)
850 is = ix->sects[indexsect0(ix, buck)];
851 if(buck < is->start || is->stop <= buck){
852 seterr(EAdmin, "index lookup out of range: %ud not found in index\n", buck);
857 if((b = getdblock(is->part, is->blockbase + ((u64int)buck << is->blocklog), mode)) == nil)
865 unpackibucket(ib, b->data, is->bucketmagic);
870 * find the number of the index section holding score
873 indexsect1(Index *ix, u8int *score)
875 return indexsect0(ix, hashbits(score, 32) / ix->div);
879 * load the index block responsible for score.
882 loadibucket1(Index *ix, u8int *score, ISect **pis, u32int *pbuck, IBucket *ib)
884 return loadibucket0(ix, hashbits(score, 32)/ix->div, pis, pbuck, ib, OREAD);
888 indexsect(Index *ix, u8int *score)
890 return indexsect1(ix, score);
894 loadibucket(Index *ix, u8int *score, ISect **pis, u32int *pbuck, IBucket *ib)
896 return loadibucket1(ix, score, pis, pbuck, ib);