// Graph Engine // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE.md file in the project root for full license information. // using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics; using Trinity; using Trinity.Storage; using Trinity.Network.Messaging; using System.IO; using Trinity.TSL.Lib; namespace KCore { class GraphServerImpl : GraphServerBase { long kCoreNum; // every local has one local thread public override void StartServerHandler(StartMessageReader request) { kCoreNum = request.kCoreNum; foreach(var nodeAccessor in Global.LocalStorage.KCoreCell_Accessor_Selector()) { if (nodeAccessor.remain_nodes < kCoreNum) { MessageSorter sorter = new MessageSorter(nodeAccessor.neighbors); nodeAccessor.alive = false; for (int i = 0; i < Global.ServerCount; i += 1) { RemoveEdgeMessageWriter msg = new RemoveEdgeMessageWriter(nodeAccessor.CellId, sorter.GetCellRecipientList(i)); Global.CloudStorage.GatherRemovedEdgesToGraphServer(i, msg); } } } } // remove edge // check degree // send message // 点数X机器数 public override void GatherRemovedEdgesHandler(RemoveEdgeMessageReader remove_edge) { // remove_edge.to_remove. remove_edge.to_remove.ForEach((removed_id) => { using(var nodeAccessor = Global.LocalStorage.UseKCoreCell(removed_id, CellAccessOptions.ReturnNullOnCellNotFound)) { if (nodeAccessor != null) { if (nodeAccessor.alive) { nodeAccessor.remain_nodes -= 1; if (nodeAccessor.remain_nodes < kCoreNum) { MessageSorter sorter = new MessageSorter(nodeAccessor.neighbors); nodeAccessor.alive = false; for (int i = 0; i < Global.ServerCount; i += 1) { RemoveEdgeMessageWriter msg = new RemoveEdgeMessageWriter(nodeAccessor.CellId, sorter.GetCellRecipientList(i)); Global.CloudStorage.GatherRemovedEdgesToGraphServer(i, msg); } } } } } }); } } class Program { public static void LoadGraphFromCSV(string fullFileName, Int16 getRows = 0) { Stopwatch st = new Stopwatch(); FileStream fs = new FileStream(fullFileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.Default); int iRow = 1;//第几行 try { Console.WriteLine("LoadGraphFromCsv start! edgeFile:{0}", fullFileName); string strLine = "";//记录每次读取的一行记录 string[] aryLine;//记录每行记录中的各字段内容 string[] separators = { " " };// { ",", ".", "!", "?", ";", ":", " " }; st.Start();//开始计时 Global.CloudStorage.ResetStorage(); while ((strLine = sr.ReadLine()) != null)//逐行读取CSV中的数据 { strLine = strLine.Trim(); aryLine = strLine.Split(separators, StringSplitOptions.RemoveEmptyEntries); long id_1 = long.Parse(aryLine[0]); long id_2 = long.Parse(aryLine[1]); if (Global.CloudStorage.Contains(id_1)) { var node_1 = Global.CloudStorage.LoadKCoreCell(id_1); node_1.neighbors.Add(id_2); Global.CloudStorage.SaveKCoreCell(node_1); } else { List neighbors = new List(); neighbors.Add(id_2); Global.CloudStorage.SaveKCoreCell(id_1, true, neighbors.Count, neighbors); } if (Global.CloudStorage.Contains(id_2)) { var node_2 = Global.CloudStorage.LoadKCoreCell(id_2); node_2.neighbors.Add(id_1); Global.CloudStorage.SaveKCoreCell(node_2); } else { List neighbors = new List(); neighbors.Add(id_1); Global.CloudStorage.SaveKCoreCell(id_2, true, neighbors.Count, neighbors); } if (iRow % 10000 == 1) { Console.WriteLine("insert ok! msg is:{0} rowNumber:{1}", strLine, iRow); } iRow++; if (getRows > 0) { if (iRow > getRows) { break; } } } Console.WriteLine("LoadGraphFromCsv finish!"); } catch (Exception ex) { Console.WriteLine("LoadGraphFromCsv failed! message:{0} trace:{1} source:{2}", ex.Message, ex.StackTrace, ex.Source); } finally { sr.Close(); fs.Close(); } st.Stop();//终止计时 Console.WriteLine("insert cost:{0}ms rowNum:{1}", st.ElapsedMilliseconds.ToString(), iRow); } static void Main(string[] args) { Console.WriteLine("args len:{0}, args:{1}", args.Length, args[0]); if (args.Length >= 1 && args[0].StartsWith("-c")) { string configFile = args[1]; TrinityConfig.LoadConfig(configFile); } if (args.Length >= 3 && args[2].StartsWith("-s")) { GraphServerImpl server = new GraphServerImpl(); server.Start(); Console.WriteLine("start server ok!"); while (true) { Thread.Sleep(100); } } //-q cellID if (args.Length >= 3 && args[2].StartsWith("-q")) { TrinityConfig.CurrentRunningMode = RunningMode.Client; var cell = Global.CloudStorage.LoadKCoreCell(long.Parse(args[3])); Console.WriteLine("Current vertex is {0}, neighbors is {1}.", cell.CellId, cell.neighbors.Count()); } //-L edgeFilePath if (args.Length >= 3 && args[2].StartsWith("-l")) { TrinityConfig.CurrentRunningMode = RunningMode.Client; string edgeFilePath = args[3]; LoadGraphFromCSV(edgeFilePath); } // start k-core if (args.Length >= 3 && args[2].StartsWith("-kcore")) { TrinityConfig.CurrentRunningMode = RunningMode.Client; for (int i = 0; i < Global.ServerCount; i++) { using (var msg = new StartMessageWriter(int.Parse(args[3].Trim()))) { Global.CloudStorage.StartServerToGraphServer(i, msg); } } } } } }