Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cql repeating logs while long process #345

Merged
merged 8 commits into from May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions client/driver.go
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"math/rand"
"strings"
"sync"
Expand Down Expand Up @@ -104,8 +105,8 @@ type ResourceMeta struct {
TargetMiners []proto.AccountAddress `json:"target-miners,omitempty"` // designated miners
Node uint16 `json:"node,omitempty"` // reserved node count
Space uint64 `json:"space,omitempty"` // reserved storage space in bytes
Memory uint64 `json:"memory",omitempty` // reserved memory in bytes
LoadAvgPerCPU float64 `json:"load-avg-per-cpu",omitempty` // max loadAvg15 per CPU
Memory uint64 `json:"memory,omitempty"` // reserved memory in bytes
LoadAvgPerCPU float64 `json:"load-avg-per-cpu,omitempty"` // max loadAvg15 per CPU
EncryptionKey string `json:"encrypt-key,omitempty"` // encryption key for database instance
UseEventualConsistency bool `json:"eventual-consistency,omitempty"` // use eventual consistency replication if enabled
ConsistencyLevel float64 `json:"consistency-level,omitempty"` // customized strong consistency level
Expand Down Expand Up @@ -263,11 +264,16 @@ func WaitBPDatabaseCreation(
req = &types.QuerySQLChainProfileReq{
DBID: dbID,
}
count = 0
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
count++
fmt.Printf("\rWaiting for miner confirmation %vs", count*int(period.Seconds()))
defer fmt.Printf("\n")

if err = rpc.RequestBP(
route.MCCQuerySQLChainProfile.String(), req, nil,
); err != nil {
Expand Down Expand Up @@ -461,6 +467,7 @@ func WaitTxConfirmation(
method = route.MCCQueryTxState
req = &types.QueryTxStateReq{Hash: txHash}
resp = &types.QueryTxStateResp{}
count = 0
)
defer ticker.Stop()
for {
Expand All @@ -470,6 +477,10 @@ func WaitTxConfirmation(
}

state = resp.State

count++
fmt.Printf("\rWaiting blockproducers confirmation %vs, state: %v ", count, state)
defer fmt.Printf("\n")
log.WithFields(log.Fields{
"tx_hash": txHash,
"tx_state": state,
Expand Down
35 changes: 7 additions & 28 deletions cmd/cql/internal/create.go
Expand Up @@ -22,11 +22,7 @@ import (
"flag"
"fmt"
"math"
"os"
"strings"
"time"

pb "gopkg.in/cheggaaa/pb.v1"

"github.com/CovenantSQL/CovenantSQL/client"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
Expand Down Expand Up @@ -184,16 +180,18 @@ func runCreate(cmd *Command, args []string) {
ConsoleLog.Info("create database requested")

if waitTxConfirmation {
cancelLoading := printLoading(int(waitTxConfirmationMaxDuration / time.Second))
defer cancelLoading()

wait(txHash)
err = wait(txHash)
if err != nil {
ConsoleLog.WithError(err).Error("create database failed durating bp creation")
SetExitStatus(1)
return
}

var ctx, cancel = context.WithTimeout(context.Background(), waitTxConfirmationMaxDuration)
defer cancel()
err = client.WaitDBCreation(ctx, dsn)
if err != nil {
ConsoleLog.WithError(err).Error("create database failed durating creation")
ConsoleLog.WithError(err).Error("create database failed durating miner creation")
SetExitStatus(1)
return
}
Expand All @@ -203,22 +201,3 @@ func runCreate(cmd *Command, args []string) {
storeOneDSN(dsn)
fmt.Printf("The connecting string beginning with 'covenantsql://' could be used as a dsn for `cql console`\n or any command, or be used in website like https://web.covenantsql.io\n")
}

func printLoading(loadingMax int) func() {
var ctxLoading, cancelLoading = context.WithCancel(context.Background())
go func(ctx context.Context) {
bar := pb.StartNew(loadingMax)
bar.Output = os.Stderr
for {
select {
case <-ctx.Done():
bar.Finish()
return
default:
bar.Increment()
time.Sleep(time.Second)
}
}
}(ctxLoading)
return cancelLoading
}
1 change: 1 addition & 0 deletions cmd/cql/internal/grant.go
Expand Up @@ -139,6 +139,7 @@ func runGrant(cmd *Command, args []string) {
if waitTxConfirmation {
err = wait(txHash)
if err != nil {
ConsoleLog.WithError(err).Error("update permission failed")
SetExitStatus(1)
return
}
Expand Down
27 changes: 26 additions & 1 deletion cmd/cql/internal/idminer.go
Expand Up @@ -131,6 +131,7 @@ func nonceGen(publicKey *asymmetric.PublicKey) *mine.NonceInfo {
ConsoleLog.Infof("cpu: %#v\n", cpuCount)
stopCh := make(chan struct{})
nonceCh := make(chan mine.NonceInfo)
progressCh := make(chan int, 100)

rand.Seed(time.Now().UnixNano())
step := 256 / cpuCount
Expand All @@ -139,7 +140,7 @@ func nonceGen(publicKey *asymmetric.PublicKey) *mine.NonceInfo {
startBit := i * step
position := startBit / 64
shift := uint(startBit % 64)
ConsoleLog.Infof("position: %#v, shift: %#v, i: %#v", position, shift, i)
ConsoleLog.Debugf("position: %#v, shift: %#v, i: %#v", position, shift, i)
var start mine.Uint256
if position == 0 {
start = mine.Uint256{A: uint64(1<<shift) + uint64(rand.Uint32())}
Expand All @@ -158,6 +159,7 @@ func nonceGen(publicKey *asymmetric.PublicKey) *mine.NonceInfo {
default:
currentHash := mine.HashBlock(publicKeyBytes, j)
currentDifficulty := currentHash.Difficulty()
progressCh <- currentDifficulty
if currentDifficulty >= difficulty {
nonce := mine.NonceInfo{
Nonce: j,
Expand All @@ -171,6 +173,29 @@ func nonceGen(publicKey *asymmetric.PublicKey) *mine.NonceInfo {
}(i)
}

go func() {
var count, current int

ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for {
select {
case <-stopCh:
fmt.Printf("\n")
break
case mined := <-progressCh:
if mined > current {
current = mined
fmt.Printf("\rnonce mining %v seconds, current difficulty: %v, target difficulty: %v", count, current, difficulty)
}
case <-ticker.C:
count++
fmt.Printf("\rnonce mining %v seconds, current difficulty: %v, target difficulty: %v", count, current, difficulty)
}
}
}()

nonce := <-nonceCh
close(stopCh)

Expand Down
1 change: 1 addition & 0 deletions cmd/cql/internal/transfer.go
Expand Up @@ -121,6 +121,7 @@ func runTransfer(cmd *Command, args []string) {
if waitTxConfirmation {
err = wait(txHash)
if err != nil {
ConsoleLog.WithError(err).Error("transfer token failed")
SetExitStatus(1)
return
}
Expand Down