-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix flushing logic and add functional test case. (#113)
- Loading branch information
1 parent
e5ab829
commit e0d1341
Showing
9 changed files
with
180 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package storage | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"time" | ||
|
||
"github.com/xichen2020/eventdb/document" | ||
"github.com/xichen2020/eventdb/parser/json" | ||
"github.com/xichen2020/eventdb/parser/json/value" | ||
"github.com/xichen2020/eventdb/sharding" | ||
|
||
"github.com/m3db/m3cluster/shard" | ||
"github.com/pborman/uuid" | ||
) | ||
|
||
const ( | ||
testJSONDataFilePath = "./testdata/testdata.json" | ||
testDataPath = "./testdata/eventdb" | ||
testMaxDocsPerSegment = 1 // Keep this # low for flushing purposes. | ||
testDataSeparator = '\n' | ||
) | ||
|
||
var ( | ||
testNamespace = []byte("testNamespace") | ||
testNumShards = 8 | ||
) | ||
|
||
type testDatabase struct { | ||
Database | ||
} | ||
|
||
func newTestDatabase() (*testDatabase, error) { | ||
namespaces := []NamespaceMetadata{ | ||
NamespaceMetadata{ | ||
id: testNamespace, | ||
opts: NewNamespaceOptions(), | ||
}, | ||
} | ||
|
||
shardIDs := make([]uint32, 0, testNumShards) | ||
for i := 0; i < testNumShards; i++ { | ||
shardIDs = append(shardIDs, uint32(i)) | ||
} | ||
shards := sharding.NewShards(shardIDs, shard.Available) | ||
hashFn := sharding.DefaultHashFn(testNumShards) | ||
shardSet, err := sharding.NewShardSet(shards, hashFn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbOpts := NewOptions(). | ||
SetNamespaceFieldName(string(testNamespace)). | ||
SetMaxNumDocsPerSegment(testMaxDocsPerSegment) | ||
|
||
db := NewDatabase(namespaces, shardSet, dbOpts) | ||
if err := db.Open(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &testDatabase{db}, nil | ||
} | ||
|
||
// Close removes all filesystem resources owned by the test database. | ||
func (tdb *testDatabase) Close() error { | ||
tdb.Database.Close() | ||
return os.RemoveAll(testDataPath) | ||
} | ||
|
||
func createTestDocuments() ([]byte, []document.Document, error) { | ||
p := json.NewParser(json.NewOptions()) | ||
|
||
data, err := ioutil.ReadFile(testJSONDataFilePath) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var ( | ||
end = bytes.IndexByte(data, testDataSeparator) | ||
docs = make([]document.Document, 0) | ||
) | ||
if end < 0 { | ||
end = len(data) | ||
} | ||
for end > 0 { | ||
v, err := p.ParseBytes(data[:end]) | ||
if err != nil { | ||
err = fmt.Errorf("cannot parse document %s: %v", data, err) | ||
return nil, nil, err | ||
} | ||
|
||
fieldIter := value.NewFieldIterator(v) | ||
doc := document.Document{ | ||
ID: uuid.NewUUID(), | ||
TimeNanos: time.Now().UnixNano(), // This doesn't need to line up to what's in the raw data. | ||
FieldIter: fieldIter, | ||
RawData: data, | ||
} | ||
docs = append(docs, doc) | ||
|
||
data = data[end+1:] | ||
end = bytes.IndexByte(data, testDataSeparator) | ||
} | ||
return []byte(testNamespace), docs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package storage | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFlush(t *testing.T) { | ||
tdb, err := newTestDatabase() | ||
require.Nil(t, err) | ||
defer tdb.Close() | ||
|
||
ns, docs, err := createTestDocuments() | ||
require.Nil(t, err) | ||
|
||
err = tdb.WriteBatch(ns, docs) | ||
require.Nil(t, err) | ||
|
||
// Get a reference to the underlying fs manager and force a flush. | ||
db := tdb.Database.(*db) | ||
mediator := db.mediator.(*mediator) | ||
fsMgr := mediator.databaseFileSystemManager.(*fileSystemManager) | ||
err = fsMgr.databaseFlushManager.Flush() | ||
require.Nil(t, err) | ||
} |
Oops, something went wrong.