UNPKG

@configurator/ravendb

Version:
110 lines 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClusterRequestExecutor = void 0; const GetClusterTopologyCommand_1 = require("../ServerWide/Commands/GetClusterTopologyCommand"); const NodeSelector_1 = require("./NodeSelector"); const os = require("os"); const BluebirdPromise = require("bluebird"); const semaphore = require("semaphore"); const LogUtil_1 = require("../Utility/LogUtil"); const RequestExecutor_1 = require("./RequestExecutor"); const Exceptions_1 = require("../Exceptions"); const ServerNode_1 = require("./ServerNode"); const Topology_1 = require("./Topology"); const GetTcpInfoCommand_1 = require("../ServerWide/Commands/GetTcpInfoCommand"); const SemaphoreUtil_1 = require("../Utility/SemaphoreUtil"); const DocumentConventions_1 = require("../Documents/Conventions/DocumentConventions"); const Constants_1 = require("../Constants"); const log = (0, LogUtil_1.getLogger)({ module: "ClusterRequestExecutor" }); class ClusterRequestExecutor extends RequestExecutor_1.RequestExecutor { constructor(authOptions, conventions) { super(null, authOptions, conventions); this._clusterTopologySemaphore = semaphore(); } static createForSingleNodeWithConfigurationUpdates(url, databaseName, opts) { return (0, Exceptions_1.throwError)("NotSupportedException"); } static createForSingleNodeWithoutConfigurationUpdates(url, databaseName, opts) { return (0, Exceptions_1.throwError)("NotSupportedException"); } static createForSingleNode(url, opts) { const initialUrls = [url]; const { authOptions, documentConventions } = opts; const urls = this.validateUrls(initialUrls, authOptions); const executor = new ClusterRequestExecutor(authOptions, documentConventions || DocumentConventions_1.DocumentConventions.defaultConventions); const serverNode = new ServerNode_1.ServerNode({ url: urls[0], serverRole: "Member" }); const topology = new Topology_1.Topology(-1, [serverNode]); const nodeSelector = new NodeSelector_1.NodeSelector(topology); executor._nodeSelector = nodeSelector; executor._topologyEtag = -2; executor._disableClientConfigurationUpdates = true; executor._disableTopologyUpdates = true; executor._topologyHeaderName = Constants_1.HEADERS.CLUSTER_TOPOLOGY_ETAG; executor.firstTopologyUpdatePromise = executor._singleTopologyUpdateAsync(urls, null); return executor; } static create(initialUrls, databaseOrOpts, opts) { if (typeof (databaseOrOpts) === "string") { return (0, Exceptions_1.throwError)("NotSupportedException"); } const { authOptions, documentConventions } = (opts || databaseOrOpts) || {}; const executor = new ClusterRequestExecutor(authOptions, documentConventions ? documentConventions : DocumentConventions_1.DocumentConventions.defaultConventions); executor._disableClientConfigurationUpdates = true; executor.firstTopologyUpdatePromise = executor._firstTopologyUpdate(initialUrls, null); executor._topologyHeaderName = Constants_1.HEADERS.CLUSTER_TOPOLOGY_ETAG; return executor; } _performHealthCheck(serverNode, nodeIndex) { return this.execute(new GetTcpInfoCommand_1.GetTcpInfoCommand("health-check"), null, { chosenNode: serverNode, nodeIndex, shouldRetry: false }); } updateTopology(parameters) { if (this._disposed) { return Promise.resolve(false); } const acquiredSemContext = (0, SemaphoreUtil_1.acquireSemaphore)(this._clusterTopologySemaphore, { timeout: parameters.timeoutInMs }); const result = BluebirdPromise.resolve(acquiredSemContext.promise) .then(() => { if (this._disposed) { return false; } const command = new GetClusterTopologyCommand_1.GetClusterTopologyCommand(parameters.debugTag); return this.execute(command, null, { chosenNode: parameters.node, nodeIndex: null, shouldRetry: false }) .then(() => { const results = command.result; const nodes = ServerNode_1.ServerNode.createFrom(results.topology); const newTopology = new Topology_1.Topology(results.etag, nodes); this._updateNodeSelector(newTopology, parameters.forceUpdate); this._onTopologyUpdatedInvoke(newTopology); }) .then(() => true); }, (reason) => { if (reason.name === "TimeoutError") { return false; } throw reason; }) .finally(() => acquiredSemContext.dispose()); return Promise.resolve(result); } _updateClientConfigurationAsync(serverNode) { return Promise.resolve(); } _throwExceptions(details) { (0, Exceptions_1.throwError)("InvalidOperationException", "Failed to retrieve cluster topology from all known nodes" + os.EOL + details); } dispose() { this._clusterTopologySemaphore.take(() => { }); super.dispose(); } } exports.ClusterRequestExecutor = ClusterRequestExecutor; //# sourceMappingURL=ClusterRequestExecutor.js.map