def GFSMasterLockHolder(ver, testver): """ find the primary gfs_mater using chubby Arguments: ver: '4.6.5' testver: 0 - not a test version. 1 - test version. Returns: 'ent1' if ent1 is the primary gfs_master None if could not find out. """ nodes = core_utils.GetNodes() if len(nodes) == 1: return None lockfile = '/ls/%s/gfs/ent/master-lock' % core_utils.GetCellName(ver) basecmd = core_utils.GetLSClientCmd(ver, testver) fi = os.popen('%s cat %s' % (basecmd, lockfile), 'r') data = fi.read() ret = fi.close() if ret: return None if data.startswith('ent'): return data.split(':', 2)[0] return None
def SetInitState(cfg, state): """Sets system's initialization state. For oneway, it stores it in C.ENT_SYSTEM_INIT_STATE. For Clusters, it stores it in chubby file /ls/ent<version>/ENT_SYSTEM_INIT_STATE. @param cfg - of type configurator. @param state - string """ # oneway? if 1 == len(core_utils.GetNodes()): cfg.setGlobalParam(C.ENT_SYSTEM_INIT_STATE, state) return tmpfile = E.mktemp('/export/hda3/tmp') try: f = open(tmpfile, 'w') f.write(state) f.close() except IOError: logging.fatal('Cannot write to temp file %s' % tmpfile) return version = cfg.getGlobalParam('VERSION') lockserv_cmd_prefix = core_utils.GetLSClientCmd(version, is_test(version)) chubby_root_dir = '/ls/%s' % core_utils.GetCellName(version) write_cmd = '%s cp %s %s/%s' % (lockserv_cmd_prefix, tmpfile, chubby_root_dir, 'ENT_SYSTEM_INIT_STATE') logging.info('setting system init state to: %s', state) E.exe_or_fail(write_cmd) E.exe('rm -rf %s' % tmpfile)
def _StatusFileCmd(cmd, version, out=[], extra_arg='', unittestdir=None): """Perform a command on the RESET_STATE status file. On a cluster, runs lockserv <cmd> /ls/ent4-x-x/RESET_STATE. On a oneway, runs cmd on /export/hda3/4.x.x/RESET_STATE cmd should be cat, setcontents, or rm. Return: None for oneway, 0 for success, 1 for error Command output returned in out. """ if unittestdir != None or 1 == len(core_utils.GetNodes()): # unitest or Oneway if unittestdir != None: file = '/%s/%s/RESET_STATE' % (unittestdir, version) else: file = '/export/hda3/%s/RESET_STATE' % version if cmd == 'cat': status = _ExecuteCommand('cat %s' % file, out=out) elif cmd == 'setcontents': status = _ExecuteCommand('echo "%s" > %s' % (extra_arg, file)) elif cmd == 'rm': status = _ExecuteCommand('rm -f %s' % file) else: logging.error('StatusFileCmd: bad command %s' % cmd) return 1 return status lockserv_cmd_prefix = core_utils.GetLSClientCmd( version, install_utilities.is_test(version)) chubby_file = '/ls/%s/RESET_STATE' % core_utils.GetCellName(version) lockserv_cmd = '%s %s %s %s' % (lockserv_cmd_prefix, cmd, chubby_file, extra_arg) logging.info('Reset index: executing %s' % lockserv_cmd) status = _ExecuteCommand(lockserv_cmd) return status
def SetSyncTime(ver, testver): """ record the gfs logs sync time in chubby Arguments: ver: '4.6.5' testver: 1 - this version in test mode. 0 - otherwise. """ lockserv_cmd_prefix = core_utils.GetLSClientCmd(ver, testver) chubby_file = '/ls/%s/GFS_LOGS_SYNC_TIME' % core_utils.GetCellName(ver) lockserv_cmd = '%s setcontents %s %s' % (lockserv_cmd_prefix, chubby_file, int(time.time())) os.system(lockserv_cmd)
def GetSyncTime(ver, testver): """ read the gfs logs sync time from chubby Arguments: ver: '4.6.5' testver: 1 - this version in test mode. 0 - otherwise. Returns: 0: if no gfs logs sync has been done before. Otherwise, it is the time of the last sync. """ lockserv_cmd_prefix = core_utils.GetLSClientCmd(ver, testver) chubby_file = '/ls/%s/GFS_LOGS_SYNC_TIME' % core_utils.GetCellName(ver) lockserv_cmd = '%s cat %s' % (lockserv_cmd_prefix, chubby_file) status, out = commands.getstatusoutput(lockserv_cmd) if status == 0: return int(out) else: return 0
def GetInitState(entcfg): """Returns System's initialization state. For oneway, it is the value of C.ENT_SYSTEM_INIT_STATE and for clusters, it is the value stored in chubby file /ls/ent<version>/ENT_SYTEM_INIT_STATE. If chubby file is non existent, it returns state C.FRESH. @param entcfg - of type googleconfig. @return - state """ # oneway? if 1 == len(core_utils.GetNodes()): return entcfg.var(C.ENT_SYSTEM_INIT_STATE) # For cluster, get the state from chubby. version = entcfg.var('VERSION') lockserv_cmd_prefix = core_utils.GetLSClientCmd(version, is_test(version)) chubby_root_dir = '/ls/%s' % core_utils.GetCellName(version) # Verify that chubby is functional. We do not want to accidentally return # FRESH state that can result in total wipe out of data. ls_cmd = '%s ls %s' % (lockserv_cmd_prefix, chubby_root_dir) (status, output) = E.getstatusoutput(ls_cmd) if E.ERR_OK != status: logging.fatal('GetInitState: Could not talk to chubby.') return None cat_cmd = '%s cat %s/%s' % (lockserv_cmd_prefix, chubby_root_dir, 'ENT_SYSTEM_INIT_STATE') (status, state) = E.getstatusoutput(cat_cmd) if E.ERR_OK != status: # For fresh install, file init_state won't exist in chubby yet. # Hence, consider this as a FRESH state. state = C.FRESH logging.info('current system init state: %s', state) return state
def FindCanonical(ver, testver=0, proto_dir=None, superblock_dir=None): """ find out a node to be the canonical First by reading superblock of GFS. If it does not work, return this node Arguments: ver: '4.6.5' testver: 1 if the version is in test mode. 0 otherwise. proto_dir: for unittest only. dir of the gfs_superblock.proto file. superblock_dir: for unittest only. dir of the master-superblock dir. Returns: GFS Replica which is upto date. """ if not superblock_dir: superblock_dir = '/ls/%s/gfs/ent' % ( core_utils.GetGFSChubbyCellName(ver)) superblock_filename = '%s/master-superblock' % superblock_dir temp_file = tempfile.mktemp() if superblock_filename.startswith('/ls'): base_cmd = core_utils.GetLSClientCmd(ver, testver) else: base_cmd = '' copy_command = '%s cp %s %s' % (base_cmd, superblock_filename, temp_file) core_utils.ExecCmd(copy_command) f = open(temp_file, 'r') data = f.read() gfs_superblock = GFS_SuperBlock() gfs_superblock.ParseFromString(data) os.remove(temp_file) num_replicas = gfs_superblock.replicas_size() logging.info('Num Replicas: %s' % str(num_replicas)) cellname = gfs_superblock.cellname() logging.info('Cell Name: %s' % cellname) last_master = gfs_superblock.last_master() if last_master.startswith('ent'): last_master = last_master.split(':')[0] logging.info('Last Master: %s' % last_master) replica_info = {} for replica in gfs_superblock.replicas_list(): logging.info('Rep Info: %s %s' % (replica.machine_name(), replica.up_to_date())) replica_info[replica.machine_name()] = replica.up_to_date() if last_master.startswith('ent') and replica_info[last_master]: return last_master for machine_name in replica_info: if replica_info[machine_name]: return machine_name # just this node in all other cases return core_utils.GetNode()