|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from testgres import PostgresNode |
| 4 | +from testgres import get_new_node |
| 5 | + |
| 6 | + |
| 7 | +class Shardlord(PostgresNode): |
| 8 | + def __init__(self, name): |
| 9 | + super(Shardlord, self).__init__(name=name) |
| 10 | + |
| 11 | + self.nodes = [] |
| 12 | + |
| 13 | + @staticmethod |
| 14 | + def _common_conf_lines(): |
| 15 | + return ( |
| 16 | + "shared_preload_libraries = 'pg_pathman, pg_shardman'\n" |
| 17 | + |
| 18 | + "log_min_messages = DEBUG1\n" |
| 19 | + "client_min_messages = NOTICE\n" |
| 20 | + "log_replication_commands = on\n" |
| 21 | + |
| 22 | + "synchronous_commit = on\n" |
| 23 | + |
| 24 | + "wal_level = logical\n" |
| 25 | + |
| 26 | + "max_replication_slots = 100\n" |
| 27 | + "max_wal_senders = 50\n" |
| 28 | + "max_connections = 200\n" |
| 29 | + ) |
| 30 | + |
| 31 | + def init(self): |
| 32 | + super(Shardlord, self).init() |
| 33 | + |
| 34 | + config_lines = ( |
| 35 | + "shardman.shardlord = on\n" |
| 36 | + "shardman.shardlord_dbname = postgres\n" |
| 37 | + "shardman.shardlord_connstring = 'dbname=postgres port={}'\n" |
| 38 | + "shardman.cmd_retry_naptime = 500\n" |
| 39 | + "shardman.poll_interval = 500\n" |
| 40 | + ).format(self.port) |
| 41 | + |
| 42 | + # add common config lines |
| 43 | + config_lines += self._common_conf_lines() |
| 44 | + |
| 45 | + self.append_conf("postgresql.conf", config_lines) |
| 46 | + |
| 47 | + return self |
| 48 | + |
| 49 | + def install(self): |
| 50 | + self.safe_psql(dbname="postgres", |
| 51 | + query="create extension pg_shardman cascade") |
| 52 | + |
| 53 | + return self |
| 54 | + |
| 55 | + def cleanup(self): |
| 56 | + super(Shardlord, self).cleanup() |
| 57 | + |
| 58 | + for node in self.nodes: |
| 59 | + node.cleanup() |
| 60 | + |
| 61 | + return self |
| 62 | + |
| 63 | + def add_node(self, name): |
| 64 | + config_lines = ( |
| 65 | + "max_logical_replication_workers = 50\n" |
| 66 | + "max_worker_processes = 60\n" |
| 67 | + "wal_receiver_timeout = 60s\n" |
| 68 | + ) |
| 69 | + |
| 70 | + # add common config lines |
| 71 | + config_lines += self._common_conf_lines() |
| 72 | + |
| 73 | + # create a new node |
| 74 | + node = get_new_node(name) |
| 75 | + self.nodes.append(node) |
| 76 | + |
| 77 | + # start this node |
| 78 | + node.init() \ |
| 79 | + .append_conf("postgresql.conf", config_lines) \ |
| 80 | + .start() \ |
| 81 | + .safe_psql(dbname="postgres", |
| 82 | + query="create extension pg_shardman cascade") |
| 83 | + |
| 84 | + # finally, register this node |
| 85 | + add_node_cmd = ( |
| 86 | + "select shardman.add_node('dbname={} port={}')" |
| 87 | + ).format("postgres", node.port) |
| 88 | + self.safe_psql("postgres", add_node_cmd) |
| 89 | + |
| 90 | + return self |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + with Shardlord("DarthVader") as lord: |
| 95 | + lord.init().start().install() |
| 96 | + |
| 97 | + lord.add_node("Luke") |
| 98 | + lord.add_node("ObiVan") |
| 99 | + lord.add_node("C3PO") |
| 100 | + |
| 101 | + print("%s:" % lord.name) |
| 102 | + print("\t-> port %i" % lord.port) |
| 103 | + print("\t-> dir %s" % lord.base_dir) |
| 104 | + |
| 105 | + for node in lord.nodes: |
| 106 | + print() |
| 107 | + |
| 108 | + print("\t=> %s:" % node.name) |
| 109 | + print("\t\t-> port %i" % node.port) |
| 110 | + print("\t\t-> dir %s" % node.base_dir) |
| 111 | + |
| 112 | + # loop until SIGINT |
| 113 | + while True: |
| 114 | + pass |
0 commit comments