Exemplo n.º 1
0
def main():
    global steem
    global tnt_server
    global steem_space
    global chain

    print('starting datafeed.py..')
    print('Connecting to ', NODE_URL)
    sys.stdout.flush()

    steem = Steem(NODE_URL)
    chain = Blockchain(steemd_instance=steem, mode='head')

    while True:
        try:
            print(f'Connecting to tarantool ({TARANTOOL_HOST}:3301)..')
            sys.stdout.flush()
            tnt_server = tarantool.connect(TARANTOOL_HOST, 3301)
            steem_space = tnt_server.space('steem')
        except Exception as e:
            print('Cannot connect to tarantool server', file=sys.stderr)
            print(str(e), file=sys.stderr)
            sys.stderr.flush()
            time.sleep(10)
            continue
        else:
            while True:
                print('Started')
                sys.stdout.flush()
                run()
                print('[run] exited, continue..')
Exemplo n.º 2
0
def main():

    connection = tarantool.connect("localhost", 3301)
    tester = connection.space('tester')
    tester1 = connection.space('tester1')

    print("\033[33mtester\033[0m:")
    print(tester.select(3))
    print("\033[33mtester\033[31m1\033[0m:")
    print(tester1.select(3))

    print("\033[33mtester\033[0m:")
    print(tester.select())
    print("\033[33mtester\033[31m1\033[0m:")
    print(tester1.select(),"\n")

    print(tester)
    print(tester1)

    print(f"\nping: {connection.ping()}")

    tester2 = connection.space('tester2')

    print("\033[33m\ntester\033[31m2\033[0m:")
    print(tester2.select(), "\n")

    connection.call('box.space.tester2:truncate', ())

    print("\033[33m\ntester\033[31m2\033[0m:")
    print(tester2.select(), "\n")

    connection.call('box.space.tester2:drop', ())

    print("\033[33m\ntester\033[31m2\033[0m:")
    print(tester2.select(), "\n")
Exemplo n.º 3
0
def main():
    global tnt_server
    global steem_space
    global followers_space
    global chain

    print('starting datafeed.py..')
    sys.stdout.flush()

    chain = Blockchain(mode='head')

    while True:
        try:
            print('Connecting to tarantool (datastore:3301)..')
            sys.stdout.flush()
            tnt_server = tarantool.connect('datastore', 3301)
            steem_space = tnt_server.space('steem')
            followers_space = tnt_server.space('followers')
        except Exception as e:
            print('Cannot connect to tarantool server', file=sys.stderr)
            print(str(e), file=sys.stderr)
            sys.stderr.flush()
            time.sleep(10)
            continue
        else:
            while True:
                run()
                print('[run] exited, continue..')
Exemplo n.º 4
0
class Place:
    conn = tarantool.connect(Config.TARANTOOL_HOST, Config.TARANTOOL_PORT)
    space_name = "place"

    def __init__(self, code, name):
        self.code = code
        self.name = name

    def __eq__(self, other):
        if isinstance(other, Place):
            return self.code == other.code and self.name == other.name
        return False

    def save(self):
        if not self.code or not self.name:
            raise ValueError('empty code or name')
        else:
            Place.conn.insert(Place.space_name, (self.code, self.name))

    @staticmethod
    def search(code):
        result = Place.conn.select(Place.space_name, code)
        if result.rowcount > 0:
            return Place(result[0][0], result[0][1])
        else:
            return None

    @staticmethod
    def delete(place):
        Place.conn.delete(Place.space_name, place.code)
Exemplo n.º 5
0
    def do_complete(self, code, cursor_pos):

        logging.info("code completion")
        logging.info(str(code))
        logging.info(str(cursor_pos))
        temp_code = code.split('\n')[-1].split(' ')[-1]
        temp_cursor_pos = len(temp_code)

        tnt = tarantool.connect(HOST, CONSOLE_PORT)

        autocompetion = """
        function jupyter_autocomplete(string, pos1, pos2)
            local json = require('json')
            local yaml = require('yaml')
            local console = require('console')
            local c = console.completion_handler(string, pos1, pos2)
            local res = c or {}
            return json.encode(res)
        end
        """
        _ = tnt.eval(autocompetion)

        cmd = f'return jupyter_autocomplete("{temp_code}",{0},{temp_cursor_pos})'
        matches = json.loads(tnt.eval(cmd)[0])

        default = {
            'matches': matches,
            'cursor_start': 0,
            'cursor_end': cursor_pos,
            'metadata': dict(),
            'status': 'ok'
        }
        return default
Exemplo n.º 6
0
def main():
    global steem
    global tnt_server
    global steem_space
    global followers_space
    global chain

    print('starting datafeed.py..')
    ws_connection = os.environ['STEEMIT_WEBSOCKET_CONNECTION']
    print('Connecting to ', ws_connection)
    sys.stdout.flush()

    steem = Steem(ws_connection)
    chain = Blockchain(steem_instance=steem, mode='head')

    while True:
        try:
            print('Connecting to tarantool (datastore:3301)..')
            sys.stdout.flush()
            tnt_server = tarantool.connect('datastore', 3301)
            steem_space = tnt_server.space('steem')
            followers_space = tnt_server.space('followers')
        except Exception as e:
            print('Cannot connect to tarantool server', file=sys.stderr)
            print(str(e), file=sys.stderr)
            sys.stderr.flush()
            time.sleep(10)
            continue
        else:
            while True:
                run()
                print('[run] exited, continue..')
Exemplo n.º 7
0
async def registerService(app):
    try:
        consulConnection = consul.Consul(host=config["consul"]["address"],
                                         port=config["consul"]["port"])
        consulConnection.agent.service.register(
            config["server"]["name"],
            address=config["server"]["address"],
            port=config["server"]["port"])
        setTokenzer(consulConnection)
        setTarantool(consulConnection)
        app["tarantool"] = tarantool.connect(
            host=config["tarantool"]["address"],
            port=config["tarantool"]["port"],
            user=config["tarantool"]["user"],
            password=config["tarantool"]["password"])
    except Exception as error:
        print({
            "type": "Fatal",
            "module": "Init",
            "section": "registerService",
            "message": error.__str__(),
            "date": datetime.datetime.now().isoformat("T")
        })
        consulConnection.agent.service.deregister(config["server"]["name"])
        sys.exit("Невозможно получить информацию о сервисах")
Exemplo n.º 8
0
def tarantool_load_sql(df):
    import tarantool
    import zlib

    c = tarantool.connect("localhost", 3301)

    space = c.space(514)

    test_name = "tarantool sql"

    for i, row in enumerate(df.values):
        tuple1 = (i,) + tuple(row)
        cmpd = zlib.compress(row[11])
        tup = (i,) + tuple(row[:11]) + (cmpd, row[12])
        space.replace(tup)
        response = space.select(i)
        if not response:
            print test_name + "empty response"
            print "original " + str(tup)
            break
        #response[0][12] = zlib.decompress(response[0][12])
        # if tuple1 not in [tuple(response[0])]:
        #     print test_name + " response is not equal to original"
        #     print "original " + str(tuple1)
        #     print "return " + str(response[0])
        #     break
    print test_name + " size: %s MB" % (c.call("mem_used", 514)[0][0])
 def setUp(self):
     host_cfg = settings.SENTRY_BUFFER_OPTIONS['hosts'][0]['host']
     host, _, port = host_cfg.rpartition(':')
     self.client = tarantool.connect(host, int(port))
     self.buffer = self.get_instance(
         'tarantool_utils.sentry.Tarantool15Buffer',
         settings.SENTRY_BUFFER_OPTIONS)
     self.clean_tnt()
Exemplo n.º 10
0
    def _init(self):

        ip = self.ip
        port = self.port
        login = self.login
        password = self.password_

        self.db = tarantool.connect(ip, user=login, port=port, password=password)
        self.call = self.db.call
        self.models = Models()
Exemplo n.º 11
0
 def connect(self):
     assert self.process.poll() is None
     if self.conn == None:
         self.conn = tarantool.connect(
             '127.0.0.1',
             self.binary_port,
             user='******',
             password=self.env['TARANTOOL_CLUSTER_COOKIE'])
     resp = self.conn.eval('return is_initialized()')
     err = resp[1] if len(resp) > 1 else None
     assert (resp[0], err) == (True, None)
Exemplo n.º 12
0
def main():
    print('Connecting to tarantool (datastore:3301)..')
    sys.stdout.flush()

    try:
        tnt_server = tarantool.connect('localhost', 3301)
    except Exception as e:
        print('Cannot connect to tarantool server', file=sys.stderr)
        print(str(e), file=sys.stderr)
        sys.stderr.flush()
    else:
        run()
Exemplo n.º 13
0
 def connect(self):
     try:
         if self.user == '':
             self.database = tarantool.connect(self.host, self.port)
         else:
             self.database = tarantool.connect(self.host,
                                               self.port,
                                               user=self.user,
                                               password=self.passwd)
         # self.database.eval(open('lua/orange.lua', 'r').read())
         self.database.eval(
             open(str(Path(__file__).parents[0]) + '/lua/orange.lua',
                  'r').read())
         self.Outputs.connection.send(self.database)
         self.disconnect_button.setEnabled(True)
         self.connect_button.setEnabled(False)
         # self.setCaption("%s:%d" % (self.host, self.port))
         self.Error.error.clear()
     except (tarantool.error.DatabaseError,
             tarantool.error.InterfaceError) as e:
         self.Error.error(e)
Exemplo n.º 14
0
 def migrate(cls,
             host: str,
             port: int,
             user: str = 'admin',
             password: str = 'admin') -> None:
     admin = tarantool.connect(host, port, user, password)
     migrations_dir = cls.__get_migrations_dir()
     for file in sorted(os.listdir(migrations_dir)):
         with open(os.path.join(migrations_dir, file)) as f:
             admin.eval(f.read().strip())
     User.set_host(host)
     User.set_port(port)
Exemplo n.º 15
0
 def __init__(self,
              host=None,
              port=None,
              user=None,
              password=None,
              space=None):
     self.connection = tarantool.connect(host=host,
                                         port=port,
                                         user=user,
                                         password=password)
     self.space = space
     self.connection.ping()
Exemplo n.º 16
0
 def get(self,
         ipaddr="localhost",
         port=3301,
         login="******",
         password=None,
         fromcache=True):
     key = "%s_%s" % (ipaddr, port)
     if key not in self._tarantool or fromcache is False:
         self._tarantool[key] = tarantool.connect(ipaddr,
                                                  user=login,
                                                  port=port,
                                                  password=password)
     return Tarantool(self._tarantool[key])
Exemplo n.º 17
0
    def __init__(self, instance, data={}, parent=None, interactive=False):
        JSConfigClient.__init__(self,
                                instance=instance,
                                data=data,
                                parent=parent,
                                template=TEMPLATE,
                                interactive=interactive)
        c = self.config.data
        ip = c['ip']
        port = c['port']
        login = c['login']
        password = c['password_']

        self.db = tarantool.connect(ip,
                                    user=login,
                                    port=port,
                                    password=password)
        self.call = self.db.call
        self.models = Models()
Exemplo n.º 18
0
def tarantool_load_nosql(objects, numbers):
    import tarantool

    c = tarantool.connect("localhost", 3301)
    
    space = c.space(513)

    test_name = "tarantool nosql"

    for i, o in enumerate(objects):
        tup = (o['name'], o['coords'],)
        c.call('insert_hash', tup[0], tup[1])
        response = c.call('my_get', tup[0])
        if tuple(response[0]) != tup:
            print test_name + " is not equal to original"
            print "original " + str(tup)
            print "return " + str(response[0])
            break
        if (i + 1) in numbers:
            print test_name + " objects: %s size: %s MB" % (i + 1, c.call("mem_used", 513)[0][0])
Exemplo n.º 19
0
def test_producer():
    server = tarantool.connect("127.0.0.1", 3301)

    server.call("producer", ((
        "1",
        "2",
        "3",
    ), ))

    loop = asyncio.get_event_loop()

    async def test():
        kafka_output = []

        async def consume():
            consumer = AIOKafkaConsumer(
                'test_producer',
                group_id="test_group",
                loop=loop,
                bootstrap_servers='localhost:9092',
                auto_offset_reset="earliest",
            )
            # Get cluster layout
            await consumer.start()

            try:
                # Consume messages
                async for msg in consumer:
                    kafka_output.append({
                        'key':
                        msg.key if msg.key is None else msg.key.decode('utf8'),
                        'value':
                        msg.value
                        if msg.value is None else msg.value.decode('utf8')
                    })

            finally:
                # Will leave consumer group; perform autocommit if enabled.
                await consumer.stop()

        try:
            await asyncio.wait_for(consume(), 10, loop=loop)
        except asyncio.TimeoutError:
            pass

        assert kafka_output == [
            {
                "key": None,
                "value": "1"
            },
            {
                "key": None,
                "value": "2"
            },
            {
                "key": None,
                "value": "3"
            },
        ]

    loop.run_until_complete(test())
Exemplo n.º 20
0
 def _tnt(self):
     host, _, port = self._servers.rpartition(':')
     client = tarantool.connect(host, int(port))
     return client
Exemplo n.º 21
0
from flask import Flask, jsonify, request
from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)

limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=['2 per second'],
)

# подключаемся к серверу и к необходимому спейсу
c = tarantool.connect('localhost', 3301, user='******', password='******')
space = c.space('api')


@app.errorhandler(429)
def ratelimit_handler(e):
    app.logger.warning(f'[ratelimit_handler]: too many requests')
    return jsonify({'message': 'too many requests', 'status': 429}), 429


@app.route('/kv/<string:key>', methods=['GET'])
def get_value(key):

    # ищем запись в базе
    val = space.select(key)
Exemplo n.º 22
0
import tarantool
server = tarantool.connect("localhost", 33013)

demo = server.space(0)

print(demo.select('AAAA'))
Exemplo n.º 23
0
#!/usr/bin/env python3
import json
import tarantool

from flask import Flask
from flask import request

TARANTOOL_HOST = "localhost"
TARANTOOL_PORT = 3301
TARANTOOL_PASSWORD = '******'

app = Flask(__name__)
server = tarantool.connect(host=TARANTOOL_HOST,
                           port=TARANTOOL_PORT,
                           password=TARANTOOL_PASSWORD)
kv = server.space('kv')


class ResponseError(BaseException):
    def __init__(self, error_code, message, errors=None):
        self.error_code = error_code
        self.message = message
        self.errors = errors

    def format(self):
        if self.errors:
            return {
                "message": self.message,
                "errors": self.errors
            }, self.error_code
        else:
Exemplo n.º 24
0
    print server.center(30, '*')
    for x in results:
        key = x[0]
        val = x[1]
        res = conn.call(procname, key)
        print res

        assert res.rowcount == 1, 'Cannot find key ' + key
        assert res[0][0] == key and res[0][1] == val, \
        'Unexpected response from ' + server + ': ' + str(res)

os.system("./tarantool-setup.sh")

time.sleep(5)

proxy = tarantool.connect(host = 'localhost', port=22121)
ptest = proxy.space('test')

con1 = tarantool.connect(host = 'localhost', port=3301)
c1test = con1.space('test')

con2 = tarantool.connect(host = 'localhost', port=3302)
c2test = con2.space('test')

con3 = tarantool.connect(host = 'localhost', port=3303)
c3test = con3.space('test')

print '=============================='
print '          ping tests          '
print '=============================='
Exemplo n.º 25
0
 def _tnt(self):
     if not hasattr(self, '_client'):
         self._client = tarantool.connect(self._host, int(self._port))
     return self._client
Exemplo n.º 26
0
 def tnt(self):
     with self._instance_lock:
         if not hasattr(self, '_tnt'):
             self._tnt = tarantool.connect(self.host, self.port)
     return self._tnt
Exemplo n.º 27
0
# -*- coding: utf-8 -*-

import time
import tarantool

benchmark = {
    "tarantool": {},
    "aiotarantool": {},
}

cnt = 0

tnt = tarantool.connect("127.0.0.1", 3301)

import string

mod_len = len(string.printable)
data = [string.printable[it] * 1536 for it in range(mod_len)]

# sync benchmark
# insert test
print("tarantool insert test")
t1 = time.time()
for it in range(100000):
    r = tnt.insert("tester", (it, data[it % mod_len]))

t2 = time.time()
benchmark["tarantool"]["insert"] = t2 - t1

# select test
print("tarantool select test")
Exemplo n.º 28
0
 def _tnt(self):
     first_host = self.hosts[0]['host']
     host, _, port = first_host.rpartition(':')
     client = tarantool.connect(host, int(port))
     return client
Exemplo n.º 29
0
import tarantool

connection = tarantool.connect('89.208.198.172', 3307)

space_default = connection.space('default')

space_prophecy = connection.space('prophecy')

space_color = connection.space('color')

space_number = connection.space('number')
		
Exemplo n.º 30
0
import tarantool
import logging
import falcon
import utils
import mware
import json

connection = tarantool.connect(host='localhost', port=3331)
logging.basicConfig(filename='app.log', level=logging.DEBUG)


def tarantool_call(function_name, *args):
    data = connection.call(function_name, args).data[0]
    logging.info(function_name)
    if data is None:
        raise falcon.HTTPBadRequest('Import not found.')

    return data


class Import():
    def on_post(self, req, resp):
        posted_data = req.context['request']
        valid, msg = utils.validate(posted_data)

        if not valid:
            resp.status = falcon.HTTP_400
            return

        formatted = utils.format_to(posted_data)
        ret = tarantool_call('import_create', formatted)
Exemplo n.º 31
0
def connect(db_host, db_port):
    global statistics, connection
    connection = tarantool.connect(db_host, db_port)
    statistics = connection.space("statistics")
Exemplo n.º 32
0
 def __init__(self, user: str = None, password: str = None):
     self.__conn = tarantool.connect(self.__HOST, self.__PORT, user,
                                     password)
Exemplo n.º 33
0
# -*- coding: utf-8 -*-

import time
import tarantool

benchmark = {
    "tarantool": {},
    "aiotarantool": {},
}

cnt = 0

tnt = tarantool.connect("127.0.0.1", 3301)

import string
mod_len = len(string.printable)
data = [string.printable[it] * 1536 for it in range(mod_len)]

# sync benchmark
# insert test
print("tarantool insert test")
t1 = time.time()
for it in range(100000):
    r = tnt.insert("tester", (it, data[it % mod_len]))

t2 = time.time()
benchmark["tarantool"]["insert"] = t2 - t1

# select test
print("tarantool select test")
t1 = time.time()
Exemplo n.º 34
0
import tarantool

connection = tarantool.connect("localhost", 3301)

space_user = connection.space('user')


class User:
    def __init__(
        self,
        user_id,
    ):
        self.user = space_user.select(user_id)
        self.user_id = user_id

    def get(self):
        if not self.user:
            self.user = space_user.insert((self.user_id, True, {
                'schedule': None,
                'homework': None,
                'del_schedule': None
            }, {}, {}))

        self.space = self.user[0]
        self.old_mes = self.space[2]
        self.schedule = self.space[3]
        self.homework = self.space[4]
        return self

    def save(self):
        space_user.replace(self.user[0])