@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
76 lines (75 loc) • 3.17 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var CongestionAlgorithm_exports = {};
__export(CongestionAlgorithm_exports, {
CongestionAlgorithm: () => CongestionAlgorithm
});
module.exports = __toCommonJS(CongestionAlgorithm_exports);
var import_constants = require("../common/constants.js");
class CongestionAlgorithm {
// The semaphore to control the degree of concurrency.
limiterQueue;
// captures metrics upto previous requests for a partition.
oldPartitionMetric;
// captures metrics upto current request for a partition.
partitionMetric;
// time to wait before adjusting the degree of concurrency.
congestionWaitTimeInMs = 1e3;
congestionIncreaseFactor = 1;
congestionDecreaseFactor = 5;
currentDegreeOfConcurrency;
constructor(limiterQueue, partitionMetric, oldPartitionMetric) {
this.limiterQueue = limiterQueue;
this.oldPartitionMetric = oldPartitionMetric;
this.partitionMetric = partitionMetric;
this.currentDegreeOfConcurrency = 1;
}
run() {
const elapsedTimeInMs = this.partitionMetric.timeTakenInMs - this.oldPartitionMetric.timeTakenInMs;
if (elapsedTimeInMs >= this.congestionWaitTimeInMs) {
const diffThrottle = this.partitionMetric.numberOfThrottles - this.oldPartitionMetric.numberOfThrottles;
const changeItemsCount = this.partitionMetric.numberOfItemsOperatedOn - this.oldPartitionMetric.numberOfItemsOperatedOn;
this.oldPartitionMetric.add(changeItemsCount, elapsedTimeInMs, diffThrottle);
if (diffThrottle > 0) {
this.decreaseConcurrency();
}
if (changeItemsCount > 0 && diffThrottle === 0) {
this.increaseConcurrency();
}
}
}
decreaseConcurrency() {
const decreaseCount = Math.min(
this.congestionDecreaseFactor,
Math.floor(this.currentDegreeOfConcurrency / 2)
);
this.currentDegreeOfConcurrency -= decreaseCount;
this.limiterQueue.setConcurrency(this.currentDegreeOfConcurrency);
this.congestionWaitTimeInMs += 1e3;
}
increaseConcurrency() {
if (this.currentDegreeOfConcurrency + this.congestionIncreaseFactor <= import_constants.Constants.BulkMaxDegreeOfConcurrency) {
this.currentDegreeOfConcurrency += this.congestionIncreaseFactor;
this.limiterQueue.setConcurrency(this.currentDegreeOfConcurrency);
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CongestionAlgorithm
});