Skip to content

Commit

Permalink
PMM-12375 do not run serviceInfo for older clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Tymchuk committed Sep 22, 2023
1 parent 94dea4f commit e17d889
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
9 changes: 2 additions & 7 deletions agent/serviceinfobroker/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func (sib *ServiceInfoBroker) getMySQLInfo(ctx context.Context, dsn string, file
return &res
}

tableCount := int32(count)
res.TableCount = int32(count)
if count > math.MaxInt32 {
tableCount = math.MaxInt32
res.TableCount = math.MaxInt32
}

var version string
Expand All @@ -138,9 +138,7 @@ func (sib *ServiceInfoBroker) getMySQLInfo(ctx context.Context, dsn string, file
return &res
}

res.TableCount = tableCount
res.Version = version

return &res
}

Expand Down Expand Up @@ -200,7 +198,6 @@ func (sib *ServiceInfoBroker) getMongoDBInfo(ctx context.Context, dsn string, fi
}

res.Version = buildInfo.Version

return &res
}

Expand Down Expand Up @@ -231,7 +228,6 @@ func (sib *ServiceInfoBroker) getPostgreSQLInfo(ctx context.Context, dsn string,
}

res.Version = version

return &res
}

Expand Down Expand Up @@ -259,6 +255,5 @@ func (sib *ServiceInfoBroker) getProxySQLInfo(ctx context.Context, dsn string) *
}

res.Version = version

return &res
}
59 changes: 45 additions & 14 deletions managed/services/agents/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/percona/pmm/api/inventorypb"
"github.com/percona/pmm/managed/models"
"github.com/percona/pmm/utils/logger"
"github.com/percona/pmm/version"
)

// ServiceInfoBroker checks if connection can be established to service.
Expand Down Expand Up @@ -140,6 +141,18 @@ func (c *ServiceInfoBroker) GetInfoFromService(ctx context.Context, q *reform.Qu
pmmAgentID = models.PMMServerAgentID
}

// Skip check connection to external exporter with old pmm-agent.
if service.ServiceType == models.ExternalServiceType || service.ServiceType == models.HAProxyServiceType {
isSupported, err := isExternalExporterServiceInfoSupported(q, pmmAgentID)
if err != nil {
return err
}

if !isSupported {
return nil
}
}

pmmAgent, err := c.r.get(pmmAgentID)
if err != nil {
return err
Expand All @@ -155,24 +168,35 @@ func (c *ServiceInfoBroker) GetInfoFromService(ctx context.Context, q *reform.Qu
sanitizedDSN = strings.ReplaceAll(request.Dsn, word, "****")
}
l.Infof("ServiceInfoRequest: type: %s, DSN: %s timeout: %s.", request.Type, sanitizedDSN, request.Timeout)

resp, err := pmmAgent.channel.SendAndWaitResponse(request)
if err != nil {
return err
}
l.Infof("ServiceInfo response: %+v.", resp)

sInfo, ok := resp.(*agentpb.ServiceInfoResponse)
if !ok {
return errors.New("failed to cast response to *agentpb.ServiceInfoResponse")
}

msg := sInfo.Error
if msg == context.Canceled.Error() || msg == context.DeadlineExceeded.Error() {
msg = fmt.Sprintf("timeout (%s)", msg)
return status.Error(codes.FailedPrecondition, fmt.Sprintf("Connection check failed: %s.", msg))
}

stype := service.ServiceType
switch stype {
case models.MySQLServiceType:
stats := resp.(*agentpb.ServiceInfoResponse) //nolint:forcetypeassert
tableCount := stats.GetTableCount()
agent.TableCount = &tableCount
l.Debugf("Updating table count: %d.", tableCount)
agent.TableCount = &sInfo.TableCount
l.Debugf("Updating table count: %d.", sInfo.TableCount)
if err = q.Update(agent); err != nil {
return errors.Wrap(err, "failed to update table count")
}
return updateServiceVersion(ctx, q, resp, service)
case models.ExternalServiceType, models.HAProxyServiceType:
return nil
case models.PostgreSQLServiceType:
return updateServiceVersion(ctx, q, resp, service)
case models.MongoDBServiceType:
Expand All @@ -182,21 +206,12 @@ func (c *ServiceInfoBroker) GetInfoFromService(ctx context.Context, q *reform.Qu
default:
return errors.Errorf("unhandled Service type %s", service.ServiceType)
}

msg := resp.(*agentpb.ServiceInfoResponse).Error //nolint:forcetypeassert
switch msg {
case "":
return nil
case context.Canceled.Error(), context.DeadlineExceeded.Error():
msg = fmt.Sprintf("timeout (%s)", msg)
}
return status.Error(codes.FailedPrecondition, fmt.Sprintf("Connection check failed: %s.", msg))
}

func updateServiceVersion(ctx context.Context, q *reform.Querier, resp agentpb.AgentResponsePayload, service *models.Service) error {
l := logger.Get(ctx)

version := resp.(*agentpb.ServiceInfoResponse).GetVersion() //nolint:forcetypeassert
version := resp.(*agentpb.ServiceInfoResponse).Version //nolint:forcetypeassert
if version == "" {
return nil
}
Expand All @@ -209,3 +224,19 @@ func updateServiceVersion(ctx context.Context, q *reform.Querier, resp agentpb.A

return nil
}

func isExternalExporterServiceInfoSupported(q *reform.Querier, pmmAgentID string) (bool, error) {
pmmAgent, err := models.FindAgentByID(q, pmmAgentID)
if err != nil {
return false, fmt.Errorf("failed to get PMM Agent: %w", err)
}
pmmAgentVersion, err := version.Parse(*pmmAgent.Version)
if err != nil {
return false, fmt.Errorf("failed to parse PMM agent version %q: %w", *pmmAgent.Version, err)
}

if pmmAgentVersion.Less(checkExternalExporterConnectionPMMVersion) {
return false, nil
}
return true, nil
}

0 comments on commit e17d889

Please sign in to comment.