public async Task CanCreateDailyAliases(DateTime utcNow) { SystemClock.SetFixedTime(utcNow); var index = new DailyEmployeeIndex(_configuration, 1); await index.DeleteAsync(); using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) { await index.ConfigureAsync(); var repository = new EmployeeRepository(index.Employee); for (int i = 0; i < 35; i += 5) { var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(i))); Assert.NotNull(employee?.Id); Assert.Equal(1, await index.GetCurrentVersionAsync()); var existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc)); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); var aliasesResponse = await _client.GetAliasesAsync(a => a.Index(index.GetIndex(employee.CreatedUtc))); _logger.Trace(() => aliasesResponse.GetRequest()); Assert.True(aliasesResponse.IsValid); Assert.Equal(1, aliasesResponse.Indices.Count); var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList(); aliases.Sort(); Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases)); } } }
public async Task MaintainDailyIndexes() { var index = new DailyEmployeeIndex(_configuration, 1); await index.DeleteAsync(); using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) { await index.ConfigureAsync(); var repository = new EmployeeRepository(index.Employee); SystemClock.AdjustTime(TimeSpan.FromDays(15)); var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: SystemClock.UtcNow)); Assert.NotNull(employee?.Id); await _client.RefreshAsync(); await index.MaintainAsync(); Assert.Equal(1, await index.GetCurrentVersionAsync()); var existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc)); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); var aliasesResponse = await _client.GetAliasesAsync(a => a.Index(index.GetIndex(employee.CreatedUtc))); _logger.Trace(() => aliasesResponse.GetRequest()); Assert.True(aliasesResponse.IsValid); Assert.Equal(1, aliasesResponse.Indices.Count); var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList(); aliases.Sort(); Assert.Equal(GetExpectedEmployeeDailyAliases(index, SystemClock.UtcNow, employee.CreatedUtc), String.Join(", ", aliases)); SystemClock.AdjustTime(TimeSpan.FromDays(9)); index.MaxIndexAge = TimeSpan.FromDays(10); await index.MaintainAsync(); existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc)); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); aliasesResponse = await _client.GetAliasesAsync(a => a.Index(index.GetIndex(employee.CreatedUtc))); _logger.Trace(() => aliasesResponse.GetRequest()); Assert.True(aliasesResponse.IsValid); Assert.Equal(1, aliasesResponse.Indices.Count); aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList(); aliases.Sort(); Assert.Equal(GetExpectedEmployeeDailyAliases(index, SystemClock.UtcNow, employee.CreatedUtc), String.Join(", ", aliases)); SystemClock.Reset(); await index.MaintainAsync(); existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc)); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.False(existsResponse.Exists); aliasesResponse = await _client.GetAliasesAsync(a => a.Index(index.GetIndex(employee.CreatedUtc))); _logger.Trace(() => aliasesResponse.GetRequest()); Assert.True(aliasesResponse.IsValid); Assert.Equal(0, aliasesResponse.Indices.Count); } }
public async Task MaintainWillCreateAliasesOnTimeSeriesIndex() { SystemClock.SetFixedTime(SystemClock.UtcNow); var version1Index = new DailyEmployeeIndex(_configuration, 1); await version1Index.DeleteAsync(); var version2Index = new DailyEmployeeIndex(_configuration, 2); await version2Index.DeleteAsync(); // Indexes don't exist yet so the current version will be the index version. Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); Assert.Equal(2, await version2Index.GetCurrentVersionAsync()); using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) { await version1Index.ConfigureAsync(); await version1Index.EnsureIndexAsync(SystemClock.UtcNow); Assert.True(_client.IndexExists(version1Index.GetVersionedIndex(SystemClock.UtcNow)).Exists); Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); // delete all aliases await _configuration.Cache.RemoveAllAsync(); await DeleteAliases(version1Index.GetVersionedIndex(SystemClock.UtcNow)); using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) { await version2Index.ConfigureAsync(); await version2Index.EnsureIndexAsync(SystemClock.UtcNow); Assert.True(_client.IndexExists(version2Index.GetVersionedIndex(SystemClock.UtcNow)).Exists); Assert.Equal(2, await version2Index.GetCurrentVersionAsync()); // delete all aliases await _configuration.Cache.RemoveAllAsync(); await DeleteAliases(version2Index.GetVersionedIndex(SystemClock.UtcNow)); await _client.RefreshAsync(); var aliasesResponse = await _client.GetAliasesAsync(a => a.Indices(version1Index.GetVersionedIndex(SystemClock.UtcNow), version2Index.GetVersionedIndex(SystemClock.UtcNow))); Assert.Equal(0, aliasesResponse.Indices.SelectMany(i => i.Value).Count()); // Indexes exist but no alias so the oldest index version will be used. Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); Assert.Equal(1, await version2Index.GetCurrentVersionAsync()); await version1Index.MaintainAsync(); aliasesResponse = await _client.GetAliasesAsync(a => a.Indices(version1Index.GetVersionedIndex(SystemClock.UtcNow))); Assert.Equal(version1Index.Aliases.Count + 1, aliasesResponse.Indices.Single().Value.Count); aliasesResponse = await _client.GetAliasesAsync(a => a.Indices(version2Index.GetVersionedIndex(SystemClock.UtcNow))); Assert.Equal(0, aliasesResponse.Indices.Single().Value.Count); Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); Assert.Equal(1, await version2Index.GetCurrentVersionAsync()); } } }
public async Task CanReindexTimeSeriesIndexWithCorrectMappings() { var version1Index = new DailyEmployeeIndex(_configuration, 1); await version1Index.DeleteAsync(); var version2Index = new DailyEmployeeIndex(_configuration, 2); version2Index.DiscardIndexesOnReindex = false; await version2Index.DeleteAsync(); using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) { await version1Index.ConfigureAsync(); var version1Repository = new EmployeeRepository(version1Index.Employee); var utcNow = SystemClock.UtcNow; var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow)); Assert.NotNull(employee?.Id); await _client.RefreshAsync(); using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) { await version2Index.ConfigureAsync(); await version2Index.ReindexAsync(); var existsResponse = await _client.IndexExistsAsync(d => d.Index(version1Index.GetVersionedIndex(utcNow, 1))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); var mappingResponse = await _client.GetMappingAsync<Employee>(m => m.Index(version1Index.GetVersionedIndex(utcNow, 1))); _logger.Trace(() => mappingResponse.GetRequest()); Assert.True(mappingResponse.IsValid); Assert.NotNull(mappingResponse.Mappings); existsResponse = await _client.IndexExistsAsync(d => d.Index(version2Index.GetVersionedIndex(utcNow, 2))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); string version1Mappings = ToJson(mappingResponse.Mappings); mappingResponse = await _client.GetMappingAsync<Employee>(m => m.Index(version1Index.GetVersionedIndex(utcNow, 2))); _logger.Trace(() => mappingResponse.GetRequest()); Assert.True(mappingResponse.IsValid); Assert.NotNull(mappingResponse.Mappings); Assert.Equal(version1Mappings, ToJson(mappingResponse.Mappings).Replace("-v2", "-v1")); } } }
public async Task CanReindexTimeSeriesIndex() { var version1Index = new DailyEmployeeIndex(_configuration, 1); await version1Index.DeleteAsync(); var version2Index = new DailyEmployeeIndex(_configuration, 2); await version2Index.DeleteAsync(); using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) { await version1Index.ConfigureAsync(); var version1Repository = new EmployeeRepository(version1Index.Employee); var utcNow = SystemClock.UtcNow; var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow)); Assert.NotNull(employee?.Id); await _client.RefreshAsync(); Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); var aliasCountResponse = await _client.CountAsync(d => d.Index(version1Index.Name)); _logger.Trace(() => aliasCountResponse.GetRequest()); Assert.True(aliasCountResponse.IsValid); Assert.Equal(1, aliasCountResponse.Count); var indexCountResponse = await _client.CountAsync(d => d.Index(version1Index.GetIndex(utcNow))); _logger.Trace(() => indexCountResponse.GetRequest()); Assert.True(indexCountResponse.IsValid); Assert.Equal(1, indexCountResponse.Count); indexCountResponse = await _client.CountAsync(d => d.Index(version1Index.GetVersionedIndex(utcNow, 1))); _logger.Trace(() => indexCountResponse.GetRequest()); Assert.True(indexCountResponse.IsValid); Assert.Equal(1, indexCountResponse.Count); using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) { await version2Index.ConfigureAsync(); Assert.Equal(1, await version2Index.GetCurrentVersionAsync()); var version2Repository = new EmployeeRepository(version2Index.Employee); // Make sure we write to the old index. await version2Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow)); await _client.RefreshAsync(); aliasCountResponse = await _client.CountAsync(d => d.Index(version1Index.Name)); _logger.Trace(() => aliasCountResponse.GetRequest()); Assert.True(aliasCountResponse.IsValid); Assert.Equal(2, aliasCountResponse.Count); indexCountResponse = await _client.CountAsync(d => d.Index(version1Index.GetVersionedIndex(utcNow, 1))); _logger.Trace(() => indexCountResponse.GetRequest()); Assert.True(indexCountResponse.IsValid); Assert.Equal(2, indexCountResponse.Count); var existsResponse = await _client.IndexExistsAsync(d => d.Index(version2Index.GetVersionedIndex(utcNow, 2))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.False(existsResponse.Exists); // alias should still point to the old version until reindex var aliasesResponse = await _client.GetAliasesAsync(a => a.Index(version1Index.GetIndex(employee.CreatedUtc))); _logger.Trace(() => aliasesResponse.GetRequest()); Assert.True(aliasesResponse.IsValid); Assert.Equal(version1Index.GetVersionedIndex(employee.CreatedUtc, 1), aliasesResponse.Indices.Single().Key); var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList(); aliases.Sort(); Assert.Equal(GetExpectedEmployeeDailyAliases(version1Index, utcNow, employee.CreatedUtc), String.Join(", ", aliases)); await version2Index.ReindexAsync(); Assert.Equal(2, await version1Index.GetCurrentVersionAsync()); Assert.Equal(2, await version2Index.GetCurrentVersionAsync()); aliasesResponse = await _client.GetAliasesAsync(a => a.Index(version1Index.GetIndex(employee.CreatedUtc))); _logger.Trace(() => aliasesResponse.GetRequest()); Assert.True(aliasesResponse.IsValid); Assert.Equal(version1Index.GetVersionedIndex(employee.CreatedUtc, 2), aliasesResponse.Indices.Single().Key); aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList(); aliases.Sort(); Assert.Equal(GetExpectedEmployeeDailyAliases(version1Index, utcNow, employee.CreatedUtc), String.Join(", ", aliases)); existsResponse = await _client.IndexExistsAsync(d => d.Index(version1Index.GetVersionedIndex(utcNow, 1))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.False(existsResponse.Exists); existsResponse = await _client.IndexExistsAsync(d => d.Index(version2Index.GetVersionedIndex(utcNow, 2))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); } } }
public async Task DailyIndexMaxAge(DateTime utcNow) { SystemClock.SetFixedTime(utcNow); var index = new DailyEmployeeIndex(_configuration, 1); index.MaxIndexAge = TimeSpan.FromDays(1); await index.DeleteAsync(); using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) { await index.ConfigureAsync(); await index.EnsureIndexAsync(utcNow); var existsResponse = await _client.IndexExistsAsync(index.GetIndex(utcNow)); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); await index.EnsureIndexAsync(utcNow.SubtractDays(1)); existsResponse = await _client.IndexExistsAsync(index.GetIndex(utcNow.SubtractDays(1))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.True(existsResponse.Exists); await Assert.ThrowsAsync<ArgumentException>(async () => await index.EnsureIndexAsync(utcNow.SubtractDays(2))); existsResponse = await _client.IndexExistsAsync(index.GetIndex(utcNow.SubtractDays(2))); _logger.Trace(() => existsResponse.GetRequest()); Assert.True(existsResponse.IsValid); Assert.False(existsResponse.Exists); } }