All files / dist/service tn-consensus-service.js

71.24% Statements 109/153
73.8% Branches 31/42
100% Functions 23/23
70.86% Lines 107/151

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243    5x 5x 5x 5x 5x   5x 5x 5x 5x       8x     8x 3x 7x 5x 4x 10x 38x 6x 23x 7x       3x     7x 1x 1x 1x 1x   6x   5x 5x   1x 1x 1x 1x                     5x 1x 1x 1x 1x   4x   3x 3x   1x 1x 1x 1x                     4x 1x 1x 1x 1x   3x   2x 2x   1x 1x 1x 1x                     10x           10x 10x 2x   1x 1x 1x 1x                                         1x 1x 1x 1x         8x       38x 1x 1x 1x 1x   37x 37x 4x   1x 1x 1x 1x     3x 3x 3x 3x                                 33x       6x     23x 1x 1x 1x 1x   22x 22x 10x   1x 1x 1x 1x     9x 9x 9x 9x                                 12x       7x     5x  
"use strict";
/*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TnConsensusService = exports.RaftInputOperation = void 0;
const grpc_js_1 = require("@grpc/grpc-js");
const tn_consensus_controller_1 = require("../controller/tn-consensus-controller");
const tn_base_service_1 = require("./tn-base-service");
var RaftInputOperation;
(function (RaftInputOperation) {
    RaftInputOperation[RaftInputOperation["Unspecified"] = 0] = "Unspecified";
    RaftInputOperation[RaftInputOperation["Put"] = 1] = "Put";
    RaftInputOperation[RaftInputOperation["Delete"] = 2] = "Delete";
})(RaftInputOperation = exports.RaftInputOperation || (exports.RaftInputOperation = {}));
class TnConsensusService extends tn_base_service_1.TnBaseService {
    constructor(getTnController, getTnConnectorOptions, isCoatyAgentOnline, debug) {
        super(getTnController, getTnConnectorOptions, isCoatyAgentOnline, debug, "TnConsensusService");
    }
    get handlers() {
        return {
            create: (call, callback) => this._create(call, callback),
            connect: (call, callback) => this._connect(call, callback),
            disconnect: (call, callback) => this._disconnect(call, callback),
            stop: (call, callback) => this._stop(call, callback),
            propose: (call, callback) => this._propose(call, callback),
            getState: (call, callback) => this._getState(call, callback),
            observeState: (call) => this._observeState(call),
            getClusterConfiguration: (call, callback) => this._getClusterConfiguration(call, callback),
            observeClusterConfiguration: (call) => this._observeClusterConfiguration(call),
        };
    }
    async _create(call, callback) {
        callback(null, this.getTnController().create(call.request));
    }
    async _connect(call, callback) {
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("Connect failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        switch (await this.getTnController().connect(call.request)) {
            case tn_consensus_controller_1.RaftError.None:
                callback(null, {});
                break;
            case tn_consensus_controller_1.RaftError.UndefinedRaftNodeId: {
                const msg = "Raft node with this id has not been created";
                this.debug("Connect failed: %s", msg);
                callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: msg });
                break;
            }
            case tn_consensus_controller_1.RaftError.OperationNotSupportedInCurrentConnectionState: {
                const msg = "Raft node is currently disconnecting or stopping";
                this.debug("Connect failed: %s", msg);
                callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                break;
            }
        }
    }
    async _disconnect(call, callback) {
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("Disconnect failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        switch (await this.getTnController().disconnect(call.request)) {
            case tn_consensus_controller_1.RaftError.None:
                callback(null, {});
                break;
            case tn_consensus_controller_1.RaftError.UndefinedRaftNodeId: {
                const msg = "Raft node with this id has not been created";
                this.debug("Disconnect failed: %s", msg);
                callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: msg });
                break;
            }
            case tn_consensus_controller_1.RaftError.OperationNotSupportedInCurrentConnectionState: {
                const msg = "Raft node is currently connecting or stopping";
                this.debug("Disconnect failed: %s", msg);
                callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                break;
            }
        }
    }
    async _stop(call, callback) {
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("Stop failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        switch (await this.getTnController().stop(call.request)) {
            case tn_consensus_controller_1.RaftError.None:
                callback(null, {});
                break;
            case tn_consensus_controller_1.RaftError.UndefinedRaftNodeId: {
                const msg = "Raft node with this id has not been created";
                this.debug("Stop failed: %s", msg);
                callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: msg });
                break;
            }
            case tn_consensus_controller_1.RaftError.OperationNotSupportedInCurrentConnectionState: {
                const msg = "Raft node is currently connecting or disconnecting";
                this.debug("Stop failed: %s", msg);
                callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                break;
            }
        }
    }
    async _propose(call, callback) {
        Iif (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("Propose failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        const stateOrError = await this.getTnController().propose(call.request);
        if (typeof stateOrError === "number") {
            switch (stateOrError) {
                case tn_consensus_controller_1.RaftError.UndefinedRaftNodeId: {
                    const msg = "Raft node with this id has not been created";
                    this.debug("Propose failed: %s", msg);
                    callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.TooManyQueuedUpInputProposals: {
                    const msg = "Too many queued up input proposals for this Raft node";
                    this.debug("Propose failed: %s", msg);
                    callback({ code: grpc_js_1.status.OUT_OF_RANGE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.OperationNotSupportedInCurrentConnectionState: {
                    const msg = "Raft node is currently not connected";
                    this.debug("Propose failed: %s", msg);
                    callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.DisconnectBeforeOperationComplete: {
                    const msg = "Raft node stopped or disconnected before proposal committed";
                    this.debug("Propose failed: %s", msg);
                    callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.Internal: {
                    const msg = "Serialization format of input value may be invalid";
                    this.debug("Propose failed: %s", msg);
                    callback({ code: grpc_js_1.status.INTERNAL, details: msg });
                    break;
                }
            }
        }
        else {
            callback(null, stateOrError);
        }
    }
    async _getState(call, callback) {
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("GetState failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        const stateOrError = await this.getTnController().getState(call.request);
        if (typeof stateOrError === "number") {
            switch (stateOrError) {
                case tn_consensus_controller_1.RaftError.UndefinedRaftNodeId: {
                    const msg = "Raft node with this id has not been created";
                    this.debug("GetState failed: %s", msg);
                    callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.OperationNotSupportedInCurrentConnectionState: {
                    const msg = "Raft node is currently not connected";
                    this.debug("GetState failed: %s", msg);
                    callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.DisconnectBeforeOperationComplete: {
                    const msg = "Raft node stopped or disconnected before state could be retrieved";
                    this.debug("GetState failed: %s", msg);
                    callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.Internal: {
                    const msg = "Unexpected error";
                    this.debug("GetState failed: %s", msg);
                    callback({ code: grpc_js_1.status.INTERNAL, details: msg });
                    break;
                }
            }
        }
        else {
            callback(null, stateOrError);
        }
    }
    async _observeState(call) {
        this.handleServerStreamingCall(call, "observeState", evt => call.write(evt));
    }
    async _getClusterConfiguration(call, callback) {
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("GetClusterConfiguration failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        const stateOrError = await this.getTnController().getClusterConfiguration(call.request);
        if (typeof stateOrError === "number") {
            switch (stateOrError) {
                case tn_consensus_controller_1.RaftError.UndefinedRaftNodeId: {
                    const msg = "Raft node with this id has not been created";
                    this.debug("GetClusterConfiguration failed: %s", msg);
                    callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.OperationNotSupportedInCurrentConnectionState: {
                    const msg = "Raft node is currently not connected";
                    this.debug("GetClusterConfiguration failed: %s", msg);
                    callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.DisconnectBeforeOperationComplete: {
                    const msg = "Raft node stopped or disconnected before state could be retrieved";
                    this.debug("GetClusterConfiguration failed: %s", msg);
                    callback({ code: grpc_js_1.status.UNAVAILABLE, details: msg });
                    break;
                }
                case tn_consensus_controller_1.RaftError.Internal: {
                    const msg = "Unexpected error";
                    this.debug("GetClusterConfiguration failed: %s", msg);
                    callback({ code: grpc_js_1.status.INTERNAL, details: msg });
                    break;
                }
            }
        }
        else {
            callback(null, stateOrError);
        }
    }
    async _observeClusterConfiguration(call) {
        this.handleServerStreamingCall(call, "observeClusterConfiguration", evt => call.write(evt));
    }
}
exports.TnConsensusService = TnConsensusService;