-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat: add explain support for non-cursor commands #2599
Merged
HanaPearlman
merged 26 commits into
mongodb:master
from
HanaPearlman:NODE-2852/master/explain-non-cursor
Nov 16, 2020
Merged
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
a8dfabe
FindOneOperation extends CommandOperation
HanaPearlman 5bded70
implement explain for write commands
HanaPearlman 4374b6b
implement explain for distinct command
HanaPearlman f00fbfe
implement explain for findAndModify commands
HanaPearlman 1d937a8
implement explain for mapReduce command
HanaPearlman 2a9b1be
first attempt: handle removing sessions for write operations
HanaPearlman 237a70f
consider explain during canRetryWrite
HanaPearlman c2d0a53
small enum cleanup
HanaPearlman 0126381
model explain after read concern
HanaPearlman 82715bd
create ExplainableCommand class
HanaPearlman 3bf0d78
check explain value in explain command constructor
HanaPearlman 7090c39
quick cursor fix
HanaPearlman 82f0e60
move explain cmd/options to separate file
HanaPearlman f5e65ed
some commenting
HanaPearlman 4bdf503
respond to comments
HanaPearlman 03333d7
test bug fix
HanaPearlman 20ef5da
Merge branch 'master' into NODE-2852/master/explain-non-cursor
HanaPearlman c1a4ff9
add explain-related exports to index
HanaPearlman 2c7334b
use aspects and throw from fromOptions
HanaPearlman d486214
use expanded explain types for clarity
HanaPearlman ddc9826
change test names and ordering
HanaPearlman 6a17656
clean up
HanaPearlman 7235fb7
Merge branch 'master' into NODE-2852/master/explain-non-cursor
HanaPearlman 001ec2e
fix explain export in index
HanaPearlman 0b37761
check explain supported in individual op classes
HanaPearlman 31bb9eb
Merge branch 'master' into NODE-2852/master/explain-non-cursor
HanaPearlman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { Document } from '.'; | ||
import type { ExplainOptions } from './operations/explainable_command'; | ||
import type { Server } from './sdam/server'; | ||
import { maxWireVersion } from './utils'; | ||
|
||
export const Verbosity = { | ||
queryPlanner: 'queryPlanner', | ||
queryPlannerExtended: 'queryPlannerExtended', | ||
executionStats: 'executionStats', | ||
allPlansExecution: 'allPlansExecution' | ||
} as const; | ||
|
||
/** | ||
* For backwards compatibility, true is interpreted as | ||
* "allPlansExecution" and false as "queryPlanner". | ||
* @public | ||
*/ | ||
export type VerbosityLike = keyof typeof Verbosity | boolean; | ||
|
||
// Minimum server versions which support explain with specific operations | ||
const SUPPORTS_EXPLAIN_WITH_REMOVE = 3; | ||
const SUPPORTS_EXPLAIN_WITH_UPDATE = 3; | ||
const SUPPORTS_EXPLAIN_WITH_DISTINCT = 4; | ||
const SUPPORTS_EXPLAIN_WITH_FIND_AND_MODIFY = 4; | ||
const SUPPORTS_EXPLAIN_WITH_MAP_REDUCE = 9; | ||
|
||
/** @internal */ | ||
export class Explain { | ||
verbosity: keyof typeof Verbosity; | ||
|
||
constructor(verbosity: VerbosityLike) { | ||
if (typeof verbosity === 'boolean') { | ||
this.verbosity = verbosity ? Verbosity.allPlansExecution : Verbosity.queryPlanner; | ||
} else { | ||
this.verbosity = Verbosity[verbosity]; | ||
} | ||
} | ||
|
||
static fromOptions(options?: ExplainOptions): Explain | undefined { | ||
if (options?.explain === undefined) { | ||
return; | ||
} | ||
return new Explain(options.explain); | ||
} | ||
|
||
static valid(options?: ExplainOptions): boolean { | ||
if (options?.explain === undefined) { | ||
return true; | ||
} | ||
const explain = options.explain; | ||
return typeof explain === 'boolean' || explain in Verbosity; | ||
} | ||
|
||
/** Checks that the server supports explain on the given operation or command.*/ | ||
static explainSupported(server: Server, op: string | Document): boolean { | ||
const wireVersion = maxWireVersion(server); | ||
if (op === 'remove' || (typeof op === 'object' && op.remove)) { | ||
return wireVersion >= SUPPORTS_EXPLAIN_WITH_REMOVE; | ||
} else if (op === 'update' || (typeof op === 'object' && op.update)) { | ||
return wireVersion >= SUPPORTS_EXPLAIN_WITH_UPDATE; | ||
} else if (op === 'distinct' || (typeof op === 'object' && op.distinct)) { | ||
return wireVersion >= SUPPORTS_EXPLAIN_WITH_DISTINCT; | ||
} else if (op === 'findAndModify' || (typeof op === 'object' && op.findAndModify)) { | ||
return wireVersion >= SUPPORTS_EXPLAIN_WITH_FIND_AND_MODIFY; | ||
} else if (op === 'mapReduce' || (typeof op === 'object' && op.mapReduce)) { | ||
return wireVersion >= SUPPORTS_EXPLAIN_WITH_MAP_REDUCE; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { CommandOperation, OperationParent, CommandOperationOptions } from './command'; | ||
import { Explain, Verbosity, VerbosityLike } from '../explain'; | ||
import { Callback, Document, MongoError, Server } from '..'; | ||
|
||
/** @public */ | ||
export interface ExplainOptions { | ||
explain?: VerbosityLike; | ||
} | ||
|
||
/** @internal */ | ||
export abstract class ExplainableCommand< | ||
T extends ExplainOptions & CommandOperationOptions, | ||
TResult = Document | ||
> extends CommandOperation<T, TResult> { | ||
explain?: Explain; | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constructor(parent?: OperationParent, options?: T) { | ||
super(parent, options); | ||
|
||
if (!Explain.valid(options)) { | ||
throw new MongoError(`explain must be one of ${Object.keys(Verbosity)} or a boolean`); | ||
} | ||
|
||
this.explain = Explain.fromOptions(options); | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
get canRetryWrite(): boolean { | ||
return this.explain === undefined; | ||
} | ||
|
||
executeCommand(server: Server, cmd: Document, callback: Callback): void { | ||
if (this.explain) { | ||
if (!Explain.explainSupported(server, cmd)) { | ||
callback(new MongoError(`server ${server.name} does not support explain on this command`)); | ||
return; | ||
} | ||
|
||
// For now, tag the command with the explain; after cmd is finalized in the super class, | ||
// it will be refactored into the required shape using the explain. | ||
cmd.explain = this.explain; | ||
} | ||
super.executeCommand(server, cmd, callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we expand
Verbosity
toExplainVerbosity
for clarity?