gen_port_setup.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python3
  2. import csv
  3. import os
  4. NODE_IPS=["192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.0.4"]
  5. NUM_CPUS=6
  6. RAFT_GROUP=3
  7. TOTAL_SLOTS=16384
  8. cluster_script = {}
  9. def unitfile(slot_start, slot_end, port, ip):
  10. myfile = """
  11. ####################################################################
  12. [Unit]
  13. Description=Redis Server Port %d
  14. After=network.target
  15. [Service]
  16. WorkingDirectory=/db/data
  17. Type=simple
  18. LimitNOFILE=65535
  19. User=redislabs
  20. ExecStart=/usr/bin/taskset -c 2-32 /usr/local/bin/redis-server --port %d --protected-mode no --dbfilename %d-redis.rdb --loadmodule /usr/local/share/redisraft/redisraft.so addr=%s:%d raft-log-fsync=no follower-proxy=no raftize-all-commands=yes cluster-start-hslot=%d cluster-end-hslot=%d raft-log-filename=%d-raftlog.db cluster-mode=yes
  21. StandardOutput=file:/db/errorlog/redis-%d.log
  22. StandardError=file:/db/errorlog/redis-%d-error.log
  23. [Install]
  24. WantedBy=multi-user.target
  25. Alias=redis-%d.service
  26. """
  27. return (myfile %(port, port, port, ip, port, slot_start, slot_end, port, port, port, port))
  28. def bucket(seq, num):
  29. avg = len(seq) / float(num)
  30. out = []
  31. last = 0.0
  32. while last < len(seq):
  33. out.append(seq[int(last):int(last + avg)])
  34. last += avg
  35. return out
  36. try:
  37. os.mkdir("/tmp/raft")
  38. except:
  39. dir = 1
  40. for ip in NODE_IPS:
  41. try:
  42. os.mkdir(os.path.join("/tmp/raft", ip))
  43. except:
  44. dir = 1
  45. with open('/tmp/raft/slots.csv', mode='w') as slots_file:
  46. slots_writer = csv.writer(
  47. slots_file,
  48. delimiter=',',
  49. quotechar='"',
  50. quoting=csv.QUOTE_MINIMAL)
  51. p = 1
  52. slot_row = []
  53. for x in bucket(range(TOTAL_SLOTS), (len(NODE_IPS)*NUM_CPUS)/RAFT_GROUP):
  54. r = "Slots: %s-%s Port: %d" %(x[0], x[-1], 19900 +p)
  55. for i in range(0,RAFT_GROUP):
  56. ip = NODE_IPS[len(slot_row)%len(NODE_IPS)]
  57. #####
  58. uf = "redis-%d.service" %(19900+p)
  59. with open(os.path.join('/tmp/raft/', ip, uf), mode='w') as of:
  60. of.write(unitfile(x[0], x[-1], 19900+p, ip))
  61. if 19900+p in cluster_script:
  62. cluster_script[19900+p].append(ip)
  63. else:
  64. cluster_script[19900+p] = [ip]
  65. if len(slot_row) == len(NODE_IPS):
  66. slots_writer.writerow(slot_row)
  67. slot_row = []
  68. slot_row.append(r)
  69. p += 1
  70. slots_writer.writerow(slot_row)
  71. with open('/tmp/raft/setup_cluster.sh', mode='w') as outf:
  72. outf.write("#!/bin/bash -ex\n\n\n")
  73. for port in cluster_script:
  74. outf.write("\necho \"Setting up Raft Groups - port %d\"\n\n" %port)
  75. outf.write("/usr/local/bin/redis-cli -h %s -p %d raft.cluster init\n" %(cluster_script[port][0], port))
  76. for x in cluster_script[port][1:]:
  77. outf.write("/usr/local/bin/redis-cli -h %s -p %d raft.cluster join %s:%d\n" %(x, port, cluster_script[port][0], port))
  78. outf.write("\n\n\n# Sleep until raft cluster is created\necho \"Waiting for Raft to stabilize\"\nsleep 10\n\n# Linking Shards\n\n")
  79. l = list(cluster_script.keys())
  80. for p in l[1:-1]:
  81. outf.write("/usr/local/bin/redis-cli -h %s -p %d RAFT.SHARDGROUP LINK %s:%d\n" %(cluster_script[l[0]][0], l[0], cluster_script[p][0], p))