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()); } } }