build_graph.py 9.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import json
import logging
from collections import defaultdict
import tqdm
import redis
from redis._compat import b, unicode, bytes, long, basestring
from rediscluster.nodemanager import NodeManager
from rediscluster.crc import crc16
import argparse
import time
import pickle
import numpy as np
import scipy.sparse as sp

log = logging.getLogger(__name__)
root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)


def encode(value):
    """
    Return a bytestring representation of the value.
    This method is copied from Redis' connection.py:Connection.encode
    """
    if isinstance(value, bytes):
        return value
    elif isinstance(value, (int, long)):
        value = b(str(value))
    elif isinstance(value, float):
        value = b(repr(value))
    elif not isinstance(value, basestring):
        value = unicode(value)
    if isinstance(value, unicode):
        value = value.encode('utf-8')
    return value


def crc16_hash(data):
    return crc16(encode(data))


def get_redis(startup_host, startup_port):
    startup_nodes = [{"host": startup_host, "port": startup_port}, ]
    nodemanager = NodeManager(startup_nodes=startup_nodes)
    nodemanager.initialize()
    rs = {}
    for node, config in nodemanager.nodes.items():
        rs[node] = redis.Redis(
            host=config["host"], port=config["port"], decode_responses=False)
    return rs, nodemanager


def load_data(edge_path):
    src, dst = [], []
    with open(edge_path, "r") as f:
        for i in tqdm.tqdm(f):
            s, d, _ = i.split()
            s = int(s)
            d = int(d)
            src.append(s)
            dst.append(d)
            dst.append(s)
            src.append(d)
    src = np.array(src, dtype="int64")
    dst = np.array(dst, dtype="int64")
    return src, dst


def build_edge_index(edge_path, num_nodes, startup_host, startup_port,
                     num_bucket):
    #src, dst = load_data(edge_path)
    rs, nodemanager = get_redis(startup_host, startup_port)

    dst_mp, edge_mp = defaultdict(list), defaultdict(list)
    with open(edge_path) as f:
        for l in tqdm.tqdm(f):
            a, b, idx = l.rstrip().split('\t')
            a, b, idx = int(a), int(b), int(idx)
            dst_mp[a].append(b)
            edge_mp[a].append(idx)
    part_dst_dicts = {}
    for i in tqdm.tqdm(range(num_nodes)):
        #if len(edge_index.v[i]) == 0:
        #    continue
        #v = edge_index.v[i].astype("int64").reshape([-1, 1])
        #e = edge_index.eid[i].astype("int64").reshape([-1, 1])
        if i not in dst_mp:
            continue
        v = np.array(dst_mp[i]).astype('int64').reshape([-1, 1])
        e = np.array(edge_mp[i]).astype('int64').reshape([-1, 1])
        o = np.hstack([v, e])
        key = "d:%s" % i
        part = crc16_hash(key) % num_bucket
        if part not in part_dst_dicts:
            part_dst_dicts[part] = {}
        dst_dicts = part_dst_dicts[part]
        dst_dicts["d:%s" % i] = o.tobytes()
        if len(dst_dicts) > 10000:
            slot = nodemanager.keyslot("part-%s" % part)
            node = nodemanager.slots[slot][0]['name']
            while True:
                res = rs[node].hmset("part-%s" % part, dst_dicts)
                if res:
                    break
                log.info("HMSET FAILED RETRY connected %s" % node)
                time.sleep(1)
            part_dst_dicts[part] = {}

    for part, dst_dicts in part_dst_dicts.items():
        if len(dst_dicts) > 0:
            slot = nodemanager.keyslot("part-%s" % part)
            node = nodemanager.slots[slot][0]['name']
            while True:
                res = rs[node].hmset("part-%s" % part, dst_dicts)
                if res:
                    break
                log.info("HMSET FAILED RETRY connected %s" % node)
                time.sleep(1)
            part_dst_dicts[part] = {}
    log.info("dst_dict Done")


def build_edge_id(edge_path, num_nodes, startup_host, startup_port,
                  num_bucket):
    src, dst = load_data(edge_path)
    rs, nodemanager = get_redis(startup_host, startup_port)
    part_edge_dict = {}
    for i in tqdm.tqdm(range(len(src))):
        key = "e:%s" % i
        part = crc16_hash(key) % num_bucket
        if part not in part_edge_dict:
            part_edge_dict[part] = {}
        edge_dict = part_edge_dict[part]
        edge_dict["e:%s" % i] = int(src[i]) * num_nodes + int(dst[i])
        if len(edge_dict) > 10000:
            slot = nodemanager.keyslot("part-%s" % part)
            node = nodemanager.slots[slot][0]['name']
            while True:
                res = rs[node].hmset("part-%s" % part, edge_dict)
                if res:
                    break
                log.info("HMSET FAILED RETRY connected %s" % node)
                time.sleep(1)

            part_edge_dict[part] = {}

    for part, edge_dict in part_edge_dict.items():
        if len(edge_dict) > 0:
            slot = nodemanager.keyslot("part-%s" % part)
            node = nodemanager.slots[slot][0]['name']
            while True:
                res = rs[node].hmset("part-%s" % part, edge_dict)
                if res:
                    break
                log.info("HMSET FAILED RETRY connected %s" % node)
                time.sleep(1)
            part_edge_dict[part] = {}


def build_infos(edge_path, num_nodes, startup_host, startup_port, num_bucket):
    src, dst = load_data(edge_path)
    rs, nodemanager = get_redis(startup_host, startup_port)
    slot = nodemanager.keyslot("num_nodes")
    node = nodemanager.slots[slot][0]['name']
    res = rs[node].set("num_nodes", num_nodes)

    slot = nodemanager.keyslot("num_edges")
    node = nodemanager.slots[slot][0]['name']
    rs[node].set("num_edges", len(src))

    slot = nodemanager.keyslot("nf:infos")
    node = nodemanager.slots[slot][0]['name']
    rs[node].set("nf:infos", json.dumps([['feats', [-1, 602], 'float32'], ]))

    slot = nodemanager.keyslot("ef:infos")
    node = nodemanager.slots[slot][0]['name']
    rs[node].set("ef:infos", json.dumps([]))


def build_node_feat(node_feat_path, num_nodes, startup_host, startup_port, num_bucket):
    assert node_feat_path != "", "node_feat_path empty!"
    feat_dict = np.load(node_feat_path)
    for k in feat_dict.keys():
        feat = feat_dict[k]
        assert feat.shape[0] == num_nodes, "num_nodes invalid"

    rs, nodemanager = get_redis(startup_host, startup_port)
    part_feat_dict = {}
    for k in feat_dict.keys():
        feat = feat_dict[k]
        for i in tqdm.tqdm(range(num_nodes)):
            key = "nf:%s:%i" % (k, i)
            value = feat[i].tobytes()
            part = crc16_hash(key) % num_bucket
            if part not in part_feat_dict:
                part_feat_dict[part] = {}
            part_feat = part_feat_dict[part]
            part_feat[key] = value
            if len(part_feat) > 100:
                slot = nodemanager.keyslot("part-%s" % part)
                node = nodemanager.slots[slot][0]['name']
                while True:
                    res = rs[node].hmset("part-%s" % part, part_feat)
                    if res:
                        break
                    log.info("HMSET FAILED RETRY connected %s" % node)
                    time.sleep(1)

                part_feat_dict[part] = {}

    for part, part_feat in part_feat_dict.items():
        if len(part_feat) > 0:
            slot = nodemanager.keyslot("part-%s" % part)
            node = nodemanager.slots[slot][0]['name']
            while True:
                res = rs[node].hmset("part-%s" % part, part_feat)
                if res:
                    break
                log.info("HMSET FAILED RETRY connected %s" % node)
                time.sleep(1)
            part_feat_dict[part] = {}


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='gen_redis_conf')
    parser.add_argument('--startup_port', type=int, required=True)
    parser.add_argument('--startup_host', type=str, required=True)
    parser.add_argument('--edge_path', type=str, default="")
    parser.add_argument('--node_feat_path', type=str, default="")
    parser.add_argument('--num_nodes', type=int, default=0)
    parser.add_argument('--num_bucket', type=int, default=64)
    parser.add_argument(
        '--mode',
        type=str,
        required=True,
        help="choose one of the following modes (clear, edge_index, edge_id, graph_attr)"
    )
    args = parser.parse_args()
    log.info("Mode: {}".format(args.mode))
    if args.mode == 'edge_index':
        build_edge_index(args.edge_path, args.num_nodes, args.startup_host,
                         args.startup_port, args.num_bucket)
    elif args.mode == 'edge_id':
        build_edge_id(args.edge_path, args.num_nodes, args.startup_host,
                      args.startup_port, args.num_bucket)
    elif args.mode == 'graph_attr':
        build_infos(args.edge_path, args.num_nodes, args.startup_host,
                    args.startup_port, args.num_bucket)
    elif args.mode == 'node_feat':
        build_node_feat(args.node_feat_path, args.num_nodes, args.startup_host,
                    args.startup_port, args.num_bucket)
    else:
        raise ValueError("%s mode not found" % args.mode)