Skip to content

Commit

Permalink
fix bug: fix test run fail(ByteStorage#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjcsjc123 committed Jul 6, 2023
2 parents cc4f2cc + cb5e81f commit 96ba7bf
Show file tree
Hide file tree
Showing 15 changed files with 992 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ jobs:
- uses: actions/first-interaction@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: "Message that will be displayed on users' first issue"
pr-message: "Message that will be displayed on users' first pull request"
issue-message: "Thank you very much for your issue and we will discuss it."
pr-message: "Thank you very much for your contribution, we will promptly review your code if there are no errors and pass ci. We will merge your pull request into the master branch."
7 changes: 5 additions & 2 deletions cluster/region/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func ReturnNewDB() *engine.DB {
func destroyRegion(r Region) {
// Close the region's database
db := r.(*TestRegionStruct).db
_ = db.Close()
err := os.RemoveAll(dirpath)
err := db.Close()
if err != nil {
return
}
err = os.RemoveAll(dirpath)
if err != nil {
return
}
Expand Down
2 changes: 2 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Options struct {
IndexType IndexerType
FIOType FIOType
Addr string // Addr DB Server Listen
IsCli bool // Is Cli
}

// IteratorOptions is the configuration for index iteration.
Expand Down Expand Up @@ -57,6 +58,7 @@ var DefaultOptions = Options{
IndexType: ART,
FIOType: MmapIOType,
Addr: DefaultAddr,
IsCli: false,
}

var DefaultIteratorOptions = IteratorOptions{
Expand Down
8 changes: 6 additions & 2 deletions engine/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)

func TestDB_WriteBatch(t *testing.T) {
opts := config.DefaultOptions
dir, _ := os.MkdirTemp("", "flydb-batch-1")
opts.DirPath = dir
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -51,7 +52,7 @@ func TestDB_WriteBatchRestart(t *testing.T) {
dir, _ := os.MkdirTemp("", "flydb-batch-2")
opts.DirPath = dir
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand All @@ -77,6 +78,8 @@ func TestDB_WriteBatchRestart(t *testing.T) {
assert.Nil(t, err)

db2, err := NewDB(opts)
time.Sleep(time.Millisecond * 100)
defer db2.Clean()
assert.Nil(t, err)

_, err = db2.Get(randkv.GetTestKey(1))
Expand All @@ -91,6 +94,7 @@ func TestDB_WriteBatch1(t *testing.T) {
dir := "/tmp/batch-3"
opts.DirPath = dir
db, err := NewDB(opts)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down
1 change: 1 addition & 0 deletions engine/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
opts.DirPath = filepath.Join("benchmark", "flydbtest")

FlyDB, err = flydb.NewFlyDB(opts)
defer FlyDB.Clean()
if err != nil {
panic(err)
}
Expand Down
52 changes: 43 additions & 9 deletions engine/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import (
"github.com/ByteStorage/FlyDB/lib/const"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"io"
"net"
"os"
"os/signal"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"syscall"
)

// DB represents a FlyDB database instance,
Expand Down Expand Up @@ -102,10 +105,6 @@ func NewDB(options config.Options) (*DB, error) {
if err := db.loadIndexFromDataFiles(); err != nil {
return nil, err
}

// start grpc server
db.startGrpcServer()

return db, nil
}

Expand Down Expand Up @@ -141,8 +140,6 @@ func (db *DB) Close() error {
return err
}
}
// close grpc server
db.server.Stop()
return nil
}

Expand Down Expand Up @@ -263,8 +260,8 @@ func (db *DB) setActiveDataFile() error {
// Get Read data according to the key
func (db *DB) Get(key []byte) ([]byte, error) {
zap.L().Info("get", zap.ByteString("key", key))
db.lock.Lock()
defer db.lock.Unlock()
db.lock.RLock()
defer db.lock.RUnlock()

// Determine the validity of the key
if len(key) == 0 {
Expand Down Expand Up @@ -556,7 +553,8 @@ func (db *DB) loadIndexFromDataFiles() error {
return nil
}

func (db *DB) startGrpcServer() {
// StartGrpcServer starts the grpc server
func (db *DB) StartGrpcServer() {
listener, err := net.Listen("tcp", db.options.Addr)
if err != nil {
panic("tcp listen error: " + err.Error())
Expand All @@ -571,5 +569,41 @@ func (db *DB) startGrpcServer() {
panic("db server start error: " + err.Error())
}
}()
//wait for server start
for {
conn, err := grpc.Dial(db.options.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
continue
}
err = conn.Close()
if err != nil {
continue
}
break
}
if db.options.IsCli {
// graceful shutdown
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL)

<-sig
}
}

func (db *DB) StopGrpcServer() {
if db.server != nil {
db.server.Stop()
}
}

// Clean the DB data directory after the test is complete
func (db *DB) Clean() {
if db != nil {
db.StopGrpcServer()
_ = db.Close()
err := os.RemoveAll(db.options.DirPath)
if err != nil {
panic(err)
}
}
}
35 changes: 14 additions & 21 deletions engine/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ import (
"os"
"sync"
"testing"
"time"
)

// Destroy the DB data directory after the test is complete
func destroyDB(db *DB) {
if db != nil {
if db.activeFile != nil {
_ = db.Close()
}
err := os.RemoveAll(db.options.DirPath)
if err != nil {
panic(err)
}
}
}

func TestNewFlyDB(t *testing.T) {
opts := config.DefaultOptions
dir, _ := os.MkdirTemp("", "flydb")
opts.DirPath = dir
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)
}
Expand All @@ -40,7 +28,7 @@ func TestDB_Put(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -82,6 +70,7 @@ func TestDB_Put(t *testing.T) {

// Restart the database
db2, err := NewDB(opts)
defer db2.Clean()
assert.Nil(t, err)
assert.NotNil(t, db2)
val4 := randkv.RandomValue(128)
Expand All @@ -98,7 +87,7 @@ func TestDB_ConcurrentPut(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -177,6 +166,7 @@ func TestDB_ConcurrentPut(t *testing.T) {

// Restart the database
db2, err := NewDB(opts)
defer db2.Clean()

val1, err := db2.Get(randkv.GetTestKey(1))
assert.Nil(t, err)
Expand Down Expand Up @@ -214,7 +204,7 @@ func TestDB_Get(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -263,6 +253,7 @@ func TestDB_Get(t *testing.T) {

// Restart the database
db2, err := NewDB(opts)
defer db2.Clean()
val6, err := db2.Get(randkv.GetTestKey(11))
assert.Nil(t, err)
assert.NotNil(t, val6)
Expand All @@ -284,7 +275,7 @@ func TestDB_Delete(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -322,6 +313,8 @@ func TestDB_Delete(t *testing.T) {

// Restart the database
db2, err := NewDB(opts)
time.Sleep(time.Millisecond * 100)
defer db2.Clean()
_, err = db2.Get(randkv.GetTestKey(11))
assert.Equal(t, _const.ErrKeyNotFound, err)

Expand All @@ -336,7 +329,7 @@ func TestDB_GetListKeys(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -370,7 +363,7 @@ func TestDB_Fold(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -413,7 +406,7 @@ func TestDB_Sync(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down
3 changes: 3 additions & 0 deletions engine/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestDB_NewIterator(t *testing.T) {
dir, _ := os.MkdirTemp("", "flydb-iterator-1")
opt.DirPath = dir
db, err := NewDB(opt)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand All @@ -26,6 +27,7 @@ func TestDB_Iterator_One_Value(t *testing.T) {
dir, _ := os.MkdirTemp("", "flydb-iterator-2")
opt.DirPath = dir
db, err := NewDB(opt)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand All @@ -45,6 +47,7 @@ func TestDB_Iterator_Multi_Value(t *testing.T) {
dir, _ := os.MkdirTemp("", "flydb-iterator-3")
opt.DirPath = dir
db, err := NewDB(opt)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down
6 changes: 3 additions & 3 deletions engine/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestDB_Merge(t *testing.T) {
dir, _ := os.MkdirTemp("", "flydb-merge-1")
opts.DirPath = dir
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand All @@ -29,7 +29,7 @@ func TestDB_Merge2(t *testing.T) {
opts.DataFileSize = 32 * 1024 * 1024
opts.DirPath = dir
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down Expand Up @@ -68,7 +68,7 @@ func TestDB_Merge3(t *testing.T) {
opts.DataFileSize = 32 * 1024 * 1024
opts.DirPath = dir
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down
2 changes: 1 addition & 1 deletion engine/put_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestPutAndGet(t *testing.T) {
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024
db, err := NewDB(opts)
defer destroyDB(db)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

Expand Down
Loading

0 comments on commit 96ba7bf

Please sign in to comment.