From 7cc06c2322508043962da2350dd961194f4d797e Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Wed, 6 May 2020 14:32:26 +0300 Subject: [PATCH] ElasticSearch: state --- .../Engine/FactoryIndexer.cs | 167 +++++++++--------- .../ASC.ElasticSearch/Service/Launcher.cs | 10 +- .../ASC.ElasticSearch/protos/SearchItem.proto | 5 + .../Server/Core/Search/FactoryIndexerFile.cs | 4 +- .../Core/Search/FactoryIndexerFolder.cs | 4 +- 5 files changed, 94 insertions(+), 96 deletions(-) diff --git a/common/services/ASC.ElasticSearch/Engine/FactoryIndexer.cs b/common/services/ASC.ElasticSearch/Engine/FactoryIndexer.cs index 7735bcfd32..d116f93e9a 100644 --- a/common/services/ASC.ElasticSearch/Engine/FactoryIndexer.cs +++ b/common/services/ASC.ElasticSearch/Engine/FactoryIndexer.cs @@ -52,49 +52,21 @@ using Nest; namespace ASC.ElasticSearch { public class FactoryIndexerHelper - { - public ICache Cache { get; } - public ILog Logger { get; } - public FactoryIndexer FactoryIndexer { get; } + { + public DateTime LastIndexed { get; set; } + public string Indexing { get; set; } - public FactoryIndexerHelper(IOptionsMonitor options, FactoryIndexer factoryIndexer) - { - Cache = AscCache.Memory; - Logger = options.Get("ASC.Indexer"); - FactoryIndexer = factoryIndexer; + public FactoryIndexerHelper(ICacheNotify cacheNotify) + { + cacheNotify.Subscribe((a) => + { + if (a.LastIndexed != 0) + { + LastIndexed = new DateTime(a.LastIndexed); + } + Indexing = a.Indexing; + }, CacheNotifyAction.Any); } - - public bool Support(T t) where T : class, ISearchItem - { - if (!FactoryIndexer.CheckState()) return false; - - var cacheTime = DateTime.UtcNow.AddMinutes(15); - var key = "elasticsearch " + t.IndexName; - try - { - var cacheValue = Cache.Get(key); - if (!string.IsNullOrEmpty(cacheValue)) - { - return Convert.ToBoolean(cacheValue); - } - - //TODO: - //var service = new Service.Service(); - - //var result = service.Support(t.IndexName); - - //Cache.Insert(key, result.ToString(CultureInfo.InvariantCulture).ToLower(), cacheTime); - - return true; - } - catch (Exception e) - { - Cache.Insert(key, "false", cacheTime); - Logger.Error("FactoryIndexer CheckState", e); - return false; - } - } - } public interface IFactoryIndexer @@ -111,34 +83,30 @@ namespace ASC.ElasticSearch public ILog Logger { get; } - public FactoryIndexerHelper FactoryIndexerHelper { get; } public TenantManager TenantManager { get; } public SearchSettingsHelper SearchSettingsHelper { get; } public FactoryIndexer FactoryIndexerCommon { get; } public BaseIndexer Indexer { get; } - public Client Client { get; } public IServiceProvider ServiceProvider { get; } public string IndexName { get => Indexer.IndexName; } + public ICache Cache { get; } public virtual string SettingsTitle { get => ""; } public FactoryIndexer( IOptionsMonitor options, - FactoryIndexerHelper factoryIndexerSupport, TenantManager tenantManager, SearchSettingsHelper searchSettingsHelper, FactoryIndexer factoryIndexer, BaseIndexer baseIndexer, - Client client, IServiceProvider serviceProvider) - { + { + Cache = AscCache.Memory; Logger = options.Get("ASC.Indexer"); - FactoryIndexerHelper = factoryIndexerSupport; TenantManager = tenantManager; SearchSettingsHelper = searchSettingsHelper; FactoryIndexerCommon = factoryIndexer; Indexer = baseIndexer; - Client = client; ServiceProvider = serviceProvider; Indexer.CreateIfNotExist(ServiceProvider.GetService()); @@ -147,7 +115,7 @@ namespace ASC.ElasticSearch public bool TrySelect(Expression, Selector>> expression, out IReadOnlyCollection result) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t) || !Indexer.CheckExist(t)) + if (!Support(t) || !Indexer.CheckExist(t)) { result = new List(); return false; @@ -169,7 +137,7 @@ namespace ASC.ElasticSearch public bool TrySelectIds(Expression, Selector>> expression, out List result) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t) || !Indexer.CheckExist(t)) + if (!Support(t) || !Indexer.CheckExist(t)) { result = new List(); return false; @@ -192,7 +160,7 @@ namespace ASC.ElasticSearch public bool TrySelectIds(Expression, Selector>> expression, out List result, out long total) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t) || !Indexer.CheckExist(t)) + if (!Support(t) || !Indexer.CheckExist(t)) { result = new List(); total = 0; @@ -222,7 +190,7 @@ namespace ASC.ElasticSearch public bool Index(T data, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return false; + if (!Support(t)) return false; try { @@ -239,7 +207,7 @@ namespace ASC.ElasticSearch public void Index(List data, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t) || !data.Any()) return; + if (!Support(t) || !data.Any()) return; try { @@ -271,7 +239,7 @@ namespace ASC.ElasticSearch public void Update(T data, bool immediately = true, params Expression>[] fields) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; try { @@ -286,7 +254,7 @@ namespace ASC.ElasticSearch public void Update(T data, UpdateAction action, Expression> field, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; try { @@ -301,7 +269,7 @@ namespace ASC.ElasticSearch public void Update(T data, Expression, Selector>> expression, bool immediately = true, params Expression>[] fields) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; try { @@ -317,7 +285,7 @@ namespace ASC.ElasticSearch public void Update(T data, Expression, Selector>> expression, UpdateAction action, Expression> fields, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; try { @@ -333,7 +301,7 @@ namespace ASC.ElasticSearch public void Delete(T data, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; try { @@ -348,7 +316,7 @@ namespace ASC.ElasticSearch public void Delete(Expression, Selector>> expression, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; var tenant = TenantManager.GetCurrentTenant().TenantId; @@ -365,35 +333,35 @@ namespace ASC.ElasticSearch public Task IndexAsync(T data, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return Task.FromResult(false); + if (!Support(t)) return Task.FromResult(false); return Queue(() => Indexer.Index(data, immediately)); } public Task IndexAsync(List data, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return Task.FromResult(false); + if (!Support(t)) return Task.FromResult(false); return Queue(() => Indexer.Index(data, immediately)); } public Task UpdateAsync(T data, bool immediately = true, params Expression>[] fields) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return Task.FromResult(false); + if (!Support(t)) return Task.FromResult(false); return Queue(() => Indexer.Update(data, immediately, fields)); } public Task DeleteAsync(T data, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return Task.FromResult(false); + if (!Support(t)) return Task.FromResult(false); return Queue(() => Indexer.Delete(data, immediately)); } public Task DeleteAsync(Expression, Selector>> expression, bool immediately = true) { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return Task.FromResult(false); + if (!Support(t)) return Task.FromResult(false); var tenant = TenantManager.GetCurrentTenant().TenantId; return Queue(() => Indexer.Delete(expression, tenant, immediately)); } @@ -402,14 +370,14 @@ namespace ASC.ElasticSearch public void Flush() { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; Indexer.Flush(); } public void Refresh() { var t = ServiceProvider.GetService(); - if (!FactoryIndexerHelper.Support(t)) return; + if (!Support(t)) return; Indexer.Refresh(); } @@ -447,11 +415,44 @@ namespace ASC.ElasticSearch { Indexer.ReIndex(); } + + public bool Support(T t) + { + if (!FactoryIndexerCommon.CheckState()) return false; + + var cacheTime = DateTime.UtcNow.AddMinutes(15); + var key = "elasticsearch " + t.IndexName; + try + { + var cacheValue = Cache.Get(key); + if (!string.IsNullOrEmpty(cacheValue)) + { + return Convert.ToBoolean(cacheValue); + } + + //TODO: + //var service = new Service.Service(); + + //var result = service.Support(t.IndexName); + + //Cache.Insert(key, result.ToString(CultureInfo.InvariantCulture).ToLower(), cacheTime); + + return true; + } + catch (Exception e) + { + Cache.Insert(key, "false", cacheTime); + Logger.Error("FactoryIndexer CheckState", e); + return false; + } + } } public class FactoryIndexer { - private static ICache cache = AscCache.Memory; + private static ICache cache = AscCache.Memory; + + public FactoryIndexerHelper FactoryIndexerHelper { get; } internal ILifetimeScope Builder { get; set; } internal static bool Init { get; set; } public ILog Log { get; } @@ -459,20 +460,23 @@ namespace ASC.ElasticSearch public CoreBaseSettings CoreBaseSettings { get; } public FactoryIndexer( - ILifetimeScope container, + ILifetimeScope container, + FactoryIndexerHelper factoryIndexerHelper, Client client, IOptionsMonitor options, - CoreBaseSettings coreBaseSettings) : this(null, client, options, coreBaseSettings) + CoreBaseSettings coreBaseSettings) : this(null, factoryIndexerHelper, client, options, coreBaseSettings) { Builder = container; } public FactoryIndexer( - IContainer container, + IContainer container, + FactoryIndexerHelper factoryIndexerHelper, Client client, IOptionsMonitor options, CoreBaseSettings coreBaseSettings) { + FactoryIndexerHelper = factoryIndexerHelper; Client = client; CoreBaseSettings = coreBaseSettings; @@ -550,12 +554,12 @@ namespace ASC.ElasticSearch State state = null; if (CoreBaseSettings.Standalone) - { - //TODO - //using (var service = new ServiceClient()) - //{ - // state = service.GetState(); - //} + { + state = new State + { + Indexing = FactoryIndexerHelper.Indexing, + LastIndexed = FactoryIndexerHelper.LastIndexed != DateTime.MinValue ? FactoryIndexerHelper.LastIndexed : default(DateTime?) + }; if (state.LastIndexed.HasValue) { @@ -566,7 +570,7 @@ namespace ASC.ElasticSearch return new { state, - //indices, + indices, status = CheckState() }; } @@ -591,19 +595,13 @@ namespace ASC.ElasticSearch { public static DIHelper AddFactoryIndexerService(this DIHelper services) { + services.TryAddSingleton(); services.TryAddScoped(); return services .AddClientService() .AddCoreBaseSettingsService(); } - public static DIHelper AddFactoryIndexerHelperService(this DIHelper services) - { - services.TryAddScoped(); - return services - .AddFactoryIndexerService(); - } - public static DIHelper AddFactoryIndexerService(this DIHelper services, bool addBase = true) where T : class, ISearchItem { if (addBase) @@ -614,7 +612,6 @@ namespace ASC.ElasticSearch services.TryAddScoped>(); return services - .AddFactoryIndexerHelperService() .AddTenantManagerService() .AddFactoryIndexerService() .AddClientService() diff --git a/common/services/ASC.ElasticSearch/Service/Launcher.cs b/common/services/ASC.ElasticSearch/Service/Launcher.cs index 792151da5f..c498898430 100644 --- a/common/services/ASC.ElasticSearch/Service/Launcher.cs +++ b/common/services/ASC.ElasticSearch/Service/Launcher.cs @@ -46,24 +46,25 @@ namespace ASC.ElasticSearch { private ILog Log { get; } private ICacheNotify Notify { get; } + public ICacheNotify IndexNotify { get; } public IServiceProvider ServiceProvider { get; } public IContainer Container { get; } private bool IsStarted { get; set; } - private string Indexing { get; set; } private CancellationTokenSource CancellationTokenSource { get; set; } private Timer Timer { get; set; } - private DateTime? LastIndexed { get; set; } private TimeSpan Period { get; set; } public ServiceLauncher( IOptionsMonitor options, ICacheNotify notify, + ICacheNotify indexNotify, IServiceProvider serviceProvider, IContainer container, Settings settings) { Log = options.Get("ASC.Indexer"); Notify = notify; + IndexNotify = indexNotify; ServiceProvider = serviceProvider; Container = container; CancellationTokenSource = new CancellationTokenSource(); @@ -142,9 +143,8 @@ namespace ASC.ElasticSearch } Timer.Change(Period, Period); - LastIndexed = DateTime.UtcNow; + IndexNotify.Publish(new IndexAction() { Indexing = "", LastIndexed = DateTime.Now.Ticks }, CacheNotifyAction.Any); IsStarted = false; - Indexing = null; } public void IndexProduct(IFactoryIndexer product, bool reindex) @@ -170,7 +170,7 @@ namespace ASC.ElasticSearch if (!IsStarted) return; Log.DebugFormat("Product {0}", product.IndexName); - Indexing = product.IndexName; + IndexNotify.Publish(new IndexAction() { Indexing = product.IndexName, LastIndexed = 0 }, CacheNotifyAction.Any); product.IndexAll(); } catch (Exception e) diff --git a/common/services/ASC.ElasticSearch/protos/SearchItem.proto b/common/services/ASC.ElasticSearch/protos/SearchItem.proto index a3c0ecaec1..486eff724e 100644 --- a/common/services/ASC.ElasticSearch/protos/SearchItem.proto +++ b/common/services/ASC.ElasticSearch/protos/SearchItem.proto @@ -9,4 +9,9 @@ message ClearIndexAction { message ReIndexAction { int32 Tenant = 1; repeated string Names = 2; +} + +message IndexAction { + string Indexing = 1; + int64 LastIndexed = 2; } \ No newline at end of file diff --git a/products/ASC.Files/Server/Core/Search/FactoryIndexerFile.cs b/products/ASC.Files/Server/Core/Search/FactoryIndexerFile.cs index 9aacc540fb..2aa4bbeb4f 100644 --- a/products/ASC.Files/Server/Core/Search/FactoryIndexerFile.cs +++ b/products/ASC.Files/Server/Core/Search/FactoryIndexerFile.cs @@ -47,15 +47,13 @@ namespace ASC.Web.Files.Core.Search public FactoryIndexerFile( IOptionsMonitor options, - FactoryIndexerHelper factoryIndexerSupport, TenantManager tenantManager, SearchSettingsHelper searchSettingsHelper, FactoryIndexer factoryIndexer, BaseIndexer baseIndexer, - Client client, IServiceProvider serviceProvider, IDaoFactory daoFactory) - : base(options, factoryIndexerSupport, tenantManager, searchSettingsHelper, factoryIndexer, baseIndexer, client, serviceProvider) + : base(options, tenantManager, searchSettingsHelper, factoryIndexer, baseIndexer, serviceProvider) { DaoFactory = daoFactory; } diff --git a/products/ASC.Files/Server/Core/Search/FactoryIndexerFolder.cs b/products/ASC.Files/Server/Core/Search/FactoryIndexerFolder.cs index 93a64b9367..9977607f30 100644 --- a/products/ASC.Files/Server/Core/Search/FactoryIndexerFolder.cs +++ b/products/ASC.Files/Server/Core/Search/FactoryIndexerFolder.cs @@ -47,15 +47,13 @@ namespace ASC.Web.Files.Core.Search public FactoryIndexerFolder( IOptionsMonitor options, - FactoryIndexerHelper factoryIndexerSupport, TenantManager tenantManager, SearchSettingsHelper searchSettingsHelper, FactoryIndexer factoryIndexer, BaseIndexer baseIndexer, - Client client, IServiceProvider serviceProvider, IDaoFactory daoFactory) - : base(options, factoryIndexerSupport, tenantManager, searchSettingsHelper, factoryIndexer, baseIndexer, client, serviceProvider) + : base(options, tenantManager, searchSettingsHelper, factoryIndexer, baseIndexer, serviceProvider) { DaoFactory = daoFactory; }