DefaultBootstrap.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Runtime.Remoting;
  8. using System.Runtime.Serialization.Formatters;
  9. using System.Text;
  10. using System.Threading;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Logging.Abstractions;
  13. using SuperSocket.Common;
  14. using SuperSocket.SocketBase;
  15. using SuperSocket.SocketBase.Config;
  16. using SuperSocket.SocketBase.Metadata;
  17. using SuperSocket.SocketBase.Provider;
  18. using SuperSocket.SocketEngine.Configuration;
  19. namespace SuperSocket.SocketEngine
  20. {
  21. /// <summary>
  22. /// SuperSocket default bootstrap
  23. /// </summary>
  24. public partial class DefaultBootstrap : IBootstrap, SocketBase.ILoggerProvider, IDisposable
  25. {
  26. private List<IWorkItem> m_AppServers;
  27. private IWorkItem m_ServerManager;
  28. /// <summary>
  29. /// Indicates whether the bootstrap is initialized
  30. /// </summary>
  31. private bool m_Initialized = false;
  32. /// <summary>
  33. /// Global configuration
  34. /// </summary>
  35. private IConfigurationSource m_Config;
  36. /// <summary>
  37. /// Global log
  38. /// </summary>
  39. private ILogger m_GlobalLog;
  40. /// <summary>
  41. /// Gets the bootstrap logger.
  42. /// </summary>
  43. ILogger SocketBase.ILoggerProvider.Logger
  44. {
  45. get { return m_GlobalLog; }
  46. }
  47. /// <summary>
  48. /// Gets the log factory.
  49. /// </summary>
  50. protected ILoggerFactory LogFactory { get; private set; }
  51. /// <summary>
  52. /// Gets all the app servers running in this bootstrap
  53. /// </summary>
  54. public IEnumerable<IWorkItem> AppServers
  55. {
  56. get { return m_AppServers; }
  57. }
  58. private readonly IRootConfig m_RootConfig;
  59. /// <summary>
  60. /// Gets the config.
  61. /// </summary>
  62. public IRootConfig Config
  63. {
  64. get
  65. {
  66. if (m_Config != null)
  67. return m_Config;
  68. return m_RootConfig;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets the startup config file.
  73. /// </summary>
  74. public string StartupConfigFile { get; private set; }
  75. /// <summary>
  76. /// Gets the <see cref="PerformanceMonitor"/> class.
  77. /// </summary>
  78. public IPerformanceMonitor PerfMonitor { get { return m_PerfMonitor; } }
  79. private PerformanceMonitor m_PerfMonitor;
  80. private readonly string m_BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
  81. /// <summary>
  82. /// Gets the base directory.
  83. /// </summary>
  84. /// <value>
  85. /// The base directory.
  86. /// </value>
  87. public string BaseDirectory
  88. {
  89. get
  90. {
  91. return m_BaseDirectory;
  92. }
  93. }
  94. partial void SetDefaultCulture(IRootConfig rootConfig);
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="DefaultBootstrap"/> class.
  97. /// </summary>
  98. /// <param name="appServers">The app servers.</param>
  99. public DefaultBootstrap(IEnumerable<IWorkItem> appServers)
  100. : this(new RootConfig(), appServers, new NullLoggerFactory())
  101. {
  102. }
  103. /// <summary>
  104. /// Initializes a new instance of the <see cref="DefaultBootstrap"/> class.
  105. /// </summary>
  106. /// <param name="rootConfig">The root config.</param>
  107. /// <param name="appServers">The app servers.</param>
  108. public DefaultBootstrap(IRootConfig rootConfig, IEnumerable<IWorkItem> appServers)
  109. : this(rootConfig, appServers, new NullLoggerFactory())
  110. {
  111. }
  112. /// <summary>
  113. /// Initializes a new instance of the <see cref="DefaultBootstrap"/> class.
  114. /// </summary>
  115. /// <param name="rootConfig">The root config.</param>
  116. /// <param name="appServers">The app servers.</param>
  117. /// <param name="logFactory">The log factory.</param>
  118. public DefaultBootstrap(IRootConfig rootConfig, IEnumerable<IWorkItem> appServers, ILoggerFactory logFactory)
  119. {
  120. if (rootConfig == null)
  121. throw new ArgumentNullException("rootConfig");
  122. if (appServers == null)
  123. throw new ArgumentNullException("appServers");
  124. if(!appServers.Any())
  125. throw new ArgumentException("appServers must have one item at least", "appServers");
  126. if (logFactory == null)
  127. throw new ArgumentNullException("logFactory");
  128. m_RootConfig = rootConfig;
  129. SetDefaultCulture(rootConfig);
  130. m_AppServers = appServers.ToList();
  131. m_GlobalLog = logFactory.CreateLogger(this.GetType().Name);
  132. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  133. if (!rootConfig.DisablePerformanceDataCollector)
  134. {
  135. m_PerfMonitor = new PerformanceMonitor();
  136. m_GlobalLog.LogDebug("The PerformanceMonitor has been initialized!");
  137. }
  138. m_GlobalLog.LogDebug("The Bootstrap has been initialized!");
  139. m_Initialized = true;
  140. }
  141. /// <summary>
  142. /// Initializes a new instance of the <see cref="DefaultBootstrap"/> class.
  143. /// </summary>
  144. /// <param name="config">The config.</param>
  145. public DefaultBootstrap(IConfigurationSource config)
  146. {
  147. if (config == null)
  148. throw new ArgumentNullException("config");
  149. SetDefaultCulture(config);
  150. var fileConfigSource = config as ConfigurationSection;
  151. if (fileConfigSource != null)
  152. StartupConfigFile = fileConfigSource.GetConfigSource();
  153. m_Config = config;
  154. AppDomain.CurrentDomain.SetData("Bootstrap", this);
  155. }
  156. /// <summary>
  157. /// Initializes a new instance of the <see cref="DefaultBootstrap"/> class.
  158. /// </summary>
  159. /// <param name="config">The config.</param>
  160. /// <param name="startupConfigFile">The startup config file.</param>
  161. public DefaultBootstrap(IConfigurationSource config, string startupConfigFile)
  162. {
  163. if (config == null)
  164. throw new ArgumentNullException("config");
  165. SetDefaultCulture(config);
  166. if (!string.IsNullOrEmpty(startupConfigFile))
  167. StartupConfigFile = startupConfigFile;
  168. m_Config = config;
  169. AppDomain.CurrentDomain.SetData("Bootstrap", this);
  170. }
  171. /// <summary>
  172. /// Creates the work item instance.
  173. /// </summary>
  174. /// <param name="serviceTypeName">Name of the service type.</param>
  175. /// <param name="serverStatusMetadata">The server status metadata.</param>
  176. /// <returns></returns>
  177. protected virtual IWorkItem CreateWorkItemInstance(string serviceTypeName, StatusInfoAttribute[] serverStatusMetadata)
  178. {
  179. var serviceType = Type.GetType(serviceTypeName, true);
  180. return Activator.CreateInstance(serviceType) as IWorkItem;
  181. }
  182. internal virtual bool SetupWorkItemInstance(IWorkItem workItem, WorkItemFactoryInfo factoryInfo)
  183. {
  184. try
  185. {
  186. //Share AppDomain AppServers also share same socket server factory and log factory instances
  187. factoryInfo.SocketServerFactory.ExportFactory.EnsureInstance();
  188. factoryInfo.LogFactory.ExportFactory.EnsureInstance();
  189. }
  190. catch (Exception e)
  191. {
  192. m_GlobalLog.LogError(e,e.Message);
  193. return false;
  194. }
  195. return workItem.Setup(this, factoryInfo.Config, factoryInfo.ProviderFactories.ToArray());
  196. }
  197. /// <summary>
  198. /// Gets the work item factory info loader.
  199. /// </summary>
  200. /// <param name="config">The config.</param>
  201. /// <param name="logFactory">The log factory.</param>
  202. /// <returns></returns>
  203. internal virtual WorkItemFactoryInfoLoader GetWorkItemFactoryInfoLoader(IConfigurationSource config, ILoggerFactory logFactory)
  204. {
  205. return new WorkItemFactoryInfoLoader(config, logFactory);
  206. }
  207. /// <summary>
  208. /// Initializes the bootstrap with a listen endpoint replacement dictionary
  209. /// </summary>
  210. /// <param name="listenEndPointReplacement">The listen end point replacement.</param>
  211. /// <returns></returns>
  212. public virtual bool Initialize(IDictionary<string, IPEndPoint> listenEndPointReplacement)
  213. {
  214. return Initialize((c) => ReplaceListenEndPoint(c, listenEndPointReplacement));
  215. }
  216. private IServerConfig ReplaceListenEndPoint(IServerConfig serverConfig, IDictionary<string, IPEndPoint> listenEndPointReplacement)
  217. {
  218. var config = new ServerConfig(serverConfig);
  219. if (serverConfig.Port > 0)
  220. {
  221. var endPointKey = serverConfig.Name + "_" + serverConfig.Port;
  222. IPEndPoint instanceEndpoint;
  223. if(!listenEndPointReplacement.TryGetValue(endPointKey, out instanceEndpoint))
  224. {
  225. throw new Exception(string.Format("Failed to find Input Endpoint configuration {0}!", endPointKey));
  226. }
  227. config.Ip = instanceEndpoint.Address.ToString();
  228. config.Port = instanceEndpoint.Port;
  229. }
  230. if (config.Listeners != null && config.Listeners.Any())
  231. {
  232. var listeners = config.Listeners.ToArray();
  233. for (var i = 0; i < listeners.Length; i++)
  234. {
  235. var listener = (ListenerConfig)listeners[i];
  236. var endPointKey = serverConfig.Name + "_" + listener.Port;
  237. IPEndPoint instanceEndpoint;
  238. if (!listenEndPointReplacement.TryGetValue(endPointKey, out instanceEndpoint))
  239. {
  240. throw new Exception(string.Format("Failed to find Input Endpoint configuration {0}!", endPointKey));
  241. }
  242. listener.Ip = instanceEndpoint.Address.ToString();
  243. listener.Port = instanceEndpoint.Port;
  244. }
  245. config.Listeners = listeners;
  246. }
  247. return config;
  248. }
  249. private IWorkItem InitializeAndSetupWorkItem(WorkItemFactoryInfo factoryInfo)
  250. {
  251. IWorkItem appServer;
  252. try
  253. {
  254. appServer = CreateWorkItemInstance(factoryInfo.ServerType, factoryInfo.StatusInfoMetadata);
  255. m_GlobalLog.LogDebug("The server instance {0} has been created!", factoryInfo.Config.Name);
  256. }
  257. catch (Exception e)
  258. {
  259. m_GlobalLog.LogError("Failed to create server instance {0}!", factoryInfo.Config.Name);
  260. m_GlobalLog.LogError(e, e.Message);
  261. return null;
  262. }
  263. var exceptionSource = appServer as IExceptionSource;
  264. if (exceptionSource != null)
  265. exceptionSource.ExceptionThrown += new EventHandler<ErrorEventArgs>(exceptionSource_ExceptionThrown);
  266. var setupResult = false;
  267. try
  268. {
  269. setupResult = SetupWorkItemInstance(appServer, factoryInfo);
  270. m_GlobalLog.LogDebug("The server instance {0} has been initialized!", appServer.Name);
  271. }
  272. catch (Exception e)
  273. {
  274. m_GlobalLog.LogError(e,e.Message);
  275. setupResult = false;
  276. }
  277. if (!setupResult)
  278. {
  279. m_GlobalLog.LogError("Failed to setup server instance!");
  280. return null;
  281. }
  282. return appServer;
  283. }
  284. /// <summary>
  285. /// Initializes the bootstrap with the configuration, config resolver and log factory.
  286. /// </summary>
  287. /// <param name="serverConfigResolver">The server config resolver.</param>
  288. /// <param name="logFactory">The log factory.</param>
  289. /// <returns></returns>
  290. public virtual bool Initialize(Func<IServerConfig, IServerConfig> serverConfigResolver, ILoggerFactory logFactory)
  291. {
  292. if (m_Initialized)
  293. throw new Exception("The server had been initialized already, you cannot initialize it again!");
  294. if (logFactory != null && !string.IsNullOrEmpty(m_Config.LogFactory))
  295. {
  296. throw new ArgumentException("You cannot pass in a logFactory parameter, if you have configured a root log factory.", "logFactory");
  297. }
  298. IEnumerable<WorkItemFactoryInfo> workItemFactories;
  299. using (var factoryInfoLoader = GetWorkItemFactoryInfoLoader(m_Config, logFactory))
  300. {
  301. var bootstrapLogFactory = factoryInfoLoader.GetBootstrapLogFactory();
  302. logFactory = bootstrapLogFactory.ExportFactory.CreateExport<ILoggerFactory>();
  303. LogFactory = logFactory;
  304. m_GlobalLog = logFactory.CreateLogger(this.GetType().Name);
  305. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  306. try
  307. {
  308. workItemFactories = factoryInfoLoader.LoadResult(serverConfigResolver);
  309. }
  310. catch (Exception e)
  311. {
  312. m_GlobalLog.LogError(e,e.Message);
  313. return false;
  314. }
  315. }
  316. m_AppServers = new List<IWorkItem>(m_Config.Servers.Count());
  317. IWorkItem serverManager = null;
  318. //Initialize servers
  319. foreach (var factoryInfo in workItemFactories)
  320. {
  321. IWorkItem appServer = InitializeAndSetupWorkItem(factoryInfo);
  322. if (appServer == null)
  323. return false;
  324. if (factoryInfo.IsServerManager)
  325. serverManager = appServer;
  326. m_AppServers.Add(appServer);
  327. }
  328. if (serverManager != null)
  329. m_ServerManager = serverManager;
  330. if (!m_Config.DisablePerformanceDataCollector)
  331. {
  332. //m_PerfMonitor = new PerformanceMonitor(m_Config, m_AppServers, serverManager, logFactory);
  333. m_PerfMonitor = new PerformanceMonitor();
  334. m_GlobalLog.LogDebug("The PerformanceMonitor has been initialized!");
  335. }
  336. m_GlobalLog.LogDebug("The Bootstrap has been initialized!");
  337. try
  338. {
  339. RegisterRemotingService();
  340. }
  341. catch (Exception e)
  342. {
  343. m_GlobalLog.LogError("Failed to register remoting access service!", e);
  344. return false;
  345. }
  346. m_Initialized = true;
  347. return true;
  348. }
  349. void exceptionSource_ExceptionThrown(object sender, ErrorEventArgs e)
  350. {
  351. m_GlobalLog.LogError(e.Exception, "The server {0} threw an exception.", ((IWorkItemBase)sender).Name);
  352. }
  353. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  354. {
  355. m_GlobalLog.LogError((Exception)e.ExceptionObject, "The process crashed for an unhandled exception!");
  356. }
  357. /// <summary>
  358. /// Initializes the bootstrap with the configuration and config resolver.
  359. /// </summary>
  360. /// <param name="serverConfigResolver">The server config resolver.</param>
  361. /// <returns></returns>
  362. public virtual bool Initialize(Func<IServerConfig, IServerConfig> serverConfigResolver)
  363. {
  364. return Initialize(serverConfigResolver, null);
  365. }
  366. /// <summary>
  367. /// Initializes the bootstrap with the configuration
  368. /// </summary>
  369. /// <param name="logFactory">The log factory.</param>
  370. /// <returns></returns>
  371. public virtual bool Initialize(ILoggerFactory logFactory)
  372. {
  373. return Initialize(c => c, logFactory);
  374. }
  375. /// <summary>
  376. /// Initializes the bootstrap with the configuration
  377. /// </summary>
  378. /// <returns></returns>
  379. public virtual bool Initialize()
  380. {
  381. return Initialize(c => c);
  382. }
  383. /// <summary>
  384. /// Starts this bootstrap.
  385. /// </summary>
  386. /// <returns></returns>
  387. public StartResult Start()
  388. {
  389. if (!m_Initialized)
  390. {
  391. m_GlobalLog.LogError("You cannot invoke method Start() before initializing!");
  392. return StartResult.Failed;
  393. }
  394. var result = StartResult.None;
  395. var succeeded = 0;
  396. foreach (var server in m_AppServers)
  397. {
  398. if (!server.Start())
  399. {
  400. m_GlobalLog.LogInformation("The server instance {0} has failed to be started!", server.Name);
  401. }
  402. else
  403. {
  404. succeeded++;
  405. if (Config.Isolation != IsolationMode.None)
  406. {
  407. m_GlobalLog.LogInformation("The server instance {0} has been started!", server.Name);
  408. }
  409. }
  410. }
  411. if (m_AppServers.Any())
  412. {
  413. if (m_AppServers.Count == succeeded)
  414. result = StartResult.Success;
  415. else if (succeeded == 0)
  416. result = StartResult.Failed;
  417. else
  418. result = StartResult.PartialSuccess;
  419. }
  420. if (m_PerfMonitor != null)
  421. {
  422. m_PerfMonitor.Start();
  423. m_GlobalLog.LogDebug("The PerformanceMonitor has been started!");
  424. }
  425. return result;
  426. }
  427. /// <summary>
  428. /// Stops this bootstrap.
  429. /// </summary>
  430. public void Stop()
  431. {
  432. var servers = m_AppServers.ToArray();
  433. if (servers.Any(s => s.Config != null && s.Config.StartupOrder != 0))
  434. {
  435. Array.Reverse(servers);
  436. }
  437. foreach (var server in servers)
  438. {
  439. if (server.State == ServerState.Running)
  440. {
  441. server.Stop();
  442. if (Config.Isolation != IsolationMode.None)
  443. {
  444. m_GlobalLog.LogInformation("The server instance {0} has been stopped!", server.Name);
  445. }
  446. }
  447. }
  448. if (m_PerfMonitor != null)
  449. {
  450. m_PerfMonitor.Stop();
  451. m_GlobalLog.LogDebug("The PerformanceMonitor has been stoppped!");
  452. }
  453. }
  454. /// <summary>
  455. /// Registers the bootstrap remoting access service.
  456. /// </summary>
  457. protected virtual void RegisterRemotingService()
  458. {
  459. return;
  460. //var bootstrapIpcPort = string.Format("SuperSocket.Bootstrap[{0}]", Math.Abs(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(System.IO.Path.DirectorySeparatorChar).GetHashCode()));
  461. //var serverChannelName = "Bootstrap";
  462. //var serverChannel = ChannelServices.RegisteredChannels.FirstOrDefault(c => c.ChannelName == serverChannelName);
  463. //if (serverChannel != null)
  464. // ChannelServices.UnregisterChannel(serverChannel);
  465. //serverChannel = new IpcServerChannel(serverChannelName, bootstrapIpcPort, new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full });
  466. //ChannelServices.RegisterChannel(serverChannel, false);
  467. //AppDomain.CurrentDomain.SetData("BootstrapIpcPort", bootstrapIpcPort);
  468. //var bootstrapProxyType = typeof(RemoteBootstrapProxy);
  469. //if (!RemotingConfiguration.GetRegisteredWellKnownServiceTypes().Any(s => s.ObjectType == bootstrapProxyType))
  470. // RemotingConfiguration.RegisterWellKnownServiceType(bootstrapProxyType, "Bootstrap.rem", WellKnownObjectMode.Singleton);
  471. }
  472. /// <summary>
  473. /// Releases unmanaged and - optionally - managed resources.
  474. /// </summary>
  475. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  476. protected virtual void Dispose(bool disposing)
  477. {
  478. if (disposing)
  479. {
  480. AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  481. }
  482. }
  483. /// <summary>
  484. /// Releases unmanaged and - optionally - managed resources.
  485. /// </summary>
  486. public void Dispose()
  487. {
  488. Dispose(true);
  489. GC.SuppressFinalize(this);
  490. }
  491. private void ResetPerfMoniter()
  492. {
  493. if (m_PerfMonitor != null)
  494. {
  495. m_PerfMonitor.Stop();
  496. m_PerfMonitor.Dispose();
  497. m_PerfMonitor = null;
  498. }
  499. m_PerfMonitor = new PerformanceMonitor();
  500. m_PerfMonitor.Start();
  501. m_GlobalLog.LogDebug("The PerformanceMonitor has been reset for new server has been added!");
  502. }
  503. }
  504. }