ClientTestManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Flyinsono.DBCopy.Tool.RpcService;
  2. using JsonRpcLite.Network;
  3. using JsonRpcLite.Rpc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Net;
  8. using System.Text.Json;
  9. using WingInterfaceLibrary.Enum;
  10. using WingInterfaceLibrary.Interface;
  11. using WingInterfaceLibrary.Request.User;
  12. using MongoDB.Bson;
  13. using MongoDB.Driver;
  14. using Flyinsono.DBCopy.Tool.Utilities;
  15. using System.Reflection.Metadata;
  16. using MongoDB.Bson.IO;
  17. using Flyinsono.DBCopy.Tool.Entities;
  18. using System.Threading.Tasks;
  19. using Flyinsono.DBCopy.Tool.Service;
  20. namespace Flyinsono.DBCopy.Tool
  21. {
  22. internal interface IClientTestManager : IClientManager
  23. {
  24. /// <summary>
  25. /// Run the test cases
  26. /// </summary>
  27. void ConnectDb(string dbPath);
  28. }
  29. internal class ClientTestManager : IClientTestManager
  30. {
  31. public void Dispose()
  32. {
  33. //throw new System.NotImplementedException();
  34. }
  35. public void ConnectDb(string dbPath)
  36. {
  37. Console.WriteLine($"Database path: {dbPath}");
  38. //string mongodPath = @"DBCopy.Packages\4.2earlier\mongod_4.2.exe";
  39. string mongodPath = @"DBCopy.Packages\3.6.20\mongod_3.6.20.exe";
  40. int port = 8333;
  41. StartLocalMongoDbServer(mongodPath, dbPath, port);
  42. CommonConfigManager.IsLocalHost = true;
  43. MongoDbClientSingle.Instance = new MongoDbClient();
  44. }
  45. private void StartLocalMongoDbServer(string mongodPath, string dbPath, int port, float cacheSize = 0.5f)
  46. {
  47. try
  48. {
  49. bool res = false;
  50. Process[] processes = Process.GetProcessesByName("mongod_4.2");
  51. if (processes.Length != 0)
  52. {
  53. res = true;
  54. return;
  55. }
  56. Logger.WriteLineInfo($"Mongodb server starting");
  57. var wiredTigerCacheSizeGB = CommonConfigManager.FilterGeneral.WiredTigerCacheSizeGB;
  58. //journal: 用于故障恢复和持久化,打开journal会使用更多的内存
  59. //bind_ip:控制请求源IP以及请求的Server端指定IP,127.0.0.1 != 本机IP
  60. //noIndexBuildRetry:在一个索引创建失败后启动mongod,可以使用noIndexBuildRetry跳过索引创建来启动
  61. //master:(存放在DB/local数据库下的oplog.$main集合中)
  62. var arguments = $"--dbpath \"{dbPath}\" --port {port} --maxConns 2000 --noIndexBuildRetry --nojournal";
  63. if (wiredTigerCacheSizeGB > 0)
  64. {
  65. arguments += " --wiredTigerCacheSizeGB " + wiredTigerCacheSizeGB;
  66. }
  67. var startInfo = new ProcessStartInfo
  68. {
  69. FileName = mongodPath,
  70. Arguments = arguments,
  71. WindowStyle = ProcessWindowStyle.Hidden,
  72. RedirectStandardInput = true,
  73. RedirectStandardOutput = true,
  74. UseShellExecute = false,
  75. CreateNoWindow = true
  76. };
  77. using (var process = Process.Start(startInfo))
  78. {
  79. int readCount = 100;
  80. string info = string.Empty;
  81. while (readCount > 0)
  82. {
  83. info = process.StandardOutput.ReadLine();
  84. if (!string.IsNullOrWhiteSpace(info) && info.IndexOf($"waiting for connections on port {port}") > -1)
  85. {
  86. Logger.WriteLineInfo($"Mongodb server started.{readCount}");
  87. //Console.WriteLine($"Mongodb server started.{readCount}");
  88. break;
  89. }
  90. //Thread.Sleep(1000);
  91. readCount--;
  92. }
  93. Logger.WriteLineInfo("Mongodb server started.");
  94. }
  95. }
  96. catch (Exception exception)
  97. {
  98. Logger.WriteLineError($"Start mongod server process filed,error:{exception}");
  99. throw;
  100. }
  101. }
  102. }
  103. }