|
| 1 | +import sys |
| 2 | +import os |
| 3 | +from threading import Thread |
| 4 | +import time |
| 5 | +import yaml |
| 6 | +from lib.tarantool_server import TarantoolServer |
| 7 | +from ....tarantool import MeshConnection |
| 8 | +from ....tarantool.const import ( |
| 9 | + SOCKET_TIMEOUT, |
| 10 | + RECONNECT_DELAY, |
| 11 | +) |
| 12 | +from ....tarantool.error import NetworkError |
| 13 | +from ....tarantool.utils import ENCODING_DEFAULT |
| 14 | + |
| 15 | +REPLICA_N = 3 |
| 16 | +ROW_N = REPLICA_N * 20 |
| 17 | + |
| 18 | + |
| 19 | +def check_cluster(addrs): |
| 20 | + con = MeshConnection(addrs=addrs, |
| 21 | + user=None, |
| 22 | + password=None, |
| 23 | + socket_timeout=SOCKET_TIMEOUT, |
| 24 | + reconnect_max_attempts=1, |
| 25 | + reconnect_delay=RECONNECT_DELAY, |
| 26 | + connect_now=True, |
| 27 | + encoding=ENCODING_DEFAULT) |
| 28 | + while True: |
| 29 | + try: |
| 30 | + x = con.ping() |
| 31 | + print 'ping Ok' |
| 32 | + except NetworkError: |
| 33 | + print 'NetworkError !' |
| 34 | + break |
| 35 | + except Exception as e: |
| 36 | + print e |
| 37 | + time.sleep(1) |
| 38 | + |
| 39 | +## |
| 40 | + |
| 41 | +# master server |
| 42 | +master = server |
| 43 | +master.admin("fiber = require('fiber')") |
| 44 | +master.admin("box.schema.user.grant('guest', 'replication')") |
| 45 | +master.admin("box.schema.user.grant('guest', 'execute', 'universe')") |
| 46 | + |
| 47 | +print '----------------------------------------------------------------------' |
| 48 | +print 'Bootstrap replicas' |
| 49 | +print '----------------------------------------------------------------------' |
| 50 | + |
| 51 | +# Start replicas |
| 52 | +master.id = master.get_param('id') |
| 53 | +cluster = [master] |
| 54 | +for i in range(REPLICA_N - 1): |
| 55 | + server = TarantoolServer(server.ini) |
| 56 | + server.script = 'cluster-py/replica.lua' |
| 57 | + server.vardir = os.path.join(server.vardir, 'replica', str(master.id + i)) |
| 58 | + server.rpl_master = master |
| 59 | + server.deploy() |
| 60 | + # Wait replica to fully bootstrap. |
| 61 | + # Otherwise can get ACCESS_DENIED error. |
| 62 | + cluster.append(server) |
| 63 | + |
| 64 | +# Make a list of servers |
| 65 | +sources = [] |
| 66 | +for server in cluster: |
| 67 | + sources.append(yaml.load(server.admin('box.cfg.listen', silent=True))[0]) |
| 68 | + server.id = server.get_param('id') |
| 69 | + |
| 70 | +print 'done' |
| 71 | + |
| 72 | +print '----------------------------------------------------------------------' |
| 73 | +print 'Make a full mesh' |
| 74 | +print '----------------------------------------------------------------------' |
| 75 | + |
| 76 | +addrs = [] |
| 77 | +for addr in sources[1:]: |
| 78 | + arr = addr.split(':') |
| 79 | + addrs.append({'host': arr[0], 'port': int(arr[1])}) |
| 80 | + |
| 81 | +# Connect each server to each other to make full mesh |
| 82 | +for server in cluster: |
| 83 | + server.iproto.py_con.eval("box.cfg { replication = ... }", [sources]) |
| 84 | + |
| 85 | +# Wait connections to establish |
| 86 | +for server in cluster: |
| 87 | + for server2 in cluster: |
| 88 | + server.iproto.py_con.eval(""" |
| 89 | + while #box.info.vclock[...] ~= nil do |
| 90 | + fiber.sleep(0.01) |
| 91 | + end;""", server2.id) |
| 92 | + print 'server', server.id, "connected" |
| 93 | + server.admin("box.info.vclock") |
| 94 | + |
| 95 | +print 'done' |
| 96 | + |
| 97 | +print '----------------------------------------------------------------------' |
| 98 | +print 'Check mesh connection' |
| 99 | +print '----------------------------------------------------------------------' |
| 100 | + |
| 101 | +thread = Thread(target=check_cluster, args=(addrs, )) |
| 102 | +thread.start() |
| 103 | + |
| 104 | +print |
| 105 | +print '----------------------------------------------------------------------' |
| 106 | +print 'Cleanup' |
| 107 | +print '----------------------------------------------------------------------' |
| 108 | + |
| 109 | +for server in cluster: |
| 110 | + server.stop() |
| 111 | + print 'server', server.id, 'done' |
| 112 | +print |
| 113 | + |
| 114 | +thread.join() |
| 115 | + |
| 116 | +master.cleanup() |
| 117 | +master.deploy() |
0 commit comments