Removing logger from realisConfig. Debugging can now be turned on by running or building with the realis_debug flag. e.g. go run -tags="gocql_debug"
This commit is contained in:
parent
d99082d1a1
commit
35111af66d
3 changed files with 44 additions and 37 deletions
5
debug_off.go
Normal file
5
debug_off.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// +build !realis_debug
|
||||||
|
|
||||||
|
package realis
|
||||||
|
|
||||||
|
const realisDebug = false
|
5
debug_on.go
Normal file
5
debug_on.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// +build realis_debug
|
||||||
|
|
||||||
|
package realis
|
||||||
|
|
||||||
|
const realisDebug = true
|
71
realis.go
71
realis.go
|
@ -27,13 +27,13 @@ import (
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"bytes"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.apache.org/thrift.git/lib/go/thrift"
|
"git.apache.org/thrift.git/lib/go/thrift"
|
||||||
"github.com/paypal/gorealis/gen-go/apache/aurora"
|
"github.com/paypal/gorealis/gen-go/apache/aurora"
|
||||||
"github.com/paypal/gorealis/response"
|
"github.com/paypal/gorealis/response"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "1.0.4"
|
const VERSION = "1.0.4"
|
||||||
|
@ -85,7 +85,6 @@ type RealisConfig struct {
|
||||||
backoff *Backoff
|
backoff *Backoff
|
||||||
transport thrift.TTransport
|
transport thrift.TTransport
|
||||||
protoFactory thrift.TProtocolFactory
|
protoFactory thrift.TProtocolFactory
|
||||||
logger *log.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Backoff struct {
|
type Backoff struct {
|
||||||
|
@ -187,26 +186,31 @@ func newTBinTransport(url string, timeout int) (thrift.TTransport, error) {
|
||||||
|
|
||||||
func NewRealisClient(options ...ClientOption) (Realis, error) {
|
func NewRealisClient(options ...ClientOption) (Realis, error) {
|
||||||
config := &RealisConfig{}
|
config := &RealisConfig{}
|
||||||
|
|
||||||
|
// Default configs
|
||||||
|
config.timeoutms = 10000
|
||||||
|
config.backoff = &defaultBackoff
|
||||||
|
|
||||||
|
// Override default configs where necessary
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
opt(config)
|
opt(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.logger == nil {
|
// Use a no-op logger if we didn't compile in debug mode.
|
||||||
buf := new(bytes.Buffer)
|
var logger *log.Logger
|
||||||
config.logger = log.New(buf, "gorealis debug: ", 0)
|
if realisDebug {
|
||||||
config.logger.SetOutput(os.Stdout)
|
logger = log.New(os.Stdout, "gorealis: ", log.Ltime|log.Ldate)
|
||||||
|
} else {
|
||||||
|
logger = log.New(ioutil.Discard, "", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
config.logger.Println("Number of options applied to config: ", len(options))
|
logger.Println("Number of options applied to config: ", len(options))
|
||||||
|
|
||||||
//Default timeout
|
|
||||||
if config.timeoutms == 0 {
|
|
||||||
config.timeoutms = 10000
|
|
||||||
}
|
|
||||||
//Set default Transport to JSON if needed.
|
//Set default Transport to JSON if needed.
|
||||||
if !config.jsonTransport && !config.binTransport {
|
if !config.jsonTransport && !config.binTransport {
|
||||||
config.jsonTransport = true
|
config.jsonTransport = true
|
||||||
}
|
}
|
||||||
|
|
||||||
var url string
|
var url string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -218,10 +222,10 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "LeaderFromZK error")
|
return nil, errors.Wrap(err, "LeaderFromZK error")
|
||||||
}
|
}
|
||||||
config.logger.Println("Scheduler URL from ZK: ", url)
|
logger.Println("Scheduler URL from ZK: ", url)
|
||||||
} else if config.url != "" {
|
} else if config.url != "" {
|
||||||
url = config.url
|
url = config.url
|
||||||
config.logger.Println("Scheduler URL: ", url)
|
logger.Println("Scheduler URL: ", url)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("Incomplete Options -- url or cluster required")
|
return nil, errors.New("Incomplete Options -- url or cluster required")
|
||||||
}
|
}
|
||||||
|
@ -231,9 +235,9 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Error creating realis")
|
return nil, errors.Wrap(err, "Error creating realis")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.transport = trans
|
config.transport = trans
|
||||||
config.protoFactory = thrift.NewTJSONProtocolFactory()
|
config.protoFactory = thrift.NewTJSONProtocolFactory()
|
||||||
|
|
||||||
} else if config.binTransport {
|
} else if config.binTransport {
|
||||||
trans, err := newTBinTransport(url, config.timeoutms)
|
trans, err := newTBinTransport(url, config.timeoutms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -243,26 +247,19 @@ func NewRealisClient(options ...ClientOption) (Realis, error) {
|
||||||
config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Printf("gorealis config url: %+v\n", config.url)
|
||||||
|
|
||||||
//Basic Authentication.
|
//Basic Authentication.
|
||||||
if config.username != "" && config.password != "" {
|
if config.username != "" && config.password != "" {
|
||||||
AddBasicAuth(config, config.username, config.password)
|
AddBasicAuth(config, config.username, config.password)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set defaultBackoff if required.
|
|
||||||
if config.backoff == nil {
|
|
||||||
config.backoff = &defaultBackoff
|
|
||||||
} else {
|
|
||||||
defaultBackoff = *config.backoff
|
|
||||||
config.logger.Printf("Updating default backoff : %+v\n", *config.backoff)
|
|
||||||
}
|
|
||||||
|
|
||||||
config.logger.Printf("gorealis config url: %+v\n", config.url)
|
|
||||||
|
|
||||||
return &realisClient{
|
return &realisClient{
|
||||||
config: config,
|
config: config,
|
||||||
client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory),
|
client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory),
|
||||||
readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory),
|
readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory),
|
||||||
adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory)}, nil
|
adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory),
|
||||||
|
logger: logger}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,7 +362,7 @@ func basicAuth(username, password string) string {
|
||||||
|
|
||||||
func (r *realisClient) ReestablishConn() error {
|
func (r *realisClient) ReestablishConn() error {
|
||||||
//close existing connection..
|
//close existing connection..
|
||||||
r.config.logger.Println("ReestablishConn begin ....")
|
r.logger.Println("ReestablishConn begin ....")
|
||||||
r.Close()
|
r.Close()
|
||||||
//First check cluster object for re-establish; if not available then try with scheduler url.
|
//First check cluster object for re-establish; if not available then try with scheduler url.
|
||||||
//var config *RealisConfig
|
//var config *RealisConfig
|
||||||
|
@ -378,7 +375,7 @@ func (r *realisClient) ReestablishConn() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("LeaderFromZK error: %+v\n ", err)
|
fmt.Errorf("LeaderFromZK error: %+v\n ", err)
|
||||||
}
|
}
|
||||||
r.config.logger.Println("ReestablishConn url: ", url)
|
r.logger.Println("ReestablishConn url: ", url)
|
||||||
if r.config.jsonTransport {
|
if r.config.jsonTransport {
|
||||||
trans, err := newTJSONTransport(url, r.config.timeoutms)
|
trans, err := newTJSONTransport(url, r.config.timeoutms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -395,7 +392,7 @@ func (r *realisClient) ReestablishConn() error {
|
||||||
r.config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
r.config.protoFactory = thrift.NewTBinaryProtocolFactoryDefault()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.config.logger.Println("error creating config: ", err)
|
r.logger.Println("error creating config: ", err)
|
||||||
}
|
}
|
||||||
// Configured for basic-auth
|
// Configured for basic-auth
|
||||||
AddBasicAuth(r.config, r.config.username, r.config.password)
|
AddBasicAuth(r.config, r.config.username, r.config.password)
|
||||||
|
@ -404,7 +401,7 @@ func (r *realisClient) ReestablishConn() error {
|
||||||
r.adminClient = aurora.NewAuroraAdminClientFactory(r.config.transport, r.config.protoFactory)
|
r.adminClient = aurora.NewAuroraAdminClientFactory(r.config.transport, r.config.protoFactory)
|
||||||
} else if r.config.url != "" && r.config.username != "" && r.config.password != "" {
|
} else if r.config.url != "" && r.config.username != "" && r.config.password != "" {
|
||||||
//Re-establish using scheduler url.
|
//Re-establish using scheduler url.
|
||||||
r.config.logger.Println("ReestablishConn url: ", r.config.url)
|
r.logger.Println("ReestablishConn url: ", r.config.url)
|
||||||
if r.config.jsonTransport {
|
if r.config.jsonTransport {
|
||||||
trans, err := newTJSONTransport(url, r.config.timeoutms)
|
trans, err := newTJSONTransport(url, r.config.timeoutms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -425,14 +422,14 @@ func (r *realisClient) ReestablishConn() error {
|
||||||
r.readonlyClient = aurora.NewReadOnlySchedulerClientFactory(r.config.transport, r.config.protoFactory)
|
r.readonlyClient = aurora.NewReadOnlySchedulerClientFactory(r.config.transport, r.config.protoFactory)
|
||||||
r.adminClient = aurora.NewAuroraAdminClientFactory(r.config.transport, r.config.protoFactory)
|
r.adminClient = aurora.NewAuroraAdminClientFactory(r.config.transport, r.config.protoFactory)
|
||||||
} else {
|
} else {
|
||||||
r.config.logger.Println(" Missing Data for ReestablishConn ")
|
r.logger.Println(" Missing Data for ReestablishConn ")
|
||||||
r.config.logger.Println(" r.config.cluster: ", r.config.cluster)
|
r.logger.Println(" r.config.cluster: ", r.config.cluster)
|
||||||
r.config.logger.Println(" r.config.username: ", r.config.username)
|
r.logger.Println(" r.config.username: ", r.config.username)
|
||||||
r.config.logger.Println(" r.config.passwd: ", r.config.password)
|
r.logger.Println(" r.config.passwd: ", r.config.password)
|
||||||
r.config.logger.Println(" r.config.url: ", r.config.url)
|
r.logger.Println(" r.config.url: ", r.config.url)
|
||||||
return errors.New(" Missing Data for ReestablishConn ")
|
return errors.New(" Missing Data for ReestablishConn ")
|
||||||
}
|
}
|
||||||
r.config.logger.Printf(" config url before return: %+v\n", r.config.url)
|
r.logger.Printf(" config url before return: %+v\n", r.config.url)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1096,7 +1093,7 @@ func (r *realisClient) MaintenanceStatus(hosts ...string) (*aurora.Response, *au
|
||||||
// Experienced an connection error
|
// Experienced an connection error
|
||||||
err1 := r.ReestablishConn()
|
err1 := r.ReestablishConn()
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
r.config.logger.Println("error in re-establishing connection: ", err1)
|
r.logger.Println("error in re-establishing connection: ", err1)
|
||||||
}
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue