/src/prod/src/managed/Microsoft.ServiceFabric.Data.Impl/Replicator/Utility.cs

https://github.com/Microsoft/service-fabric · C# · 953 lines · 734 code · 141 blank · 78 comment · 131 complexity · 59c9ce677a78b95ebcaa346365f3513e MD5 · raw file

  1. // ------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
  4. // ------------------------------------------------------------
  5. namespace Microsoft.ServiceFabric.Replicator
  6. {
  7. using System;
  8. using System.Diagnostics;
  9. using System.Fabric;
  10. using System.Fabric.Common;
  11. using System.Fabric.Interop;
  12. using System.IO;
  13. using System.Runtime.CompilerServices;
  14. using System.Runtime.InteropServices;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using Microsoft.Win32.SafeHandles;
  19. /// <summary>
  20. /// Replicator utility class.
  21. /// </summary>
  22. internal static class Utility
  23. {
  24. public static void SetIoPriorityHint(SafeFileHandle safeFileHandle, Kernel32Types.PRIORITY_HINT priorityHintInfo)
  25. {
  26. Kernel32Types.FileInformation fileInformation = new Kernel32Types.FileInformation();
  27. fileInformation.FILE_IO_PRIORITY_HINT_INFO.PriorityHint = priorityHintInfo;
  28. bool isSet = FabricFile.SetFileInformationByHandle(
  29. safeFileHandle,
  30. Kernel32Types.FILE_INFO_BY_HANDLE_CLASS.FileIoPriorityHintInfo,
  31. ref fileInformation,
  32. Marshal.SizeOf(fileInformation.FILE_IO_PRIORITY_HINT_INFO));
  33. if (isSet == false)
  34. {
  35. int status = Marshal.GetLastWin32Error();
  36. Assert(false, "SetFileInformationByHandle failed: ErrorCode: {0}", status);
  37. }
  38. }
  39. #if !DotNetCoreClr
  40. // A few tests depends on this. Remove this after clearing up the tests.
  41. internal static object[] ArgListToParams(RuntimeArgumentHandle handle)
  42. {
  43. var iter = new ArgIterator(handle);
  44. var count = iter.GetRemainingCount();
  45. var args = new object[count];
  46. for (var i = 0; i < count; i++)
  47. {
  48. args[i] = TypedReference.ToObject(iter.GetNextArg());
  49. }
  50. return args;
  51. }
  52. #endif
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. internal static void Assert(bool condition, string format)
  55. {
  56. if (condition == false)
  57. {
  58. CodingError(format, new object[0]);
  59. // AMW - Force break into debugger for ease of debugging
  60. Debugger.Break();
  61. }
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. internal static void Assert(bool condition, string format, long param1)
  65. {
  66. if (condition == false)
  67. {
  68. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  69. FailFast(failFastMessage);
  70. // AMW - Force break into debugger for ease of debugging
  71. Debugger.Break();
  72. }
  73. }
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. internal static void Assert(bool condition, string format, long param1, long param2)
  76. {
  77. if (condition == false)
  78. {
  79. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  80. FailFast(failFastMessage);
  81. // AMW - Force break into debugger for ease of debugging
  82. Debugger.Break();
  83. }
  84. }
  85. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  86. internal static void Assert(bool condition, string format, string param1)
  87. {
  88. if (condition == false)
  89. {
  90. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  91. FailFast(failFastMessage);
  92. // AMW - Force break into debugger for ease of debugging
  93. Debugger.Break();
  94. }
  95. }
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. internal static void Assert(bool condition, string format, LogRecordType param1)
  98. {
  99. if (condition == false)
  100. {
  101. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  102. FailFast(failFastMessage);
  103. // AMW - Force break into debugger for ease of debugging
  104. Debugger.Break();
  105. }
  106. }
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. internal static void Assert(bool condition, string format, TransactionState param1, long param2)
  109. {
  110. if (condition == false)
  111. {
  112. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  113. FailFast(failFastMessage);
  114. // AMW - Force break into debugger for ease of debugging
  115. Debugger.Break();
  116. }
  117. }
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. internal static void Assert(bool condition, string format, ReplicaRole param1)
  120. {
  121. if (condition == false)
  122. {
  123. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  124. FailFast(failFastMessage);
  125. // AMW - Force break into debugger for ease of debugging
  126. Debugger.Break();
  127. }
  128. }
  129. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  130. internal static void Assert(bool condition, string format, DrainingStream param1)
  131. {
  132. if (condition == false)
  133. {
  134. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  135. FailFast(failFastMessage);
  136. // AMW - Force break into debugger for ease of debugging
  137. Debugger.Break();
  138. }
  139. }
  140. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  141. internal static void Assert(bool condition, string format, Exception param1)
  142. {
  143. if (condition == false)
  144. {
  145. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  146. FailFast(failFastMessage);
  147. // AMW - Force break into debugger for ease of debugging
  148. Debugger.Break();
  149. }
  150. }
  151. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  152. internal static void Assert(bool condition, string format, ApplyContext param1)
  153. {
  154. if (condition == false)
  155. {
  156. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  157. FailFast(failFastMessage);
  158. // AMW - Force break into debugger for ease of debugging
  159. Debugger.Break();
  160. }
  161. }
  162. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  163. internal static void Assert(bool condition, string format, string param1, long param2)
  164. {
  165. if (condition == false)
  166. {
  167. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  168. FailFast(failFastMessage);
  169. // AMW - Force break into debugger for ease of debugging
  170. Debugger.Break();
  171. }
  172. }
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. internal static void Assert(bool condition, string format, Metadata param1)
  175. {
  176. if (condition == false)
  177. {
  178. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  179. FailFast(failFastMessage);
  180. // AMW - Force break into debugger for ease of debugging
  181. Debugger.Break();
  182. }
  183. }
  184. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  185. internal static void Assert(bool condition, string format, Uri param1)
  186. {
  187. if (condition == false)
  188. {
  189. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  190. FailFast(failFastMessage);
  191. // AMW - Force break into debugger for ease of debugging
  192. Debugger.Break();
  193. }
  194. }
  195. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  196. internal static void Assert(bool condition, string format, StateManagerApplyType param1)
  197. {
  198. if (condition == false)
  199. {
  200. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  201. FailFast(failFastMessage);
  202. // AMW - Force break into debugger for ease of debugging
  203. Debugger.Break();
  204. }
  205. }
  206. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  207. internal static void Assert(bool condition, string format, ReplicationMetadata param1)
  208. {
  209. if (condition == false)
  210. {
  211. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  212. FailFast(failFastMessage);
  213. // AMW - Force break into debugger for ease of debugging
  214. Debugger.Break();
  215. }
  216. }
  217. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  218. internal static void Assert(bool condition, string format, bool param1)
  219. {
  220. if (condition == false)
  221. {
  222. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  223. FailFast(failFastMessage);
  224. // AMW - Force break into debugger for ease of debugging
  225. Debugger.Break();
  226. }
  227. }
  228. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  229. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1)
  230. {
  231. if (condition == false)
  232. {
  233. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  234. FailFast(failFastMessage);
  235. // AMW - Force break into debugger for ease of debugging
  236. Debugger.Break();
  237. }
  238. }
  239. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  240. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1, LogicalSequenceNumber param2)
  241. {
  242. if (condition == false)
  243. {
  244. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  245. FailFast(failFastMessage);
  246. // AMW - Force break into debugger for ease of debugging
  247. Debugger.Break();
  248. }
  249. }
  250. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  251. internal static void Assert(bool condition, string format, bool param1, bool param2)
  252. {
  253. if (condition == false)
  254. {
  255. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  256. FailFast(failFastMessage);
  257. // AMW - Force break into debugger for ease of debugging
  258. Debugger.Break();
  259. }
  260. }
  261. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  262. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1, RecoveredOrCopiedCheckpointLsn param2, bool param3)
  263. {
  264. if (condition == false)
  265. {
  266. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  267. FailFast(failFastMessage);
  268. // AMW - Force break into debugger for ease of debugging
  269. Debugger.Break();
  270. }
  271. }
  272. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  273. internal static void Assert(bool condition, string format, string param1, LogicalSequenceNumber param2, LogicalSequenceNumber param3)
  274. {
  275. if (condition == false)
  276. {
  277. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  278. FailFast(failFastMessage);
  279. // AMW - Force break into debugger for ease of debugging
  280. Debugger.Break();
  281. }
  282. }
  283. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  284. internal static void Assert(bool condition, string format, string param1, long param2, long param3)
  285. {
  286. if (condition == false)
  287. {
  288. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  289. FailFast(failFastMessage);
  290. // AMW - Force break into debugger for ease of debugging
  291. Debugger.Break();
  292. }
  293. }
  294. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  295. internal static void Assert(bool condition, string format, long param1, long param2, int param3, StateManagerApplyType param4)
  296. {
  297. if (condition == false)
  298. {
  299. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3, param4);
  300. FailFast(failFastMessage);
  301. // AMW - Force break into debugger for ease of debugging
  302. Debugger.Break();
  303. }
  304. }
  305. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  306. internal static void Assert(bool condition, string format, DrainingStream param1, LogRecordType param2)
  307. {
  308. if (condition == false)
  309. {
  310. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  311. FailFast(failFastMessage);
  312. // AMW - Force break into debugger for ease of debugging
  313. Debugger.Break();
  314. }
  315. }
  316. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  317. internal static void Assert(bool condition, string format, bool param1, LogRecordType param2)
  318. {
  319. if (condition == false)
  320. {
  321. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  322. FailFast(failFastMessage);
  323. // AMW - Force break into debugger for ease of debugging
  324. Debugger.Break();
  325. }
  326. }
  327. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  328. internal static void Assert(bool condition, string format, CheckpointState param1, CheckpointState param2)
  329. {
  330. if (condition == false)
  331. {
  332. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  333. FailFast(failFastMessage);
  334. // AMW - Force break into debugger for ease of debugging
  335. Debugger.Break();
  336. }
  337. }
  338. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  339. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1, PhysicalSequenceNumber param2)
  340. {
  341. if (condition == false)
  342. {
  343. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  344. FailFast(failFastMessage);
  345. // AMW - Force break into debugger for ease of debugging
  346. Debugger.Break();
  347. }
  348. }
  349. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  350. internal static void Assert(bool condition, string format, string param1, long param2, long param3, long param4)
  351. {
  352. if (condition == false)
  353. {
  354. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3, param4);
  355. FailFast(failFastMessage);
  356. // AMW - Force break into debugger for ease of debugging
  357. Debugger.Break();
  358. }
  359. }
  360. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  361. internal static void Assert(bool condition, string format, ulong param1)
  362. {
  363. if (condition == false)
  364. {
  365. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  366. FailFast(failFastMessage);
  367. // AMW - Force break into debugger for ease of debugging
  368. Debugger.Break();
  369. }
  370. }
  371. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  372. internal static void Assert(bool condition, string format, LogRecord param1)
  373. {
  374. if (condition == false)
  375. {
  376. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  377. FailFast(failFastMessage);
  378. // AMW - Force break into debugger for ease of debugging
  379. Debugger.Break();
  380. }
  381. }
  382. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  383. internal static void Assert(bool condition, string format, PhysicalSequenceNumber param1, PhysicalSequenceNumber param2)
  384. {
  385. if (condition == false)
  386. {
  387. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  388. FailFast(failFastMessage);
  389. // AMW - Force break into debugger for ease of debugging
  390. Debugger.Break();
  391. }
  392. }
  393. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  394. internal static void Assert(bool condition, string format, string param1, string param2)
  395. {
  396. if (condition == false)
  397. {
  398. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  399. FailFast(failFastMessage);
  400. // AMW - Force break into debugger for ease of debugging
  401. Debugger.Break();
  402. }
  403. }
  404. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  405. internal static void Assert(bool condition, string format, string param1, string param2, string param3)
  406. {
  407. if (condition == false)
  408. {
  409. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  410. FailFast(failFastMessage);
  411. // AMW - Force break into debugger for ease of debugging
  412. Debugger.Break();
  413. }
  414. }
  415. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  416. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1, long param2, bool param3)
  417. {
  418. if (condition == false)
  419. {
  420. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  421. FailFast(failFastMessage);
  422. // AMW - Force break into debugger for ease of debugging
  423. Debugger.Break();
  424. }
  425. }
  426. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  427. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1, LogicalSequenceNumber param2, LogRecordType param3)
  428. {
  429. if (condition == false)
  430. {
  431. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  432. FailFast(failFastMessage);
  433. // AMW - Force break into debugger for ease of debugging
  434. Debugger.Break();
  435. }
  436. }
  437. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  438. internal static void Assert(bool condition, string format, LogicalSequenceNumber param1, LogRecordType param2)
  439. {
  440. if (condition == false)
  441. {
  442. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  443. FailFast(failFastMessage);
  444. // AMW - Force break into debugger for ease of debugging
  445. Debugger.Break();
  446. }
  447. }
  448. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  449. internal static void Assert(bool condition, string format, RecordProcessingMode param1)
  450. {
  451. if (condition == false)
  452. {
  453. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  454. FailFast(failFastMessage);
  455. // AMW - Force break into debugger for ease of debugging
  456. Debugger.Break();
  457. }
  458. }
  459. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  460. internal static void Assert(bool condition, string format, DrainingStream param1, LogicalSequenceNumber param2, PhysicalSequenceNumber param3)
  461. {
  462. if (condition == false)
  463. {
  464. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  465. FailFast(failFastMessage);
  466. // AMW - Force break into debugger for ease of debugging
  467. Debugger.Break();
  468. }
  469. }
  470. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  471. internal static void Assert(bool condition, string format, OperationLogRecord param1, BeginCheckpointLogRecord param2)
  472. {
  473. if (condition == false)
  474. {
  475. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  476. FailFast(failFastMessage);
  477. // AMW - Force break into debugger for ease of debugging
  478. Debugger.Break();
  479. }
  480. }
  481. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  482. internal static void Assert(bool condition, string format, ReplicationMetadata param1, Metadata param2)
  483. {
  484. if (condition == false)
  485. {
  486. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  487. FailFast(failFastMessage);
  488. // AMW - Force break into debugger for ease of debugging
  489. Debugger.Break();
  490. }
  491. }
  492. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  493. internal static void Assert(bool condition, string format, long param1, Metadata param2, long param3)
  494. {
  495. if (condition == false)
  496. {
  497. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  498. FailFast(failFastMessage);
  499. // AMW - Force break into debugger for ease of debugging
  500. Debugger.Break();
  501. }
  502. }
  503. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  504. internal static void Assert(bool condition, string format, Metadata param1, long param2, long param3)
  505. {
  506. if (condition == false)
  507. {
  508. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3);
  509. FailFast(failFastMessage);
  510. // AMW - Force break into debugger for ease of debugging
  511. Debugger.Break();
  512. }
  513. }
  514. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  515. internal static void Assert(bool condition, string format, CopyStage param1)
  516. {
  517. if (condition == false)
  518. {
  519. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  520. FailFast(failFastMessage);
  521. // AMW - Force break into debugger for ease of debugging
  522. Debugger.Break();
  523. }
  524. }
  525. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  526. internal static void Assert(bool condition, string format, long param1, TransactionState param2)
  527. {
  528. if (condition == false)
  529. {
  530. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  531. FailFast(failFastMessage);
  532. // AMW - Force break into debugger for ease of debugging
  533. Debugger.Break();
  534. }
  535. }
  536. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  537. internal static void Assert(bool condition, string format, ulong param1, ulong param2)
  538. {
  539. if (condition == false)
  540. {
  541. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  542. FailFast(failFastMessage);
  543. // AMW - Force break into debugger for ease of debugging
  544. Debugger.Break();
  545. }
  546. }
  547. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  548. internal static void Assert(bool condition, string format, string param1, bool param2, string param3, string param4)
  549. {
  550. if (condition == false)
  551. {
  552. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2, param3, param4);
  553. FailFast(failFastMessage);
  554. // AMW - Force break into debugger for ease of debugging
  555. Debugger.Break();
  556. }
  557. }
  558. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  559. internal static void Assert(bool condition, string format, BeginCheckpointLogRecord param1, LogRecord param2)
  560. {
  561. if (condition == false)
  562. {
  563. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  564. FailFast(failFastMessage);
  565. // AMW - Force break into debugger for ease of debugging
  566. Debugger.Break();
  567. }
  568. }
  569. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  570. internal static void Assert(bool condition, string format, TruncateHeadLogRecord param1, LogRecord param2)
  571. {
  572. if (condition == false)
  573. {
  574. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1, param2);
  575. FailFast(failFastMessage);
  576. // AMW - Force break into debugger for ease of debugging
  577. Debugger.Break();
  578. }
  579. }
  580. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  581. internal static void Assert(bool condition, string format, CheckpointState param1)
  582. {
  583. if (condition == false)
  584. {
  585. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  586. FailFast(failFastMessage);
  587. // AMW - Force break into debugger for ease of debugging
  588. Debugger.Break();
  589. }
  590. }
  591. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  592. internal static void Assert(bool condition, string format, TruncationState param1)
  593. {
  594. if (condition == false)
  595. {
  596. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, param1);
  597. FailFast(failFastMessage);
  598. // AMW - Force break into debugger for ease of debugging
  599. Debugger.Break();
  600. }
  601. }
  602. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  603. internal static void Assert(bool condition, string format, params object[] list)
  604. {
  605. if (condition == false)
  606. {
  607. CodingError(format, list);
  608. // AMW - Force break into debugger for ease of debugging
  609. Debugger.Break();
  610. }
  611. }
  612. internal static void FailFast(string failFastMessage)
  613. {
  614. var processId = Process.GetCurrentProcess().Id;
  615. var threadId = Thread.CurrentThread.ManagedThreadId;
  616. var component = string.Format(
  617. System.Globalization.CultureInfo.InvariantCulture,
  618. "{0}:{1}@{2}",
  619. processId,
  620. threadId,
  621. DateTime.UtcNow);
  622. var errorMessage = string.Format(
  623. System.Globalization.CultureInfo.InvariantCulture,
  624. "Assert or Coding error occurred.\n{0}",
  625. Environment.StackTrace);
  626. AppTrace.TraceSource.WriteError(
  627. component,
  628. @"Assert or Coding error with message {0}. Stack : {1}",
  629. failFastMessage,
  630. errorMessage);
  631. if (Debugger.IsAttached)
  632. {
  633. Debugger.Break();
  634. }
  635. if (IsDebuggerPresent())
  636. {
  637. OutputDebugString(component);
  638. OutputDebugString(errorMessage);
  639. DebugBreak();
  640. }
  641. Environment.FailFast(failFastMessage);
  642. return;
  643. }
  644. internal static void CodingError(string format, params object[] args)
  645. {
  646. var failFastMessage = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, args);
  647. FailFast(failFastMessage);
  648. }
  649. internal static void CreateHardLinkOrCopyFile(string sourceFileName, string destinationFileName)
  650. {
  651. if (!FabricFile.Exists(sourceFileName))
  652. {
  653. return;
  654. }
  655. // Since the source file won't be changed, we can simply create a hardlink
  656. if (!FabricFile.CreateHardLink(destinationFileName, sourceFileName))
  657. {
  658. // Hardlink can fail, e.g. if the destination is on another drive or the drive isn't NTFS.
  659. // Fallback to a full file copy.
  660. FabricFile.Copy(sourceFileName, destinationFileName, overwrite: true);
  661. }
  662. }
  663. internal static Exception FlattenException(Exception e, out int innerHResult)
  664. {
  665. if (e.InnerException != null)
  666. {
  667. innerHResult = e.InnerException.HResult;
  668. }
  669. else
  670. {
  671. innerHResult = 0;
  672. }
  673. var exception = e as AggregateException;
  674. if (exception != null)
  675. {
  676. e = exception.Flatten().InnerException;
  677. }
  678. return e;
  679. }
  680. internal static Type GetSerializedType(string typeName)
  681. {
  682. try
  683. {
  684. var simpleTypeName = GetSimpleTypeName(typeName);
  685. var type = Type.GetType(simpleTypeName);
  686. if (type != null)
  687. {
  688. return type;
  689. }
  690. }
  691. catch (Exception)
  692. {
  693. }
  694. try
  695. {
  696. return Type.GetType(typeName);
  697. }
  698. catch (Exception)
  699. {
  700. }
  701. return null;
  702. }
  703. internal static string GetSimpleTypeName(Type type)
  704. {
  705. var typeName = type.AssemblyQualifiedName;
  706. return GetSimpleTypeName(typeName);
  707. }
  708. internal static long GetOperationSize(OperationData operationData)
  709. {
  710. long size = 0;
  711. if (operationData != null && operationData.Count > 0)
  712. {
  713. foreach (var segment in operationData)
  714. {
  715. size += segment.Count;
  716. }
  717. }
  718. return size;
  719. }
  720. /// <summary>
  721. /// Transforms type names from assembly qualified format to a simpler format for serialization.
  722. ///
  723. /// Transforms type names from:
  724. /// - Namespace.Foo`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  725. /// to:
  726. /// - Namespace.Foo`1[[System.Int32, mscorlib]], MyAssembly
  727. /// </summary>
  728. /// <param name="typeName">Type name to transform.</param>
  729. /// <returns>Type name usable for replicated serialization.</returns>
  730. internal static string GetSimpleTypeName(string typeName)
  731. {
  732. var writer = new StringBuilder(typeName.Length);
  733. var currentIndex = 0;
  734. var commaActive = false;
  735. // Walk the string and only append characters associated with the type name and assembly name
  736. while (currentIndex < typeName.Length)
  737. {
  738. // Commas are the important delimiters between entities
  739. if (typeName[currentIndex] == ',')
  740. {
  741. if (commaActive)
  742. {
  743. // If this is the second comma we've seen, it delimits extraneous
  744. // entities (Version, Culture, PublicKeyToken, etc.) that we can discard
  745. while (currentIndex < typeName.Length && typeName[currentIndex] != ']')
  746. {
  747. currentIndex++;
  748. }
  749. commaActive = false;
  750. continue;
  751. }
  752. else
  753. {
  754. commaActive = true;
  755. }
  756. }
  757. else if (typeName[currentIndex] == '[')
  758. {
  759. // This is the start of a generic type
  760. commaActive = false;
  761. }
  762. writer.Append(typeName[currentIndex]);
  763. currentIndex++;
  764. }
  765. return writer.ToString();
  766. }
  767. internal static void ProcessUnexpectedException(
  768. string component,
  769. ITracer tracer,
  770. string messagePrefix,
  771. Exception e)
  772. {
  773. int innerHResult = 0;
  774. var flattenedException = FlattenException(e, out innerHResult);
  775. tracer.WriteError(
  776. component,
  777. Environment.NewLine + " Encountered unexpected exception " + messagePrefix + ": {0}" + Environment.NewLine +
  778. " Message: {1} HResult: 0x{2:X8}" + Environment.NewLine + "{3}",
  779. flattenedException.GetType().ToString(),
  780. flattenedException.Message,
  781. flattenedException.HResult != 0 ? flattenedException.HResult : innerHResult,
  782. flattenedException.StackTrace);
  783. // We should never arrive here
  784. Assert(false, "ProcessUnexpectedException");
  785. }
  786. internal static async Task ReadAsync(Stream stream, byte[] buffer)
  787. {
  788. int numberOfBytesRead = 0, totalNumberOfBytesToRead = buffer.Length;
  789. do
  790. {
  791. var numberOfBytesToRead = totalNumberOfBytesToRead - numberOfBytesRead;
  792. var currentNumberOfBytesRead = await stream.ReadAsync(buffer, numberOfBytesRead, numberOfBytesToRead).ConfigureAwait(false);
  793. Assert(
  794. currentNumberOfBytesRead > 0,
  795. "ReadAsync: currentNumberOfBytesRead ({0}) > 0",
  796. currentNumberOfBytesRead);
  797. numberOfBytesRead += currentNumberOfBytesRead;
  798. } while (numberOfBytesRead < totalNumberOfBytesToRead);
  799. return;
  800. }
  801. internal static string ToString(Epoch epoch)
  802. {
  803. return string.Format("{0}:{1:x}", epoch.DataLossNumber, epoch.ConfigurationNumber);
  804. }
  805. [DllImport("kernel32.dll")]
  806. private static extern void DebugBreak();
  807. [DllImport("kernel32.dll")]
  808. private static extern bool IsDebuggerPresent();
  809. [DllImport("kernel32.dll")]
  810. private static extern void OutputDebugString(string message);
  811. }
  812. }