Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database_type config to Split up sql queries by engine type #7934

Merged
merged 14 commits into from
Sep 8, 2020

Conversation

denzilribeiro
Copy link
Contributor

Required for all PRs:

  • [X ] Signed CLA.
  • [ X] Associated README.md updated.
  • [ X] Has appropriate unit tests.

This is a big PR that addresses the issues in #7713

For example I just realized that DatabaseIO was broken on Managed instances with the prior fix for on-prem stuff.
Fundamental principals
a. Split out queries by each database type - that simplifies queries, bug in a change will only affect a subset of input target platforms.
b. Maintain the same measures if the underlying core DMV is the same.
c. Reduces number of queries that are executed against each target or have ability to reduce them.
d. Ability to change the "default queries" per target.

Resolves #7713

@denzilribeiro
Copy link
Contributor Author

@Trovalo , @danielnelson can you both review this please given a big PR - still may make a small change or 2 but took a stab at this.
Also not sure about the failure as has to do with : influxdata/telegraf/plugins/inputs/tcp_listener.TestRunParserInvalidMsg()

Copy link
Contributor

@danielnelson danielnelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I switched jobs recently and won't be working on Telegraf much anymore, @ssoroka and @reimda are now leading the project and will help you get this pr merged.

I expect there will be a conflict between this and #7842, discuss with @Trovalo and decide which order to merge in.

@denzilribeiro
Copy link
Contributor Author

There may not be a conflict with #7842 , as that is one of the few queries I haven't changed
@ssoroka / @reimda the failed check doesn't seem to be related to my PR - could you help resolve
@danielnelson - Good luck in your new job.

@@ -2,6 +2,8 @@ package sqlserver

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the SQL Related queries have been moved in their own file, what about putting the "standard" edition SQL Queries in their own file too?
I think this will be even cleaner as we won't have queries in the main GO file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was thinking about doing that ran out of time, will do that in next commit, will split V2 in own file and clean up queries to just have SQLServer queries in own file .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split V2 in latest commit - Will do SQLServer as separate file later.

Copy link
Collaborator

@Trovalo Trovalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I've added a bunch of comments, mostly about documentation.
  • I would just put even the v2 queries in their own file in order to have an even cleaner structure
  • we could add a warning to prompt if they use the updated/deprecated parameters "azuredb" or "query_version", I saw this approach in the past

I'll soon run a full test of the plugin, but since nothing has been changed in the queries themselves everything should work fine.

About the possible Merge conflicts or order with other open PRs, I'll have a look at it later.

@danielnelson - thanks and best of luck for your new job

@denzilribeiro
Copy link
Contributor Author

@Trovalo if you want me to just include the PR #7842 in this too let me know will be small change maybe easier to do.

@Trovalo
Copy link
Collaborator

Trovalo commented Aug 10, 2020

About open PRs which might need to be handled before or after this one, those are the one I've found
#7808 - additional field in perf-counters
#7842 - new query "query_stats"
#7834 - edit of existing query "sql_requests"

The first 2 are mine, so I've no problems with merging them later, personally I think there won't be any problem even with the last one since the query is the same for all the versions

@Trovalo
Copy link
Collaborator

Trovalo commented Aug 10, 2020

@Trovalo if you want me to just include the PR #7842 in this too let me know will be small change maybe easier to do.

That's fine for me. If you do so just tell me so I can close my PR

Copy link
Collaborator

@Trovalo Trovalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just noticed a few errors

@@ -82,7 +91,8 @@ GO
## - AzureMIMemoryClerks
## - AzureMIPerformanceCounters

## Version 2:
## Version 2 by default collects the following queries
## Version 1 is being deprecated, please consider using database_type.
Copy link
Collaborator

@Trovalo Trovalo Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a small error
"Version 1 is being ..."→ "Version 2 is being ..."

plugins/inputs/sqlserver/README.md Show resolved Hide resolved

## Optional parameter, setting this to 2 will use a new version
## of the collection queries that break compatibility with the original
## dashboards.
## of the collection queries that break compatibility with the original dashboards.
## Version 2 - is compatible from SQL Server 2012 and later versions and also for SQL Azure DB
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"from SQL Server 2012 and" → "from SQL Server 2008 SP3 and"

The `sqlserver` plugin provides metrics for your SQL Server instance. It
currently works with SQL Server 2008 SP3 and newer. Recorded metrics are
lightweight and use Dynamic Management Views supplied by SQL Server.

### THe SQL Server plugin supports the following editions/versions of SQL Server
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an extra capital H "THe" → "The"

The plugin as it stands today is increasing complexity of queries given different engine types
Current Engine type ssupported Azure SQL DB, Managed instance, SQL Server versions 2008 - 2019
Config file now has a database_type which is SQLServer / AzureSQLDB / AzureSQLManagedInstance.
This enables us to split up the queries so one change doesn't regress others and reduce testing surface.
This will take only if users use database_type in config, if not it remains as before.
Also split queries for V1 and queries for Azure SQL into separate files.
SQL Managed instance Database IO was broken with prior change
Also all managed instance queries do not need DB_NAME in all measures, that was introduced for Azure Single DB
This will make maintainability much easier and have to test only with the one plaform a query has changed.
Currently on-prem SQL Server queries have not been refactored
They are effectively the same V2 queries but only the ones applicable to SQL Server (not Azure related DMVS)
Ideally they should be refactored in another PR to simplify and get rid of cloud portions, separated out into own file.
Added few new queries to tests
Making readme changes per PR comments , added deprecation notice
Also split out V2 queries into its own file.
@denzilribeiro
Copy link
Contributor Author

Had to rebase, due to other commits, sorry about that.

Split on-prem sql server queries for database_type="SQL Server"
Updated readme, also minor naming changes for consistency of queries
Commit for queries for database_type = "SQLServer"
@denzilribeiro
Copy link
Contributor Author

@ssoroka - any idea why the test was failing?

--- FAIL: TestAlignedTickerJitter (0.01s)
tick_test.go:64:
Error Trace: tick_test.go:64
Error: Should be true
Test: TestAlignedTickerJitter

@denzilribeiro
Copy link
Contributor Author

@Trovalo I did take care of ensuring these are included. the query_stats one can revisit after merge.
#7808
#7834
#7724

@ssoroka
Copy link
Contributor

ssoroka commented Aug 17, 2020

@denzilribeiro Sorry, was just a flakey test.

@denzilribeiro
Copy link
Contributor Author

@Trovalo any other feedback?
@ssoroka -- can we merge this?

@ssoroka
Copy link
Contributor

ssoroka commented Aug 19, 2020

@Trovalo, did you get a chance to run your tests on this branch? happy?

@Trovalo
Copy link
Collaborator

Trovalo commented Aug 19, 2020

I'm on vacation and I won't be able to run any test until this weekend.
I'll let you know the result as soon as I run them

Copy link
Collaborator

@Trovalo Trovalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I run all the tests for SQL Server 2008 to 2019.
I've just found 2 errors in the sqlServerRequests query, for compatibility reasons (with 2008 and 2008R2) two columns should be fetched in a different way.

All the other queries are working properly

, REPLACE(@@SERVERNAME,'\',':') AS [sql_instance]
, s.session_id
, ISNULL(r.request_id,0) as request_id
, DB_NAME(s.database_id) as session_db_name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 2008 and 2008R2 compatibility get col from dm_exec_requests

, DB_NAME(r.database_id) as session_db_name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, this was a change I picked up from 45a83b9 and can revert it.

, s.program_name
, s.host_name
, s.nt_user_name
, s.open_transaction_count AS open_transaction
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 2008 and 2008R2 compatibility the same can be obtained with

,open_transaction =
        CASE
            WHEN r.session_id IS NOT NULL THEN r.open_transaction_count
            ELSE (SELECT COUNT(*) FROM sys.dm_tran_session_transactions AS t WHERE t.session_id = s.session_id)
        END

Thanks @spaghettidba

Copy link
Contributor Author

@denzilribeiro denzilribeiro Sep 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't something I changed, haven't committed so far, can discuss under different PR. Having a count(*) from open transactions for every request isn't a cheap operation ( think of 5000 active requests). This results in a nested loop join executed for every request. SQL 2008/R2 is past extended support from a SQL Server perspective, at some point trying to get parity for everything new isn't going to be possible and will complicate existing queries.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been changed in the 45a83b9, the priority has been given to the sessions table (s) before thew column was picked from the request table (r). (which to me still makes sense tbh, since the table is detailed "by request" and not by session.
Probably the best solution is to use COALESCE(r.open_transaction_count, s.open_transaction_count) AS [open_transaction_count], by doing so we get the data per-request, but if there is no request then it makes sense to fetch it from the session table.

We can revert it back to r.___ and get the data from the requests, or I can split the query in 2 versions using dynamic SQL in order to manage this better.

I'm aware of the fact that SQL 2008/R2 is past extended support, and I hope to don't see it around anymore.... but for now it's still around...
So I'd like to keep the full compatibility at least for the V2 of the queries, but I agree on the fact that a certain point the support for it should be stopped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are totally 2 different discussions
a. I get the Requests v/s Sessions argument for open_transactions, that is a trivial change and shouldn't be different or if ti is the session count should override anyways as tran is still open
b. what I was commenting on is the fact that open_transactions isn't there before 2012 and hence the select to get the count from dm_tran_session_transactions is more expensive.. Either way this wasn't a change

I am not sure where you are seeing it changed in 45a83b9 I just took a look a the line says
, s.open_transaction_count AS open_transaction

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I would point to solution "a", so just get the column form requests instead of sessions (for compatibility reasons, otherwise the query explodes on SQL 2008).

Just abort the idea of the subquery on "dm_tran_session_transactions" (it was not a great idea), as you said it's heavier and it's not worth running it just to keep compatibility with SQL 2008 and R2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up changing to dynamic SQL to get appropriate column. Changing to r.open_transactions means that even for versions after 2008R2 you will not get the open tran count for an Idle session ( think of a session that has an open transaction and no request).

Reverting a change made in 45a83b9 that had incorporated in this commit.
SQL 2008 / R2 doesn't have database_id in sys.dm_exec_sessions.
@denzilribeiro
Copy link
Contributor Author

@ssoroka are we good to merge this?
@Trovalo any other concerns?

@Trovalo
Copy link
Collaborator

Trovalo commented Sep 4, 2020

@ssoroka are we good to merge this?
@Trovalo any other concerns?

Except for the open point about the column s."open_transaction_count" (sessions) vs r."open_transaction_count (requests) we are good to go.
(if we get the column form requests the query will run on SQL 2008 and R2, otherwise, it will break)

sys.dm_exec_sessions does not have open_transactions column prior to SQL 2012
To accomodate 2008/R2 switched to dynamic SQL
@denzilribeiro
Copy link
Contributor Author

@ssoroka are we good to merge this?
@Trovalo any other concerns?

Except for the open point about the column s."open_transaction_count" (sessions) vs r."open_transaction_count (requests) we are good to go.
(if we get the column form requests the query will run on SQL 2008 and R2, otherwise, it will break)

Changes done with Dynamic SQL

Copy link
Collaborator

@Trovalo Trovalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last fix is needed.

Then this is approved to me.

, DB_NAME() as [database_name]
, s.session_id
, ISNULL(r.request_id,0) as request_id
, DB_NAME(r.database_id) as session_db_name
, DB_NAME(s.database_id) as session_db_name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.database_id is not available in SQL 2008 and R2, it should be r.database_id

But since this is dynamic SQL now you can just pass it in the column parameter

IF @MajorMinorVersion >= 1200  
	SET @Columns = ',s.open_transaction_count as open_transaction
	,DB_NAME(s.database_id) as session_db_name'
ELSE
	SET @Columns = ',r.open_transaction_count as open_transaction
	,DB_NAME(r.database_id) as session_db_name'

after this the PR is approved to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang it sorry moving target should install a 2008/R2 instance at some point. should be done now.

Incorporated dynamic SQL for database_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Split up sql script and config options for SQL Server plugin on database_type
4 participants