code beautify

This commit is contained in:
Carlos Salguero
2019-08-15 23:46:40 -03:00
parent 47d36cfaa8
commit e03706942c
6 changed files with 71 additions and 29 deletions

View File

@@ -2,7 +2,9 @@ ARG GOLANG_DOCKERHUB_TAG
FROM golang:$GOLANG_DOCKERHUB_TAG
RUN wget https://repo.percona.com/apt/percona-release_0.1-7.stretch_all.deb && dpkg -i percona-release_0.1-7.stretch_all.deb
RUN apt-get update && apt-get install -y percona-server-mongodb-36-server
RUN apt-get update && apt-get install -y --no-install-recommends percona-server-mongodb-36-server \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /go/src/github.com/percona/percona-backup-mongodb
COPY . .

View File

@@ -9,7 +9,7 @@ while [ $tries -lt $max_tries ]; do
[ $? == 0 ] && break
echo "# INFO: 'init' has not completed, retrying check in $sleep_secs secs (try $tries/$max_tries)"
sleep $sleep_secs
tries=$(($tries + 1))
tries=$((tries + 1))
done
if [ $tries -ge $max_tries ]; then
echo "# ERROR: reached max tries $max_tries, exiting"

View File

@@ -36,32 +36,51 @@ const (
)
var (
// MongoDBHost is the hostname. Since it runs locally, it is localhost
MongoDBHost = "127.0.0.1"
//
MongoDBShard1ReplsetName = os.Getenv(envMongoDBShard1ReplsetName)
MongoDBShard1PrimaryPort = os.Getenv(envMongoDBShard1PrimaryPort)
// MongoDBShard1ReplsetName Replicaset name for shard 1
MongoDBShard1ReplsetName = os.Getenv(envMongoDBShard1ReplsetName)
// MongoDBShard1PrimaryPort is the port for the primary instance of shard 1
MongoDBShard1PrimaryPort = os.Getenv(envMongoDBShard1PrimaryPort)
// MongoDBShard1Secondary1Port is the port for the secondary instance 1 of shard 1
MongoDBShard1Secondary1Port = os.Getenv(envMongoDBShard1Secondary1Port)
// MongoDBShard1Secondary2Port is the port for the secondary instance 2 of shard 1
MongoDBShard1Secondary2Port = os.Getenv(envMongoDBShard1Secondary2Port)
//
MongoDBShard2ReplsetName = os.Getenv(envMongoDBShard2ReplsetName)
MongoDBShard2PrimaryPort = os.Getenv(envMongoDBShard2PrimaryPort)
// MongoDBShard2ReplsetName Replicaset name for shard 2
MongoDBShard2ReplsetName = os.Getenv(envMongoDBShard2ReplsetName)
// MongoDBShard2PrimaryPort is the port for the primary instance of shard 2
MongoDBShard2PrimaryPort = os.Getenv(envMongoDBShard2PrimaryPort)
// MongoDBShard2Secondary1Port is the port for the secondary instance 1 of shard 2
MongoDBShard2Secondary1Port = os.Getenv(envMongoDBShard2Secondary1Port)
// MongoDBShard2Secondary2Port is the port for the secondary instance 1 of shard 2
MongoDBShard2Secondary2Port = os.Getenv(envMongoDBShard2Secondary2Port)
//
MongoDBShard3ReplsetName = os.Getenv(envMongoDBShard3ReplsetName)
MongoDBShard3PrimaryPort = os.Getenv(envMongoDBShard3PrimaryPort)
// MongoDBShard3ReplsetName Replicaset name for the 3rd cluster
MongoDBShard3ReplsetName = os.Getenv(envMongoDBShard3ReplsetName)
// MongoDBShard3PrimaryPort is the port for the primary instance of 3rd cluster (non-sharded)
MongoDBShard3PrimaryPort = os.Getenv(envMongoDBShard3PrimaryPort)
// MongoDBShard3Secondary1Port is the port for the secondary instance 1 on the 3rd cluster
MongoDBShard3Secondary1Port = os.Getenv(envMongoDBShard3Secondary1Port)
// MongoDBShard3Secondary2Port is the port for the secondary instance 2 on the 3rd cluster
MongoDBShard3Secondary2Port = os.Getenv(envMongoDBShard3Secondary2Port)
//
// MongoDBConfigsvrReplsetName Replicaset name for the config servers
MongoDBConfigsvrReplsetName = os.Getenv(envMongoDBConfigsvrReplsetName)
MongoDBConfigsvr1Port = os.Getenv(envMongoDBConfigsvr1Port)
MongoDBConfigsvr2Port = os.Getenv(envMongoDBConfigsvr2Port)
MongoDBConfigsvr3Port = os.Getenv(envMongoDBConfigsvr3Port)
//
// MongoDBConfigsvr1Port Config server primary's port
MongoDBConfigsvr1Port = os.Getenv(envMongoDBConfigsvr1Port)
// MongoDBConfigsvr2Port = os.Getenv(envMongoDBConfigsvr2Port)
// MongoDBConfigsvr3Port = os.Getenv(envMongoDBConfigsvr3Port)
// MongoDBMongosPort mongos port
MongoDBMongosPort = os.Getenv(envMongoDBMongosPort)
MongoDBUser = os.Getenv(envMongoDBUser)
MongoDBPassword = os.Getenv(envMongoDBPassword)
MongoDBTimeout = time.Duration(10) * time.Second
// MongoDBUser username for all instances
MongoDBUser = os.Getenv(envMongoDBUser)
// MongoDBPassword password for all instances
MongoDBPassword = os.Getenv(envMongoDBPassword)
// MongoDBTimeout global connection timeout
MongoDBTimeout = time.Duration(10) * time.Second
// test mongodb hosts map
hosts = map[string]map[string]string{
@@ -86,9 +105,12 @@ var (
}
// The values here are just placeholders. They will be overridden by init()
basedir string
MongoDBSSLDir = "../docker/test/ssl"
basedir string
// MongoDBSSLDir is the directory having the SSL certs
MongoDBSSLDir = "../docker/test/ssl"
// MongoDBSSLPEMKeyFile PEM file used on all instances
MongoDBSSLPEMKeyFile = filepath.Join(MongoDBSSLDir, "client.pem")
// MongoDBSSLCACertFile CA file used on all instances
MongoDBSSLCACertFile = filepath.Join(MongoDBSSLDir, "rootCA.crt")
)
@@ -112,6 +134,8 @@ func BaseDir() string {
return basedir
}
// GetMongoDBAddr returns the address of an instance by replicaset name and instance type like
// (rs1, primary) or (rs1, secondary1)
func GetMongoDBAddr(rs, name string) string {
if _, ok := hosts[rs]; !ok {
return ""
@@ -123,6 +147,7 @@ func GetMongoDBAddr(rs, name string) string {
return ""
}
// GetMongoDBReplsetAddrs return the addresses of all instances for a replicaset name
func GetMongoDBReplsetAddrs(rs string) []string {
addrs := []string{}
if _, ok := hosts[rs]; !ok {

View File

@@ -24,16 +24,19 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)
// Enable enabled the mongo profiler
func Enable(ctx context.Context, client *mongo.Client) error {
res := client.Database("admin").RunCommand(ctx, primitive.M{"profile": 2})
return res.Err()
}
// Disable disables the mongo profiler
func Disable(ctx context.Context, client *mongo.Client) error {
res := client.Database("admin").RunCommand(ctx, primitive.M{"profile": 0})
return res.Err()
}
// Drop drops the system.profile collection for clean up
func Drop(ctx context.Context, client *mongo.Client) error {
return client.Database("").Collection("system.profile").Drop(ctx)
}

View File

@@ -10,19 +10,22 @@ import (
"github.com/percona/percona-toolkit/src/go/mongolib/proto"
)
type explain struct {
// Explain contains unexported fields of the query explainer
type Explain struct {
ctx context.Context
client *mongo.Client
}
func New(ctx context.Context, client *mongo.Client) *explain {
return &explain{
// New returns a new instance of the query explainer
func New(ctx context.Context, client *mongo.Client) *Explain {
return &Explain{
ctx: ctx,
client: client,
}
}
func (e *explain) Explain(db string, query []byte) ([]byte, error) {
// Run runs mongo's explain for the selected database/query
func (e *Explain) Run(db string, query []byte) ([]byte, error) {
var err error
var eq proto.ExampleQuery
@@ -45,10 +48,10 @@ func (e *explain) Explain(db string, query []byte) ([]byte, error) {
return nil, err
}
resultJson, err := bson.MarshalExtJSON(result, true, true)
resultJSON, err := bson.MarshalExtJSON(result, true, true)
if err != nil {
return nil, fmt.Errorf("explain: unable to encode explain result of %s: %s", string(query), err)
}
return resultJson, nil
return resultJSON, nil
}

View File

@@ -16,6 +16,7 @@ var (
DocsBufferSize = 100
)
// Profiler interface
type Profiler interface {
GetLastError() error
QueriesChan() chan stats.Queries
@@ -25,6 +26,7 @@ type Profiler interface {
Stop()
}
// Profile has unexported variables for the profiler
type Profile struct {
// dependencies
cursor *mongo.Cursor
@@ -48,6 +50,7 @@ type Profile struct {
stopWaitGroup sync.WaitGroup
}
// NewProfiler returns a new instance of the profiler interface
func NewProfiler(cursor *mongo.Cursor, filters []filter.Filter, ticker <-chan time.Time, stats Stats) Profiler {
return &Profile{
cursor: cursor,
@@ -62,14 +65,17 @@ func NewProfiler(cursor *mongo.Cursor, filters []filter.Filter, ticker <-chan ti
}
}
// GetLastError return the latest error
func (p *Profile) GetLastError() error {
return p.lastError
}
// QueriesChan returns the channels used to read the queries from the profiler
func (p *Profile) QueriesChan() chan stats.Queries {
return p.queriesChan
}
// Start the profiler
func (p *Profile) Start(ctx context.Context) {
p.lock.Lock()
defer p.lock.Unlock()
@@ -81,6 +87,7 @@ func (p *Profile) Start(ctx context.Context) {
}
}
// Stop the profiler
func (p *Profile) Stop() {
p.lock.Lock()
defer p.lock.Unlock()
@@ -94,6 +101,7 @@ func (p *Profile) Stop() {
}
}
// TimeoutsChan returns the channels to receive timeout signals
func (p *Profile) TimeoutsChan() <-chan time.Time {
p.lock.Lock()
defer p.lock.Unlock()
@@ -111,7 +119,7 @@ func (p *Profile) getData(ctx context.Context) {
p.FlushQueries()
case <-p.stopChan:
// Close the iterator to break the loop on getDocs
p.cursor.Close(ctx)
p.lastError = p.cursor.Close(ctx)
return
}
}
@@ -138,10 +146,11 @@ func (p *Profile) getDocs(ctx context.Context) {
if !valid {
continue
}
p.stats.Add(doc)
p.lastError = p.stats.Add(doc)
}
}
// FlushQueries clean all the queries from the queries chan
func (p *Profile) FlushQueries() {
p.queriesChan <- p.stats.Queries()
p.stats.Reset()