Ejemplo n.º 1
0
def add_shards_to_cluster(mysql_conn_params, cluster_name, config_path,
                          install_names):
    meta_conn = mysql.connector.connect(**mysql_conn_params)

    jsconf = open(config_path)
    jstr = jsconf.read()
    jscfg0 = json.loads(jstr)
    meta_cursor = meta_conn.cursor(prepared=True)
    get_cluster_id_stmt = "select id from db_clusters where name=%s"
    meta_cursor.execute(get_cluster_id_stmt, (cluster_name, ))
    row = meta_cursor.fetchone()
    cluster_id = row[0]

    num_nodes = 0

    add_shard_stmt = "insert into shards(name, when_created, num_nodes, db_cluster_id) values(%s, now(), 0, %s)"
    add_shard_node_stmt = "insert into shard_nodes(ip, port, user_name, passwd, shard_id, db_cluster_id, svr_node_id, master_priority) values(%s, %s, %s, %s, %s, %s, 0,0)"

    jscfg = []
    nshards = 0

    # fetch the list of target shards to jscfg. may be all shards or specified ones in the config file.
    if install_names[0] == '':
        jscfg = jscfg0
        nshards = len(jscfg)
    else:
        shard_names2add = ''
        sepstr = ''
        for cfg in jscfg0:
            if install_names.count(cfg['shard_name']) > 0:
                if nshards == 1:
                    sepstr = ', '
                shard_names2add = shard_names2add + sepstr + cfg['shard_name']
                jscfg.append(cfg)
                nshards = nshards + 1

        print "Shards to add: {}".format(shard_names2add)

    masters = []

    # check storage shard topology and version first.
    for shardcfg in jscfg:
        # set to False to disable checking master of storage shards if MGR isn't used
        shard_master = common.mysql_shard_check(shardcfg['shard_nodes'], True)
        masters.append(shard_master)

    # add shards info to metadata-cluster tables.
    meta_cursor0 = meta_conn.cursor(buffered=True, dictionary=True)
    meta_cursor0.execute("start transaction")
    for shardcfg in jscfg:
        meta_cursor.execute(add_shard_stmt,
                            (shardcfg['shard_name'], cluster_id))
        meta_cursor0.execute("select last_insert_id() as id")
        row = meta_cursor0.fetchone()
        shard_id = row['id']
        shardcfg['shard_id'] = shard_id
        num_nodes = 0
        for val in shardcfg['shard_nodes']:
            meta_cursor.execute(add_shard_node_stmt,
                                (val['ip'], val['port'], val['user'],
                                 val['password'], shard_id, cluster_id))
            meta_cursor0.execute("select last_insert_id() as id")
            row0 = meta_cursor0.fetchone()
            val['shard_node_id'] = row0['id']
            num_nodes = num_nodes + 1

        shardcfg['num_nodes'] = num_nodes
        meta_cursor.execute("update shards set num_nodes=? where id=?",
                            (num_nodes, shard_id))

    meta_cursor0.execute("commit")
    meta_cursor.close()
    meta_cursor0.close()

    # create the default database in each new shard.
    for master in masters:
        master_conn = mysql.connector.connect(**master)
        master_cursor = master_conn.cursor()
        master_cursor.execute(
            "create database postgres_$$_public CHARACTER set=utf8")
        master_cursor.close()
        master_conn.close()

    add_new_shards_to_all_computing_nodes(cluster_id, meta_conn, jscfg)
    meta_conn.close()
    jsconf.close()
    return nshards
Ejemplo n.º 2
0
        type=str,
        help=
        "target shards to install, specified by shard names. If none, add all shards."
    )

    args = parser.parse_args()

    install_names = []

    if args.targets:
        idstr = args.targets.split(',')
        for id in idstr:
            id = id.strip()
            if id == '':
                raise Exception("Must specifiy valid shard names.")
            install_names.append(id)
    else:
        install_names = ['']  # install all shards

    meta_jsconf = open(args.meta_config)
    meta_jstr = meta_jsconf.read()
    meta_jscfg = json.loads(meta_jstr)
    mysql_conn_params = {}

    mysql_conn_params = common.mysql_shard_check(meta_jscfg, True)
    mysql_conn_params['database'] = 'Kunlun_Metadata_DB'
    num_done = add_shards_to_cluster(mysql_conn_params, args.cluster_name,
                                     args.config, install_names)
    print "Shard nodes successfully added to cluster {} : {}".format(
        args.cluster_name, num_done)