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

83.63% Statements 92/110
70.83% Branches 34/48
94.73% Functions 18/19
83.48% Lines 91/109

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    5x 5x 5x 5x 5x 5x 5x     14x 14x     8x       3x 3x             3x 3x 3x     6x 6x 1x   5x 5x 5x             7x 7x 1x   6x 6x 6x             3x 3x 1x   2x 2x 2x               10x 10x 1x   9x 9x 2x   9x 9x 8x 8x   9x 1x 1x   8x                               37x 37x 1x   36x 36x     3x     3x 3x           5x 5x 1x   4x 4x 6x     3x       22x 22x 1x   21x 21x         9x     9x 9x           6x 6x 1x   5x 5x 5x     3x       5x   5x 5x 5x 5x 5x 5x 5x       3x     37x   37x 37x                   329x           5x  
"use strict";
/*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.KVRaftStateMachine = exports.RaftError = exports.TnConsensusController = void 0;
const consensus_raft_1 = require("@coaty/consensus.raft");
const operators_1 = require("rxjs/operators");
const tn_base_service_1 = require("../service/tn-base-service");
const tn_consensus_service_1 = require("../service/tn-consensus-service");
const tn_base_controller_1 = require("./tn-base-controller");
class TnConsensusController extends tn_base_controller_1.TnBaseController {
    constructor() {
        super(...arguments);
        this._raftControllers = new Map();
    }
    async onServiceStopping() {
        await Promise.allSettled(Array.from(this._raftControllers.values()).map(rc => this.disconnect({ id: rc.options.id })));
    }
    create(createOptions) {
        var _a;
        const id = this.runtime.newUuid();
        const options = {
            id,
            cluster: (_a = createOptions.cluster) !== null && _a !== void 0 ? _a : "",
            stateMachine: new KVRaftStateMachine(),
            shouldCreateCluster: !!createOptions.shouldCreateCluster,
            databaseKey: "raftStore",
        };
        const ctrl = this.container.registerController("RaftController" + id, consensus_raft_1.RaftController, options);
        this._raftControllers.set(id, ctrl);
        return { id };
    }
    async connect(nodeRef) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            return RaftError.UndefinedRaftNodeId;
        }
        try {
            await ctrl.connect();
            return RaftError.None;
        }
        catch (error) {
            return RaftError.OperationNotSupportedInCurrentConnectionState;
        }
    }
    async disconnect(nodeRef) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            return RaftError.UndefinedRaftNodeId;
        }
        try {
            await ctrl.disconnect();
            return RaftError.None;
        }
        catch (error) {
            return RaftError.OperationNotSupportedInCurrentConnectionState;
        }
    }
    async stop(nodeRef) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            return RaftError.UndefinedRaftNodeId;
        }
        try {
            await ctrl.stop();
            return RaftError.None;
        }
        catch (error) {
            return RaftError.OperationNotSupportedInCurrentConnectionState;
        }
    }
    async propose(input) {
        var _a;
        const ctrl = this._raftControllers.get((_a = input.ref) === null || _a === void 0 ? void 0 : _a.id);
        if (ctrl === undefined) {
            return RaftError.UndefinedRaftNodeId;
        }
        try {
            if (input.value === null) {
                input.value = { nullValue: 0 };
            }
            let isEmptyObject = true;
            for (const _key in input.value) {
                isEmptyObject = false;
                break;
            }
            if (isEmptyObject) {
                this.debug("Proposed input rejected: value is not of type google.protobuf.Value");
                return RaftError.Internal;
            }
            return { keyValuePairs: Object.fromEntries(await ctrl.propose(input)) };
        }
        catch (error) {
            if (error instanceof consensus_raft_1.TooManyQueuedUpInputProposalsError) {
                return RaftError.TooManyQueuedUpInputProposals;
            }
            if (error instanceof consensus_raft_1.DisconnectBeforeOperationCompleteError) {
                return RaftError.DisconnectBeforeOperationComplete;
            }
            if (error instanceof consensus_raft_1.OperationNotSupportedInCurrentConnectionStateError) {
                return RaftError.OperationNotSupportedInCurrentConnectionState;
            }
            return RaftError.Internal;
        }
    }
    async getState(nodeRef) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            return RaftError.UndefinedRaftNodeId;
        }
        try {
            return { keyValuePairs: Object.fromEntries(await ctrl.getState()) };
        }
        catch (error) {
            Iif (error instanceof consensus_raft_1.DisconnectBeforeOperationCompleteError) {
                return RaftError.DisconnectBeforeOperationComplete;
            }
            Eif (error instanceof consensus_raft_1.OperationNotSupportedInCurrentConnectionStateError) {
                return RaftError.OperationNotSupportedInCurrentConnectionState;
            }
            return RaftError.Internal;
        }
    }
    observeState(nodeRef, onCancelled$) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            throw new Error("Raft node with this id has not been created");
        }
        try {
            return ctrl.observeState()
                .pipe((0, operators_1.takeUntil)(onCancelled$), (0, operators_1.map)(state => ({ keyValuePairs: Object.fromEntries(state) })));
        }
        catch (error) {
            throw new tn_base_service_1.UnavailableError("Raft node is currently not connected");
        }
    }
    async getClusterConfiguration(nodeRef) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            return RaftError.UndefinedRaftNodeId;
        }
        try {
            return {
                ids: (await ctrl.getClusterConfiguration()),
            };
        }
        catch (error) {
            Iif (error instanceof consensus_raft_1.DisconnectBeforeOperationCompleteError) {
                return RaftError.DisconnectBeforeOperationComplete;
            }
            Eif (error instanceof consensus_raft_1.OperationNotSupportedInCurrentConnectionStateError) {
                return RaftError.OperationNotSupportedInCurrentConnectionState;
            }
            return RaftError.Internal;
        }
    }
    observeClusterConfiguration(nodeRef, onCancelled$) {
        const ctrl = this._raftControllers.get(nodeRef.id);
        if (ctrl === undefined) {
            throw new Error("Raft node with this id has not been created");
        }
        try {
            return ctrl.observeClusterConfiguration()
                .pipe((0, operators_1.takeUntil)(onCancelled$), (0, operators_1.map)(ids => ({ ids })));
        }
        catch (error) {
            throw new tn_base_service_1.UnavailableError("Raft node is currently not connected");
        }
    }
}
exports.TnConsensusController = TnConsensusController;
var RaftError;
(function (RaftError) {
    RaftError[RaftError["None"] = 0] = "None";
    RaftError[RaftError["Internal"] = 1] = "Internal";
    RaftError[RaftError["DisconnectBeforeOperationComplete"] = 2] = "DisconnectBeforeOperationComplete";
    RaftError[RaftError["OperationNotSupportedInCurrentConnectionState"] = 3] = "OperationNotSupportedInCurrentConnectionState";
    RaftError[RaftError["TooManyQueuedUpInputProposals"] = 4] = "TooManyQueuedUpInputProposals";
    RaftError[RaftError["UndefinedRaftNodeId"] = 5] = "UndefinedRaftNodeId";
})(RaftError = exports.RaftError || (exports.RaftError = {}));
class KVRaftStateMachine {
    constructor() {
        this._state = new Map();
    }
    processInput(input) {
        switch (input.op) {
            case tn_consensus_service_1.RaftInputOperation.Put:
                this._state.set(input.key, input.value);
                break;
            case tn_consensus_service_1.RaftInputOperation.Delete:
                this._state.delete(input.key);
                break;
            case tn_consensus_service_1.RaftInputOperation.Unspecified:
            default:
                break;
        }
    }
    getState() {
        return Array.from(this._state);
    }
    setState(state) {
        this._state = new Map(state);
    }
}
exports.KVRaftStateMachine = KVRaftStateMachine;