Ejemplo n.º 1
0
    def get_pg_stat_replication(self):
        '''Returns the pg_stat_replication result as a list'''
        if self.stdby.wait_for_walsender() == 0:
            raise WalReplException('Standby Replication has not started')

        with DbConn(utility=True, dbname='template1') as conn:
            return conn.execute("""
                SELECT
                    procpid,
                    usesysid,
                    usename,
                    application_name,
                    client_addr,
                    client_port,
                    backend_start,
                    state,
                    sent_location,
                    write_location,
                    flush_location,
                    replay_location,
                    sync_priority,
                    sync_state
                FROM
                    pg_stat_replication
             """)
Ejemplo n.º 2
0
 def get_primary_pid(self):
     pid = self.pgutil.get_pid_by_keyword(pgport=os.environ.get('PGPORT'),
                                          keyword=self.mdd)
     if int(pid) == -1:
         raise WalReplException(
             'Unable to get pid of primary master process')
     else:
         return int(pid)
Ejemplo n.º 3
0
 def get_standby_pid(self, standby_host, standby_port, standby_mdd):
     '''
     Don't use member funciton to get standby hsot, port, and mdd, as the standby may have already
     been activated and old master has been stopped.
     '''
     pid = self.pgutil.get_pid_by_keyword(host=standby_host, pgport=standby_port, keyword=standby_mdd)
     if pid ==  -1:
         raise WalReplException('Unable to get pid of standby master process')
     else:
         return pid
Ejemplo n.º 4
0
 def create_dir_on_standby(self, standby, location):
     fs_cmd = "gpssh -h %s -e 'rm -rf %s; mkdir -p %s' " % (
         standby, location, location)
     cmd = Command(
         name='Make dierctory on standby before running the command',
         cmdStr=fs_cmd)
     tinctest.logger.info('%s' % cmd)
     cmd.run(validateAfter=True)
     result = cmd.get_results()
     if result.rc != 0:
         raise WalReplException('Unable to create directory on standby')
     else:
         return True
Ejemplo n.º 5
0
class GpactivateStandby(object):
    '''Class for gpactivatestandby operations '''

    standby_port = '5656'
    db_name = 'walrepl'

    def __init__(self):
        self.gpinit = GpinitStandby()
        self.pgutil = GpUtility()
        self.runmixin = StandbyRunMixin()
        self.runmixin.createdb(dbname='walrepl')
        self.gphome = os.environ.get('GPHOME')
        self.pgport = os.environ.get('PGPORT')
        self.mdd = os.environ.get('MASTER_DATA_DIRECTORY')
        self.config = GPDBConfig()
        self.host = socket.gethostname()

        dburl = dbconn.DbURL()
        gparray = GpArray.initFromCatalog(dburl, utility=True)
        self.numcontent = gparray.getNumSegmentContents()
        self.orig_master = gparray.master

    def run_remote(self, standbyhost, rmt_cmd, pgport='', standbydd=''):
        '''Runs remote command and returns rc, result '''
        export_cmd = "source %s/greenplum_path.sh;export PGPORT=%s;export MASTER_DATA_DIRECTORY=%s" % (
            self.gphome, pgport, standbydd)
        remote_cmd = "gpssh -h %s -e '%s; %s'" % (standbyhost, export_cmd,
                                                  rmt_cmd)
        cmd = Command(name='Running Remote command', cmdStr='%s' % remote_cmd)
        tinctest.logger.info(" %s" % cmd)
        cmd.run(validateAfter=False)
        result = cmd.get_results()
        return result.rc, result.stdout

    def activate(self, option=''):
        ''' Stop the master and activate current standby to master'''
        standby_host = self.get_current_standby()
        standby_port = self.get_standby_port()
        standby_loc = self.get_standby_dd()

        self.run_remote(self.host,
                        'gpstop -aim',
                        pgport=self.pgport,
                        standbydd=self.mdd)

        gpactivate_cmd = 'gpactivatestandby -a -d %s %s' % (standby_loc,
                                                            option)
        (rc, result) = self.run_remote(standby_host,
                                       gpactivate_cmd,
                                       pgport=standby_port,
                                       standbydd=standby_loc)
        tinctest.logger.info(
            'Result without force option to activate standby %s' % result)
        if (rc != 0) and result.find('Force activation required') != -1:
            tinctest.logger.info(
                'activating standby failed, try force activation...')
            gpactivate_cmd = 'gpactivatestandby -a -f -d %s %s' % (standby_loc,
                                                                   option)
            (rc, result) = self.run_remote(standby_host,
                                           gpactivate_cmd,
                                           pgport=standby_port,
                                           standbydd=standby_loc)
            if (rc != 0):
                tinctest.logger.error('Force activating standby failed!')
                return False
        tinctest.logger.info('standby acvitated, host value %s' % standby_host)
        return True

    def remove_standby(self):
        return self.gpinit.run(option='-r')

    def failback_to_original_master(self):
        # Check if master is running.
        bashCmd = (
            self.gphome
        ) + '/bin/pg_ctl status -D $MASTER_DATA_DIRECTORY | grep \'pg_ctl: server is running\''
        cmd = Command(name='Running cmd %s' % bashCmd,
                      cmdStr="source %s/greenplum_path.sh; %s" %
                      (self.gphome, bashCmd))
        try:
            cmd.run()
        except Exception, e:
            tinctest.logger.error("Error running command %s\n" % e)
            return

        result = cmd.get_results()
        out = result.stdout
        if not out:
            tinctest.logger.info('Start the old master again ...')
            master = gp.MasterStart("Starting orig Master",
                                    self.orig_master.datadir,
                                    self.orig_master.port,
                                    self.orig_master.dbid, 0, self.numcontent,
                                    None, None, None)
            master.run(validateAfter=True)
            result = master.get_results()
            tinctest.logger.info('orig Master started result : %s' %
                                 result.stdout)
            if result.rc != 0:
                raise WalReplException(
                    'Unable to start original master process')
            Command('gpinitstandby -ra', 'gpinitstandby -ra').run()
            # failing back to old master, it takes a little bit to prepare the cluster ready for connection
            if os.path.exists(local_path('drop_filespace.sql')):
                PSQL.run_sql_file(local_path('drop_filespace.sql'),
                                  dbname=self.db_name)