mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
This is logically two separate things, but the individual changes are thoroughly intertwined in the code. The first change is a logical change to the design of the snapshot queue, which is that it now adjusts the maxOpN the background scan targets, allowing it to lower that value over time when things are quiet. We do this because it turns out that on large data sets, this can make a factor-of-four difference in memory usage! So, in general, on a quiet system, each pass through the holder aims for about 1/4 of the existing fragments to get snapshotted. When there's more load, we adjust those values up. We also make the snapshot queue a bit less chatty, to make testing less annoying -- we only print stats if the queue enqueues at least two snapshots, or skips any. The second change is threading the holder through things. We've always threaded the logger through, and then added the snapshot queue, and some of the Inspect-related work led to wanting to have a way to thread options through, so what if we just threaded the holder itself through, and removed the direct copying around of the logger, snapshot queue, and so on. Similarly, everything can now use holder.PartitionN instead of having to get its own copy of PartitionN handed out to each index. This does imply ensuring that test cases always get a reasonable default holder. This is a precursor to adding additional information to the holder, such as whether it's in a special read-only mode, which would imply not modifying on-disk files. This is already semi-supported for the specific case of the background snapshot queue and cache flushing, which are attached to the (created in a previous commit) new holder Activate method, instead of being automatic on holder Open. The change to a snapshot queue can also cause races in tests, because the fragment.Clean method's "sanity check" accesses a fragment without a lock. Fix that. Since there's a couple of t.Fatalf(), but we need to release the lock before closing, we use an anonymous function with a defer to handle that. Whee!
96 lines
2.5 KiB
Go
96 lines
2.5 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 test
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/pilosa/pilosa/v2"
|
|
)
|
|
|
|
// Field represents a test wrapper for pilosa.Field.
|
|
type Field struct {
|
|
*pilosa.Field
|
|
}
|
|
|
|
// newField returns a new instance of Field d/0.
|
|
func newField(opts pilosa.FieldOption) *Field {
|
|
path, err := ioutil.TempDir("", "pilosa-field-")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
field, err := pilosa.NewField(pilosa.NewHolder(pilosa.DefaultPartitionN), path, "i", "f", opts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &Field{Field: field}
|
|
}
|
|
|
|
// mustOpenField returns a new, opened field at a temporary path. Panic on error.
|
|
func mustOpenField(opts pilosa.FieldOption) *Field {
|
|
f := newField(opts)
|
|
if err := f.Open(); err != nil {
|
|
panic(err)
|
|
}
|
|
return f
|
|
}
|
|
|
|
// close closes the field and removes the underlying data.
|
|
func (f *Field) close() error { // nolint: unparam
|
|
defer os.RemoveAll(f.Path())
|
|
return f.Field.Close()
|
|
}
|
|
|
|
// reopen closes the index and reopens it.
|
|
func (f *Field) reopen() error {
|
|
var err error
|
|
if err := f.Field.Close(); err != nil {
|
|
return err
|
|
}
|
|
|
|
path, index, name := f.Path(), f.Index(), f.Name()
|
|
f.Field, err = pilosa.NewField(pilosa.NewHolder(pilosa.DefaultPartitionN), path, index, name, pilosa.OptFieldTypeDefault())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := f.Open(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Ensure field can set its cache
|
|
func TestField_SetCacheSize(t *testing.T) {
|
|
f := mustOpenField(pilosa.OptFieldTypeDefault())
|
|
defer f.close()
|
|
cacheSize := uint32(100)
|
|
|
|
// Set & retrieve field cache size.
|
|
if err := f.SetCacheSize(cacheSize); err != nil {
|
|
t.Fatal(err)
|
|
} else if q := f.CacheSize(); q != cacheSize {
|
|
t.Fatalf("unexpected field cache size: %d", q)
|
|
}
|
|
|
|
// Reload field and verify that it is persisted.
|
|
if err := f.reopen(); err != nil {
|
|
t.Fatal(err)
|
|
} else if q := f.CacheSize(); q != cacheSize {
|
|
t.Fatalf("unexpected field cache size (reopen): %d", q)
|
|
}
|
|
}
|