public virtual void ConfigureIndexes(IElasticClient client, IEnumerable<IElasticIndex> indexes = null) { if (indexes == null) indexes = GetIndexes(); foreach (var idx in indexes) { int currentVersion = GetAliasVersion(client, idx.AliasName); IIndicesOperationResponse response = null; var templatedIndex = idx as ITemplatedElasticIndex; if (templatedIndex != null) response = client.PutTemplate(idx.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(idx.AliasName)); else if (!client.IndexExists(idx.VersionedName).Exists) response = client.CreateIndex(idx.VersionedName, descriptor => idx.CreateIndex(descriptor).AddAlias(idx.AliasName)); Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template."); // Add existing indexes to the alias. if (!client.AliasExists(idx.AliasName).Exists) { if (templatedIndex != null) { var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(idx.VersionedName)).Select(kvp => kvp.Key).ToList(); if (indices.Count > 0) { var descriptor = new AliasDescriptor(); foreach (string name in indices) descriptor.Add(add => add.Index(name).Alias(idx.AliasName)); response = client.Alias(descriptor); } } else { response = client.Alias(a => a.Add(add => add.Index(idx.VersionedName).Alias(idx.AliasName))); } Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias."); } // already on current version if (currentVersion >= idx.Version || currentVersion < 1) continue; var reindexWorkItem = new ReindexWorkItem { OldIndex = String.Concat(idx.AliasName, "-v", currentVersion), NewIndex = idx.VersionedName, Alias = idx.AliasName, DeleteOld = true, ParentMaps = idx.GetIndexTypes() .Select(kvp => new ParentMap {Type = kvp.Value.Name, ParentPath = kvp.Value.ParentPath}) .Where(m => !String.IsNullOrEmpty(m.ParentPath)) .ToList() }; bool isReindexing = _lockProvider.IsLockedAsync(String.Concat("reindex:", reindexWorkItem.Alias, reindexWorkItem.OldIndex, reindexWorkItem.NewIndex)).Result; // already reindexing if (isReindexing) continue; // enqueue reindex to new version _lockProvider.TryUsingAsync("enqueue-reindex", () => _workItemQueue.EnqueueAsync(reindexWorkItem), TimeSpan.Zero, CancellationToken.None).Wait(); } }
public void ConfigureIndexes(IElasticClient client) { foreach (var index in GetIndexes()) { IIndicesOperationResponse response = null; int currentVersion = GetAliasVersion(client, index.Name); var templatedIndex = index as ITemplatedElasticSeachIndex; if (templatedIndex != null) response = client.PutTemplate(index.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(index.Name)); else if (!client.IndexExists(index.VersionedName).Exists) response = client.CreateIndex(index.VersionedName, descriptor => index.CreateIndex(descriptor).AddAlias(index.Name)); Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template."); // Add existing indexes to the alias. if (!client.AliasExists(index.Name).Exists) { if (templatedIndex != null) { var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(index.VersionedName)).Select(kvp => kvp.Key).ToList(); if (indices.Count > 0) { var descriptor = new AliasDescriptor(); foreach (string name in indices) descriptor.Add(add => add.Index(name).Alias(index.Name)); response = client.Alias(descriptor); } } else { response = client.Alias(a => a.Add(add => add.Index(index.VersionedName).Alias(index.Name))); } Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias."); } // already on current version if (currentVersion >= index.Version || currentVersion < 1) continue; // upgrade _lockProvider.TryUsingAsync("reindex", async () => { await _workItemQueue.EnqueueAsync(new ReindexWorkItem { OldIndex = String.Concat(index.Name, "-v", currentVersion), NewIndex = index.VersionedName, Alias = index.Name, DeleteOld = true }); }, TimeSpan.Zero, CancellationToken.None); } }
public void ConfigureIndexes(IElasticClient client) { foreach (var index in GetIndexes()) { IIndicesOperationResponse response = null; int currentVersion = GetAliasVersion(client, index.Name); var templatedIndex = index as ITemplatedElasticSeachIndex; if (templatedIndex != null) { response = client.PutTemplate(index.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(index.Name)); } else if (!client.IndexExists(index.VersionedName).Exists) { response = client.CreateIndex(index.VersionedName, descriptor => index.CreateIndex(descriptor).AddAlias(index.Name)); } Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template."); // Add existing indexes to the alias. if (!client.AliasExists(index.Name).Exists) { if (templatedIndex != null) { var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(index.VersionedName)).Select(kvp => kvp.Key).ToList(); if (indices.Count > 0) { var descriptor = new AliasDescriptor(); foreach (string name in indices) { descriptor.Add(add => add.Index(name).Alias(index.Name)); } response = client.Alias(descriptor); } } else { response = client.Alias(a => a.Add(add => add.Index(index.VersionedName).Alias(index.Name))); } Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias."); } // already on current version if (currentVersion >= index.Version || currentVersion < 1) { continue; } // upgrade _lockProvider.TryUsingAsync("reindex", async() => { await _workItemQueue.EnqueueAsync(new ReindexWorkItem { OldIndex = String.Concat(index.Name, "-v", currentVersion), NewIndex = index.VersionedName, Alias = index.Name, DeleteOld = true }); }, TimeSpan.Zero, CancellationToken.None); } }
public virtual void ConfigureIndexes(IElasticClient client, IEnumerable <IElasticIndex> indexes = null) { if (indexes == null) { indexes = GetIndexes(); } foreach (var idx in indexes) { int currentVersion = GetAliasVersion(client, idx.AliasName); IIndicesOperationResponse response = null; var templatedIndex = idx as ITemplatedElasticIndex; if (templatedIndex != null) { response = client.PutTemplate(idx.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(idx.AliasName)); } else if (!client.IndexExists(idx.VersionedName).Exists) { response = client.CreateIndex(idx.VersionedName, descriptor => idx.CreateIndex(descriptor).AddAlias(idx.AliasName)); } Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template."); // Add existing indexes to the alias. if (!client.AliasExists(idx.AliasName).Exists) { if (templatedIndex != null) { var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(idx.VersionedName)).Select(kvp => kvp.Key).ToList(); if (indices.Count > 0) { var descriptor = new AliasDescriptor(); foreach (string name in indices) { descriptor.Add(add => add.Index(name).Alias(idx.AliasName)); } response = client.Alias(descriptor); } } else { response = client.Alias(a => a.Add(add => add.Index(idx.VersionedName).Alias(idx.AliasName))); } Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias."); } // already on current version if (currentVersion >= idx.Version || currentVersion < 1) { continue; } var reindexWorkItem = new ReindexWorkItem { OldIndex = String.Concat(idx.AliasName, "-v", currentVersion), NewIndex = idx.VersionedName, Alias = idx.AliasName, DeleteOld = true, ParentMaps = idx.GetIndexTypes() .Select(kvp => new ParentMap { Type = kvp.Value.Name, ParentPath = kvp.Value.ParentPath }) .Where(m => !String.IsNullOrEmpty(m.ParentPath)) .ToList() }; bool isReindexing = _lockProvider.IsLockedAsync(String.Concat("reindex:", reindexWorkItem.Alias, reindexWorkItem.OldIndex, reindexWorkItem.NewIndex)).Result; // already reindexing if (isReindexing) { continue; } // enqueue reindex to new version _lockProvider.TryUsingAsync("enqueue-reindex", () => _workItemQueue.EnqueueAsync(reindexWorkItem), TimeSpan.Zero, CancellationToken.None).Wait(); } }
protected virtual async Task UpdateAliasesAsync(IList <IndexInfo> indexes) { if (indexes.Count == 0) { return; } var aliasDescriptor = new AliasDescriptor(); foreach (var indexGroup in indexes.OrderBy(i => i.Version).GroupBy(i => i.DateUtc)) { var indexExpirationDate = GetIndexExpirationDate(indexGroup.Key); // Ensure the current version is always set. if (SystemClock.UtcNow < indexExpirationDate) { var oldestIndex = indexGroup.First(); if (oldestIndex.CurrentVersion < 0) { try { await CreateAliasAsync(oldestIndex.Index, GetIndex(indexGroup.Key)).AnyContext(); } catch (Exception ex) { _logger.Error(ex, $"Error setting current index version. Will use oldest index version: {oldestIndex.Version}"); } foreach (var indexInfo in indexGroup) { indexInfo.CurrentVersion = oldestIndex.Version; } } } foreach (var index in indexGroup) { if (SystemClock.UtcNow >= indexExpirationDate || index.Version != index.CurrentVersion) { foreach (var alias in Aliases) { aliasDescriptor = aliasDescriptor.Remove(r => r.Index(index.Index).Alias(alias.Name)); } continue; } foreach (var alias in Aliases) { if (ShouldCreateAlias(indexGroup.Key, alias)) { aliasDescriptor = aliasDescriptor.Add(r => r.Index(index.Index).Alias(alias.Name)); } else { aliasDescriptor = aliasDescriptor.Remove(r => r.Index(index.Index).Alias(alias.Name)); } } } } var response = await Configuration.Client.AliasAsync(aliasDescriptor).AnyContext(); _logger.Trace(() => response.GetRequest()); if (!response.IsValid) { if (response.ConnectionStatus.HttpStatusCode.GetValueOrDefault() == 404) { return; } string message = $"Error updating aliases: {response.GetErrorMessage()}"; _logger.Error().Exception(response.ConnectionStatus.OriginalException).Message(message).Property("request", response.GetRequest()).Write(); throw new ApplicationException(message, response.ConnectionStatus.OriginalException); } }