Skip to content

Commit

Permalink
fix: Retain defaulted scaling method name (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybell committed Jul 23, 2024
1 parent 5f51453 commit 9cc106e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/scaler/scaler-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,37 @@ const spannerRestApi = GoogleApis.spanner({
/**
* Get scaling method function by name.
*
* @param {string} methodName
* @param {string} projectId
* @param {string} instanceId
* @param {AutoscalerSpanner} spanner
* @return {{
* calculateSize: function(AutoscalerSpanner):number,
* calculateNumNodes: function(AutoscalerSpanner): number
* }}
*/
function getScalingMethod(methodName, projectId, instanceId) {
function getScalingMethod(spanner) {
const SCALING_METHODS_FOLDER = './scaling-methods/';
const DEFAULT_METHOD_NAME = 'STEPWISE';

// sanitize the method name before using
// to prevent risk of directory traversal.
methodName = sanitize(methodName);
const methodName = sanitize(spanner.scalingMethod);
let scalingMethod;
try {
scalingMethod = require(SCALING_METHODS_FOLDER + methodName.toLowerCase());
} catch (err) {
logger.warn({
message: `Unknown scaling method '${methodName}'`,
projectId: projectId,
instanceId: instanceId,
projectId: spanner.projectId,
instanceId: spanner.instanceId,
});
scalingMethod = require(
SCALING_METHODS_FOLDER + DEFAULT_METHOD_NAME.toLowerCase(),
);
methodName = DEFAULT_METHOD_NAME;
spanner.scalingMethod = DEFAULT_METHOD_NAME;
}
logger.info({
message: `Using scaling method: ${methodName}`,
projectId: projectId,
instanceId: instanceId,
message: `Using scaling method: ${spanner.scalingMethod}`,
projectId: spanner.projectId,
instanceId: spanner.instanceId,
});
return scalingMethod;
}
Expand Down Expand Up @@ -291,11 +289,7 @@ function withinCooldownPeriod(spanner, suggestedSize, autoscalerState, now) {
* @return {number}
*/
function getSuggestedSize(spanner) {
const scalingMethod = getScalingMethod(
spanner.scalingMethod,
spanner.projectId,
spanner.instanceId,
);
const scalingMethod = getScalingMethod(spanner);
if (scalingMethod.calculateSize) {
return scalingMethod.calculateSize(spanner);
} else if (scalingMethod.calculateNumNodes) {
Expand Down
21 changes: 21 additions & 0 deletions src/scaler/scaler-core/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ afterEach(() => {
sinon.restore();
});

describe('#getScalingMethod', () => {
const app = rewire('../index.js');
const getScalingMethod = app.__get__('getScalingMethod');

it('should return the configured scaling method function', async function () {
const spanner = createSpannerParameters();
spanner.scalingMethod = 'LINEAR';
const scalingFunction = getScalingMethod(spanner);
assert.isFunction(scalingFunction.calculateSize);
assert.equals(spanner.scalingMethod, 'LINEAR');
});

it('should default to STEPWISE scaling', async function () {
const spanner = createSpannerParameters();
spanner.scalingMethod = 'UNKNOWN_SCALING_METHOD';
const scalingFunction = getScalingMethod(spanner);
assert.isFunction(scalingFunction.calculateSize);
assert.equals(spanner.scalingMethod, 'STEPWISE');
});
});

describe('#getNewMetadata', () => {
const app = rewire('../index.js');
const getNewMetadata = app.__get__('getNewMetadata');
Expand Down

0 comments on commit 9cc106e

Please sign in to comment.