File: multi.test.py

package info (click to toggle)
tarantool 2.6.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,396 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,176; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (71 lines) | stat: -rw-r--r-- 2,107 bytes parent folder | download | duplicates (3)
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
import sys
import os
import time
import yaml
from lib.tarantool_server import TarantoolServer
sys.path.append('../tarantool')
from mesh_connection import MeshConnection
from tarantool.const import (
    SOCKET_TIMEOUT,
    RECONNECT_DELAY,
)
from tarantool.error import NetworkError
from tarantool.utils import ENCODING_DEFAULT

INSTANCE_N = 2


def check_connection(con):
    try:
        s = con.space('test')
        print s.select()
    except NetworkError:
        print 'NetworkError !'
    except Exception as e:
        print e


# Start instances
master = server
cluster = [master]
for i in range(INSTANCE_N):
    server = TarantoolServer(server.ini)
    server.script = 'cluster-py/instance%d.lua' % (i+1)
    server.vardir = os.path.join(server.vardir, 'instance', str(i))
    server.deploy()
    server.admin("box.schema.user.grant('guest', 'read,write,execute', 'universe')")
    server.admin("_ = box.schema.space.create('test')")
    server.admin("_ = box.space.test:create_index('primary')")
    server.admin("box.space.test:insert{%d, %s}" % (1, i), silent = True)
    cluster.append(server)

# Make a list of servers
sources = []
for server in cluster[1:]:
    sources.append(yaml.safe_load(server.admin('box.cfg.listen', silent=True))[0])

addrs = []
for addr in sources:
    addrs.append({'host': None, 'port': addr})

con = MeshConnection(addrs=addrs,
                     user=None,
                     password=None,
                     socket_timeout=SOCKET_TIMEOUT,
                     reconnect_max_attempts=0,
                     reconnect_delay=RECONNECT_DELAY,
                     connect_now=True,
                     encoding=ENCODING_DEFAULT)

cluster[0].stop()       # stop server - no effect
check_connection(con)   # instance#1
cluster[1].stop()       # stop instance#1
check_connection(con)   # instance#2
cluster[1].start()      # start instance#1
cluster[2].stop()       # stop instance#2
check_connection(con)   # instance#1 again
cluster[1].stop()       # stop instance#1
check_connection(con)   # both stopped: NetworkError !

master.cleanup()
master.deploy()