Skip to content

Commit

Permalink
SNOW-871416: [HTAP] Query Context Caching for Node (#631)
Browse files Browse the repository at this point in the history
* Query Context Cache implementation
  • Loading branch information
sfc-gh-ext-simba-jy authored Sep 22, 2023
1 parent 319adc8 commit 6de55e6
Show file tree
Hide file tree
Showing 12 changed files with 729 additions and 15 deletions.
20 changes: 19 additions & 1 deletion lib/connection/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const DEFAULT_PARAMS =
'validateDefaultParameters',
'arrayBindingThreshold',
'gcsUseDownscopedCredential',
'forceStageBindError'
'forceStageBindError',
'disableQueryContextCache',
];

function consolidateHostAndAccount(options)
Expand Down Expand Up @@ -468,6 +469,14 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo)
forceStageBindError = options.forceStageBindError;
}

let disableQueryContextCache = false;
if (Util.exists(options.disableQueryContextCache)) {
Errors.checkArgumentValid(Util.isBoolean(options.disableQueryContextCache),
ErrorCodes.ERR_CONN_CREATE_INVALID_DISABLED_QUERY_CONTEXT_CACHE);

disableQueryContextCache = options.disableQueryContextCache;
}

if (validateDefaultParameters)
{
for (const [key] of Object.entries(options))
Expand Down Expand Up @@ -747,6 +756,15 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo)
return forceStageBindError;
};

/**
* Returns whether the Query Context Cache is enabled or not by the configuration
*
* @returns {Boolean}
*/
this.getDisableQueryContextCache = function () {
return disableQueryContextCache;
}

// save config options
this.username = options.username;
this.password = options.password;
Expand Down
7 changes: 6 additions & 1 deletion lib/connection/result/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function Result(options) {
this._returnedRows = data.returned;
this._totalRows = data.total;
this._statementTypeId = data.statementTypeId;
this._queryContext = data.queryContext;

// if no chunk headers were specified, but a query-result-master-key (qrmk)
// was specified, build the chunk headers from the qrmk
Expand All @@ -73,7 +74,7 @@ function Result(options) {

// convert the parameters array to a map
parametersMap = {};
parametersArray = data.parameters;
parametersArray = data.parameters || [];
for (index = 0, length = parametersArray.length; index < length; index++) {
parameter = parametersArray[index];
parametersMap[parameter.name] = parameter.value;
Expand Down Expand Up @@ -125,6 +126,10 @@ function Result(options) {
this._statement,
this._services);

this.getQueryContext = function () {
return this._queryContext;
}

/* Disable the ChunkCache until the implementation is complete.
*
* // create a chunk cache and save a reference to it in case we need to
Expand Down
20 changes: 19 additions & 1 deletion lib/connection/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,15 @@ function BaseStatement(
sendCancelStatement(context, statement, callback);
};

//Integration Testing purpose.
this.getQueryContextCacheSize = function () {
return services.sf.getQueryContextCacheSize();
}

this.getQueryContextDTOSize = function () {
return services.sf.getQueryContextDTO().entries.length;
}

/**
* Issues a request to get the statement result again.
*
Expand Down Expand Up @@ -806,8 +815,9 @@ function createOnStatementRequestSuccRow(statement, context)
connectionConfig: context.connectionConfig,
rowMode: context.rowMode
});
// save the query id

context.queryId = context.result.getQueryId();
this.services.sf.deserializeQueryContext(context.result.getQueryContext());
}
}
else
Expand Down Expand Up @@ -1334,6 +1344,10 @@ function sendRequestPreExec(statementContext, onResultAvailable)
json.isInternal = statementContext.internal;
}

if(!statementContext.disableQueryContextCache){
json.queryContextDTO = statementContext.services.sf.getQueryContextDTO();
}

// use the snowflake service to issue the request
sendSfRequest(statementContext,
{
Expand Down Expand Up @@ -1389,6 +1403,10 @@ this.sendRequest = function (statementContext, onResultAvailable)
json.isInternal = statementContext.internal;
}

if(!statementContext.disableQueryContextCache){
json.queryContextDTO = statementContext.services.sf.getQueryContextDTO();
}

var options =
{
method: 'POST',
Expand Down
1 change: 1 addition & 0 deletions lib/constants/error_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ exports[404037] = 'Invalid arrayBindingThreshold. The specified value must be a
exports[404038] = 'Invalid gcsUseDownscopedCredential. The specified value must be a boolean.';
exports[404039] = 'Invalid forceStageBindError. The specified value must be a number.';
exports[404040] = 'Invalid browser timeout value. The specified value must be a positive number.';
exports[404041] = 'Invalid disablQueryContextCache. The specified value must be a boolean.';

// 405001
exports[405001] = 'Invalid callback. The specified value must be a function.';
Expand Down
1 change: 1 addition & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ codes.ERR_CONN_CREATE_INVALID_ARRAY_BINDING_THRESHOLD = 404037;
codes.ERR_CONN_CREATE_INVALID_GCS_USE_DOWNSCOPED_CREDENTIAL = 404038;
codes.ERR_CONN_CREATE_INVALID_FORCE_STAGE_BIND_ERROR = 404039;
codes.ERR_CONN_CREATE_INVALID_BROWSER_TIMEOUT = 404040;
codes.ERR_CONN_CREATE_INVALID_DISABLED_QUERY_CONTEXT_CACHE = 404041

// 405001
codes.ERR_CONN_CONNECT_INVALID_CALLBACK = 405001;
Expand Down
7 changes: 7 additions & 0 deletions lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ names.CLIENT_SESSION_KEEP_ALIVE_HEARTBEAT_FREQUENCY = 'CLIENT_SESSION_KEEP_ALIVE
names.JS_TREAT_INTEGER_AS_BIGINT = 'JS_TREAT_INTEGER_AS_BIGINT';
names.CLIENT_STAGE_ARRAY_BINDING_THRESHOLD = 'CLIENT_STAGE_ARRAY_BINDING_THRESHOLD';
names.MULTI_STATEMENT_COUNT = 'MULTI_STATEMENT_COUNT';
names.QUERY_CONTEXT_CACHE_SIZE = 'QUERY_CONTEXT_CACHE_SIZE';

var parameters =
[
Expand Down Expand Up @@ -106,6 +107,12 @@ var parameters =
value: 1,
desc: 'When 1, multi statement is disable, when 0, multi statement is unlimited'
}),
new Parameter(
{
name: names.QUERY_CONTEXT_CACHE_SIZE,
value: 5,
desc: 'Query Context Cache Size'
}),
];

// put all the parameters in a map so they're easy to retrieve and update
Expand Down
Loading

0 comments on commit 6de55e6

Please sign in to comment.