mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-12-11 02:04:38 +08:00
Merge pull request #446 from percona/PT-1822_pt-mongodb-summary.fails.on.standalone
PT-1822 Fixed get hostnames for standalone
This commit is contained in:
6
Gopkg.lock
generated
6
Gopkg.lock
generated
@@ -188,12 +188,12 @@
|
||||
revision = "197f4ad8db8d1b04ff408042119176907c971f0a"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:1d7e1867c49a6dd9856598ef7c3123604ea3daabf5b83f303ff457bcbc410b1d"
|
||||
digest = "1:c45802472e0c06928cd997661f2af610accd85217023b1d5f6331bebce0671d3"
|
||||
name = "github.com/pkg/errors"
|
||||
packages = ["."]
|
||||
pruneopts = ""
|
||||
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
|
||||
version = "v0.8.1"
|
||||
revision = "614d223910a179a466c1767a985424175c39b465"
|
||||
version = "v0.9.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:55dcddb2ba6ab25098ee6b96f176f39305f1fde7ea3d138e7e10bb64a5bf45be"
|
||||
|
||||
@@ -18,7 +18,7 @@ BIN_DIR=$(shell git rev-parse --show-toplevel)/bin
|
||||
SRC_DIR=$(shell git rev-parse --show-toplevel)/src/go
|
||||
LDFLAGS="-X main.Version=${VERSION} -X main.Build=${BUILD} -X main.GoVersion=${GOVERSION} -X main.Commit=${COMMIT} -s -w"
|
||||
|
||||
TEST_PSMDB_VERSION?=3.6
|
||||
TEST_PSMDB_VERSION?=4.0
|
||||
TEST_MONGODB_FLAVOR?=percona/percona-server-mongodb
|
||||
TEST_MONGODB_ADMIN_USERNAME?=admin
|
||||
TEST_MONGODB_ADMIN_PASSWORD?=admin123456
|
||||
|
||||
@@ -4,13 +4,10 @@ services:
|
||||
standalone:
|
||||
network_mode: host
|
||||
image: ${TEST_MONGODB_FLAVOR}:${TEST_PSMDB_VERSION}
|
||||
environment:
|
||||
MONGO_INITDB_ROOT_USERNAME: ${TEST_MONGODB_ADMIN_USERNAME}
|
||||
MONGO_INITDB_ROOT_PASSWORD: ${TEST_MONGODB_ADMIN_PASSWORD}
|
||||
command: --port=27017
|
||||
volumes:
|
||||
- ./docker/test/entrypoint-mongod.sh:/entrypoint.sh:ro
|
||||
- ./docker/test/entrypoint-mongod.sh:/usr/local/bin/docker-entrypoint.sh:ro
|
||||
- ./docker/test/mongod.key:/mongod.key:ro
|
||||
- ./docker/test/ssl/rootCA.crt:/rootCA.crt:ro
|
||||
- ./docker/test/ssl/mongodb.pem:/mongod.pem:ro
|
||||
s1-mongo1:
|
||||
network_mode: host
|
||||
image: ${TEST_MONGODB_FLAVOR}:${TEST_PSMDB_VERSION}
|
||||
|
||||
@@ -30,6 +30,8 @@ const (
|
||||
envMongoDBConfigsvr3Port = "TEST_MONGODB_CONFIGSVR3_PORT"
|
||||
//
|
||||
envMongoDBMongosPort = "TEST_MONGODB_MONGOS_PORT"
|
||||
|
||||
envMongoDBStandalonePort = "TEST_MONGODB_STANDALONE_PORT"
|
||||
//
|
||||
envMongoDBUser = "TEST_MONGODB_ADMIN_USERNAME"
|
||||
envMongoDBPassword = "TEST_MONGODB_ADMIN_PASSWORD"
|
||||
@@ -39,6 +41,9 @@ var (
|
||||
// MongoDBHost is the hostname. Since it runs locally, it is localhost
|
||||
MongoDBHost = "127.0.0.1"
|
||||
|
||||
// Port for standalone instance
|
||||
MongoDBStandalonePort = os.Getenv(envMongoDBStandalonePort)
|
||||
|
||||
// MongoDBShard1ReplsetName Replicaset name for shard 1
|
||||
MongoDBShard1ReplsetName = os.Getenv(envMongoDBShard1ReplsetName)
|
||||
// MongoDBShard1PrimaryPort is the port for the primary instance of shard 1
|
||||
|
||||
@@ -2,7 +2,6 @@ package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -13,8 +12,13 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const (
|
||||
shardingNotEnabledErrorCode = 203
|
||||
)
|
||||
|
||||
var (
|
||||
CANNOT_GET_QUERY_ERROR = errors.New("cannot get query field from the profile document (it is not a map)")
|
||||
CannotGetQueryError = errors.New("cannot get query field from the profile document (it is not a map)")
|
||||
ShardingNotEnabledError = errors.New("sharding not enabled")
|
||||
)
|
||||
|
||||
func GetReplicasetMembers(ctx context.Context, clientOptions *options.ClientOptions) ([]proto.Members, error) {
|
||||
@@ -92,7 +96,7 @@ func GetReplicasetMembers(ctx context.Context, clientOptions *options.ClientOpti
|
||||
membersMap[m.Name] = m
|
||||
}
|
||||
|
||||
client.Disconnect(ctx)
|
||||
client.Disconnect(ctx) //nolint
|
||||
}
|
||||
|
||||
for _, member := range membersMap {
|
||||
@@ -119,6 +123,9 @@ func GetHostnames(ctx context.Context, client *mongo.Client) ([]string, error) {
|
||||
var shardsMap proto.ShardsMap
|
||||
smRes := client.Database("admin").RunCommand(ctx, primitive.M{"getShardMap": 1})
|
||||
if smRes.Err() != nil {
|
||||
if e, ok := smRes.Err().(mongo.CommandError); ok && e.Code == shardingNotEnabledErrorCode {
|
||||
return nil, ShardingNotEnabledError // standalone instance
|
||||
}
|
||||
return nil, errors.Wrap(smRes.Err(), "cannot getShardMap for GetHostnames")
|
||||
}
|
||||
if err := smRes.Decode(&shardsMap); err != nil {
|
||||
@@ -134,7 +141,8 @@ func GetHostnames(ctx context.Context, client *mongo.Client) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("cannot get shards map")
|
||||
// Some MongoDB servers won't return ShardingNotEnabledError for stand alone instances.
|
||||
return nil, nil // standalone instance
|
||||
}
|
||||
|
||||
func buildHostsListFromReplStatus(replStatus proto.ReplicaSetStatus) []string {
|
||||
@@ -265,7 +273,7 @@ func GetQueryField(doc proto.SystemProfile) (primitive.M, error) {
|
||||
if ssquery, ok := squery.(primitive.M); ok {
|
||||
return ssquery, nil
|
||||
}
|
||||
return nil, CANNOT_GET_QUERY_ERROR
|
||||
return nil, CannotGetQueryError
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -301,7 +309,7 @@ func GetQueryField(doc proto.SystemProfile) (primitive.M, error) {
|
||||
if ssquery, ok := squery.(primitive.M); ok {
|
||||
return ssquery, nil
|
||||
}
|
||||
return nil, CANNOT_GET_QUERY_ERROR
|
||||
return nil, CannotGetQueryError
|
||||
}
|
||||
|
||||
// "query" in MongoDB 3.2+ is better structured and always has a "filter" subkey:
|
||||
@@ -309,7 +317,7 @@ func GetQueryField(doc proto.SystemProfile) (primitive.M, error) {
|
||||
if ssquery, ok := squery.(primitive.M); ok {
|
||||
return ssquery, nil
|
||||
}
|
||||
return nil, CANNOT_GET_QUERY_ERROR
|
||||
return nil, CannotGetQueryError
|
||||
}
|
||||
|
||||
// {"ns":"test.system.js","op":"query","query":{"find":"system.js"}}
|
||||
|
||||
@@ -14,24 +14,34 @@ import (
|
||||
|
||||
func TestGetHostnames(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
uri string
|
||||
want []string
|
||||
name string
|
||||
uri string
|
||||
want []string
|
||||
wantError bool
|
||||
}{
|
||||
{
|
||||
name: "from_mongos",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBMongosPort),
|
||||
want: []string{"127.0.0.1:17001", "127.0.0.1:17002", "127.0.0.1:17004", "127.0.0.1:17005", "127.0.0.1:17007"},
|
||||
name: "from_mongos",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBMongosPort),
|
||||
want: []string{"127.0.0.1:17001", "127.0.0.1:17002", "127.0.0.1:17004", "127.0.0.1:17005", "127.0.0.1:17007"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "from_mongod",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard1PrimaryPort),
|
||||
want: []string{"127.0.0.1:17001", "127.0.0.1:17002", "127.0.0.1:17003"},
|
||||
name: "from_mongod",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard1PrimaryPort),
|
||||
want: []string{"127.0.0.1:17001", "127.0.0.1:17002", "127.0.0.1:17003"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "from_non_sharded",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard3PrimaryPort),
|
||||
want: []string{"127.0.0.1:17021", "127.0.0.1:17022", "127.0.0.1:17023"},
|
||||
name: "from_non_sharded",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard3PrimaryPort),
|
||||
want: []string{"127.0.0.1:17021", "127.0.0.1:17022", "127.0.0.1:17023"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "from_standalone",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBStandalonePort),
|
||||
want: nil,
|
||||
wantError: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -49,12 +59,12 @@ func TestGetHostnames(t *testing.T) {
|
||||
}
|
||||
|
||||
hostnames, err := GetHostnames(ctx, client)
|
||||
if err != nil {
|
||||
t.Errorf("getHostnames: %v", err)
|
||||
if err != nil && !test.wantError {
|
||||
t.Errorf("Expecting error=nil, got: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(hostnames, test.want) {
|
||||
t.Errorf("Invalid hostnames from mongos. Got: %+v, want %+v", hostnames, test.want)
|
||||
t.Errorf("Invalid hostnames. Got: %+v, want %+v", hostnames, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -81,24 +91,34 @@ func TestGetServerStatus(t *testing.T) {
|
||||
|
||||
func TestGetReplicasetMembers(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
uri string
|
||||
want int
|
||||
name string
|
||||
uri string
|
||||
want int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "from_mongos",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBMongosPort),
|
||||
want: 7,
|
||||
name: "from_mongos",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBMongosPort),
|
||||
want: 7,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "from_mongod",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard1PrimaryPort),
|
||||
want: 3,
|
||||
name: "from_mongod",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard1PrimaryPort),
|
||||
want: 3,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "from_non_sharded",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard3PrimaryPort),
|
||||
want: 3,
|
||||
name: "from_non_sharded",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBShard3PrimaryPort),
|
||||
want: 3,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "from_standalone",
|
||||
uri: fmt.Sprintf("mongodb://%s:%s@%s:%s", tu.MongoDBUser, tu.MongoDBPassword, tu.MongoDBHost, tu.MongoDBStandalonePort),
|
||||
want: 0,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -109,7 +129,7 @@ func TestGetReplicasetMembers(t *testing.T) {
|
||||
defer cancel()
|
||||
|
||||
rsm, err := GetReplicasetMembers(ctx, clientOptions)
|
||||
if err != nil {
|
||||
if err != nil && !test.wantErr {
|
||||
t.Errorf("Got an error while getting replicaset members: %s", err)
|
||||
}
|
||||
if len(rsm) != test.want {
|
||||
@@ -146,7 +166,7 @@ func TestGetShardedHosts(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
for i, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
clientOptions := options.Client().ApplyURI(test.uri)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
@@ -156,6 +176,10 @@ func TestGetShardedHosts(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Cannot get a new client for host %s: %s", test.uri, err)
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatalf("mongodb client is nil i: %d, uri: %s\n", i, test.uri)
|
||||
}
|
||||
|
||||
if err := client.Connect(ctx); err != nil {
|
||||
t.Errorf("Cannot connect to host %s: %s", test.uri, err)
|
||||
}
|
||||
@@ -38,13 +38,19 @@ const (
|
||||
DefaultRunningOpsSamples = 5
|
||||
DefaultOutputFormat = "text"
|
||||
typeMongos = "mongos"
|
||||
|
||||
// Exit Codes
|
||||
cannotFormatResults = 1
|
||||
cannotParseCommandLineParameters = 2
|
||||
cannotGetHostInfo = 3
|
||||
cannotGetReplicasetMembers = 4
|
||||
)
|
||||
|
||||
var (
|
||||
Build string = "2020-04-23" // nolint
|
||||
GoVersion string = "1.14.1" // nolint
|
||||
Version string = "3.2.0" // nolint
|
||||
Commit string // nolint
|
||||
Version string = "3.2.0"
|
||||
Commit string
|
||||
)
|
||||
|
||||
type TimedStats struct {
|
||||
@@ -158,7 +164,7 @@ func main() {
|
||||
opts, err := parseFlags()
|
||||
if err != nil {
|
||||
log.Errorf("cannot get parameters: %s", err.Error())
|
||||
os.Exit(2)
|
||||
os.Exit(cannotParseCommandLineParameters)
|
||||
}
|
||||
if opts == nil && err == nil {
|
||||
return
|
||||
@@ -206,7 +212,7 @@ func main() {
|
||||
defer client.Disconnect(ctx) // nolint
|
||||
|
||||
hostnames, err := util.GetHostnames(ctx, client)
|
||||
if err != nil {
|
||||
if err != nil && errors.Is(err, util.ShardingNotEnabledError) {
|
||||
log.Errorf("Cannot get hostnames: %s", err)
|
||||
}
|
||||
log.Debugf("hostnames: %v", hostnames)
|
||||
@@ -217,12 +223,11 @@ func main() {
|
||||
if err != nil {
|
||||
message := fmt.Sprintf("Cannot get host info for %q: %s", opts.Host, err.Error())
|
||||
log.Errorf(message)
|
||||
os.Exit(2)
|
||||
os.Exit(cannotGetHostInfo)
|
||||
}
|
||||
|
||||
if ci.ReplicaMembers, err = util.GetReplicasetMembers(ctx, clientOptions); err != nil {
|
||||
log.Warnf("[Error] cannot get replicaset members: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
log.Debugf("replicaMembers:\n%+v\n", ci.ReplicaMembers)
|
||||
|
||||
@@ -270,10 +275,9 @@ func main() {
|
||||
out, err := formatResults(ci, opts.OutputFormat)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot format the results: %s", err.Error())
|
||||
os.Exit(1)
|
||||
os.Exit(cannotFormatResults)
|
||||
}
|
||||
fmt.Println(string(out))
|
||||
|
||||
}
|
||||
|
||||
func formatResults(ci *collectedInfo, format string) ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user