Skip to content

Commit

Permalink
Merge pull request #109 from TRON-US/feature/checkMultiDBRuntime
Browse files Browse the repository at this point in the history
Feature/check multi db runtime
  • Loading branch information
Eric Chen authored Apr 21, 2020
2 parents d244545 + 8096227 commit 8da3cda
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion utils/grpc/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *RuntimeServer) CheckRuntime(ctx context.Context, req *sharedpb.SignedRu
}

//check runtime in shared
res, err := utils.CheckRuntime(ctx, req, connection)
res, err := utils.CheckDBConnection(ctx, req, connection)
if err != nil {
return nil, err
}
Expand Down
36 changes: 27 additions & 9 deletions utils/grpc/setup_server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package grpc

import (
"context"
"fmt"
"net"

"github.com/tron-us/go-btfs-common/controller"
"github.com/tron-us/go-btfs-common/protos/escrow"
"github.com/tron-us/go-btfs-common/protos/guard"
"github.com/tron-us/go-btfs-common/protos/hub"
"github.com/tron-us/go-btfs-common/protos/shared"
"github.com/tron-us/go-btfs-common/protos/status"
"github.com/tron-us/go-btfs-common/utils"
"github.com/tron-us/go-common/v2/db"
"net"

"github.com/tron-us/go-common/v2/constant"
"github.com/tron-us/go-common/v2/log"
Expand Down Expand Up @@ -64,13 +66,29 @@ func (s *GrpcServer) GrpcServer(port string, dbURLs map[string]string, rdURL str

s.lis = lis

s.CreateServer(s.serverName, options...).
CreateHealthServer().
RegisterServer(server).
RegisterHealthServer().
WithReflection().
WithGracefulTermDetectAndExec().
AcceptConnection()
done := make(chan bool)

go func() {
s.CreateServer(s.serverName, options...).
CreateHealthServer().
RegisterServer(server).
RegisterHealthServer().
WithReflection().
WithGracefulTermDetectAndExec().
AcceptConnection()
done <- true
}()

ctx := context.Background()
req := new(shared.SignedRuntimeInfoRequest)
connection := db.ConnectionUrls{RdURL: rdURL, PgURL: dbURLs}

_, err = utils.CheckDBConnection(ctx, req, connection)
if err != nil {
log.Panic("Got error", zap.Error(err))
}

<-done

return s
}
Expand Down
55 changes: 38 additions & 17 deletions utils/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,73 @@ import (
const DBURLDNE = "DB URL does not exist !!"
const RDURLDNE = "RD URL does not exist !!"

func CheckRuntime(ctx context.Context, runtime *sharedpb.SignedRuntimeInfoRequest, connection db.ConnectionUrls) (*sharedpb.RuntimeInfoReport, error) {
func CheckDBConnection(ctx context.Context, runtime *sharedpb.SignedRuntimeInfoRequest, connection db.ConnectionUrls) (*sharedpb.RuntimeInfoReport, error) {
// db runtime
report := new(sharedpb.RuntimeInfoReport)
report.Status = sharedpb.RuntimeInfoReport_RUNNING
report.DbStatusExtra = map[string]string{}
report.DbStatusExtra = make(map[string]string)

for key, url := range connection.PgURL {
// Assume the database connection is healthy
report.DbStatusExtra[key] = DBURLDNE

if url != "" {
// Log the connection string
pgdb := postgres.CreateTGPGDB(url)
log.Info("Postgres",
zap.String("name", key),
zap.String("user", pgdb.Options().User),
zap.String("host", pgdb.Options().Addr),
zap.String("db", pgdb.Options().Database),
)

// Check postgres dbWrite
PGDBWrite := postgres.CreateTGPGDB(url)
if err := PGDBWrite.Ping(); err != nil {
if err := pgdb.Ping(); err != nil {
report.DbStatusExtra[key] = constant.DBWriteConnectionError
report.Status = sharedpb.RuntimeInfoReport_SICK
log.Error(constant.DBWriteConnectionError, zap.Error(err))
}
// Check postgres dbRead
PGDBRead := postgres.CreateTGPGDB(url)
if err := PGDBRead.Ping(); err != nil {
report.DbStatusExtra[key] = constant.DBReadConnectionError
report.Status = sharedpb.RuntimeInfoReport_SICK
log.Error(constant.DBReadConnectionError, zap.Error(err))
}
// Assume the database connection is healthy

// Set the database connection is healthy
report.DbStatusExtra[key] = constant.DBConnectionHealthy
} else {
report.DbStatusExtra[key] = DBURLDNE
}

// Log status
log.Info(key + ":" + report.DbStatusExtra[key])
}

// Assume the redis connection is not present
report.RdStatusExtra = RDURLDNE

// Check redis environment variable
if connection.RdURL != "" {
// Parse redis url
opts, errParse := redis.ParseRedisURL(connection.RdURL)
if errParse != nil {
log.Error(constant.RDURLParseError, zap.Error(errParse))
}

// Log connection string
log.Info("Redis URL",
zap.String("host", opts.Addr),
zap.String("db", string(opts.DB)),
)

// Check redis connection
errConn := redis.CheckRedisConnection(redis.NewRedisConn(opts))
if errConn != nil {
report.RdStatusExtra = constant.RDConnectionError
report.Status = sharedpb.RuntimeInfoReport_SICK
log.Error(constant.RDConnectionError, zap.Error(errConn))
}
// Assume the redis connection is healthy

// Set redis connection to healthy
report.RdStatusExtra = constant.RDConnectionHealthy
} else {
report.RdStatusExtra = RDURLDNE
}

// Log status
log.Info(report.RdStatusExtra)

// Remaining fields will be populated by the calling service
// Reserve: only pass fatal error to higher level
return report, nil
Expand Down
12 changes: 6 additions & 6 deletions utils/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
}
}

func TestCheckRuntimeDB(t *testing.T) {
func TestCheckDBConnection(t *testing.T) {

//setup connection (postgres) object
pgConMaps := map[string]string{"DB_URL_STATUS": config.DbStatusURL, "DB_URL_GUARD": config.DbGuardURL}
Expand All @@ -34,7 +34,7 @@ func TestCheckRuntimeDB(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
shared := new(sharedpb.SignedRuntimeInfoRequest)
runtimeInfoReportPass, err := CheckRuntime(ctx, shared, connection)
runtimeInfoReportPass, err := CheckDBConnection(ctx, shared, connection)
assert.Nil(t, err, zap.Error(err))
assert.True(t, strings.Contains(runtimeInfoReportPass.DbStatusExtra["DB_URL_STATUS"], constant.DBConnectionHealthy), "connection not successful")
assert.True(t, strings.Contains(runtimeInfoReportPass.DbStatusExtra["DB_URL_GUARD"], constant.DBConnectionHealthy), "connection not successful")
Expand All @@ -45,12 +45,12 @@ func TestCheckRuntimeDB(t *testing.T) {
PgURL: pgEmptyConMaps,
RdURL: "",
}
runtimeInfoReportFail, err := CheckRuntime(ctx, shared, emptyConnection)
runtimeInfoReportFail, err := CheckDBConnection(ctx, shared, emptyConnection)
assert.Nil(t, err, zap.Error(err))
assert.True(t, strings.Contains(runtimeInfoReportFail.DbStatusExtra["DB_URL_STATUS"], "DB URL does not exist !!"), "DB URL does not exist !!")
assert.True(t, strings.Contains(runtimeInfoReportFail.DbStatusExtra["DB_URL_GUARD"], "DB URL does not exist !!"), "DB URL does not exist !!")
}
func TestCheckRuntimeRD(t *testing.T) {
func TestCheckDBConnectionRD(t *testing.T) {
const RDURLDNE = "RD URL does not exist !!"
//setup connection (redis) object
var connection = db.ConnectionUrls{
Expand All @@ -60,12 +60,12 @@ func TestCheckRuntimeRD(t *testing.T) {
shared := new(sharedpb.SignedRuntimeInfoRequest)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
runtimeInfoReportPass, err := CheckRuntime(ctx, shared, connection)
runtimeInfoReportPass, err := CheckDBConnection(ctx, shared, connection)
assert.Nil(t, err, zap.Error(err))
assert.True(t, strings.Contains(runtimeInfoReportPass.RdStatusExtra, constant.RDConnectionHealthy), "Redis is not running")
//disable connection string
connection.RdURL = ""
runtimeInfoReportFail, err := CheckRuntime(ctx, shared, connection)
runtimeInfoReportFail, err := CheckDBConnection(ctx, shared, connection)
assert.Nil(t, err, zap.Error(err))
assert.True(t, strings.Contains(runtimeInfoReportFail.RdStatusExtra, RDURLDNE), "Redis connection is still provided, error!")
}

0 comments on commit 8da3cda

Please sign in to comment.