vCloudServerSelector.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using JsonRpcLite.Network;
  2. using JsonRpcLite.Rpc;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Vinno.IUS.Common.Log;
  8. using Vinno.IUS.Common.Network.Leaf;
  9. using Vinno.IUS.Common.Network.Tcp;
  10. using Vinno.vCloud.Common.FIS.Helper;
  11. using WingInterfaceLibrary.Interface;
  12. namespace Vinno.vCloud.Common.FIS
  13. {
  14. public enum ServerTestResult
  15. {
  16. NotSure,
  17. OldServer,
  18. NewServer
  19. }
  20. public class vCloudServerSelector : IDisposable
  21. {
  22. private string _prefix;
  23. private string _host;
  24. private int _port;
  25. private ClientLeaf _leaf;
  26. private JsonRpcClient _client;
  27. private ServerTestResult _testResult;
  28. private ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
  29. private CancellationTokenSource _cts;
  30. public ServerTestResult Select(string prefix, string host, int port)
  31. {
  32. _prefix = prefix;
  33. _host = host;
  34. _port = port;
  35. _testResult = ServerTestResult.NotSure;
  36. _cts = new CancellationTokenSource();
  37. _manualResetEvent.Reset();
  38. try
  39. {
  40. Task.Run(CheckLeaf);
  41. Task.Run(CheckJsonRpc);
  42. if (_manualResetEvent.WaitOne(5000))
  43. {
  44. Logger.WriteLineInfo($"vCloudServerSelector TestResult is {_testResult}");
  45. }
  46. else
  47. {
  48. Logger.WriteLineError($"vCloudServerSelector Test Timeout.The TestResult is {_testResult}");
  49. }
  50. _cts.Cancel();
  51. return _testResult;
  52. }
  53. catch (Exception ex)
  54. {
  55. Logger.WriteLineError($"vCloudServerSelector Test error:{ex}");
  56. _cts.Cancel();
  57. return _testResult;
  58. }
  59. }
  60. private void CheckLeaf()
  61. {
  62. var stopwatch = new Stopwatch();
  63. stopwatch.Start();
  64. var result = LeafTest();
  65. stopwatch.Stop();
  66. Logger.WriteLineInfo($"LeafTest 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  67. if (!result && !_cts.IsCancellationRequested)
  68. {
  69. stopwatch.Restart();
  70. result = LeafTest();
  71. stopwatch.Stop();
  72. Logger.WriteLineInfo($"LeafTest 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  73. }
  74. }
  75. private bool LeafTest()
  76. {
  77. try
  78. {
  79. if (_leaf != null)
  80. {
  81. _leaf.Close();
  82. _leaf = null;
  83. }
  84. var url = $"{_host}:{_port}";
  85. _leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(url));
  86. if (_leaf.Online)
  87. {
  88. _testResult = ServerTestResult.OldServer;
  89. _manualResetEvent.Set();
  90. return true;
  91. }
  92. return false;
  93. }
  94. catch
  95. {
  96. return false;
  97. }
  98. }
  99. private void CheckJsonRpc()
  100. {
  101. var stopwatch = new Stopwatch();
  102. stopwatch.Start();
  103. var result = JsonRpcTest();
  104. stopwatch.Stop();
  105. Logger.WriteLineInfo($"JsonRpcTest 1st Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  106. if (!result && !_cts.IsCancellationRequested)
  107. {
  108. stopwatch.Restart();
  109. result = JsonRpcTest();
  110. stopwatch.Stop();
  111. Logger.WriteLineInfo($"JsonRpcTest 2nd Time Cost {stopwatch.ElapsedMilliseconds}ms,result is {result}");
  112. }
  113. }
  114. private bool JsonRpcTest()
  115. {
  116. try
  117. {
  118. if (_client != null)
  119. {
  120. _client.Dispose();
  121. _client = null;
  122. }
  123. var url = $"{_prefix}{_host}:{_port}";
  124. _client = new JsonRpcClient();
  125. var clientEngine = new JsonRpcHttpClientEngine(url);
  126. _client.UseEngine(clientEngine);
  127. var vinnoServerService = _client.CreateProxy<IVinnoServerService>(5000);
  128. var echoResult = JsonRpcHelper.Echo(vinnoServerService);
  129. if (echoResult != null && echoResult.Code == 0)
  130. {
  131. _testResult = ServerTestResult.NewServer;
  132. _manualResetEvent.Set();
  133. return true;
  134. }
  135. return false;
  136. }
  137. catch
  138. {
  139. return false;
  140. }
  141. }
  142. public void Dispose()
  143. {
  144. try
  145. {
  146. _manualResetEvent.Set();
  147. _cts?.Cancel();
  148. if (_leaf != null)
  149. {
  150. _leaf.Close();
  151. _leaf = null;
  152. }
  153. if (_client != null)
  154. {
  155. _client.Dispose();
  156. _client = null;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Logger.WriteLineError($"vCloudServerSelector Dispose Error:{ex}");
  162. }
  163. }
  164. }
  165. }