From c8c4004fe3e068e7ba96bda33868083c3dee98d1 Mon Sep 17 00:00:00 2001 From: Bourne Shi Date: Fri, 20 Sep 2024 17:18:38 +0800 Subject: [PATCH] feat: remove route key related handling logic --- .../AElfEntityMappingElasticsearchModule.cs | 2 +- .../ElasticsearchCollectionNameProvider.cs | 13 +- .../Repositories/ElasticsearchRepository.cs | 449 ++++++------- .../Services/ElasticIndexService.cs | 36 +- .../Services/EnsureIndexBuildService.cs | 6 +- .../Services/IElasticIndexService.cs | 4 +- .../Sharding/CollectionRouteKeyProvider.cs | 610 +++++++++--------- .../Sharding/ICollectionRouteKeyProvider.cs | 54 +- ...lasticsearchCollectionNameProviderTests.cs | 6 +- .../ElasticsearchRepositoryTests.cs | 30 +- .../CollectionRouteKeyProviderTests.cs | 260 ++++---- 11 files changed, 735 insertions(+), 735 deletions(-) diff --git a/src/AElf.EntityMapping.Elasticsearch/AElfEntityMappingElasticsearchModule.cs b/src/AElf.EntityMapping.Elasticsearch/AElfEntityMappingElasticsearchModule.cs index dc333940..083c4928 100644 --- a/src/AElf.EntityMapping.Elasticsearch/AElfEntityMappingElasticsearchModule.cs +++ b/src/AElf.EntityMapping.Elasticsearch/AElfEntityMappingElasticsearchModule.cs @@ -24,7 +24,7 @@ public override void ConfigureServices(ServiceConfigurationContext context) services.AddTransient(typeof(IElasticsearchRepository<,>), typeof(ElasticsearchRepository<,>)); services.AddTransient(typeof(ICollectionNameProvider<>), typeof(ElasticsearchCollectionNameProvider<>)); services.AddSingleton(typeof (IShardingKeyProvider<>), typeof (ShardingKeyProvider<>)); - services.AddSingleton(typeof(ICollectionRouteKeyProvider<>), typeof(CollectionRouteKeyProvider<>)); + // services.AddSingleton(typeof(ICollectionRouteKeyProvider<>), typeof(CollectionRouteKeyProvider<>)); services.AddSingleton(typeof(IElasticsearchQueryableFactory<>), typeof(ElasticsearchQueryableFactory<>)); services.AddSingleton(typeof (IShardingCollectionTailProvider<>), typeof (ShardingCollectionTailProvider<>)); var configuration = context.Services.GetConfiguration(); diff --git a/src/AElf.EntityMapping.Elasticsearch/ElasticsearchCollectionNameProvider.cs b/src/AElf.EntityMapping.Elasticsearch/ElasticsearchCollectionNameProvider.cs index 2d45ff20..f9f68744 100644 --- a/src/AElf.EntityMapping.Elasticsearch/ElasticsearchCollectionNameProvider.cs +++ b/src/AElf.EntityMapping.Elasticsearch/ElasticsearchCollectionNameProvider.cs @@ -12,19 +12,19 @@ public class ElasticsearchCollectionNameProvider : CollectionNameProvid { private readonly IElasticIndexService _elasticIndexService; private readonly IShardingKeyProvider _shardingKeyProvider; - private readonly ICollectionRouteKeyProvider _collectionRouteKeyProvider; + // private readonly ICollectionRouteKeyProvider _collectionRouteKeyProvider; private readonly AElfEntityMappingOptions _entityMappingOptions; private readonly ILogger> _logger; public ElasticsearchCollectionNameProvider(IShardingKeyProvider shardingKeyProvider, IElasticIndexService elasticIndexService, - ICollectionRouteKeyProvider collectionRouteKeyProvider, + // ICollectionRouteKeyProvider collectionRouteKeyProvider, IOptions entityMappingOptions, ILogger> logger) { _elasticIndexService = elasticIndexService; _shardingKeyProvider = shardingKeyProvider; - _collectionRouteKeyProvider = collectionRouteKeyProvider; + // _collectionRouteKeyProvider = collectionRouteKeyProvider; _entityMappingOptions = entityMappingOptions.Value; _logger = logger; } @@ -42,7 +42,8 @@ protected override async Task> GetCollectionNameAsync(List { GetDefaultCollectionName() }; } return shardKeyCollectionNames; @@ -71,9 +72,9 @@ protected override async Task> GetCollectionNameByEntityAsync(List< protected override async Task GetCollectionNameByIdAsync(TKey id) { - if (!_shardingKeyProvider.IsShardingCollection()) + // if (!_shardingKeyProvider.IsShardingCollection()) return GetDefaultCollectionName(); - return await _collectionRouteKeyProvider.GetCollectionNameAsync(id.ToString()); + // return await _collectionRouteKeyProvider.GetCollectionNameAsync(id.ToString()); } protected override string FormatCollectionName(string name) diff --git a/src/AElf.EntityMapping.Elasticsearch/Repositories/ElasticsearchRepository.cs b/src/AElf.EntityMapping.Elasticsearch/Repositories/ElasticsearchRepository.cs index 0ffb1c07..84bf5ebe 100644 --- a/src/AElf.EntityMapping.Elasticsearch/Repositories/ElasticsearchRepository.cs +++ b/src/AElf.EntityMapping.Elasticsearch/Repositories/ElasticsearchRepository.cs @@ -21,7 +21,7 @@ public class ElasticsearchRepository : IElasticsearchRepository _collectionNameProvider; private readonly IShardingKeyProvider _shardingKeyProvider; - private readonly ICollectionRouteKeyProvider _collectionRouteKeyProvider; + // private readonly ICollectionRouteKeyProvider _collectionRouteKeyProvider; private readonly IElasticIndexService _elasticIndexService; private readonly IElasticsearchQueryableFactory _elasticsearchQueryableFactory; private readonly ILogger> _logger; @@ -31,7 +31,8 @@ public ElasticsearchRepository(IElasticsearchClientProvider elasticsearchClientP IOptions aelfEntityMappingOptions, ILogger> logger, IOptions options, ICollectionNameProvider collectionNameProvider, - IShardingKeyProvider shardingKeyProvider, ICollectionRouteKeyProvider collectionRouteKeyProvider, + IShardingKeyProvider shardingKeyProvider, + // ICollectionRouteKeyProvider collectionRouteKeyProvider, IElasticIndexService elasticIndexService, IElasticsearchQueryableFactory elasticsearchQueryableFactory) { _logger = logger; @@ -40,7 +41,7 @@ public ElasticsearchRepository(IElasticsearchClientProvider elasticsearchClientP _aelfEntityMappingOptions = aelfEntityMappingOptions.Value; _elasticsearchOptions = options.Value; _shardingKeyProvider = shardingKeyProvider; - _collectionRouteKeyProvider = collectionRouteKeyProvider; + // _collectionRouteKeyProvider = collectionRouteKeyProvider; _elasticIndexService = elasticIndexService; _elasticsearchQueryableFactory = elasticsearchQueryableFactory; } @@ -105,7 +106,7 @@ public async Task AddAsync(TEntity model, string collectionName = null, Cancella var result = await client.IndexAsync(model, ss => ss.Index(indexName).Refresh(_elasticsearchOptions.Refresh), cancellationToken); - await _collectionRouteKeyProvider.AddCollectionRouteKeyAsync(model, indexName, cancellationToken); + // await _collectionRouteKeyProvider.AddCollectionRouteKeyAsync(model, indexName, cancellationToken); if (result.IsValid) return; @@ -127,7 +128,7 @@ public async Task AddOrUpdateAsync(TEntity model, string collectionName = null, ss => ss.Index(indexName).Doc(model).RetryOnConflict(3).Refresh(_elasticsearchOptions.Refresh), cancellationToken); - await _collectionRouteKeyProvider.UpdateCollectionRouteKeyAsync(model, cancellationToken); + // await _collectionRouteKeyProvider.UpdateCollectionRouteKeyAsync(model, cancellationToken); if (result.IsValid) return; @@ -140,7 +141,7 @@ public async Task AddOrUpdateAsync(TEntity model, string collectionName = null, await client.IndexAsync(model, ss => ss.Index(indexName).Refresh(_elasticsearchOptions.Refresh), cancellationToken); - await _collectionRouteKeyProvider.AddCollectionRouteKeyAsync(model, indexName, cancellationToken); + // await _collectionRouteKeyProvider.AddCollectionRouteKeyAsync(model, indexName, cancellationToken); if (result.IsValid) return; @@ -161,30 +162,30 @@ public async Task AddOrUpdateManyAsync(List list, string collectionName var isSharding = _shardingKeyProvider.IsShardingCollection(); var client = await GetElasticsearchClientAsync(cancellationToken); - if (!isSharding) - { + // if (!isSharding) + // { await BulkAddAsync(client, indexNames, list, isSharding, cancellationToken); return; - } - - _logger.LogDebug("[{1}]Before GetBulkAddTaskAsync time: {0} ", - DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), - entityName); - var bulkAddTaskList = new List(); - bulkAddTaskList.Add(BulkAddAsync(client, indexNames, list, isSharding, cancellationToken)); - _logger.LogDebug("[{1}]After GetBulkAddTaskAsync time: {0} ", - DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), entityName); - var routeKeyTaskList = - await GetBulkAddCollectionRouteKeyTasksAsync(isSharding, list, indexNames, cancellationToken); - if (routeKeyTaskList.Count > 0) - { - bulkAddTaskList.AddRange(routeKeyTaskList); - } - _logger.LogDebug("[{1}]Before Task.WhenAll time: {0} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), - entityName); - await Task.WhenAll(bulkAddTaskList.ToArray()); - _logger.LogDebug("[{1}]After Task.WhenAll time: {0} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), - entityName); + // } + // + // _logger.LogDebug("[{1}]Before GetBulkAddTaskAsync time: {0} ", + // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), + // entityName); + // var bulkAddTaskList = new List(); + // bulkAddTaskList.Add(BulkAddAsync(client, indexNames, list, isSharding, cancellationToken)); + // _logger.LogDebug("[{1}]After GetBulkAddTaskAsync time: {0} ", + // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), entityName); + // var routeKeyTaskList = + // await GetBulkAddCollectionRouteKeyTasksAsync(isSharding, list, indexNames, cancellationToken); + // if (routeKeyTaskList.Count > 0) + // { + // bulkAddTaskList.AddRange(routeKeyTaskList); + // } + // _logger.LogDebug("[{1}]Before Task.WhenAll time: {0} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), + // entityName); + // await Task.WhenAll(bulkAddTaskList.ToArray()); + // _logger.LogDebug("[{1}]After Task.WhenAll time: {0} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), + // entityName); } private async Task BulkAddAsync(IElasticClient client,List indexNames,List list, bool isSharding, CancellationToken cancellationToken = default) @@ -240,7 +241,7 @@ public async Task UpdateAsync(TEntity model, string collectionName = null, ss => ss.Index(indexName).Doc(model).RetryOnConflict(3).Refresh(_elasticsearchOptions.Refresh), cancellationToken); - await _collectionRouteKeyProvider.UpdateCollectionRouteKeyAsync(model, cancellationToken); + // await _collectionRouteKeyProvider.UpdateCollectionRouteKeyAsync(model, cancellationToken); if (result.IsValid) return; @@ -254,21 +255,21 @@ public async Task UpdateManyAsync(List list, string collectionName = nu var indexNames = await GetFullCollectionNameAsync(collectionName, list); var client = await GetElasticsearchClientAsync(cancellationToken); var isSharding = _shardingKeyProvider.IsShardingCollection(); - if (!isSharding) - { + // if (!isSharding) + // { await BulkUpdateAsync(client, indexNames, list, isSharding, cancellationToken); return; - } - - var bulkUpdateTaskList = new List(); - bulkUpdateTaskList.Add(BulkUpdateAsync(client, indexNames, list, isSharding, cancellationToken)); - var routeKeyTaskList = - await GetBulkUpdateCollectionRouteKeyTasksAsync(isSharding, list, indexNames, cancellationToken); - if (routeKeyTaskList.Count > 0) - { - bulkUpdateTaskList.AddRange(routeKeyTaskList); - } - await Task.WhenAll(bulkUpdateTaskList.ToArray()); + // } + // + // var bulkUpdateTaskList = new List(); + // bulkUpdateTaskList.Add(BulkUpdateAsync(client, indexNames, list, isSharding, cancellationToken)); + // var routeKeyTaskList = + // await GetBulkUpdateCollectionRouteKeyTasksAsync(isSharding, list, indexNames, cancellationToken); + // if (routeKeyTaskList.Count > 0) + // { + // bulkUpdateTaskList.AddRange(routeKeyTaskList); + // } + // await Task.WhenAll(bulkUpdateTaskList.ToArray()); } private async Task BulkUpdateAsync(IElasticClient client, List indexNames, List list, bool isSharding, @@ -325,7 +326,7 @@ await client.DeleteAsync( new DeleteRequest(indexName, new Id(new { id = id.ToString() })) { Refresh = _elasticsearchOptions.Refresh }, cancellationToken); - await _collectionRouteKeyProvider.DeleteCollectionRouteKeyAsync(id.ToString(), cancellationToken); + // await _collectionRouteKeyProvider.DeleteCollectionRouteKeyAsync(id.ToString(), cancellationToken); if (response.ServerError == null) { @@ -345,7 +346,7 @@ await client.DeleteAsync( new DeleteRequest(indexName, new Id(model)) { Refresh = _elasticsearchOptions.Refresh }, cancellationToken); - await _collectionRouteKeyProvider.DeleteCollectionRouteKeyAsync(model.Id.ToString(), cancellationToken); + // await _collectionRouteKeyProvider.DeleteCollectionRouteKeyAsync(model.Id.ToString(), cancellationToken); if (response.ServerError == null) { @@ -363,21 +364,21 @@ public async Task DeleteManyAsync(List list, string collectionName = nu var isSharding = _shardingKeyProvider.IsShardingCollection(); var client = await GetElasticsearchClientAsync(cancellationToken); - if (!isSharding) - { + // if (!isSharding) + // { await BulkDeleteAsync(client, indexNames, list, isSharding, cancellationToken); return; - } - - var bulkDeleteTaskList = new List(); - bulkDeleteTaskList.Add(BulkDeleteAsync(client, indexNames, list, isSharding, cancellationToken)); - var routeKeyTaskList = - await GetBulkDeleteCollectionRouteKeyTasksAsync(isSharding, list, cancellationToken); - if (routeKeyTaskList.Count > 0) - { - bulkDeleteTaskList.AddRange(routeKeyTaskList); - } - await Task.WhenAll(bulkDeleteTaskList.ToArray()); + // } + // + // var bulkDeleteTaskList = new List(); + // bulkDeleteTaskList.Add(BulkDeleteAsync(client, indexNames, list, isSharding, cancellationToken)); + // var routeKeyTaskList = + // await GetBulkDeleteCollectionRouteKeyTasksAsync(isSharding, list, cancellationToken); + // if (routeKeyTaskList.Count > 0) + // { + // bulkDeleteTaskList.AddRange(routeKeyTaskList); + // } + // await Task.WhenAll(bulkDeleteTaskList.ToArray()); } @@ -466,172 +467,172 @@ private async Task GetFullCollectionNameByIdAsync(TKey id, string collec } - private async Task> GetBulkAddCollectionRouteKeyTasksAsync(bool isSharding, List modelList, - List fullCollectionNameList, CancellationToken cancellationToken = default) - { - var collectionRouteKeys = await _collectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); - if (collectionRouteKeys != null && collectionRouteKeys.Any() && isSharding) - { - var routeKeyTaskList = new List(); - var client = await GetElasticsearchClientAsync(cancellationToken); - foreach (var collectionRouteKey in collectionRouteKeys) - { - routeKeyTaskList.Add(BulkAddRouteKey(client, modelList, collectionRouteKey, fullCollectionNameList, - cancellationToken)); - } - - return routeKeyTaskList; - } - - return new List(); - } - - private async Task BulkAddRouteKey(IElasticClient client, List modelList, - CollectionRouteKeyItem collectionRouteKey, List fullCollectionNameList, - CancellationToken cancellationToken) - { - var collectionRouteKeyIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, - _aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyBulk = new BulkRequest(collectionRouteKeyIndexName) - { - Operations = new List(), - Refresh = _elasticsearchOptions.Refresh - }; - int indexNameCount = 0; - foreach (var item in modelList) - { - // var value = item.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(item); - var value = collectionRouteKey.GetRouteKeyValueFunc(item); - string indexName = IndexNameHelper.RemoveCollectionPrefix(fullCollectionNameList[indexNameCount], - _aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyIndexModel = new RouteKeyCollection() - { - Id = item.Id.ToString(), - CollectionName = indexName, - // SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType) - CollectionRouteKey = value?.ToString() - }; - collectionRouteKeyBulk.Operations.Add( - new BulkIndexOperation(collectionRouteKeyIndexModel)); - indexNameCount++; - } - - var response = await client.BulkAsync(collectionRouteKeyBulk, cancellationToken); - if (!response.IsValid) - { - throw new ElasticsearchException( - $"Bulk InsertOrUpdate Document failed at index {collectionRouteKeyIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(response)}"); - } - } - - private async Task> GetBulkUpdateCollectionRouteKeyTasksAsync(bool isSharding, List modelList, - List fullCollectionNameList, CancellationToken cancellationToken = default) - { - var collectionRouteKeys = await _collectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); - if (collectionRouteKeys != null && collectionRouteKeys.Any() && isSharding) - { - var routeKeyTaskList = new List(); - var client = await GetElasticsearchClientAsync(cancellationToken); - foreach (var collectionRouteKey in collectionRouteKeys) - { - routeKeyTaskList.Add(BulkUpdateRouteKey(client, modelList, collectionRouteKey, fullCollectionNameList, - cancellationToken)); - } - - return routeKeyTaskList; - } - - return new List(); - } - - private async Task BulkUpdateRouteKey(IElasticClient client, List modelList, - CollectionRouteKeyItem collectionRouteKey, List fullCollectionNameList, - CancellationToken cancellationToken) - { - var collectionRouteKeyIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, - _aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyBulk = new BulkRequest(collectionRouteKeyIndexName) - { - Operations = new List(), - Refresh = _elasticsearchOptions.Refresh - }; - int indexNameCount = 0; - foreach (var item in modelList) - { - // var value = item.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(item); - var value = collectionRouteKey.GetRouteKeyValueFunc(item); - string indexName = IndexNameHelper.RemoveCollectionPrefix(fullCollectionNameList[indexNameCount], - _aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyIndexModel = new RouteKeyCollection() - { - Id = item.Id.ToString(), - CollectionName = indexName, - // SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType) - CollectionRouteKey = value?.ToString() - }; - var updateOperation = new BulkUpdateOperation(new Id(collectionRouteKeyIndexModel)) - { - Doc = collectionRouteKeyIndexModel, - Index = collectionRouteKeyIndexName - }; - collectionRouteKeyBulk.Operations.Add(updateOperation); - indexNameCount++; - } - - var response = await client.BulkAsync(collectionRouteKeyBulk, cancellationToken); - if (!response.IsValid) - { - throw new ElasticsearchException( - $"Bulk Update Document failed at index {collectionRouteKeyIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(response)}"); - } - } - - private async Task> GetBulkDeleteCollectionRouteKeyTasksAsync(bool isSharding, List modelList, - CancellationToken cancellationToken = default) - { - var collectionRouteKeys = await _collectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); - if (collectionRouteKeys != null && collectionRouteKeys.Any() && isSharding) - { - var routeKeyTaskList = new List(); - var client = await GetElasticsearchClientAsync(cancellationToken); - foreach (var collectionRouteKey in collectionRouteKeys) - { - routeKeyTaskList.Add(BulkDeleteRouteKey(client, modelList, collectionRouteKey, cancellationToken)); - } - - return routeKeyTaskList; - } - - return new List(); - } - - private async Task BulkDeleteRouteKey(IElasticClient client, List modelList, - CollectionRouteKeyItem collectionRouteKey, CancellationToken cancellationToken) - { - var collectionRouteKeyRouteIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, - _aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyRouteBulk = new BulkRequest(collectionRouteKeyRouteIndexName) - { - Operations = new List(), - Refresh = _elasticsearchOptions.Refresh - }; - foreach (var item in modelList) - { - collectionRouteKeyRouteBulk.Operations.Add(new BulkDeleteOperation(new Id(item))); - } - - var response = await client.BulkAsync(collectionRouteKeyRouteBulk, cancellationToken); - - if (response.ServerError == null) - { - return; - } - - throw new ElasticsearchException( - $"Bulk Delete Document at index {collectionRouteKeyRouteIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(response)}"); - } + // private async Task> GetBulkAddCollectionRouteKeyTasksAsync(bool isSharding, List modelList, + // List fullCollectionNameList, CancellationToken cancellationToken = default) + // { + // var collectionRouteKeys = await _collectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); + // if (collectionRouteKeys != null && collectionRouteKeys.Any() && isSharding) + // { + // var routeKeyTaskList = new List(); + // var client = await GetElasticsearchClientAsync(cancellationToken); + // foreach (var collectionRouteKey in collectionRouteKeys) + // { + // routeKeyTaskList.Add(BulkAddRouteKey(client, modelList, collectionRouteKey, fullCollectionNameList, + // cancellationToken)); + // } + // + // return routeKeyTaskList; + // } + // + // return new List(); + // } + + // private async Task BulkAddRouteKey(IElasticClient client, List modelList, + // CollectionRouteKeyItem collectionRouteKey, List fullCollectionNameList, + // CancellationToken cancellationToken) + // { + // var collectionRouteKeyIndexName = + // IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, + // _aelfEntityMappingOptions.CollectionPrefix); + // var collectionRouteKeyBulk = new BulkRequest(collectionRouteKeyIndexName) + // { + // Operations = new List(), + // Refresh = _elasticsearchOptions.Refresh + // }; + // int indexNameCount = 0; + // foreach (var item in modelList) + // { + // // var value = item.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(item); + // var value = collectionRouteKey.GetRouteKeyValueFunc(item); + // string indexName = IndexNameHelper.RemoveCollectionPrefix(fullCollectionNameList[indexNameCount], + // _aelfEntityMappingOptions.CollectionPrefix); + // var collectionRouteKeyIndexModel = new RouteKeyCollection() + // { + // Id = item.Id.ToString(), + // CollectionName = indexName, + // // SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType) + // CollectionRouteKey = value?.ToString() + // }; + // collectionRouteKeyBulk.Operations.Add( + // new BulkIndexOperation(collectionRouteKeyIndexModel)); + // indexNameCount++; + // } + // + // var response = await client.BulkAsync(collectionRouteKeyBulk, cancellationToken); + // if (!response.IsValid) + // { + // throw new ElasticsearchException( + // $"Bulk InsertOrUpdate Document failed at index {collectionRouteKeyIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(response)}"); + // } + // } + + // private async Task> GetBulkUpdateCollectionRouteKeyTasksAsync(bool isSharding, List modelList, + // List fullCollectionNameList, CancellationToken cancellationToken = default) + // { + // var collectionRouteKeys = await _collectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); + // if (collectionRouteKeys != null && collectionRouteKeys.Any() && isSharding) + // { + // var routeKeyTaskList = new List(); + // var client = await GetElasticsearchClientAsync(cancellationToken); + // foreach (var collectionRouteKey in collectionRouteKeys) + // { + // routeKeyTaskList.Add(BulkUpdateRouteKey(client, modelList, collectionRouteKey, fullCollectionNameList, + // cancellationToken)); + // } + // + // return routeKeyTaskList; + // } + // + // return new List(); + // } + + // private async Task BulkUpdateRouteKey(IElasticClient client, List modelList, + // CollectionRouteKeyItem collectionRouteKey, List fullCollectionNameList, + // CancellationToken cancellationToken) + // { + // var collectionRouteKeyIndexName = + // IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, + // _aelfEntityMappingOptions.CollectionPrefix); + // var collectionRouteKeyBulk = new BulkRequest(collectionRouteKeyIndexName) + // { + // Operations = new List(), + // Refresh = _elasticsearchOptions.Refresh + // }; + // int indexNameCount = 0; + // foreach (var item in modelList) + // { + // // var value = item.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(item); + // var value = collectionRouteKey.GetRouteKeyValueFunc(item); + // string indexName = IndexNameHelper.RemoveCollectionPrefix(fullCollectionNameList[indexNameCount], + // _aelfEntityMappingOptions.CollectionPrefix); + // var collectionRouteKeyIndexModel = new RouteKeyCollection() + // { + // Id = item.Id.ToString(), + // CollectionName = indexName, + // // SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType) + // CollectionRouteKey = value?.ToString() + // }; + // var updateOperation = new BulkUpdateOperation(new Id(collectionRouteKeyIndexModel)) + // { + // Doc = collectionRouteKeyIndexModel, + // Index = collectionRouteKeyIndexName + // }; + // collectionRouteKeyBulk.Operations.Add(updateOperation); + // indexNameCount++; + // } + // + // var response = await client.BulkAsync(collectionRouteKeyBulk, cancellationToken); + // if (!response.IsValid) + // { + // throw new ElasticsearchException( + // $"Bulk Update Document failed at index {collectionRouteKeyIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(response)}"); + // } + // } + + // private async Task> GetBulkDeleteCollectionRouteKeyTasksAsync(bool isSharding, List modelList, + // CancellationToken cancellationToken = default) + // { + // var collectionRouteKeys = await _collectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); + // if (collectionRouteKeys != null && collectionRouteKeys.Any() && isSharding) + // { + // var routeKeyTaskList = new List(); + // var client = await GetElasticsearchClientAsync(cancellationToken); + // foreach (var collectionRouteKey in collectionRouteKeys) + // { + // routeKeyTaskList.Add(BulkDeleteRouteKey(client, modelList, collectionRouteKey, cancellationToken)); + // } + // + // return routeKeyTaskList; + // } + // + // return new List(); + // } + + // private async Task BulkDeleteRouteKey(IElasticClient client, List modelList, + // CollectionRouteKeyItem collectionRouteKey, CancellationToken cancellationToken) + // { + // var collectionRouteKeyRouteIndexName = + // IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, + // _aelfEntityMappingOptions.CollectionPrefix); + // var collectionRouteKeyRouteBulk = new BulkRequest(collectionRouteKeyRouteIndexName) + // { + // Operations = new List(), + // Refresh = _elasticsearchOptions.Refresh + // }; + // foreach (var item in modelList) + // { + // collectionRouteKeyRouteBulk.Operations.Add(new BulkDeleteOperation(new Id(item))); + // } + // + // var response = await client.BulkAsync(collectionRouteKeyRouteBulk, cancellationToken); + // + // if (response.ServerError == null) + // { + // return; + // } + // + // throw new ElasticsearchException( + // $"Bulk Delete Document at index {collectionRouteKeyRouteIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(response)}"); + // } } \ No newline at end of file diff --git a/src/AElf.EntityMapping.Elasticsearch/Services/ElasticIndexService.cs b/src/AElf.EntityMapping.Elasticsearch/Services/ElasticIndexService.cs index 4f9d2a4c..73f29009 100644 --- a/src/AElf.EntityMapping.Elasticsearch/Services/ElasticIndexService.cs +++ b/src/AElf.EntityMapping.Elasticsearch/Services/ElasticIndexService.cs @@ -128,24 +128,24 @@ public async Task CreateIndexTemplateAsync(string indexTemplateName,string index } } - public async Task CreateCollectionRouteKeyIndexAsync(Type type, int shard = 1, int numberOfReplicas = 1) - { - var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - foreach (var property in properties) - { - CollectionRouteKeyAttribute shardRouteAttribute = (CollectionRouteKeyAttribute)Attribute.GetCustomAttribute(property, typeof(CollectionRouteKeyAttribute)); - if (shardRouteAttribute != null) - { - if (property.PropertyType != typeof(string)) - { - throw new NotSupportedException( - $"{type.Name} Attribute Error! NeedShardRouteAttribute only support string type, please check field: {property.Name}"); - } - var indexName = IndexNameHelper.GetCollectionRouteKeyIndexName(type, property.Name,_entityMappingOptions.CollectionPrefix); - await CreateIndexAsync(indexName, typeof(RouteKeyCollection), shard, numberOfReplicas); - } - } - } + // public async Task CreateCollectionRouteKeyIndexAsync(Type type, int shard = 1, int numberOfReplicas = 1) + // { + // var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + // foreach (var property in properties) + // { + // CollectionRouteKeyAttribute shardRouteAttribute = (CollectionRouteKeyAttribute)Attribute.GetCustomAttribute(property, typeof(CollectionRouteKeyAttribute)); + // if (shardRouteAttribute != null) + // { + // if (property.PropertyType != typeof(string)) + // { + // throw new NotSupportedException( + // $"{type.Name} Attribute Error! NeedShardRouteAttribute only support string type, please check field: {property.Name}"); + // } + // var indexName = IndexNameHelper.GetCollectionRouteKeyIndexName(type, property.Name,_entityMappingOptions.CollectionPrefix); + // await CreateIndexAsync(indexName, typeof(RouteKeyCollection), shard, numberOfReplicas); + // } + // } + // } public async Task DeleteIndexAsync(string collectionName = null, CancellationToken cancellationToken = default) { diff --git a/src/AElf.EntityMapping.Elasticsearch/Services/EnsureIndexBuildService.cs b/src/AElf.EntityMapping.Elasticsearch/Services/EnsureIndexBuildService.cs index 5bfdf065..55396c5d 100644 --- a/src/AElf.EntityMapping.Elasticsearch/Services/EnsureIndexBuildService.cs +++ b/src/AElf.EntityMapping.Elasticsearch/Services/EnsureIndexBuildService.cs @@ -54,11 +54,9 @@ private async Task HandleModuleAsync(Type moduleType) await _elasticIndexService.CreateIndexTemplateAsync(indexTemplateName, indexName, t, _elasticsearchOptions.NumberOfShards, _elasticsearchOptions.NumberOfReplicas); - //create index marked field cache - // await _elasticIndexService.InitializeCollectionRouteKeyCacheAsync(t); //create non shard key route index - await _elasticIndexService.CreateCollectionRouteKeyIndexAsync(t, _elasticsearchOptions.NumberOfShards, - _elasticsearchOptions.NumberOfReplicas); + // await _elasticIndexService.CreateCollectionRouteKeyIndexAsync(t, _elasticsearchOptions.NumberOfShards, + // _elasticsearchOptions.NumberOfReplicas); await CreateShardingCollectionTailIndexAsync(); } else diff --git a/src/AElf.EntityMapping.Elasticsearch/Services/IElasticIndexService.cs b/src/AElf.EntityMapping.Elasticsearch/Services/IElasticIndexService.cs index b4b9f702..ad73edce 100644 --- a/src/AElf.EntityMapping.Elasticsearch/Services/IElasticIndexService.cs +++ b/src/AElf.EntityMapping.Elasticsearch/Services/IElasticIndexService.cs @@ -8,7 +8,7 @@ Task CreateIndexAsync(string indexName, Type indexEntityType, int shard = 1, int Task CreateIndexTemplateAsync(string indexTemplateName, string indexName, Type indexEntityType, int numberOfShards, int numberOfReplicas); - Task CreateCollectionRouteKeyIndexAsync(Type indexEntityType, int numberOfShards, - int numberOfReplicas); + // Task CreateCollectionRouteKeyIndexAsync(Type indexEntityType, int numberOfShards, + // int numberOfReplicas); Task DeleteIndexAsync(string collectionName = null, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/AElf.EntityMapping.Elasticsearch/Sharding/CollectionRouteKeyProvider.cs b/src/AElf.EntityMapping.Elasticsearch/Sharding/CollectionRouteKeyProvider.cs index 3e535d36..be259151 100644 --- a/src/AElf.EntityMapping.Elasticsearch/Sharding/CollectionRouteKeyProvider.cs +++ b/src/AElf.EntityMapping.Elasticsearch/Sharding/CollectionRouteKeyProvider.cs @@ -14,309 +14,307 @@ namespace AElf.EntityMapping.Elasticsearch.Sharding; -public class CollectionRouteKeyProvider:ICollectionRouteKeyProvider where TEntity : class, IEntity -{ - private readonly IAbpLazyServiceProvider _lazyServiceProvider; - // private IElasticsearchRepository _collectionRouteKeyIndexRepository => LazyServiceProvider - // .LazyGetRequiredService>(); - private readonly IElasticIndexService _elasticIndexService; - private readonly IShardingKeyProvider _shardingKeyProvider; - private List> _collectionRouteKeys; - private readonly IElasticsearchClientProvider _elasticsearchClientProvider; - private readonly AElfEntityMappingOptions _aelfEntityMappingOptions; - private readonly ElasticsearchOptions _elasticsearchOptions; - private readonly ILogger> _logger; - - public CollectionRouteKeyProvider(IElasticsearchClientProvider elasticsearchClientProvider, - IShardingKeyProvider shardingKeyProvider, - IAbpLazyServiceProvider lazyServiceProvider, - IOptions aelfEntityMappingOptions, - IOptions elasticsearchOptions, - ILogger> logger, - IElasticIndexService elasticIndexService) - { - _elasticIndexService = elasticIndexService; - _elasticsearchClientProvider = elasticsearchClientProvider; - _shardingKeyProvider = shardingKeyProvider; - _lazyServiceProvider= lazyServiceProvider; - _aelfEntityMappingOptions = aelfEntityMappingOptions.Value; - _elasticsearchOptions = elasticsearchOptions.Value; - _logger = logger; - - InitializeCollectionRouteKeys(); - } - - private void InitializeCollectionRouteKeys() - { - if (_collectionRouteKeys == null) - { - _collectionRouteKeys = new List>(); - Type type = typeof(TEntity); - var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - foreach (var property in properties) - { - var collectionRouteKeyItem = new CollectionRouteKeyItem() - { - FieldName = property.Name, - CollectionName = type.Name - }; - //Find the field with the CollectionRouteKeyAttribute annotation set - CollectionRouteKeyAttribute routeKeyAttribute = (CollectionRouteKeyAttribute)Attribute.GetCustomAttribute(property, typeof(CollectionRouteKeyAttribute)); - if (routeKeyAttribute != null) - { - // Creates a Func expression that gets the value of the property - var parameter = Expression.Parameter(type, "entity"); - var propertyAccess = Expression.Property(parameter, property); - var getPropertyFunc = Expression.Lambda>(propertyAccess, parameter).Compile(); - collectionRouteKeyItem.GetRouteKeyValueFunc = getPropertyFunc; - _collectionRouteKeys.Add(collectionRouteKeyItem); - } - } - // _logger.LogDebug($"CollectionRouteKeyProvider.InitializeCollectionRouteKeys: _collectionRouteKeys: {JsonConvert.SerializeObject(_collectionRouteKeys.Select(n=>n.FieldName).ToList())}"); - - } - } - - public async Task> GetCollectionNameAsync(List conditions) - { - var collectionNameList = new List(); - if (_collectionRouteKeys == null || _collectionRouteKeys.Count == 0) - { - return collectionNameList; - } - - foreach (var condition in conditions) - { - var collectionRouteKey = _collectionRouteKeys.FirstOrDefault(f => f.FieldName == condition.Key); - if (collectionRouteKey == null) - { - continue; - } - - // _logger.LogDebug($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: " + - // $"collectionRouteKey: {JsonConvert.SerializeObject(collectionRouteKey.FieldName)}"); - - if (condition.Value == null) - { - continue; - } - - // var fieldValue = Convert.ChangeType(condition.Value, collectionRouteKey.FieldValueType); - var fieldValue = condition.Value.ToString(); - var collectionRouteKeyIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, - _aelfEntityMappingOptions.CollectionPrefix); - _logger.LogDebug($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: " + - $"collectionRouteKeyIndexName: {collectionRouteKeyIndexName}"); - if (condition.Type == ConditionType.Equal) - { - if (_elasticsearchClientProvider == null) - { - _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: elasticsearchClientProvider is null"); - } - var client = _elasticsearchClientProvider.GetClient(); - if (client == null) - { - _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: client is null"); - } - var result = await client.SearchAsync(s => - s.Index(collectionRouteKeyIndexName) - .Query(q => q.Term(t => t.Field(f => f.CollectionRouteKey).Value(fieldValue))) - .Collapse(c => c.Field(f => f.CollectionName)) - .Size(1000)); - if (result == null) - { - _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: result is null fieldValue:{fieldValue}"); - } - if (!result.IsValid) - { - if (result.ServerError == null || result.ServerError.Error == null || result.ServerError.Error.Reason == null) - { - _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: result.ServerError is null result:{JsonConvert.SerializeObject(result)}"); - } - throw new ElasticsearchException($"Search document failed at index {collectionRouteKeyIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(result)}"); - } - - if (result.Documents == null) - { - _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: result.Documents is null fieldValue:{fieldValue}"); - } - var collectionList = result.Documents.ToList(); - _logger.LogDebug($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: " + - $"collectionList: {JsonConvert.SerializeObject(collectionList)}"); - var nameList = collectionList.Select(x => x.CollectionName).Distinct().ToList(); - if (collectionNameList.Count == 0) - { - collectionNameList.AddRange(nameList); - } - else - { - collectionNameList = collectionNameList.Intersect(nameList).ToList(); - } - } - } - - return collectionNameList; - } - - public async Task GetCollectionNameAsync(string id) - { - var collectionName=string.Empty; - if (_collectionRouteKeys == null || _collectionRouteKeys.Count == 0) - { - return collectionName; - } - - var collectionRouteKey= _collectionRouteKeys[0]; - var collectionRouteKeyIndexName = IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); - // var routeIndex=await _collectionRouteKeyIndexRepository.GetAsync(id, collectionRouteKeyIndexName); - var routeIndex = await GetRouteKeyCollectionAsync(id, collectionRouteKeyIndexName); - if (routeIndex != null) - { - collectionName = routeIndex.CollectionName; - } - - return collectionName; - } - - public Task>> GetCollectionRouteKeyItemsAsync() - { - return Task.FromResult(_collectionRouteKeys); - } - - public async Task GetRouteKeyCollectionAsync(string id, string indexName, CancellationToken cancellationToken = default) - { - var client = _elasticsearchClientProvider.GetClient(); - var selector = new Func, IGetRequest>(s => s - .Index(indexName)); - var result = new GetResponse(); - try - { - result = await client.GetAsync(new Nest.DocumentPath(new Id(new { id = id.ToString() })), - selector, cancellationToken); - } - catch (Exception e) - { - throw new ElasticsearchException($"Get Document failed at index {indexName} id {id.ToString()}", e); - } - return result.Found ? result.Source : null; - } - - - - public async Task AddCollectionRouteKeyAsync(TEntity model,string fullCollectionName,CancellationToken cancellationToken = default) - { - if (!_shardingKeyProvider.IsShardingCollection()) - { - return; - } - - string indexName = - IndexNameHelper.RemoveCollectionPrefix(fullCollectionName, _aelfEntityMappingOptions.CollectionPrefix); - - if (_collectionRouteKeys!=null && _collectionRouteKeys.Any()) - { - var client = await GetElasticsearchClientAsync(cancellationToken); - foreach (var collectionRouteKey in _collectionRouteKeys) - { - // var value = model.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(model); - var value = collectionRouteKey.GetRouteKeyValueFunc(model); - var collectionRouteKeyIndexModel = new RouteKeyCollection() - { - Id = model.Id.ToString(), - CollectionName = indexName, - // SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType) - CollectionRouteKey = value?.ToString() - }; - - var collectionRouteKeyIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyResult = await client.IndexAsync(collectionRouteKeyIndexModel, - ss => ss.Index(collectionRouteKeyIndexName).Refresh(_elasticsearchOptions.Refresh), - cancellationToken); - if (!collectionRouteKeyResult.IsValid) - { - throw new ElasticsearchException( - $"Index document failed at index {collectionRouteKeyIndexName} id {(collectionRouteKeyIndexModel == null ? "" : collectionRouteKeyIndexModel.Id)} :" + - ElasticsearchResponseHelper.GetErrorMessage(collectionRouteKeyResult)); - } - - } - } - } - - public async Task UpdateCollectionRouteKeyAsync(TEntity model, CancellationToken cancellationToken = default) - { - if (!_shardingKeyProvider.IsShardingCollection()) - { - return; - } - - if (_collectionRouteKeys!=null && _collectionRouteKeys.Any()) - { - var client = await GetElasticsearchClientAsync(cancellationToken); - foreach (var collectionRouteKey in _collectionRouteKeys) - { - var collectionRouteKeyIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyIndexId = model.Id.ToString(); - var collectionRouteKeyIndexModel = - await GetRouteKeyCollectionAsync(collectionRouteKeyIndexId, - collectionRouteKeyIndexName, cancellationToken); - // var collectionRouteKeyIndexModel = GetAsync((TKey)Convert.ChangeType(collectionRouteKeyIndexId, typeof(TKey)), collectionRouteKeyIndexName) as RouteKeyCollection; - - // var value = model.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(model); - var value = collectionRouteKey.GetRouteKeyValueFunc(model); - if (collectionRouteKeyIndexModel != null && collectionRouteKeyIndexModel.CollectionRouteKey != value?.ToString()) - { - // collectionRouteKeyIndexModel.SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType); - collectionRouteKeyIndexModel.CollectionRouteKey = value?.ToString(); - - var collectionRouteKeyResult = await client.UpdateAsync( - DocumentPath.Id(new Id(collectionRouteKeyIndexModel)), - ss => ss.Index(collectionRouteKeyIndexName).Doc((RouteKeyCollection)collectionRouteKeyIndexModel).RetryOnConflict(3) - .Refresh(_elasticsearchOptions.Refresh), - cancellationToken); - if (!collectionRouteKeyResult.IsValid) - { - throw new ElasticsearchException( - $"Update document failed at index {collectionRouteKeyIndexName} id {(collectionRouteKeyIndexModel == null ? "" : collectionRouteKeyIndexModel.Id)} :" + - ElasticsearchResponseHelper.GetErrorMessage(collectionRouteKeyResult)); - } - } - } - } - } - - public async Task DeleteCollectionRouteKeyAsync(string id, CancellationToken cancellationToken = default) - { - if (!_shardingKeyProvider.IsShardingCollection()) - { - return; - } - if (_collectionRouteKeys!=null && _collectionRouteKeys.Any()) - { - var client = await GetElasticsearchClientAsync(cancellationToken); - foreach (var collectionRouteKey in _collectionRouteKeys) - { - var collectionRouteKeyIndexName = - IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); - var collectionRouteKeyIndexId = id; - var collectionRouteKeyResult=await client.DeleteAsync( - new DeleteRequest(collectionRouteKeyIndexName, new Id(new { id = collectionRouteKeyIndexId.ToString() })) - { Refresh = _elasticsearchOptions.Refresh }, cancellationToken); - if (collectionRouteKeyResult.ServerError != null) - { - throw new ElasticsearchException( - $"Delete document failed at index {collectionRouteKeyIndexName} id {collectionRouteKeyIndexId} :" + - ElasticsearchResponseHelper.GetErrorMessage(collectionRouteKeyResult)); - } - } - } - } - - private Task GetElasticsearchClientAsync(CancellationToken cancellationToken = default) - { - return Task.FromResult(_elasticsearchClientProvider.GetClient()); - } -} \ No newline at end of file +// public class CollectionRouteKeyProvider:ICollectionRouteKeyProvider where TEntity : class, IEntity +// { +// private readonly IAbpLazyServiceProvider _lazyServiceProvider; +// private readonly IElasticIndexService _elasticIndexService; +// private readonly IShardingKeyProvider _shardingKeyProvider; +// private List> _collectionRouteKeys; +// private readonly IElasticsearchClientProvider _elasticsearchClientProvider; +// private readonly AElfEntityMappingOptions _aelfEntityMappingOptions; +// private readonly ElasticsearchOptions _elasticsearchOptions; +// private readonly ILogger> _logger; +// +// public CollectionRouteKeyProvider(IElasticsearchClientProvider elasticsearchClientProvider, +// IShardingKeyProvider shardingKeyProvider, +// IAbpLazyServiceProvider lazyServiceProvider, +// IOptions aelfEntityMappingOptions, +// IOptions elasticsearchOptions, +// ILogger> logger, +// IElasticIndexService elasticIndexService) +// { +// _elasticIndexService = elasticIndexService; +// _elasticsearchClientProvider = elasticsearchClientProvider; +// _shardingKeyProvider = shardingKeyProvider; +// _lazyServiceProvider= lazyServiceProvider; +// _aelfEntityMappingOptions = aelfEntityMappingOptions.Value; +// _elasticsearchOptions = elasticsearchOptions.Value; +// _logger = logger; +// +// InitializeCollectionRouteKeys(); +// } +// +// private void InitializeCollectionRouteKeys() +// { +// if (_collectionRouteKeys == null) +// { +// _collectionRouteKeys = new List>(); +// Type type = typeof(TEntity); +// var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); +// foreach (var property in properties) +// { +// var collectionRouteKeyItem = new CollectionRouteKeyItem() +// { +// FieldName = property.Name, +// CollectionName = type.Name +// }; +// //Find the field with the CollectionRouteKeyAttribute annotation set +// CollectionRouteKeyAttribute routeKeyAttribute = (CollectionRouteKeyAttribute)Attribute.GetCustomAttribute(property, typeof(CollectionRouteKeyAttribute)); +// if (routeKeyAttribute != null) +// { +// // Creates a Func expression that gets the value of the property +// var parameter = Expression.Parameter(type, "entity"); +// var propertyAccess = Expression.Property(parameter, property); +// var getPropertyFunc = Expression.Lambda>(propertyAccess, parameter).Compile(); +// collectionRouteKeyItem.GetRouteKeyValueFunc = getPropertyFunc; +// _collectionRouteKeys.Add(collectionRouteKeyItem); +// } +// } +// // _logger.LogDebug($"CollectionRouteKeyProvider.InitializeCollectionRouteKeys: _collectionRouteKeys: {JsonConvert.SerializeObject(_collectionRouteKeys.Select(n=>n.FieldName).ToList())}"); +// +// } +// } +// +// public async Task> GetCollectionNameAsync(List conditions) +// { +// var collectionNameList = new List(); +// if (_collectionRouteKeys == null || _collectionRouteKeys.Count == 0) +// { +// return collectionNameList; +// } +// +// foreach (var condition in conditions) +// { +// var collectionRouteKey = _collectionRouteKeys.FirstOrDefault(f => f.FieldName == condition.Key); +// if (collectionRouteKey == null) +// { +// continue; +// } +// +// // _logger.LogDebug($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: " + +// // $"collectionRouteKey: {JsonConvert.SerializeObject(collectionRouteKey.FieldName)}"); +// +// if (condition.Value == null) +// { +// continue; +// } +// +// // var fieldValue = Convert.ChangeType(condition.Value, collectionRouteKey.FieldValueType); +// var fieldValue = condition.Value.ToString(); +// var collectionRouteKeyIndexName = +// IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName, +// _aelfEntityMappingOptions.CollectionPrefix); +// _logger.LogDebug($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: " + +// $"collectionRouteKeyIndexName: {collectionRouteKeyIndexName}"); +// if (condition.Type == ConditionType.Equal) +// { +// if (_elasticsearchClientProvider == null) +// { +// _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: elasticsearchClientProvider is null"); +// } +// var client = _elasticsearchClientProvider.GetClient(); +// if (client == null) +// { +// _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: client is null"); +// } +// var result = await client.SearchAsync(s => +// s.Index(collectionRouteKeyIndexName) +// .Query(q => q.Term(t => t.Field(f => f.CollectionRouteKey).Value(fieldValue))) +// .Collapse(c => c.Field(f => f.CollectionName)) +// .Size(1000)); +// if (result == null) +// { +// _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: result is null fieldValue:{fieldValue}"); +// } +// if (!result.IsValid) +// { +// if (result.ServerError == null || result.ServerError.Error == null || result.ServerError.Error.Reason == null) +// { +// _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: result.ServerError is null result:{JsonConvert.SerializeObject(result)}"); +// } +// throw new ElasticsearchException($"Search document failed at index {collectionRouteKeyIndexName} :{ElasticsearchResponseHelper.GetErrorMessage(result)}"); +// } +// +// if (result.Documents == null) +// { +// _logger.LogError($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: result.Documents is null fieldValue:{fieldValue}"); +// } +// var collectionList = result.Documents.ToList(); +// _logger.LogDebug($"CollectionRouteKeyProvider.GetShardCollectionNameListByConditionsAsync: " + +// $"collectionList: {JsonConvert.SerializeObject(collectionList)}"); +// var nameList = collectionList.Select(x => x.CollectionName).Distinct().ToList(); +// if (collectionNameList.Count == 0) +// { +// collectionNameList.AddRange(nameList); +// } +// else +// { +// collectionNameList = collectionNameList.Intersect(nameList).ToList(); +// } +// } +// } +// +// return collectionNameList; +// } +// + // public async Task GetCollectionNameAsync(string id) + // { + // var collectionName=string.Empty; + // if (_collectionRouteKeys == null || _collectionRouteKeys.Count == 0) + // { + // return collectionName; + // } + // + // var collectionRouteKey= _collectionRouteKeys[0]; + // var collectionRouteKeyIndexName = IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); + // // var routeIndex=await _collectionRouteKeyIndexRepository.GetAsync(id, collectionRouteKeyIndexName); + // var routeIndex = await GetRouteKeyCollectionAsync(id, collectionRouteKeyIndexName); + // if (routeIndex != null) + // { + // collectionName = routeIndex.CollectionName; + // } + // + // return collectionName; + // } +// +// public Task>> GetCollectionRouteKeyItemsAsync() +// { +// return Task.FromResult(_collectionRouteKeys); +// } +// + // public async Task GetRouteKeyCollectionAsync(string id, string indexName, CancellationToken cancellationToken = default) + // { + // var client = _elasticsearchClientProvider.GetClient(); + // var selector = new Func, IGetRequest>(s => s + // .Index(indexName)); + // var result = new GetResponse(); + // try + // { + // result = await client.GetAsync(new Nest.DocumentPath(new Id(new { id = id.ToString() })), + // selector, cancellationToken); + // } + // catch (Exception e) + // { + // throw new ElasticsearchException($"Get Document failed at index {indexName} id {id.ToString()}", e); + // } + // return result.Found ? result.Source : null; + // } +// +// +// +// public async Task AddCollectionRouteKeyAsync(TEntity model,string fullCollectionName,CancellationToken cancellationToken = default) +// { +// if (!_shardingKeyProvider.IsShardingCollection()) +// { +// return; +// } +// +// string indexName = +// IndexNameHelper.RemoveCollectionPrefix(fullCollectionName, _aelfEntityMappingOptions.CollectionPrefix); +// +// if (_collectionRouteKeys!=null && _collectionRouteKeys.Any()) +// { +// var client = await GetElasticsearchClientAsync(cancellationToken); +// foreach (var collectionRouteKey in _collectionRouteKeys) +// { +// // var value = model.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(model); +// var value = collectionRouteKey.GetRouteKeyValueFunc(model); +// var collectionRouteKeyIndexModel = new RouteKeyCollection() +// { +// Id = model.Id.ToString(), +// CollectionName = indexName, +// // SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType) +// CollectionRouteKey = value?.ToString() +// }; +// +// var collectionRouteKeyIndexName = +// IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); +// var collectionRouteKeyResult = await client.IndexAsync(collectionRouteKeyIndexModel, +// ss => ss.Index(collectionRouteKeyIndexName).Refresh(_elasticsearchOptions.Refresh), +// cancellationToken); +// if (!collectionRouteKeyResult.IsValid) +// { +// throw new ElasticsearchException( +// $"Index document failed at index {collectionRouteKeyIndexName} id {(collectionRouteKeyIndexModel == null ? "" : collectionRouteKeyIndexModel.Id)} :" + +// ElasticsearchResponseHelper.GetErrorMessage(collectionRouteKeyResult)); +// } +// +// } +// } +// } +// +// public async Task UpdateCollectionRouteKeyAsync(TEntity model, CancellationToken cancellationToken = default) +// { +// if (!_shardingKeyProvider.IsShardingCollection()) +// { +// return; +// } +// +// if (_collectionRouteKeys!=null && _collectionRouteKeys.Any()) +// { +// var client = await GetElasticsearchClientAsync(cancellationToken); +// foreach (var collectionRouteKey in _collectionRouteKeys) +// { +// var collectionRouteKeyIndexName = +// IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); +// var collectionRouteKeyIndexId = model.Id.ToString(); +// var collectionRouteKeyIndexModel = +// await GetRouteKeyCollectionAsync(collectionRouteKeyIndexId, +// collectionRouteKeyIndexName, cancellationToken); +// // var collectionRouteKeyIndexModel = GetAsync((TKey)Convert.ChangeType(collectionRouteKeyIndexId, typeof(TKey)), collectionRouteKeyIndexName) as RouteKeyCollection; +// +// // var value = model.GetType().GetProperty(collectionRouteKey.FieldName)?.GetValue(model); +// var value = collectionRouteKey.GetRouteKeyValueFunc(model); +// if (collectionRouteKeyIndexModel != null && collectionRouteKeyIndexModel.CollectionRouteKey != value?.ToString()) +// { +// // collectionRouteKeyIndexModel.SearchKey = Convert.ChangeType(value, collectionRouteKey.FieldValueType); +// collectionRouteKeyIndexModel.CollectionRouteKey = value?.ToString(); +// +// var collectionRouteKeyResult = await client.UpdateAsync( +// DocumentPath.Id(new Id(collectionRouteKeyIndexModel)), +// ss => ss.Index(collectionRouteKeyIndexName).Doc((RouteKeyCollection)collectionRouteKeyIndexModel).RetryOnConflict(3) +// .Refresh(_elasticsearchOptions.Refresh), +// cancellationToken); +// if (!collectionRouteKeyResult.IsValid) +// { +// throw new ElasticsearchException( +// $"Update document failed at index {collectionRouteKeyIndexName} id {(collectionRouteKeyIndexModel == null ? "" : collectionRouteKeyIndexModel.Id)} :" + +// ElasticsearchResponseHelper.GetErrorMessage(collectionRouteKeyResult)); +// } +// } +// } +// } +// } +// +// public async Task DeleteCollectionRouteKeyAsync(string id, CancellationToken cancellationToken = default) +// { +// if (!_shardingKeyProvider.IsShardingCollection()) +// { +// return; +// } +// if (_collectionRouteKeys!=null && _collectionRouteKeys.Any()) +// { +// var client = await GetElasticsearchClientAsync(cancellationToken); +// foreach (var collectionRouteKey in _collectionRouteKeys) +// { +// var collectionRouteKeyIndexName = +// IndexNameHelper.GetCollectionRouteKeyIndexName(typeof(TEntity), collectionRouteKey.FieldName,_aelfEntityMappingOptions.CollectionPrefix); +// var collectionRouteKeyIndexId = id; +// var collectionRouteKeyResult=await client.DeleteAsync( +// new DeleteRequest(collectionRouteKeyIndexName, new Id(new { id = collectionRouteKeyIndexId.ToString() })) +// { Refresh = _elasticsearchOptions.Refresh }, cancellationToken); +// if (collectionRouteKeyResult.ServerError != null) +// { +// throw new ElasticsearchException( +// $"Delete document failed at index {collectionRouteKeyIndexName} id {collectionRouteKeyIndexId} :" + +// ElasticsearchResponseHelper.GetErrorMessage(collectionRouteKeyResult)); +// } +// } +// } +// } +// +// private Task GetElasticsearchClientAsync(CancellationToken cancellationToken = default) +// { +// return Task.FromResult(_elasticsearchClientProvider.GetClient()); +// } +// } \ No newline at end of file diff --git a/src/AElf.EntityMapping/Sharding/ICollectionRouteKeyProvider.cs b/src/AElf.EntityMapping/Sharding/ICollectionRouteKeyProvider.cs index 54246b16..0d207e3c 100644 --- a/src/AElf.EntityMapping/Sharding/ICollectionRouteKeyProvider.cs +++ b/src/AElf.EntityMapping/Sharding/ICollectionRouteKeyProvider.cs @@ -1,29 +1,29 @@ namespace AElf.EntityMapping.Sharding; -public interface ICollectionRouteKeyProvider where TEntity : class -{ - /// - /// - /// - /// - /// ElasticSearch Index Entity - /// - Task> GetCollectionNameAsync(List conditions); - - Task GetCollectionNameAsync(string id); - - Task>> GetCollectionRouteKeyItemsAsync(); - - Task GetRouteKeyCollectionAsync(string id, string collectionName, CancellationToken cancellationToken = default); - - // Task>> AddManyCollectionRouteKeyAsync(List modelList, List fullCollectionNameList, CancellationToken cancellationToken = default); - - Task AddCollectionRouteKeyAsync(TEntity model, string fullCollectionName, CancellationToken cancellationToken = default); - - Task UpdateCollectionRouteKeyAsync(TEntity model, CancellationToken cancellationToken = default); - - // Task>> DeleteManyCollectionRouteKeyAsync(List modelList, CancellationToken cancellationToken = default); - - Task DeleteCollectionRouteKeyAsync(string id, CancellationToken cancellationToken = default); - -} \ No newline at end of file +// public interface ICollectionRouteKeyProvider where TEntity : class +// { +// /// +// /// +// /// +// /// +// /// ElasticSearch Index Entity +// /// +// Task> GetCollectionNameAsync(List conditions); +// + // Task GetCollectionNameAsync(string id); +// +// Task>> GetCollectionRouteKeyItemsAsync(); +// +// Task GetRouteKeyCollectionAsync(string id, string collectionName, CancellationToken cancellationToken = default); +// +// // Task>> AddManyCollectionRouteKeyAsync(List modelList, List fullCollectionNameList, CancellationToken cancellationToken = default); +// +// Task AddCollectionRouteKeyAsync(TEntity model, string fullCollectionName, CancellationToken cancellationToken = default); +// +// Task UpdateCollectionRouteKeyAsync(TEntity model, CancellationToken cancellationToken = default); +// +// // Task>> DeleteManyCollectionRouteKeyAsync(List modelList, CancellationToken cancellationToken = default); +// +// Task DeleteCollectionRouteKeyAsync(string id, CancellationToken cancellationToken = default); +// +// } \ No newline at end of file diff --git a/test/AElf.EntityMapping.Elasticsearch.Tests/ElasticsearchCollectionNameProviderTests.cs b/test/AElf.EntityMapping.Elasticsearch.Tests/ElasticsearchCollectionNameProviderTests.cs index fcbc73ef..d67131f0 100644 --- a/test/AElf.EntityMapping.Elasticsearch.Tests/ElasticsearchCollectionNameProviderTests.cs +++ b/test/AElf.EntityMapping.Elasticsearch.Tests/ElasticsearchCollectionNameProviderTests.cs @@ -150,10 +150,12 @@ public async Task GetFullCollectionNameById_Test() await InitBlocksAsync(); var collectionNames = await _collectionNameProvider.GetFullCollectionNameByIdAsync("block1"); - collectionNames.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); + // collectionNames.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); + collectionNames.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex"); collectionNames = await _collectionNameProvider.GetFullCollectionNameByIdAsync("block6"); - collectionNames.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); + // collectionNames.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); + collectionNames.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex"); } private async Task InitBlocksAsync() diff --git a/test/AElf.EntityMapping.Elasticsearch.Tests/Repositories/ElasticsearchRepositoryTests.cs b/test/AElf.EntityMapping.Elasticsearch.Tests/Repositories/ElasticsearchRepositoryTests.cs index 90cbe5e9..bee149dc 100644 --- a/test/AElf.EntityMapping.Elasticsearch.Tests/Repositories/ElasticsearchRepositoryTests.cs +++ b/test/AElf.EntityMapping.Elasticsearch.Tests/Repositories/ElasticsearchRepositoryTests.cs @@ -414,21 +414,21 @@ public async Task Get_Test() block.LogEventCount.ShouldBe(blockIndex.LogEventCount); block.ChainId.ShouldBe(blockIndex.ChainId); - for (int i = 1; i <= 7; i++) - { - await _elasticsearchRepository.AddAsync(new BlockIndex - { - Id = "block" + i, - BlockHash = "BlockHash" + i, - BlockHeight = i, - BlockTime = DateTime.Now.AddDays(-10 + i), - LogEventCount = i, - ChainId = "AELF" - }); - } - - block = await _elasticsearchRepository.GetAsync("block7"); - block.Id.ShouldBe("block7"); + // for (int i = 1; i <= 7; i++) + // { + // await _elasticsearchRepository.AddAsync(new BlockIndex + // { + // Id = "block" + i, + // BlockHash = "BlockHash" + i, + // BlockHeight = i, + // BlockTime = DateTime.Now.AddDays(-10 + i), + // LogEventCount = i, + // ChainId = "AELF" + // }); + // } + // + // block = await _elasticsearchRepository.GetAsync("block7"); + // block.Id.ShouldBe("block7"); } [Fact] diff --git a/test/AElf.EntityMapping.Elasticsearch.Tests/Sharding/CollectionRouteKeyProviderTests.cs b/test/AElf.EntityMapping.Elasticsearch.Tests/Sharding/CollectionRouteKeyProviderTests.cs index eeb07758..d41503b5 100644 --- a/test/AElf.EntityMapping.Elasticsearch.Tests/Sharding/CollectionRouteKeyProviderTests.cs +++ b/test/AElf.EntityMapping.Elasticsearch.Tests/Sharding/CollectionRouteKeyProviderTests.cs @@ -8,133 +8,133 @@ namespace AElf.EntityMapping.Elasticsearch.Sharding; -public class CollectionRouteKeyProviderTests: AElfElasticsearchTestBase -{ - private readonly ICollectionRouteKeyProvider _blockIndexCollectionRouteKeyProvider; - private readonly IElasticsearchRepository _elasticsearchRepository; - private readonly AElfEntityMappingOptions _option; - - public CollectionRouteKeyProviderTests() - { - _blockIndexCollectionRouteKeyProvider = GetRequiredService>(); - _elasticsearchRepository = GetRequiredService>(); - _option = GetRequiredService>().Value; - } - - [Fact] - public async Task GetRouteKeys_Test() - { - List> routeKeys = await _blockIndexCollectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); - - routeKeys.Count.ShouldBe(1); - routeKeys[0].FieldName.ShouldBe(nameof(BlockIndex.BlockHash)); - // routeKeys[0].FieldValueType.ShouldBe(typeof(string).ToString()); - // routeKeys[0].IsRouteKey.ShouldBeTrue(); - } - - [Fact] - public async Task GetShardCollectionNameListByConditions_Test() - { - await InitBlocksAsync(); - - var collectionNameCondition = new List(); - var indexes = - await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( - collectionNameCondition); - indexes.Count.ShouldBe(0); - - collectionNameCondition.Add(new CollectionNameCondition - { - Key = nameof(BlockIndex.BlockHash), - Value = "BlockHash7", - Type = ConditionType.Equal - }); - indexes = - await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( - collectionNameCondition); - indexes.Count.ShouldBe(1); - // indexes[0].ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); - indexes[0].ShouldBe($"blockindex-aelf-1"); - - collectionNameCondition.Add(new CollectionNameCondition - { - Key = nameof(BlockIndex.BlockHash), - Value = "BlockHash6", - Type = ConditionType.Equal - }); - indexes = - await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( - collectionNameCondition); - indexes.Count.ShouldBe(1); - // indexes[0].ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); - indexes[0].ShouldBe($"blockindex-aelf-1"); - - collectionNameCondition.Add(new CollectionNameCondition - { - Key = nameof(BlockIndex.BlockHash), - Value = "BlockHash1", - Type = ConditionType.Equal - }); - indexes = - await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( - collectionNameCondition); - indexes.Count.ShouldBe(0); - } - - [Fact] - public async Task GetShardCollectionNameById_Test() - { - await InitBlocksAsync(); - - var index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block1"); - // index.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); - index.ShouldBe($"blockindex-aelf-0"); - - index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block2"); - // index.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); - index.ShouldBe($"blockindex-aelf-0"); - - index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block7"); - // index.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); - index.ShouldBe($"blockindex-aelf-1"); - - index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block8"); - index.ShouldBeEmpty(); - } - - [Fact] - public async Task GetCollectionRouteKeyIndex_Test() - { - var routeIndex = $"{_option.CollectionPrefix.ToLower()}.route.blockindex.blockhash"; - await InitBlocksAsync(); - - var route = await _blockIndexCollectionRouteKeyProvider.GetRouteKeyCollectionAsync("block1", routeIndex); - route.Id.ShouldBe("block1"); - route.CollectionRouteKey.ShouldBe("BlockHash1"); - // route.ShardCollectionName.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); - route.CollectionName.ShouldBe($"blockindex-aelf-0"); - - route = await _blockIndexCollectionRouteKeyProvider.GetRouteKeyCollectionAsync("block6", routeIndex); - route.Id.ShouldBe("block6"); - route.CollectionRouteKey.ShouldBe("BlockHash6"); - // route.ShardCollectionName.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); - route.CollectionName.ShouldBe($"blockindex-aelf-1"); - } - - private async Task InitBlocksAsync() - { - for (int i = 1; i <= 7; i++) - { - var blockIndex = new BlockIndex - { - Id = "block" + i, - BlockHash = "BlockHash" + i, - BlockHeight = i, - BlockTime = DateTime.Now.AddDays(-10 + i), - LogEventCount = i, - ChainId = "AELF" - }; - await _elasticsearchRepository.AddAsync(blockIndex); - } - } -} \ No newline at end of file +// public class CollectionRouteKeyProviderTests: AElfElasticsearchTestBase +// { +// private readonly ICollectionRouteKeyProvider _blockIndexCollectionRouteKeyProvider; +// private readonly IElasticsearchRepository _elasticsearchRepository; +// private readonly AElfEntityMappingOptions _option; +// +// public CollectionRouteKeyProviderTests() +// { +// _blockIndexCollectionRouteKeyProvider = GetRequiredService>(); +// _elasticsearchRepository = GetRequiredService>(); +// _option = GetRequiredService>().Value; +// } +// +// [Fact] +// public async Task GetRouteKeys_Test() +// { +// List> routeKeys = await _blockIndexCollectionRouteKeyProvider.GetCollectionRouteKeyItemsAsync(); +// +// routeKeys.Count.ShouldBe(1); +// routeKeys[0].FieldName.ShouldBe(nameof(BlockIndex.BlockHash)); +// // routeKeys[0].FieldValueType.ShouldBe(typeof(string).ToString()); +// // routeKeys[0].IsRouteKey.ShouldBeTrue(); +// } +// +// [Fact] +// public async Task GetShardCollectionNameListByConditions_Test() +// { +// await InitBlocksAsync(); +// +// var collectionNameCondition = new List(); +// var indexes = +// await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( +// collectionNameCondition); +// indexes.Count.ShouldBe(0); +// +// collectionNameCondition.Add(new CollectionNameCondition +// { +// Key = nameof(BlockIndex.BlockHash), +// Value = "BlockHash7", +// Type = ConditionType.Equal +// }); +// indexes = +// await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( +// collectionNameCondition); +// indexes.Count.ShouldBe(1); +// // indexes[0].ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); +// indexes[0].ShouldBe($"blockindex-aelf-1"); +// +// collectionNameCondition.Add(new CollectionNameCondition +// { +// Key = nameof(BlockIndex.BlockHash), +// Value = "BlockHash6", +// Type = ConditionType.Equal +// }); +// indexes = +// await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( +// collectionNameCondition); +// indexes.Count.ShouldBe(1); +// // indexes[0].ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); +// indexes[0].ShouldBe($"blockindex-aelf-1"); +// +// collectionNameCondition.Add(new CollectionNameCondition +// { +// Key = nameof(BlockIndex.BlockHash), +// Value = "BlockHash1", +// Type = ConditionType.Equal +// }); +// indexes = +// await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync( +// collectionNameCondition); +// indexes.Count.ShouldBe(0); +// } +// +// [Fact] +// public async Task GetShardCollectionNameById_Test() +// { +// await InitBlocksAsync(); +// +// var index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block1"); +// // index.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); +// index.ShouldBe($"blockindex-aelf-0"); +// +// index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block2"); +// // index.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); +// index.ShouldBe($"blockindex-aelf-0"); +// +// index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block7"); +// // index.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); +// index.ShouldBe($"blockindex-aelf-1"); +// +// index = await _blockIndexCollectionRouteKeyProvider.GetCollectionNameAsync("block8"); +// index.ShouldBeEmpty(); +// } +// +// [Fact] +// public async Task GetCollectionRouteKeyIndex_Test() +// { +// var routeIndex = $"{_option.CollectionPrefix.ToLower()}.route.blockindex.blockhash"; +// await InitBlocksAsync(); +// +// var route = await _blockIndexCollectionRouteKeyProvider.GetRouteKeyCollectionAsync("block1", routeIndex); +// route.Id.ShouldBe("block1"); +// route.CollectionRouteKey.ShouldBe("BlockHash1"); +// // route.ShardCollectionName.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-0"); +// route.CollectionName.ShouldBe($"blockindex-aelf-0"); +// +// route = await _blockIndexCollectionRouteKeyProvider.GetRouteKeyCollectionAsync("block6", routeIndex); +// route.Id.ShouldBe("block6"); +// route.CollectionRouteKey.ShouldBe("BlockHash6"); +// // route.ShardCollectionName.ShouldBe($"{_option.CollectionPrefix.ToLower()}.blockindex-aelf-1"); +// route.CollectionName.ShouldBe($"blockindex-aelf-1"); +// } +// +// private async Task InitBlocksAsync() +// { +// for (int i = 1; i <= 7; i++) +// { +// var blockIndex = new BlockIndex +// { +// Id = "block" + i, +// BlockHash = "BlockHash" + i, +// BlockHeight = i, +// BlockTime = DateTime.Now.AddDays(-10 + i), +// LogEventCount = i, +// ChainId = "AELF" +// }; +// await _elasticsearchRepository.AddAsync(blockIndex); +// } +// } +// } \ No newline at end of file