PT-2101 pt-mongodb-query-digest does not work on standalone server (#630)

* PT-2101 - pt-mongodb-query-digest doesn't work on standalone server

Restoring test case, disabled for new sandbox that was never created.
Added debugging output to find out why the tool behaves not as expected.
Most of changes into main.go will be removed after the fix is done.

* PT-2101 - pt-mongodb-query-digest doesn't work on standalone server

- Changed code so it works with the standalone server
- Updated main_test.go so it works for MongoDB 5.0
- Removed eval.js and group.js, because these command are not supported since MongoDB 4.2

* PT-2101 - pt-mongodb-query-digest doesn't work on standalone server

Updated go.mod and go.sum from the 3.x branch
This commit is contained in:
Sveta Smirnova
2023-06-14 15:04:57 +03:00
committed by GitHub
parent b566350b64
commit e998bd5b55
7 changed files with 534 additions and 500 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"sort"
@@ -133,7 +134,7 @@ func main() {
log.Fatalf("Cannot connect to MongoDB: %s", err)
}
isProfilerEnabled, err := isProfilerEnabled(ctx, clientOptions)
isProfilerEnabled, err := isProfilerEnabled(ctx, clientOptions, opts.Database)
if err != nil {
log.Errorf("Cannot get profiler status: %s", err.Error())
os.Exit(4)
@@ -523,19 +524,37 @@ func sortQueries(queries []stats.QueryStats, orderby []string) []stats.QueryStat
return queries
}
func isProfilerEnabled(ctx context.Context, clientOptions *options.ClientOptions) (bool, error) {
func isProfilerEnabled(ctx context.Context, clientOptions *options.ClientOptions, dbname string) (bool, error) {
var ps proto.ProfilerStatus
replicaMembers, err := util.GetReplicasetMembers(ctx, clientOptions)
if err != nil {
if err != nil && !errors.Is(err, util.ShardingNotEnabledError) {
return false, err
}
if len(replicaMembers) == 0 {
client, err := mongo.NewClient(clientOptions)
if err != nil {
return false, err
}
if err = client.Connect(ctx); err != nil {
return false, err
}
client.Database(dbname).RunCommand(ctx, primitive.M{"profile": -1}).Decode(&ps)
if ps.Was == 0 {
return false, nil
}
}
for _, member := range replicaMembers {
// Stand alone instances return state = REPLICA_SET_MEMBER_STARTUP
client, err := util.GetClientForHost(clientOptions, member.Name)
if err != nil {
continue
}
if err := client.Connect(ctx); err != nil {
log.Fatalf("Cannot connect to MongoDB: %s", err)
}
isReplicaEnabled := isReplicasetEnabled(ctx, client)
@@ -546,7 +565,7 @@ func isProfilerEnabled(ctx context.Context, clientOptions *options.ClientOptions
if isReplicaEnabled && member.State != proto.REPLICA_SET_MEMBER_PRIMARY {
continue
}
if err := client.Database("admin").RunCommand(ctx, primitive.M{"profile": -1}).Decode(&ps); err != nil {
if err := client.Database(dbname).RunCommand(ctx, primitive.M{"profile": -1}).Decode(&ps); err != nil {
continue
}