DefaultBootstrap.cs 22 KB

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