/ToMigrate/Raven.Tests/Storage/Voron/IndexingStorageActionsTests.cs

https://github.com/fitzchak/ravendb · C# · 490 lines · 414 code · 70 blank · 6 comment · 5 complexity · c0a90a177fabfadc1ce4344bbe1551d1 MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="IndexingStorageActionsTests.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System.Diagnostics;
  7. using Raven.Json.Linq;
  8. using Raven.Tests.Common;
  9. namespace Raven.Tests.Storage.Voron
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading;
  15. using Microsoft.Isam.Esent.Interop;
  16. using Raven.Abstractions;
  17. using Raven.Abstractions.Data;
  18. using Raven.Database.Indexing;
  19. using Xunit;
  20. using Xunit.Extensions;
  21. using global::Voron.Exceptions;
  22. [Trait("VoronTest", "StorageActionsTests")]
  23. public class IndexingStorageActionsTests : TransactionalStorageTestBase
  24. {
  25. [Theory]
  26. [PropertyData("Storages")]
  27. public void IndexCreation_In_DifferentBatches(string requestedStorage)
  28. {
  29. using (var storage = NewTransactionalStorage(requestedStorage))
  30. {
  31. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  32. //make sure that index already exists check works correctly
  33. Assert.DoesNotThrow(() => storage.Batch(accessor => accessor.Indexing.AddIndex(202, false)));
  34. }
  35. }
  36. [Theory]
  37. [PropertyData("Storages")]
  38. public void IndexCreation(string requestedStorage)
  39. {
  40. using (var storage = NewTransactionalStorage(requestedStorage))
  41. {
  42. storage.Batch(accessor =>
  43. {
  44. accessor.Indexing.AddIndex(101, false);
  45. accessor.Indexing.AddIndex(202, true);
  46. });
  47. storage.Batch(accessor =>
  48. {
  49. var stats = accessor.Indexing.GetIndexesStats().ToList();
  50. Assert.Equal(2, stats.Count);
  51. var stat1 = stats[0];
  52. Assert.Equal(101, stat1.Id);
  53. Assert.Equal(0, stat1.IndexingAttempts);
  54. Assert.Equal(0, stat1.IndexingSuccesses);
  55. Assert.Equal(0, stat1.IndexingErrors);
  56. Assert.Equal(IndexingPriority.Normal, stat1.Priority);
  57. Assert.Equal(0, stat1.TouchCount);
  58. Assert.Equal(UtcNow, stat1.CreatedTimestamp);
  59. Assert.Equal(DateTime.MinValue, stat1.LastIndexingTime);
  60. Assert.Null(stat1.ReduceIndexingAttempts);
  61. Assert.Null(stat1.ReduceIndexingSuccesses);
  62. Assert.Null(stat1.ReduceIndexingErrors);
  63. Assert.Null(stat1.LastReducedEtag);
  64. Assert.Null(stat1.LastReducedTimestamp);
  65. Assert.Equal(Etag.Empty, stat1.LastIndexedEtag);
  66. Assert.Equal(DateTime.MinValue, stat1.LastIndexedTimestamp);
  67. var stat2 = stats[1];
  68. Assert.Equal(202, stat2.Id);
  69. Assert.Equal(0, stat2.IndexingAttempts);
  70. Assert.Equal(0, stat2.IndexingSuccesses);
  71. Assert.Equal(0, stat2.IndexingErrors);
  72. Assert.Equal(IndexingPriority.Normal, stat2.Priority);
  73. Assert.Equal(0, stat2.TouchCount);
  74. Assert.Equal(UtcNow, stat2.CreatedTimestamp);
  75. Assert.Equal(DateTime.MinValue, stat2.LastIndexingTime);
  76. Assert.Equal(0, stat2.ReduceIndexingAttempts);
  77. Assert.Equal(0, stat2.ReduceIndexingSuccesses);
  78. Assert.Equal(0, stat2.ReduceIndexingErrors);
  79. Assert.Equal(Etag.Empty, stat2.LastReducedEtag);
  80. Assert.Equal(DateTime.MinValue, stat2.LastReducedTimestamp);
  81. Assert.Equal(Etag.Empty, stat2.LastIndexedEtag);
  82. Assert.Equal(DateTime.MinValue, stat2.LastIndexedTimestamp);
  83. });
  84. storage.Batch(accessor =>
  85. {
  86. accessor.Indexing.PrepareIndexForDeletion(101);
  87. accessor.Indexing.PrepareIndexForDeletion(202);
  88. accessor.Indexing.DeleteIndex(101, new CancellationToken());
  89. accessor.Indexing.DeleteIndex(202, new CancellationToken());
  90. });
  91. storage.Batch(accessor =>
  92. {
  93. var stats = accessor.Indexing.GetIndexesStats().ToList();
  94. Assert.Equal(0, stats.Count);
  95. });
  96. }
  97. }
  98. [Theory]
  99. [PropertyData("Storages")]
  100. public void CannotAddDuplicateIndex(string requestedStorage)
  101. {
  102. Type exception1Type = null;
  103. Type exception2Type = null;
  104. string exception1Message = null;
  105. string exception2Message = null;
  106. if (requestedStorage == "esent")
  107. {
  108. exception1Type = typeof(EsentKeyDuplicateException);
  109. exception1Message = "Illegal duplicate key";
  110. exception2Type = typeof(EsentKeyDuplicateException);
  111. exception2Message = "Illegal duplicate key";
  112. }
  113. else if (requestedStorage == "voron")
  114. {
  115. exception1Type = typeof(ArgumentException);
  116. exception1Message = "There is already an index with the name: '101'";
  117. exception2Type = typeof(ArgumentException);
  118. exception2Message = "There is already an index with the name: '202'";
  119. }
  120. using (var storage = NewTransactionalStorage(requestedStorage))
  121. {
  122. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  123. var e1 = Assert.Throws(exception1Type, () => storage.Batch(accessor => accessor.Indexing.AddIndex(101, false)));
  124. Assert.Equal(exception1Message, e1.Message);
  125. var e2 = Assert.Throws(exception2Type,
  126. () => storage.Batch(
  127. accessor =>
  128. {
  129. accessor.Indexing.AddIndex(202, false);
  130. accessor.Indexing.AddIndex(202, true);
  131. }));
  132. Assert.Equal(exception2Message, e2.Message);
  133. }
  134. }
  135. [Theory]
  136. [PropertyData("Storages")]
  137. public void IndexStats(string requestedStorage)
  138. {
  139. using (var storage = NewTransactionalStorage(requestedStorage))
  140. {
  141. storage.Batch(
  142. accessor =>
  143. {
  144. accessor.Indexing.AddIndex(101, false);
  145. accessor.Indexing.AddIndex(202, true);
  146. });
  147. storage.Batch(accessor =>
  148. {
  149. var stats = accessor.Indexing.GetIndexesStats().ToList();
  150. Assert.Equal(2, stats.Count);
  151. var stat1 = accessor.Indexing.GetIndexStats(101);
  152. Assert.Equal(101, stat1.Id);
  153. Assert.Equal(0, stat1.IndexingAttempts);
  154. Assert.Equal(0, stat1.IndexingSuccesses);
  155. Assert.Equal(0, stat1.IndexingErrors);
  156. Assert.Equal(IndexingPriority.Normal, stat1.Priority);
  157. Assert.Equal(0, stat1.TouchCount);
  158. Assert.Equal(UtcNow, stat1.CreatedTimestamp);
  159. Assert.Equal(DateTime.MinValue, stat1.LastIndexingTime);
  160. Assert.Null(stat1.ReduceIndexingAttempts);
  161. Assert.Null(stat1.ReduceIndexingSuccesses);
  162. Assert.Null(stat1.ReduceIndexingErrors);
  163. Assert.Null(stat1.LastReducedEtag);
  164. Assert.Null(stat1.LastReducedTimestamp);
  165. Assert.Equal(Etag.Empty, stat1.LastIndexedEtag);
  166. Assert.Equal(DateTime.MinValue, stat1.LastIndexedTimestamp);
  167. var stat2 = accessor.Indexing.GetIndexStats(202);
  168. Assert.Equal(202, stat2.Id);
  169. Assert.Equal(0, stat2.IndexingAttempts);
  170. Assert.Equal(0, stat2.IndexingSuccesses);
  171. Assert.Equal(0, stat2.IndexingErrors);
  172. Assert.Equal(IndexingPriority.Normal, stat2.Priority);
  173. Assert.Equal(0, stat2.TouchCount);
  174. Assert.Equal(UtcNow, stat2.CreatedTimestamp);
  175. Assert.Equal(DateTime.MinValue, stat2.LastIndexingTime);
  176. Assert.Equal(0, stat2.ReduceIndexingAttempts);
  177. Assert.Equal(0, stat2.ReduceIndexingSuccesses);
  178. Assert.Equal(0, stat2.ReduceIndexingErrors);
  179. Assert.Equal(Etag.Empty, stat2.LastReducedEtag);
  180. Assert.Equal(DateTime.MinValue, stat2.LastReducedTimestamp);
  181. Assert.Equal(Etag.Empty, stat2.LastIndexedEtag);
  182. Assert.Equal(DateTime.MinValue, stat2.LastIndexedTimestamp);
  183. });
  184. }
  185. }
  186. [Theory]
  187. [PropertyData("Storages")]
  188. public void IndexPriority(string requestedStorage)
  189. {
  190. using (var storage = NewTransactionalStorage(requestedStorage))
  191. {
  192. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  193. storage.Batch(
  194. accessor =>
  195. {
  196. var stats = accessor.Indexing.GetIndexStats(101);
  197. Assert.Equal(IndexingPriority.Normal, stats.Priority);
  198. accessor.Indexing.SetIndexPriority(101, IndexingPriority.Forced);
  199. });
  200. storage.Batch(
  201. accessor =>
  202. {
  203. var stats = accessor.Indexing.GetIndexStats(101);
  204. Assert.Equal(IndexingPriority.Forced, stats.Priority);
  205. });
  206. }
  207. }
  208. [Theory]
  209. [PropertyData("Storages")]
  210. public void FailureRate(string requestedStorage)
  211. {
  212. using (var storage = NewTransactionalStorage(requestedStorage))
  213. {
  214. storage.Batch(
  215. accessor =>
  216. {
  217. accessor.Indexing.AddIndex(101, false);
  218. accessor.Indexing.AddIndex(202, true);
  219. });
  220. storage.Batch(accessor =>
  221. {
  222. var rate1 = accessor.Indexing.GetFailureRate(101);
  223. Assert.NotNull(rate1);
  224. Assert.Equal(0, rate1.Attempts);
  225. Assert.Equal(0, rate1.Errors);
  226. Assert.Equal(0, rate1.Successes);
  227. Assert.Null(rate1.ReduceAttempts);
  228. Assert.Null(rate1.ReduceErrors);
  229. Assert.Null(rate1.ReduceSuccesses);
  230. var rate2 = accessor.Indexing.GetFailureRate(202);
  231. Assert.NotNull(rate2);
  232. Assert.Equal(0, rate2.Attempts);
  233. Assert.Equal(0, rate2.Errors);
  234. Assert.Equal(0, rate2.Successes);
  235. Assert.Equal(0, rate2.ReduceAttempts);
  236. Assert.Equal(0, rate2.ReduceErrors);
  237. Assert.Equal(0, rate2.ReduceSuccesses);
  238. });
  239. }
  240. }
  241. [Theory]
  242. [PropertyData("Storages")]
  243. public void UpdateLastIndexed(string requestedStorage)
  244. {
  245. using (var storage = NewTransactionalStorage(requestedStorage))
  246. {
  247. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  248. var etag = new Etag(Guid.NewGuid().ToString());
  249. var date = DateTime.Now.AddDays(1);
  250. storage.Batch(accessor => accessor.Indexing.UpdateLastIndexed(101, etag, date));
  251. storage.Batch(accessor =>
  252. {
  253. var stat1 = accessor.Indexing.GetIndexStats(101);
  254. Assert.Equal(101, stat1.Id);
  255. Assert.Equal(etag, stat1.LastIndexedEtag);
  256. Assert.Equal(date, stat1.LastIndexedTimestamp);
  257. });
  258. }
  259. }
  260. [Theory]
  261. [PropertyData("Storages")]
  262. public void UpdateLastReduced(string requestedStorage)
  263. {
  264. using (var storage = NewTransactionalStorage(requestedStorage))
  265. {
  266. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  267. var etag = new Etag().Setup(UuidType.ScheduledReductions, 2);
  268. var date = DateTime.Now.AddDays(1);
  269. storage.Batch(accessor => accessor.Indexing.UpdateLastReduced(101, etag, date));
  270. storage.Batch(accessor =>
  271. {
  272. var stat1 = accessor.Indexing.GetIndexStats(101);
  273. Assert.Equal(101, stat1.Id);
  274. Assert.Equal(etag, stat1.LastReducedEtag);
  275. Assert.Equal(date, stat1.LastReducedTimestamp);
  276. });
  277. }
  278. }
  279. [Theory]
  280. [PropertyData("Storages")]
  281. public void TouchIndexEtag(string requestedStorage)
  282. {
  283. using (var storage = NewTransactionalStorage(requestedStorage))
  284. {
  285. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  286. storage.Batch(accessor =>
  287. {
  288. var stat1 = accessor.Indexing.GetIndexStats(101);
  289. Assert.Equal(101, stat1.Id);
  290. Assert.Equal(0, stat1.TouchCount);
  291. });
  292. storage.Batch(accessor => accessor.Indexing.TouchIndexEtag(101));
  293. storage.Batch(accessor =>
  294. {
  295. var stat1 = accessor.Indexing.GetIndexStats(101);
  296. Assert.Equal(101, stat1.Id);
  297. Assert.Equal(1, stat1.TouchCount);
  298. });
  299. storage.Batch(accessor => accessor.Indexing.TouchIndexEtag(101));
  300. storage.Batch(accessor =>
  301. {
  302. var stat1 = accessor.Indexing.GetIndexStats(101);
  303. Assert.Equal(101, stat1.Id);
  304. Assert.Equal(2, stat1.TouchCount);
  305. });
  306. }
  307. }
  308. [Theory]
  309. [PropertyData("Storages")]
  310. public void UpdateIndexingStats(string requestedStorage)
  311. {
  312. using (var storage = NewTransactionalStorage(requestedStorage))
  313. {
  314. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  315. storage.Batch(accessor => accessor.Indexing.UpdateIndexingStats(101, new IndexingWorkStats
  316. {
  317. IndexingAttempts = 11,
  318. IndexingErrors = 3,
  319. IndexingSuccesses = 2,
  320. Operation = IndexingWorkStats.Status.Reduce,
  321. ReduceAttempts = 6,
  322. ReduceErrors = 7,
  323. ReduceSuccesses = 9
  324. }));
  325. storage.Batch(accessor =>
  326. {
  327. var stat = accessor.Indexing.GetIndexStats(101);
  328. Assert.NotNull(stat);
  329. Assert.Equal(11, stat.IndexingAttempts);
  330. Assert.Equal(3, stat.IndexingErrors);
  331. Assert.Equal(2, stat.IndexingSuccesses);
  332. Assert.Equal(UtcNow, stat.LastIndexingTime);
  333. Assert.Equal(0, stat.ReduceIndexingAttempts);
  334. Assert.Equal(0, stat.ReduceIndexingErrors);
  335. Assert.Equal(0, stat.ReduceIndexingSuccesses);
  336. });
  337. }
  338. }
  339. [Theory]
  340. [PropertyData("Storages")]
  341. public void UpdateReduceStats(string requestedStorage)
  342. {
  343. using (var storage = NewTransactionalStorage(requestedStorage))
  344. {
  345. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  346. storage.Batch(accessor => accessor.Indexing.UpdateReduceStats(101, new IndexingWorkStats
  347. {
  348. IndexingAttempts = 11,
  349. IndexingErrors = 3,
  350. IndexingSuccesses = 2,
  351. Operation = IndexingWorkStats.Status.Reduce,
  352. ReduceAttempts = 6,
  353. ReduceErrors = 7,
  354. ReduceSuccesses = 9
  355. }));
  356. storage.Batch(accessor =>
  357. {
  358. var stat = accessor.Indexing.GetIndexStats(101);
  359. Assert.NotNull(stat);
  360. Assert.Equal(0, stat.IndexingAttempts);
  361. Assert.Equal(0, stat.IndexingErrors);
  362. Assert.Equal(0, stat.IndexingSuccesses);
  363. Assert.Equal(6, stat.ReduceIndexingAttempts);
  364. Assert.Equal(7, stat.ReduceIndexingErrors);
  365. Assert.Equal(9, stat.ReduceIndexingSuccesses);
  366. });
  367. }
  368. }
  369. [Theory]
  370. [PropertyData("Storages")]
  371. public void DocumentReferences1(string requestedStorage)
  372. {
  373. using (var storage = NewTransactionalStorage(requestedStorage))
  374. {
  375. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  376. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key1", new HashSet<string>()));
  377. storage.Batch(accessor =>
  378. {
  379. var references = accessor.Indexing.GetDocumentsReferencesFrom("key1").ToList();
  380. Assert.Equal(0, references.Count);
  381. });
  382. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key1", new HashSet<string> { "key2", "key3" }));
  383. storage.Batch(accessor =>
  384. {
  385. var references = accessor.Indexing.GetDocumentsReferencesFrom("key1").ToList();
  386. Assert.Equal(2, references.Count);
  387. Assert.True(references.Contains("key2"));
  388. Assert.True(references.Contains("key3"));
  389. });
  390. storage.Batch(accessor => accessor.Indexing.RemoveAllDocumentReferencesFrom("key1"));
  391. storage.Batch(accessor =>
  392. {
  393. var references = accessor.Indexing.GetDocumentsReferencesFrom("key1").ToList();
  394. Assert.Equal(0, references.Count);
  395. });
  396. }
  397. }
  398. [Theory]
  399. [PropertyData("Storages")]
  400. public void DocumentReferences2(string requestedStorage)
  401. {
  402. using (var storage = NewTransactionalStorage(requestedStorage))
  403. {
  404. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  405. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key1", new HashSet<string> { "key2", "key3" }));
  406. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key2", new HashSet<string> { "key1", "key3" }));
  407. storage.Batch(accessor =>
  408. {
  409. var documents = accessor.Indexing.GetDocumentsReferencing("key3").ToList();
  410. Assert.Equal(2, documents.Count);
  411. Assert.True(documents.Contains("key1"));
  412. Assert.True(documents.Contains("key2"));
  413. Assert.Equal(documents.Count, accessor.Indexing.GetCountOfDocumentsReferencing("key3"));
  414. Assert.Equal(1, accessor.Indexing.GetCountOfDocumentsReferencing("key1"));
  415. Assert.Equal(1, accessor.Indexing.GetCountOfDocumentsReferencing("key2"));
  416. });
  417. }
  418. }
  419. }
  420. }