PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Source 2/AppFabric Caching AdminTool/Entities/Cache Cluster/CacheCluster.cs

#
C# | 338 lines | 261 code | 31 blank | 46 comment | 8 complexity | 4a26f2822c7ce48b54e0244c2f804184 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.Data.Common;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Management.Automation.Runspaces;
  9. using System.Text;
  10. using System.Xml.Linq;
  11. using Microsoft.Data.Caching.AdminApi;
  12. using System.ComponentModel;
  13. namespace MDCAdminTool
  14. {
  15. public class CacheCluster : PowerShellBase<object , CacheHost>
  16. {
  17. #region Fields
  18. private string _Provider;
  19. private string _ConnectionString;
  20. private bool _IsOpen;
  21. private string _ConfigFile;
  22. private string _ImportConfigFile;
  23. private string _ExportConfigFile;
  24. private Collection<CacheHost> _Hosts;
  25. #endregion
  26. #region Properties
  27. public string Provider
  28. {
  29. get { return _Provider; }
  30. set { _Provider = value; OnPropertyChanged( "Provider" ); }
  31. }
  32. public string ConnectionString
  33. {
  34. get { return _ConnectionString; }
  35. set { _ConnectionString = value; OnPropertyChanged( "ConnectionString" ); }
  36. }
  37. public bool IsOpen
  38. {
  39. get { return _IsOpen; }
  40. set { _IsOpen = value; OnPropertyChanged( "IsOpen" ); }
  41. }
  42. public string ConfigFile
  43. {
  44. get { return _ConfigFile; }
  45. set { _ConfigFile = value; OnPropertyChanged( "ConfigFile" ); }
  46. }
  47. public string ExportConfigFile
  48. {
  49. get { return _ExportConfigFile; }
  50. set { _ExportConfigFile = value; OnPropertyChanged( "ExportConfigFile" ); }
  51. }
  52. public string ImportConfigFile
  53. {
  54. get { return _ImportConfigFile; }
  55. set { _ImportConfigFile = value; OnPropertyChanged( "ImportConfigFile" ); }
  56. }
  57. public Collection<CacheHost> Hosts
  58. {
  59. get { return _Hosts; }
  60. set { _Hosts = value; OnPropertyChanged( "Hosts" ); }
  61. }
  62. #endregion
  63. private DataCacheConfigSection dataCacheConfig;
  64. public CacheCluster()
  65. {
  66. Type = CacheType.CacheCluster;
  67. Name = "No Cluster";
  68. Hosts = new Collection<CacheHost>();
  69. }
  70. public CacheCluster( string configFile ) : this()
  71. {
  72. InitCacheCluster( configFile );
  73. }
  74. #region Cache Cluster-related Commands
  75. /// <summary>
  76. /// Sets the context of your PowerShell session to the desired cache cluster.
  77. /// When starting a PowerShell session on a cache host, this command runs automatically,
  78. /// by using the connection parameters specified in the cache host configuration file.
  79. /// </summary>
  80. /// <param name="name">The name of the cache cluster.</param>
  81. /// <param name="provider">The provider that is used to store the cluster configuration settings.</param>
  82. /// <param name="connectionString">The connection string to the database.</param>
  83. public void UseCacheCluster( string name , string provider , string connectionString )
  84. {
  85. Name = name;
  86. Provider = provider;
  87. ConnectionString = connectionString;
  88. UseCacheCluster();
  89. }
  90. /// <summary>
  91. /// Sets the context of your PowerShell session to the desired cache cluster.
  92. /// When starting a PowerShell session on a cache host, this command runs automatically,
  93. /// by using the connection parameters specified in the cache host configuration file.
  94. /// </summary>
  95. public void UseCacheCluster()
  96. {
  97. var p = new Dictionary<string , object>
  98. {
  99. {"ConnectionString", ConnectionString},
  100. {"Name" , Name},
  101. {"Provider" , Provider}
  102. };
  103. //if ( CheackConnectionStringAndProvider() )
  104. {
  105. Invoke( "Use-CacheCluster" , p );
  106. }
  107. //else
  108. //{
  109. // Log( new ErrorMessage { Message = "Bad connection string or provider !" } );
  110. //}
  111. }
  112. /// <summary>
  113. /// Starts all cache host services in the cluster. Lead hosts are started first.
  114. /// </summary>
  115. public override void Start()
  116. {
  117. Invoke( "Start-CacheCluster" );
  118. LoadCacheClusterInfo();
  119. LoadCacheHosts();
  120. }
  121. /// <summary>
  122. /// Stops all cache host services in the cluster.
  123. /// </summary>
  124. public override void Stop()
  125. {
  126. Invoke( "Stop-CacheCluster" );
  127. LoadCacheHosts();
  128. }
  129. /// <summary>
  130. /// Restarts all cache host services in the cluster in the correct sequence.
  131. /// </summary>
  132. public override void Restart()
  133. {
  134. Invoke( "Restart-CacheCluster" );
  135. UseCacheCluster();
  136. LoadCacheClusterInfo();
  137. LoadCacheHosts();
  138. }
  139. /// <summary>
  140. /// This command exports the cluster configuration settings, as they currently exist in the cluster,
  141. /// to the specified XML-based configuration file. This command can be used regardless of
  142. /// where you chose to store your cluster configuration settings (SQL Server or shared network folder).
  143. /// </summary>
  144. /// <param name="file">The fully qualified path and name for the XML-based configuration file. If a file already exists by this name, it will be overwritten.</param>
  145. public void ExportConfig()
  146. {
  147. var p = new Dictionary<string , object> { { "File" , ExportConfigFile } };
  148. Invoke( "Export-CacheClusterConfig" , p );
  149. }
  150. /// <summary>
  151. /// This command imports the cluster configuration settings as described in the specified
  152. /// XML-based configuration file. This command can be used regardless of where you chose to
  153. /// store your cluster configuration settings (SQL Server or shared network folder).
  154. /// You must restart the cluster with the Restart-CacheCluster for any changes to take effect.
  155. /// </summary>
  156. /// <param name="file">The fully qualified path and name of the XML-based configuration file that describes the cache cluster configuration settings to be applied to the cluster. </param>
  157. public void ImportConfig()
  158. {
  159. var p = new Dictionary<string , object> { { "File" , ImportConfigFile } };
  160. Invoke( "Import-CacheClusterConfig" , p );
  161. }
  162. /// <summary>
  163. /// Load all cache host services that are members of the cache cluster.
  164. /// </summary>
  165. public void LoadCacheHosts()
  166. {
  167. var r = Invoke( "Get-CacheHost" );
  168. if ( r == null ) return;
  169. var hosts = new Collection<CacheHost>();
  170. foreach ( var host in r )
  171. {
  172. if ( host.BaseObject is HostInfo )
  173. {
  174. var h = host.BaseObject as HostInfo;
  175. hosts.Add( new CacheHost( h , this ) );
  176. }
  177. }
  178. Hosts = hosts;
  179. }
  180. public override void LoadChildren()
  181. {
  182. LoadCacheHosts();
  183. }
  184. #endregion
  185. #region Helper Method
  186. private bool CheackConnectionStringAndProvider()
  187. {
  188. bool result = false;
  189. DbConnection conn = null;
  190. try
  191. {
  192. var factory = DbProviderFactories.GetFactory( Provider );
  193. conn = factory.CreateConnection();
  194. conn.ConnectionString = ConnectionString;
  195. conn.Open();
  196. result = true;
  197. }
  198. catch ( Exception ex )
  199. {
  200. Log( new ErrorMessage { Message = ex.Message } );
  201. result = false;
  202. }
  203. finally
  204. {
  205. if( conn != null )
  206. conn.Dispose();
  207. }
  208. return result;
  209. }
  210. public void LoadCacheClusterInfo()
  211. {
  212. try
  213. {
  214. if ( File.Exists( ConfigFile ) )
  215. {
  216. InitDataCacheConfigSection( ConfigFile );
  217. }
  218. else
  219. {
  220. Log( new ErrorMessage { Message = "Can't find the DistributedCache123.exe.config file." } );
  221. }
  222. }
  223. catch ( Exception ex )
  224. {
  225. Log( new ErrorMessage { Message = ex.Message } );
  226. }
  227. }
  228. private void LoadCacheClusterInfo( string configFile )
  229. {
  230. ConfigFile = configFile;
  231. LoadCacheClusterInfo();
  232. }
  233. private void InitDataCacheConfigSection( string file )
  234. {
  235. var config = XElement.Load( file );
  236. XElement dcc = config.Descendants( "dataCacheConfig" ).First();
  237. if ( dcc == null )
  238. {
  239. Log( new ErrorMessage { Message = "Can't find the <dataCacheConfig> tag" } );
  240. return;
  241. }
  242. dataCacheConfig = new DataCacheConfigSection
  243. {
  244. ClusterName = dcc.Attribute( "clusterName" ).Value ,
  245. HostName = dcc.Attribute( "hostName" ).Value ,
  246. CacheHostName = dcc.Attribute( "cacheHostName" ).Value ,
  247. Provider = dcc.Element( "clusterConfig" ).Attribute( "provider" ).Value ,
  248. ConnectionString = dcc.Element( "clusterConfig" ).Attribute( "connectionString" ).Value
  249. };
  250. InitCacheCluster( dataCacheConfig );
  251. }
  252. public void InitCacheCluster( DataCacheConfigSection dccs )
  253. {
  254. Name = dccs.ClusterName;
  255. Provider = dccs.Provider;
  256. ConnectionString = dccs.ConnectionString;
  257. }
  258. public void InitCacheCluster( string configFile )
  259. {
  260. ConfigFile = configFile;
  261. try
  262. {
  263. // Step 1: Get Config Info.
  264. LoadCacheClusterInfo();
  265. // Step 2: Chooce Cache Cluster
  266. UseCacheCluster( Name , Provider , ConnectionString );
  267. // Must be last.
  268. LoadCacheHosts();
  269. }
  270. catch ( Exception ex )
  271. {
  272. Log( new ErrorMessage { Message = ex.Message } );
  273. }
  274. }
  275. #endregion
  276. #region Not Implemented
  277. public override CacheStats LoadStatistics()
  278. {
  279. throw new NotImplementedException();
  280. }
  281. public override void LoadConfig()
  282. {
  283. throw new NotImplementedException();
  284. }
  285. public override void SaveConfig()
  286. {
  287. throw new NotImplementedException();
  288. }
  289. public override void Add( CacheHost name )
  290. {
  291. throw new NotImplementedException();
  292. }
  293. public override void Remove( CacheHost name )
  294. {
  295. throw new NotImplementedException();
  296. }
  297. public override void Add( CacheEntityBase item )
  298. {
  299. throw new NotImplementedException();
  300. }
  301. public override void Remove( CacheEntityBase item )
  302. {
  303. throw new NotImplementedException();
  304. }
  305. #endregion
  306. }
  307. }