123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using Flyinsono.DBCopy.Tool.RpcService;
- using JsonRpcLite.Network;
- using JsonRpcLite.Rpc;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Net;
- using System.Text.Json;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request.User;
- using MongoDB.Bson;
- using MongoDB.Driver;
- using Flyinsono.DBCopy.Tool.Utilities;
- using System.Reflection.Metadata;
- using MongoDB.Bson.IO;
- using Flyinsono.DBCopy.Tool.Entities;
- using System.Threading.Tasks;
- using Flyinsono.DBCopy.Tool.Service;
- namespace Flyinsono.DBCopy.Tool
- {
- internal interface IClientTestManager : IClientManager
- {
- /// <summary>
- /// Run the test cases
- /// </summary>
- void ConnectDb(string dbPath);
- }
- internal class ClientTestManager : IClientTestManager
- {
- public void Dispose()
- {
- //throw new System.NotImplementedException();
- }
- public void ConnectDb(string dbPath)
- {
- Console.WriteLine($"Database path: {dbPath}");
- //string mongodPath = @"DBCopy.Packages\4.2earlier\mongod_4.2.exe";
- string mongodPath = @"DBCopy.Packages\3.6.20\mongod_3.6.20.exe";
- int port = 8333;
- StartLocalMongoDbServer(mongodPath, dbPath, port);
- CommonConfigManager.IsLocalHost = true;
- MongoDbClientSingle.Instance = new MongoDbClient();
- }
-
- private void StartLocalMongoDbServer(string mongodPath, string dbPath, int port, float cacheSize = 0.5f)
- {
- try
- {
- bool res = false;
- Process[] processes = Process.GetProcessesByName("mongod_4.2");
- if (processes.Length != 0)
- {
- res = true;
- return;
- }
- Logger.WriteLineInfo($"Mongodb server starting");
- var wiredTigerCacheSizeGB = CommonConfigManager.FilterGeneral.WiredTigerCacheSizeGB;
- //journal: 用于故障恢复和持久化,打开journal会使用更多的内存
- //bind_ip:控制请求源IP以及请求的Server端指定IP,127.0.0.1 != 本机IP
- //noIndexBuildRetry:在一个索引创建失败后启动mongod,可以使用noIndexBuildRetry跳过索引创建来启动
- //master:(存放在DB/local数据库下的oplog.$main集合中)
- var arguments = $"--dbpath \"{dbPath}\" --port {port} --maxConns 2000 --noIndexBuildRetry --nojournal";
- if (wiredTigerCacheSizeGB > 0)
- {
- arguments += " --wiredTigerCacheSizeGB " + wiredTigerCacheSizeGB;
- }
- var startInfo = new ProcessStartInfo
- {
- FileName = mongodPath,
- Arguments = arguments,
- WindowStyle = ProcessWindowStyle.Hidden,
- RedirectStandardInput = true,
- RedirectStandardOutput = true,
- UseShellExecute = false,
- CreateNoWindow = true
- };
- using (var process = Process.Start(startInfo))
- {
- int readCount = 100;
- string info = string.Empty;
- while (readCount > 0)
- {
- info = process.StandardOutput.ReadLine();
- if (!string.IsNullOrWhiteSpace(info) && info.IndexOf($"waiting for connections on port {port}") > -1)
- {
- Logger.WriteLineInfo($"Mongodb server started.{readCount}");
- //Console.WriteLine($"Mongodb server started.{readCount}");
- break;
- }
- //Thread.Sleep(1000);
- readCount--;
- }
- Logger.WriteLineInfo("Mongodb server started.");
- }
- }
- catch (Exception exception)
- {
- Logger.WriteLineError($"Start mongod server process filed,error:{exception}");
- throw;
- }
- }
- }
- }
|