def createSegmentRows()

in automation/tinc/main/ext/qautils/gppylib/gparray.py [0:0]


def createSegmentRows( hostlist
                     , interface_list
                     , primary_list
                     , primary_portbase
                     , mirror_type
                     , mirror_list
                     , mirror_portbase
                     , dir_prefix
                     , primary_replication_portbase
                     , mirror_replication_portbase
                     , primary_fs_list = None
                     , mirror_fs_list = None
                     ):
    """
    This method will return a list of SegmentRow objects that represent new segments on each host.
    The "hostlist" parameter contains both existing hosts as well as any new hosts that are
    a result of expansion.
    """

    rows    =[]    
    dbid    = 0
    content = 0

    for host in hostlist:
        isprimary='t'
        port=primary_portbase
        prPort = primary_replication_portbase
        index = 0
        for pdir in primary_list:
            fulldir = "%s/%s%d" % (pdir,dir_prefix,content)
            if len(interface_list) > 0:
                interfaceNumber = interface_list[index % len(interface_list)]
                address = host + '-' + str(interfaceNumber)
            else:
                address = host
            fsDict = {}
            if primary_fs_list != None and len(primary_fs_list) > index:
                fsDict = primary_fs_list[index]
            fullFsDict = {}
            for oid in fsDict:
                fullFsDict[oid] = "%s/%s%d" % (fsDict[oid], dir_prefix, content)
            rows.append( SegmentRow( content = content
                                   , isprimary = isprimary
                                   , dbid = dbid
                                   , host = host
                                   , address = address
                                   , port = port
                                   , fulldir = fulldir
                                   , prPort = prPort
                                   , fileSpaceDictionary = fullFsDict
                                   ) )
            port += 1
            if prPort != None:
                prPort += 1
            content += 1
            dbid += 1
            index = index + 1
    
    #mirrors
    if mirror_type is None or mirror_type == 'none':
        return rows
    elif mirror_type.lower().strip() == 'spread':
        #TODO: must be sure to put mirrors on a different subnet than primary.
        #      this is a general problem for GPDB these days. perhaps we should
        #      add something to gpdetective to be able to detect this and fix it.
        #      best to have the interface mapping stuff 1st.
        content=0
        isprimary='f'
        num_hosts = len(hostlist)
        num_dirs=len(primary_list)
        if num_hosts <= num_dirs:
            raise Exception("Not enough hosts for spread mirroring.  You must have more hosts than primary segments per host")
        
        mirror_port = {}
        mirror_replication_port = {}

        mirror_host_offset=1
        last_mirror_offset=1
        for host in hostlist:
            mirror_host_offset = last_mirror_offset + 1
            last_mirror_offset += 1
            index = 0
            for mdir in mirror_list:
                fulldir = "%s/%s%d" % (mdir,dir_prefix,content)
                fsDict = {}
                if mirror_fs_list != None and len(mirror_fs_list) > index:
                    fsDict = mirror_fs_list[index]
                fullFsDict = {}
                for oid in fsDict:
                    fullFsDict[oid] = "%s/%s%d" % (fsDict[oid], dir_prefix, content)
                mirror_host = hostlist[mirror_host_offset % num_hosts]
                if mirror_host == host:
                    mirror_host_offset += 1
                    mirror_host = hostlist[mirror_host_offset % num_hosts]
                if len(interface_list) > 0:
                    interfaceNumber = interface_list[mirror_host_offset % len(interface_list)]
                    address = mirror_host + '-' + str(interfaceNumber)
                else:
                    address = mirror_host
                    
                if not mirror_port.has_key(mirror_host):
                    mirror_port[mirror_host] = mirror_portbase
                if not mirror_replication_port.has_key(mirror_host):
                    mirror_replication_port[mirror_host] = mirror_replication_portbase
                    
                rows.append( SegmentRow( content = content
                                       , isprimary = isprimary
                                       , dbid = dbid
                                       , host = mirror_host
                                       , address = address
                                       , port = mirror_port[mirror_host]
                                       , fulldir = fulldir
                                       , prPort = mirror_replication_port[mirror_host]
                                       , fileSpaceDictionary = fullFsDict
                                       ) )

                mirror_port[mirror_host] += 1
                mirror_replication_port[mirror_host] += 1
                content += 1
                dbid += 1
                mirror_host_offset += 1
                index = index + 1
                
        
    elif mirror_type.lower().strip() == 'grouped':
        content = 0
        num_hosts = len(hostlist)
        
        if num_hosts < 2:
            raise Exception("Not enough hosts for grouped mirroring.  You must have at least 2")
        
        #we'll pick our mirror host to be 1 host "ahead" of the primary.
        mirror_host_offset = 1
        
        isprimary='f'
        for host in hostlist:            
            mirror_host = hostlist[mirror_host_offset % num_hosts]
            mirror_host_offset += 1
            port = mirror_portbase
            mrPort = mirror_replication_portbase
            index = 0
            for mdir in mirror_list:
                fulldir = "%s/%s%d" % (mdir,dir_prefix,content)
                if len(interface_list) > 0:
                    interfaceNumber = interface_list[(index + 1) % len(interface_list)]
                    address = mirror_host + '-' + str(interfaceNumber)
                else:
                    address = mirror_host
                fsDict = {}
                if mirror_fs_list != None and len(mirror_fs_list) > index:
                    fsDict = mirror_fs_list[index]
                fullFsDict = {}
                for oid in fsDict:
                    fullFsDict[oid] = "%s/%s%d" % (fsDict[oid], dir_prefix, content)
                rows.append( SegmentRow( content = content
                                       , isprimary = isprimary
                                       , dbid = dbid
                                       , host = mirror_host
                                       , address = address
                                       , port = port
                                       , fulldir = fulldir
                                       , prPort = mrPort
                                       , fileSpaceDictionary = fullFsDict
                                       ) )
                port += 1
                mrPort +=1
                content += 1
                dbid += 1
                index = index + 1
        
    else:
        raise Exception("Invalid mirror type specified: %s" % mirror_type)
    
    return rows