示例#1
0
class Londiste3Cluster:
    def __init__(self, root_db_name, branch_db_name, leaf_db_name=None, basedir = 'st3test', howto=None):
        self.root_db_name = root_db_name
        self.branch_db_name = branch_db_name
        self.leaf_db_name = leaf_db_name
        self.basedir = basedir
        self.pgqd = PgQD(basedir = self.basedir)
        self.db1 = PGDatabase(self.root_db_name)
        self.node1 = Londiste3Node(self.db1, 'root', 'node1', basedir = self.basedir)
        self.pgb = PGBench(2,self.db1)
        self.db2 = PGDatabase(self.branch_db_name)
        self.node2 = Londiste3Node(self.db2, 'branch', 'node2', provider_db=self.db1, basedir = self.basedir)
        if self.leaf_db_name:
            self.init_leaf_node()
        self._howto = howto
    def howto(self, text):
        if not self._howto: return
        print text
    def init_leaf_node(self):
        self.db3 = PGDatabase(self.leaf_db_name )
        self.node3 = Londiste3Node(self.db3, 'leaf', 'node3', provider_db=self.db1, basedir = self.basedir)
    def start(self):
        self.howto('== Set up schema for root database')
        self.setup_root_schema()
        self.setup_replicated_schema(self.db2)
        if self.leaf_db_name:
            self.setup_replicated_schema(self.db3)
        self.setup_node(self.node1)
        self.setup_node(self.node2)
        if self.leaf_db_name:
            self.setup_node(self.node3)
        self.pgqd.create_ini_file()
        self.pgqd.start()
    def setup_root_schema(self):
        # create database
        self.howto('=== Create database ===')
        self.db1.createdb()
        # create schemas, populate with data and modify for replication
        self.howto('=== set up schema for pgbench ===')
        self.pgb.pgb_init_db()
        self.howto('=== and add primary and foreign keys needed for replication ===')
        self.pgb.modify_db_for_replication()
    def setup_replicated_schema(self, db):
        # create database
        db.createdb()
        # copy schema from root database
        db.copy_schema_from(self.db1)
    def setup_node(self, node):
        # install londiste3 support and start replay process
        node.create_ini_file()
        node.create_node()
        node.start()
    def check_processes(self):
        time.sleep(1) # let things settle
        print 'pgqd.check()', self.pgqd.check()
        print 'l3_node1.check()', self.node1.check()
        print 'l3_node2.check()', self.node2.check()
        # print replication tree
        self.node1.status()
    def add_tables(self, tablelist=['--all'], wait=True):
        self.node1.add_tables(tablelist)
        self.node2.add_tables(tablelist)
        if wait:
            print 'waiting for subscription to finish'
            time.sleep(3) # let things settle
            self.node2.wait_replication_state_ok()
    def start_pgbench_in_background(self, seconds=120):
        # starts a long pgbench run in background to run for 2 minutes
        with open('/tmp/throttled.pgbench', 'w') as f:
            f.write(throttled_pgbench_script)
        self.pgb.pgb_run(seconds=seconds, concurrency=5, filename='/tmp/throttled.pgbench', background=True)
    def check_data(self):
        self.node2.compare_tables()
        if self.leaf_db_name:
            self.node3.compare_tables()
    def tearDown(self):
        self.pgqd.stop()
        self.node1.stop()
        self.node2.stop()
        if self.leaf_db_name:
            self.node3.stop()
        self.db1.dropdb()
        self.db2.dropdb()
        if self.leaf_db_name:
            self.db3.dropdb()