Skip to content

Commit

Permalink
Merge pull request #259 from CovenantSQL/feature/beta_wait_create_tx
Browse files Browse the repository at this point in the history
 Add transaction hash as return value in create/drop
  • Loading branch information
leventeliu committed Feb 19, 2019
2 parents b0c46a4 + 970277b commit ec05168
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 27 deletions.
4 changes: 2 additions & 2 deletions client/clientbench_test.go
Expand Up @@ -66,7 +66,7 @@ func BenchmarkCovenantSQLDriver(b *testing.B) {
// create
meta := ResourceMeta{}
meta.Node = 3
dsn, err := Create(meta)
_, dsn, err := Create(meta)
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func BenchmarkCovenantSQLDriver(b *testing.B) {
if err != nil {
b.Fatal(err)
}
err = Drop(dsn)
_, err = Drop(dsn)
if err != nil {
b.Fatal(err)
}
Expand Down
9 changes: 5 additions & 4 deletions client/driver.go
Expand Up @@ -144,8 +144,8 @@ func Init(configFile string, masterKey []byte) (err error) {
return
}

// Create send create database operation to block producer.
func Create(meta ResourceMeta) (dsn string, err error) {
// Create sends create database operation to block producer.
func Create(meta ResourceMeta) (txHash hash.Hash, dsn string, err error) {
if atomic.LoadUint32(&driverInitialized) == 0 {
err = ErrNotInitialized
return
Expand Down Expand Up @@ -202,6 +202,7 @@ func Create(meta ResourceMeta) (dsn string, err error) {
return
}

txHash = req.Tx.Hash()
cfg := NewConfig()
cfg.DatabaseID = string(proto.FromAccountAndNonce(clientAddr, uint32(nonceResp.Nonce)))
dsn = cfg.FormatDSN()
Expand Down Expand Up @@ -268,8 +269,8 @@ func WaitBPDatabaseCreation(
}
}

// Drop send drop database operation to block producer.
func Drop(dsn string) (err error) {
// Drop sends drop database operation to block producer.
func Drop(dsn string) (txHash hash.Hash, err error) {
if atomic.LoadUint32(&driverInitialized) == 0 {
err = ErrNotInitialized
return
Expand Down
14 changes: 7 additions & 7 deletions client/driver_test.go
Expand Up @@ -118,20 +118,20 @@ func TestCreate(t *testing.T) {
var err error
var dsn string

dsn, err = Create(ResourceMeta{})
_, dsn, err = Create(ResourceMeta{})
So(err, ShouldEqual, ErrNotInitialized)

// fake driver initialized
atomic.StoreUint32(&driverInitialized, 1)
dsn, err = Create(ResourceMeta{})
_, dsn, err = Create(ResourceMeta{})
So(err, ShouldNotBeNil)
// reset driver not initialized
atomic.StoreUint32(&driverInitialized, 0)

stopTestService, _, err = startTestService()
So(err, ShouldBeNil)
defer stopTestService()
dsn, err = Create(ResourceMeta{})
_, dsn, err = Create(ResourceMeta{})
So(err, ShouldBeNil)
dsnCfg, err := ParseDSN(dsn)
So(err, ShouldBeNil)
Expand Down Expand Up @@ -171,16 +171,16 @@ func TestDrop(t *testing.T) {
var stopTestService func()
var err error

err = Drop("covenantsql://db")
_, err = Drop("covenantsql://db")
So(err, ShouldEqual, ErrNotInitialized)

stopTestService, _, err = startTestService()
So(err, ShouldBeNil)
defer stopTestService()
err = Drop("covenantsql://db")
_, err = Drop("covenantsql://db")
So(err, ShouldBeNil)

err = Drop("invalid dsn")
_, err = Drop("invalid dsn")
So(err, ShouldNotBeNil)
})
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestWaitDBCreation(t *testing.T) {
err = WaitDBCreation(ctx, "covenantsql://db")
So(err, ShouldBeNil)

dsn, err = Create(ResourceMeta{})
_, dsn, err = Create(ResourceMeta{})
So(err, ShouldBeNil)
err = WaitDBCreation(ctx, dsn)
So(err, ShouldNotBeNil)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cql-adapter/storage/covenantsql.go
Expand Up @@ -37,7 +37,7 @@ func (s *CovenantSQLStorage) Create(nodeCnt int) (dbID string, err error) {
meta.Node = uint16(nodeCnt)

var dsn string
if dsn, err = client.Create(meta); err != nil {
if _, dsn, err = client.Create(meta); err != nil {
return
}

Expand All @@ -54,7 +54,7 @@ func (s *CovenantSQLStorage) Create(nodeCnt int) (dbID string, err error) {
func (s *CovenantSQLStorage) Drop(dbID string) (err error) {
cfg := client.NewConfig()
cfg.DatabaseID = dbID
err = client.Drop(cfg.FormatDSN())
_, err = client.Drop(cfg.FormatDSN())
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cql-fuse/block_test.go
Expand Up @@ -256,7 +256,7 @@ func initTestDB() (*sql.DB, func()) {
// create
meta := client.ResourceMeta{}
meta.Node = 1
dsn, err := client.Create(meta)
_, dsn, err := client.Create(meta)
if err != nil {
log.Errorf("create db failed: %v", err)
return nil, stopNodes
Expand Down
10 changes: 5 additions & 5 deletions cmd/cql-minerd/integration_test.go
Expand Up @@ -395,7 +395,7 @@ func TestFullProcess(t *testing.T) {
t.Fatalf("wait for chain service failed: %v", err)
}

dsn, err := client.Create(meta)
_, dsn, err := client.Create(meta)
So(err, ShouldBeNil)
dsnCfg, err := client.ParseDSN(dsn)
So(err, ShouldBeNil)
Expand Down Expand Up @@ -769,7 +769,7 @@ func benchMiner(b *testing.B, minerCount uint16, bypassSign bool, useEventualCon
// create
meta := client.ResourceMeta{
ResourceMeta: types.ResourceMeta{
Node: minerCount,
Node: minerCount,
UseEventualConsistency: useEventualConsistency,
},
}
Expand All @@ -781,7 +781,7 @@ func benchMiner(b *testing.B, minerCount uint16, bypassSign bool, useEventualCon
b.Fatalf("wait for chain service failed: %v", err)
}

dsn, err = client.Create(meta)
_, dsn, err = client.Create(meta)
So(err, ShouldBeNil)
log.Infof("the created database dsn is %v", dsn)
err = ioutil.WriteFile(dsnFile, []byte(dsn), 0666)
Expand All @@ -804,7 +804,7 @@ func benchMiner(b *testing.B, minerCount uint16, bypassSign bool, useEventualCon

benchDB(b, db, minerCount > 0)

err = client.Drop(dsn)
_, err = client.Drop(dsn)
So(err, ShouldBeNil)
time.Sleep(5 * time.Second)
stopNodes()
Expand Down Expand Up @@ -892,7 +892,7 @@ func benchOutsideMinerWithTargetMinerList(
b.Fatalf("wait for chain service failed: %v", err)
}

dsn, err = client.Create(meta)
_, dsn, err = client.Create(meta)
So(err, ShouldBeNil)
log.Infof("the created database dsn is %v", dsn)

Expand Down
4 changes: 2 additions & 2 deletions cmd/cql-observer/observation_test.go
Expand Up @@ -696,10 +696,10 @@ func TestFullProcess(t *testing.T) {
So(ensureSuccess(res.Int("block", "height")), ShouldBeGreaterThanOrEqualTo, 0)
log.Info(err, res)

err = client.Drop(dsn)
_, err = client.Drop(dsn)
So(err, ShouldBeNil)

err = client.Drop(dsn2)
_, err = client.Drop(dsn2)
So(err, ShouldBeNil)

observerCmd.Cmd.Process.Signal(os.Interrupt)
Expand Down
13 changes: 9 additions & 4 deletions cmd/cql/main.go
Expand Up @@ -316,12 +316,17 @@ func main() {
dropDB = cfg.FormatDSN()
}

if err := client.Drop(dropDB); err != nil {
txHash, err := client.Drop(dropDB)
if err != nil {
// drop database failed
log.WithField("db", dropDB).WithError(err).Error("drop database failed")
return
}

if waitTxConfirmation {
wait(txHash)
}

// drop database success
log.Infof("drop database %#v success", dropDB)
return
Expand All @@ -346,21 +351,21 @@ func main() {
meta.Node = uint16(nodeCnt)
}

dsn, err := client.Create(meta)
txHash, dsn, err := client.Create(meta)
if err != nil {
log.WithError(err).Error("create database failed")
os.Exit(-1)
return
}

if waitTxConfirmation {
wait(txHash)
var ctx, cancel = context.WithTimeout(context.Background(), waitTxConfirmationMaxDuration)
defer cancel()
err = client.WaitDBCreation(ctx, dsn)
if err != nil {
log.WithError(err).Error("create database failed durating creation")
log.WithError(err).Error("wait database failed during creation")
os.Exit(-1)
return
}
}

Expand Down

0 comments on commit ec05168

Please sign in to comment.