Search code examples
pythonnode.jsthriftassertionsuber-api

Tchannel server code not working. (Python and nodejs)


I have just started learning Uber's Tchannel. I'm trying to run the code from tchannel documentation in python and nodejs. In both the cases I am not able to connect the client to the server.

This is how my code looks like in nodejs, which i followed from http://tchannel-node.readthedocs.org/en/latest/GUIDE/:

var TChannel = require('tchannel');
var myLocalIp = require('my-local-ip');

var rootChannel = TChannel();
rootChannel.listen(0,myLocalIp());
rootChannel.on('listening', function onListen() {
  console.log('got a server', rootChannel.address());
});

var TChannelThrift = rootChannel.TChannelAsThrift;

var keyChan = rootChannel.makeSubChannel({
    serviceName: process.env.USER || 'keyvalue'
});
var fs = require('fs');
var keyThrift = TChannelThrift({
    source: fs.readFileSync('./keyvalue.thrift', 'utf8')
});
var ctx = {
    store: {}
};

keyThrift.register(keyChan, 'KeyValue::get_v1', ctx, get);
keyThrift.register(keyChan, 'KeyValue::put_v1', ctx, put);

function get(context, req, head, body, cb) {
    cb(null, {
        ok: true,
        body: {
            value: context.store[body.key]
        }
    });
}
function put(context, req, head, body, cb) {
    context.store[body.key] = body.value;
    cb(null, {
        ok: true,
        body: null
    });
}

When i run this code i get this error:

node sever.js

assert.js:93
  throw new assert.AssertionError({
        ^
AssertionError: every field must be marked optional, required, or have a default value on GetResult including "value" in strict mode
    at ThriftStruct.link (/home/bhaskar/node_modules/thriftrw/struct.js:154:13)
    at Thrift.link (/home/bhaskar/node_modules/thriftrw/thrift.js:199:32)
    at new Thrift (/home/bhaskar/node_modules/thriftrw/thrift.js:69:10)
    at new TChannelAsThrift (/home/bhaskar/node_modules/tchannel/as/thrift.js:46:17)
    at TChannelAsThrift (/home/bhaskar/node_modules/tchannel/as/thrift.js:38:16)
    at Object.<anonymous> (/home/bhaskar/uber/tchannel/thrift/sever.js:16:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

Similar, I have also tried the same thing in python by following http://tchannel.readthedocs.org/projects/tchannel-python/en/latest/guide.html link. code in python looks like this:

from __future__ import absolute_import

from tornado import ioloop
from tornado import gen

from service import KeyValue
from tchannel import TChannel


tchannel = TChannel('keyvalue-server')

values={}
@tchannel.thrift.register(KeyValue)
def getValue(request):
    key = request.body.key
    value = values.get(key)

    if value is None:
        raise KeyValue.NotFoundError(key)

    return value

@tchannel.thrift.register(KeyValue)
def setValue(request):
    key = request.body.key
    value = request.body.value
    values[key] = value

def run():
    tchannel.listen()
    print('Listening on %s' % tchannel.hostport)


if __name__ == '__main__':
    run()
    ioloop.IOLoop.current().start()

when i run this with python server.py command i get ` Listening on my-local-ip:58092

` but when i try to connect it with the client using tcurl as:

tcurl -p localhost:58092 -t ~/keyvalue/thrift/service.thrift service KeyValue::setValue -3 '{"key": "hello", "value": "world"}'

I get this:

assert.js:93
  throw new assert.AssertionError({
        ^
AssertionError: every field must be marked optional, required, or have a default value on NotFoundError including "key" in strict mode
    at ThriftException.link (/usr/lib/node_modules/tcurl/node_modules/thriftrw/struct.js:154:13)
    at Thrift.link (/usr/lib/node_modules/tcurl/node_modules/thriftrw/thrift.js:199:32)
    at new Thrift (/usr/lib/node_modules/tcurl/node_modules/thriftrw/thrift.js:69:10)
    at new TChannelAsThrift (/usr/lib/node_modules/tcurl/node_modules/tchannel/as/thrift.js:46:17)
    at asThrift (/usr/lib/node_modules/tcurl/index.js:324:18)
    at onIdentified (/usr/lib/node_modules/tcurl/index.js:278:13)
    at finish (/usr/lib/node_modules/tcurl/node_modules/tchannel/peer.js:266:9)
    at Array.onIdentified [as 1] (/usr/lib/node_modules/tcurl/node_modules/tchannel/peer.js:257:9)
    at DefinedEvent.emit (/usr/lib/node_modules/tcurl/node_modules/tchannel/lib/event_emitter.js:90:25)
    at TChannelConnection.onOutIdentified (/usr/lib/node_modules/tcurl/node_modules/tchannel/connection.js:383:26)

Can anyone tell me what is the mistake?


Solution

  • for the node example, the thrift file in the guide needs to be updated. try to use the following (i just added a required keyword for every field):

    struct GetResult {
        1: required string value
    }
    
    service KeyValue {
        GetResult get_v1(
            1: required string key
        )
        void put_v1(
            1: required string key,
            2: required string value
        )
    }