Code coverage report for hyperbahn/entry.js

Statements: 81.69% (58 / 71)      Branches: 61.11% (11 / 18)      Functions: 92.31% (12 / 13)      Lines: 81.69% (58 / 71)      Ignored: 2 statements, 1 branch     

All files » hyperbahn/ » entry.js
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                                            1   1                                                             1           1   1 556 278   278     278 278 278     1   1   1 1   1       1   1 7 7   7       1 7       7                           1   1   1             1     1 1 7   7         7 7 7                     1         1             1 10 10       10     1 1 1   1   1 10 10   10       1 10       10                             1 1                 1 1 10           1      
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
'use strict';
 
var TypedError = require('error/typed');
 
var collectParallel = require('./lib/collect-parallel.js');
 
/* Idea
 
== Support unique serviceNames
 
If we force setting a `gitRepository` in the registration
we can have the exit nodes tell use whether there is a conflict.
 
This means the entry node can check all exit nodes results
for whether there are any conflicts and if there are, broadcast
unregister and fail the register
 
== Support "provides" / "aliases"
 
There was mention about wanting to say that your serviceName
is either `paxon` or `uber.surge.query`. A more common thing
might be either `logtron` or `utilities.logging`.
 
Allowing services to have aliases means they can say what
implementation they provide rather then saying what unique
snowflake name they have as a service.
 
Supporting aliases would basically require a secondary index
on the ringpop membership group. We would need to implement
a secondary index for ringpop and have a way to replicate the
secondary index information between all entry nodes
 
*/
 
// # EntryNodeCouldNotFindExitNodesError
var EntryNodeCouldNotFindExitNodesError = TypedError({
    type: 'autobahn.entry-node.could-not-find-exit-nodes',
    message: 'Autobahn: Entry node could not find any exit nodes!',
    nameAsThrift: 'couldNotFindExitNodes'
});
 
module.exports = EntryNode;
 
function EntryNode(clients) {
    if (!(this instanceof EntryNode)) {
        return new EntryNode(clients);
    }
    var self = this;
    // Store the clients internally. The external user
    // should not touch the clients directly, only methods.
    self._clients = clients;
    self.serviceProxy = clients.serviceProxy;
    self.egressNodes = clients.egressNodes;
}
 
EntryNode.prototype.getHostsConnectionsForService =
function getHostsConnectionsForService(opts, cb) {
    var self = this;
 
    var exitNodes = self.egressNodes.exitsFor(opts.serviceName);
    var hosts = Object.keys(exitNodes);
 
    Iif (hosts.length === 0) {
        return cb(EntryNodeCouldNotFindExitNodesError());
    }
 
    collectParallel(hosts, requestExitConnection, onAllCollected);
 
    function requestExitConnection(host, key, callback) {
        var autobahnChannel = self._clients.autobahnChannel;
        var tchannelJSON = self._clients.tchannelJSON;
 
        autobahnChannel.waitForIdentified({
            host: host
        }, onIdentified);
 
        function onIdentified(err) {
            Iif (err) {
                return callback(err);
            }
 
            tchannelJSON.send(autobahnChannel.request({
                host: host,
                timeout: 5000,
                serviceName: 'autobahn',
                parent: opts.inreq,
                headers: {
                    cn: 'autobahn'
                }
            }), 'exit_connections_v1', null, {
                serviceName: opts.serviceName
            }, callback);
        }
    }
 
    function onAllCollected(err, collection) {
        /* istanbul ignore if */
        Iif (err) {
            // collectParallel never passes errors forward.
            self._clients.logger.error(
                'unexpected service-connections collection error',
                {
                    error: err,
                    serviceName: opts.serviceName
                }
            );
            return cb(err);
        }
 
        var results = {};
        collection.forEach(function buildResult(nodeResult, index) {
            var nodeHost = hosts[index];
 
            Iif (nodeResult.err) {
                results[nodeHost] = {
                    err: nodeResult.err
                };
            } else {
                var res = nodeResult.value;
                Eif (res.ok) {
                    results[nodeHost] = {
                        instances: nodeResult.value.body
                    };
                } else {
                    results[nodeHost] = {
                        err: nodeResult.value.body
                    };
                }
            }
        });
 
        cb(null, results);
 
    }
};
 
EntryNode.prototype.isExitNodeFor = function isExitNodeFor(service) {
    var self = this;
    var exitNodes = self.egressNodes.exitsFor(service);
    var whoami = self._clients.ringpop.whoami();
    return exitNodes[whoami] !== undefined;
};
 
EntryNode.prototype.setK = function setK(serviceName, k) {
    var self = this;
    self._clients.logger.info('k set', {
        serviceName: serviceName,
        k: k
    });
    self.egressNodes.setKValueFor(serviceName, k);
};
 
EntryNode.prototype.fanoutSetK = function fanoutSetK(opts, cb) {
    var self = this;
    var ringpop = self._clients.ringpop;
 
    collectParallel(ringpop.membership.members, exitSetK, onAllSet);
 
    function exitSetK(member, key, callback) {
        var autobahnChannel = self._clients.autobahnChannel;
        var tchannelJSON = self._clients.tchannelJSON;
 
        autobahnChannel.waitForIdentified({
            host: member.address
        }, onIdentified);
 
        function onIdentified(err) {
            Iif (err) {
                return callback(err);
            }
 
            tchannelJSON.send(autobahnChannel.request({
                host: member.address,
                parent: opts.inreq,
                timeout: 2000,
                serviceName: 'autobahn',
                headers: {
                    cn: 'autobahn'
                }
            }), 'exit_set_k_v1', null, {
                serviceName: opts.serviceName,
                k: opts.k
            }, callback);
        }
    }
 
    function onAllSet(err, collection) {
        Iif (err) {
            self._clients.logger.error('unexpected k fanout error', {
                error: err,
                serviceName: opts.serviceName,
                k: opts.k
            });
            return cb(err);
        }
 
        var results = {};
        collection.forEach(function buildResult(setKResult, index) {
            Iif (setKResult.err) {
                var member = ringpop.membership.members[index];
                results[member.address] = setKResult.err;
            }
        });
 
        cb(null, results);
    }
};