mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-05 16:15:56 +00:00
This code represents an attempt at providing reliable tracking of whether any bitmaps still in use have access to a given block of mmapped data, allowing us to unmap the data when nothing is using it anymore. The basic approach is as follows: Each mmap is associated with a new object, called a "generation". A generation reflects a particular instance of a given file being mapped. When a bitmap is built from an mmapped data source, the bitmap is given a pointer to the generation as its Source. When bitmap operations combine containers from other bitmaps, they produce new bitmaps that are tagged with the combined set of sources. When we snapshot a file, or for some other reason wish to remap it, the corresponding bitmap has all its containers updated to use the new storage, and the bitmap's source is changed. However, previously-handed-out containers might still have references to the old storage. Those containers would be in bitmaps with the old source. After a bunch of study of trying to reference-count and track this, I realized: We don't actually need to do that, because we already have something suitable for determining whether anything can reach a given object. It's the garbage collector. So we set a finalizer on the generation object, which handles unmapping. There's additional sanity-checks here to confirm things like "we thought this generation should be expiring", and we track timestamps. We could also have things check whether a given bitmap's source was marked as obsolete "a while ago", but that isn't implemented yet. There's a debug version of this which tracks finalization, creation, and ending timestamps, and has a call to provide diagnostics for this. Identical generation IDs get separated out with random suffixes in this case -- there's sometimes a second or third instance of the same name due to a holder closing and reopening, but this basically only happens in testing. Note that generations are still used even when there's no mmapping, but unless debugging is turned on, they shouldn't propagate much -- we don't consider a generation to be the source of a bitmap unless the bitmap actually mapped things from that generation's mmapped storage, or debugging is on. There's a couple of other, possibly more subtle, changes and bug fixes that got caught by the testing on this: * If a fragment is partially opened and then opening some later part fails, we close the earlier parts before returning the error so we aren't leaving it partially open. * Several operations on segments which were requesting that a frozen copy of a bitmap be created are now actually *replacing* their bitmap with the frozen bitmap, rather than discarding it. * intersectRunRun, if it decides to create an array or bitmap, will yield that container instead of discarding it. And why all of this? Why, so we can actually implement the thing where when a fragment has a valid roaring bitmap, but the ops log is corrupt, we can truncate the corrupt part of the ops log and reopen it. Which I did. When the generationdebug build tag is in use, every generation has a finalizer all the time. When it's not, they only get finalizers when we expect them to be done -- say, when closing a fragment. This is because finalizers appear to be possibly-expensive. There's some logical cleanup to openStorage here, dividing part of its work into applyStorage and importStorage, which have a common case for handling "there's no data in this file".
222 lines
7.2 KiB
Go
222 lines
7.2 KiB
Go
// Copyright 2017 Pilosa Corp.
|
|
//
|
|
// 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.
|
|
|
|
package roaring
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// UnmarshalBinary decodes b from a binary-encoded byte slice. data can be in
|
|
// either official roaring format or Pilosa's roaring format.
|
|
func (b *Bitmap) UnmarshalBinary(data []byte) error {
|
|
if data == nil {
|
|
// Nothing to unmarshal
|
|
return nil
|
|
}
|
|
statsHit("Bitmap/UnmarshalBinary")
|
|
// reset ops/opN since we're reading new data.
|
|
b.ops = 0
|
|
b.opN = 0
|
|
fileMagic := uint32(binary.LittleEndian.Uint16(data[0:2]))
|
|
if fileMagic == MagicNumber { // if pilosa roaring
|
|
return errors.Wrap(b.unmarshalPilosaRoaring(data), "unmarshaling as pilosa roaring")
|
|
}
|
|
|
|
keyN, containerTyper, header, pos, haveRuns, err := readOfficialHeader(data)
|
|
if err != nil {
|
|
return errors.Wrap(err, "reading roaring header")
|
|
}
|
|
// Only the Pilosa roaring format has flags. The official Roaring format
|
|
// hasn't got space in its header for flags.
|
|
b.Flags = 0
|
|
|
|
b.Containers.ResetN(int(keyN))
|
|
// Descriptive header section: Read container keys and cardinalities.
|
|
for i, buf := uint(0), data[header:]; i < uint(keyN); i, buf = i+1, buf[4:] {
|
|
card := int(binary.LittleEndian.Uint16(buf[2:4])) + 1
|
|
b.Containers.PutContainerValues(
|
|
uint64(binary.LittleEndian.Uint16(buf[0:2])),
|
|
containerTyper(i, card), /// container type voodo with isRunBitmap
|
|
card,
|
|
true)
|
|
}
|
|
|
|
// Read container offsets and attach data.
|
|
if haveRuns {
|
|
err := readWithRuns(b, data, pos, keyN)
|
|
if err != nil {
|
|
return errors.Wrap(err, "reading offsets from official roaring format")
|
|
}
|
|
} else {
|
|
err := readOffsets(b, data, pos, keyN)
|
|
if err != nil {
|
|
return errors.Wrap(err, "reading official roaring format")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func readOffsets(b *Bitmap, data []byte, pos int, keyN uint32) error {
|
|
|
|
citer, _ := b.Containers.Iterator(0)
|
|
for i, buf := 0, data[pos:]; i < int(keyN); i, buf = i+1, buf[4:] {
|
|
// Verify the offset is fully formed
|
|
if len(buf) < 4 {
|
|
return fmt.Errorf("insufficient data for offsets: len=%d", len(buf))
|
|
}
|
|
offset := binary.LittleEndian.Uint32(buf[0:4])
|
|
// Verify the offset is within the bounds of the input data.
|
|
if int(offset) >= len(data) {
|
|
return fmt.Errorf("offset out of bounds: off=%d, len=%d", offset, len(data))
|
|
}
|
|
|
|
// Map byte slice directly to the container data.
|
|
citer.Next()
|
|
_, c := citer.Value()
|
|
switch c.typ() {
|
|
case containerArray:
|
|
c.setArray((*[0xFFFFFFF]uint16)(unsafe.Pointer(&data[offset]))[:c.N():c.N()])
|
|
case containerBitmap:
|
|
c.setBitmap((*[0xFFFFFFF]uint64)(unsafe.Pointer(&data[offset]))[:bitmapN:bitmapN])
|
|
default:
|
|
return fmt.Errorf("unsupported container type %d", c.typ())
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func readWithRuns(b *Bitmap, data []byte, pos int, keyN uint32) error {
|
|
if len(data) < pos+runCountHeaderSize {
|
|
return fmt.Errorf("insufficient data for offsets(run): len=%d", len(data))
|
|
}
|
|
citer, _ := b.Containers.Iterator(0)
|
|
for i := 0; i < int(keyN); i++ {
|
|
citer.Next()
|
|
_, c := citer.Value()
|
|
switch c.typ() {
|
|
case containerRun:
|
|
runCount := binary.LittleEndian.Uint16(data[pos : pos+runCountHeaderSize])
|
|
c.setRuns((*[0xFFFFFFF]interval16)(unsafe.Pointer(&data[pos+runCountHeaderSize]))[:runCount:runCount])
|
|
runs := c.runs()
|
|
|
|
for o := range runs { // must convert from start:length to start:end :(
|
|
runs[o].last = runs[o].start + runs[o].last
|
|
}
|
|
pos += int((runCount * interval16Size) + runCountHeaderSize)
|
|
case containerArray:
|
|
c.setArray((*[0xFFFFFFF]uint16)(unsafe.Pointer(&data[pos]))[:c.N():c.N()])
|
|
pos += int(c.N() * 2)
|
|
case containerBitmap:
|
|
c.setBitmap((*[0xFFFFFFF]uint64)(unsafe.Pointer(&data[pos]))[:bitmapN:bitmapN])
|
|
pos += bitmapN * 8
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *Bitmap) unmarshalPilosaRoaring(data []byte) error {
|
|
if len(data) < headerBaseSize {
|
|
return errors.New("data too small")
|
|
}
|
|
|
|
// Verify the first two bytes are a valid MagicNumber, and second two bytes match current storageVersion.
|
|
fileMagic := uint32(binary.LittleEndian.Uint16(data[0:2]))
|
|
fileVersion := uint32(data[2])
|
|
b.Flags = data[3]
|
|
if fileMagic != MagicNumber {
|
|
return fmt.Errorf("invalid roaring file, magic number %v is incorrect", fileMagic)
|
|
}
|
|
|
|
if fileVersion != storageVersion {
|
|
return fmt.Errorf("wrong roaring version, file is v%d, server requires v%d", fileVersion, storageVersion)
|
|
}
|
|
|
|
// Read key count in bytes sizeof(cookie)+sizeof(flag):(sizeof(cookie)+sizeof(uint32)).
|
|
keyN := binary.LittleEndian.Uint32(data[3+1 : 8])
|
|
if uint32(len(data)) < headerBaseSize+keyN*12 {
|
|
return fmt.Errorf("insufficient data for header + offsets: key-cardinality not provided for %d containers", int(keyN)/12)
|
|
}
|
|
|
|
headerSize := headerBaseSize
|
|
b.Containers.ResetN(int(keyN))
|
|
// Descriptive header section: Read container keys and cardinalities.
|
|
for i, buf := 0, data[headerSize:]; i < int(keyN); i, buf = i+1, buf[12:] {
|
|
b.Containers.PutContainerValues(
|
|
binary.LittleEndian.Uint64(buf[0:8]),
|
|
byte(binary.LittleEndian.Uint16(buf[8:10])),
|
|
int(binary.LittleEndian.Uint16(buf[10:12]))+1,
|
|
true)
|
|
}
|
|
opsOffset := headerSize + int(keyN)*12
|
|
|
|
// Read container offsets and attach data.
|
|
citer, _ := b.Containers.Iterator(0)
|
|
for i, buf := 0, data[opsOffset:]; i < int(keyN); i, buf = i+1, buf[4:] {
|
|
offset := binary.LittleEndian.Uint32(buf[0:4])
|
|
// Verify the offset is within the bounds of the input data.
|
|
if int(offset) >= len(data) {
|
|
return fmt.Errorf("offset out of bounds: off=%d, len=%d", offset, len(data))
|
|
}
|
|
|
|
// Map byte slice directly to the container data.
|
|
citer.Next()
|
|
_, c := citer.Value()
|
|
|
|
// this shouldn't happen, since we don't normally store nils.
|
|
if c == nil {
|
|
continue
|
|
}
|
|
switch c.typ() {
|
|
case containerRun:
|
|
runCount := binary.LittleEndian.Uint16(data[offset : offset+runCountHeaderSize])
|
|
c.setRuns((*[0xFFFFFFF]interval16)(unsafe.Pointer(&data[offset+runCountHeaderSize]))[:runCount:runCount])
|
|
opsOffset = int(offset) + runCountHeaderSize + len(c.runs())*interval16Size
|
|
case containerArray:
|
|
c.setArray((*[0xFFFFFFF]uint16)(unsafe.Pointer(&data[offset]))[:c.N():c.N()])
|
|
opsOffset = int(offset) + len(c.array())*2 // sizeof(uint32)
|
|
case containerBitmap:
|
|
c.setBitmap((*[0xFFFFFFF]uint64)(unsafe.Pointer(&data[offset]))[:bitmapN:bitmapN])
|
|
opsOffset = int(offset) + len(c.bitmap())*8 // sizeof(uint64)
|
|
}
|
|
}
|
|
|
|
// Read ops log until the end of the file.
|
|
buf := data[opsOffset:]
|
|
|
|
for {
|
|
// Exit when there are no more ops to parse.
|
|
if len(buf) == 0 {
|
|
break
|
|
}
|
|
// Unmarshal the op and apply it.
|
|
var opr op
|
|
if err := opr.UnmarshalBinary(buf); err != nil {
|
|
return newFileShouldBeTruncatedError(err, int64(opsOffset))
|
|
}
|
|
opr.apply(b)
|
|
// Increase the op count.
|
|
b.ops++
|
|
b.opN += opr.count()
|
|
opsOffset += opr.size()
|
|
// Move the buffer forward.
|
|
buf = data[opsOffset:]
|
|
}
|
|
|
|
return nil
|
|
}
|