DocSpace-client/common/services/ASC.ElasticSearch/Engine/FactoryIndexer.cs

633 lines
19 KiB
C#
Raw Normal View History

2020-01-24 13:07:51 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2020-01-24 13:07:51 +00:00
using ASC.Common.Caching;
using ASC.Common.Logging;
2020-02-17 08:58:14 +00:00
using ASC.Core;
2020-01-24 13:07:51 +00:00
using ASC.Core.Tenants;
using ASC.ElasticSearch.Core;
2020-02-17 08:58:14 +00:00
2020-01-24 13:07:51 +00:00
using Autofac;
2020-02-17 08:58:14 +00:00
2020-01-24 13:07:51 +00:00
using Elasticsearch.Net;
2020-02-17 08:58:14 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
2020-01-24 13:07:51 +00:00
using Nest;
namespace ASC.ElasticSearch
2020-10-19 15:53:15 +00:00
{
[Singletone]
2020-02-17 08:58:14 +00:00
public class FactoryIndexerHelper
2020-05-06 11:32:26 +00:00
{
public DateTime LastIndexed { get; set; }
public string Indexing { get; set; }
2020-02-17 08:58:14 +00:00
2020-05-06 11:32:26 +00:00
public FactoryIndexerHelper(ICacheNotify<IndexAction> cacheNotify)
{
cacheNotify.Subscribe((a) =>
{
if (a.LastIndexed != 0)
{
LastIndexed = new DateTime(a.LastIndexed);
}
Indexing = a.Indexing;
}, CacheNotifyAction.Any);
2020-02-17 08:58:14 +00:00
}
}
2020-04-28 15:11:10 +00:00
public interface IFactoryIndexer
{
void IndexAll();
string IndexName { get; }
void ReIndex();
2020-04-29 16:01:38 +00:00
string SettingsTitle { get; }
2020-04-28 15:11:10 +00:00
}
2020-10-19 15:53:15 +00:00
[Scope]
2020-04-28 15:11:10 +00:00
public class FactoryIndexer<T> : IFactoryIndexer where T : class, ISearchItem
2020-01-24 13:07:51 +00:00
{
private static readonly TaskScheduler Scheduler = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, 10).ConcurrentScheduler;
2020-02-17 08:58:14 +00:00
public ILog Logger { get; }
2020-08-12 09:58:08 +00:00
protected TenantManager TenantManager { get; }
private SearchSettingsHelper SearchSettingsHelper { get; }
private FactoryIndexer FactoryIndexerCommon { get; }
protected BaseIndexer<T> Indexer { get; }
private IServiceProvider ServiceProvider { get; }
2020-04-27 16:59:52 +00:00
public string IndexName { get => Indexer.IndexName; }
2020-05-06 11:32:26 +00:00
public ICache Cache { get; }
2020-04-29 16:01:38 +00:00
public virtual string SettingsTitle { get => ""; }
2020-02-17 08:58:14 +00:00
public FactoryIndexer(
IOptionsMonitor<ILog> options,
TenantManager tenantManager,
SearchSettingsHelper searchSettingsHelper,
FactoryIndexer factoryIndexer,
BaseIndexer<T> baseIndexer,
IServiceProvider serviceProvider)
2020-05-06 11:32:26 +00:00
{
Cache = AscCache.Memory;
2020-02-17 08:58:14 +00:00
Logger = options.Get("ASC.Indexer");
TenantManager = tenantManager;
SearchSettingsHelper = searchSettingsHelper;
FactoryIndexerCommon = factoryIndexer;
Indexer = baseIndexer;
ServiceProvider = serviceProvider;
2020-01-24 13:07:51 +00:00
}
public bool TrySelect(Expression<Func<Selector<T>, Selector<T>>> expression, out IReadOnlyCollection<T> result)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t) || !Indexer.CheckExist(t))
2020-01-24 13:07:51 +00:00
{
result = new List<T>();
return false;
}
try
{
result = Indexer.Select(expression);
2020-02-17 08:58:14 +00:00
}
catch (Exception e)
{
Logger.Error("Select", e);
result = new List<T>();
return false;
2020-01-24 13:07:51 +00:00
}
return true;
}
public bool TrySelectIds(Expression<Func<Selector<T>, Selector<T>>> expression, out List<int> result)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t) || !Indexer.CheckExist(t))
2020-01-24 13:07:51 +00:00
{
result = new List<int>();
return false;
2020-02-17 08:58:14 +00:00
}
try
{
result = Indexer.Select(expression, true).Select(r => r.Id).ToList();
}
catch (Exception e)
{
Logger.Error("Select", e);
result = new List<int>();
return false;
2020-01-24 13:07:51 +00:00
}
return true;
}
public bool TrySelectIds(Expression<Func<Selector<T>, Selector<T>>> expression, out List<int> result, out long total)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t) || !Indexer.CheckExist(t))
2020-01-24 13:07:51 +00:00
{
result = new List<int>();
total = 0;
return false;
2020-02-17 08:58:14 +00:00
}
try
{
result = Indexer.Select(expression, true, out total).Select(r => r.Id).ToList();
}
catch (Exception e)
{
Logger.Error("Select", e);
total = 0;
result = new List<int>();
return false;
2020-01-24 13:07:51 +00:00
}
return true;
}
public bool CanSearchByContent()
{
return SearchSettingsHelper.CanSearchByContent<T>(TenantManager.GetCurrentTenant().TenantId);
}
public bool Index(T data, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return false;
2020-01-24 13:07:51 +00:00
try
{
Indexer.Index(data, immediately);
return true;
}
catch (Exception e)
{
Logger.Error("Index", e);
}
return false;
}
public void Index(List<T> data, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t) || !data.Any()) return;
2020-01-24 13:07:51 +00:00
try
{
Indexer.Index(data, immediately);
2020-02-17 08:58:14 +00:00
}
catch (AggregateException e)
{
if (e.InnerExceptions.Count == 0) throw;
var inner = e.InnerExceptions.OfType<ElasticsearchClientException>().FirstOrDefault();
Logger.Error(inner);
if (inner != null)
{
Logger.Error("inner", inner.Response.OriginalException);
if (inner.Response.HttpStatusCode == 413)
{
data.ForEach(r => Index(r, immediately));
}
}
else
{
throw;
}
}
}
2020-01-24 13:07:51 +00:00
public void Update(T data, bool immediately = true, params Expression<Func<T, object>>[] fields)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-01-24 13:07:51 +00:00
try
{
Indexer.Update(data, immediately, fields);
}
catch (Exception e)
{
Logger.Error("Update", e);
}
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
public void Update(T data, UpdateAction action, Expression<Func<T, IList>> field, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-02-11 15:10:30 +00:00
2020-01-24 13:07:51 +00:00
try
{
Indexer.Update(data, action, field, immediately);
}
catch (Exception e)
{
Logger.Error("Update", e);
}
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
public void Update(T data, Expression<Func<Selector<T>, Selector<T>>> expression, bool immediately = true, params Expression<Func<T, object>>[] fields)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-02-11 15:10:30 +00:00
2020-01-24 13:07:51 +00:00
try
2020-02-17 08:58:14 +00:00
{
2020-01-24 13:07:51 +00:00
var tenant = TenantManager.GetCurrentTenant().TenantId;
Indexer.Update(data, expression, tenant, immediately, fields);
}
catch (Exception e)
{
Logger.Error("Update", e);
}
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
public void Update(T data, Expression<Func<Selector<T>, Selector<T>>> expression, UpdateAction action, Expression<Func<T, IList>> fields, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-02-11 15:10:30 +00:00
2020-01-24 13:07:51 +00:00
try
2020-02-17 08:58:14 +00:00
{
2020-01-24 13:07:51 +00:00
var tenant = TenantManager.GetCurrentTenant().TenantId;
Indexer.Update(data, expression, tenant, action, fields, immediately);
}
catch (Exception e)
{
Logger.Error("Update", e);
}
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
public void Delete(T data, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-02-11 15:10:30 +00:00
2020-01-24 13:07:51 +00:00
try
{
Indexer.Delete(data, immediately);
}
catch (Exception e)
{
Logger.Error("Delete", e);
}
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
public void Delete(Expression<Func<Selector<T>, Selector<T>>> expression, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-02-11 15:10:30 +00:00
2020-01-24 13:07:51 +00:00
var tenant = TenantManager.GetCurrentTenant().TenantId;
try
2020-02-17 08:58:14 +00:00
{
Indexer.Delete(expression, tenant, immediately);
}
catch (Exception e)
{
Logger.Error("Index", e);
}
}
2020-11-24 10:15:11 +00:00
public async Task<bool> IndexAsync(T data, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
var t = ServiceProvider.GetService<T>();
2020-11-24 10:15:11 +00:00
if (!await SupportAsync(t)) return false;
return await Queue(() => Indexer.Index(data, immediately));
2020-02-17 08:58:14 +00:00
}
2020-11-24 10:15:11 +00:00
public async Task<bool> IndexAsync(List<T> data, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-11-24 10:15:11 +00:00
if (!await SupportAsync(t)) return false;
return await Queue(() => Indexer.Index(data, immediately));
2020-02-17 08:58:14 +00:00
}
2020-11-24 10:15:11 +00:00
public async Task<bool> UpdateAsync(T data, bool immediately = true, params Expression<Func<T, object>>[] fields)
2020-02-17 08:58:14 +00:00
{
var t = ServiceProvider.GetService<T>();
2020-11-24 10:15:11 +00:00
if (!await SupportAsync(t)) return false;
return await Queue(() => Indexer.Update(data, immediately, fields));
2020-02-17 08:58:14 +00:00
}
2020-11-24 10:15:11 +00:00
public async Task<bool> DeleteAsync(T data, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
var t = ServiceProvider.GetService<T>();
2020-11-24 10:15:11 +00:00
if (!await SupportAsync(t)) return false;
return await Queue(() => Indexer.Delete(data, immediately));
2020-02-17 08:58:14 +00:00
}
2020-11-24 10:15:11 +00:00
public async Task<bool> DeleteAsync(Expression<Func<Selector<T>, Selector<T>>> expression, bool immediately = true)
2020-02-17 08:58:14 +00:00
{
var t = ServiceProvider.GetService<T>();
2020-11-24 10:15:11 +00:00
if (!await SupportAsync(t)) return false;
2020-01-24 13:07:51 +00:00
var tenant = TenantManager.GetCurrentTenant().TenantId;
2020-11-24 10:15:11 +00:00
return await Queue(() => Indexer.Delete(expression, tenant, immediately));
2020-01-24 13:07:51 +00:00
}
public void Flush()
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-01-24 13:07:51 +00:00
Indexer.Flush();
}
public void Refresh()
2020-02-17 08:58:14 +00:00
{
2020-02-11 15:10:30 +00:00
var t = ServiceProvider.GetService<T>();
2020-05-06 11:32:26 +00:00
if (!Support(t)) return;
2020-01-24 13:07:51 +00:00
Indexer.Refresh();
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
private Task<bool> Queue(Action actionData)
{
var task = new Task<bool>(() =>
{
try
{
actionData();
return true;
}
catch (AggregateException agg)
2020-02-17 08:58:14 +00:00
{
foreach (var e in agg.InnerExceptions)
{
Logger.Error(e);
2020-01-24 13:07:51 +00:00
}
throw;
}
}, TaskCreationOptions.LongRunning);
2020-10-12 19:39:23 +00:00
task.ConfigureAwait(false);
2020-01-24 13:07:51 +00:00
task.Start(Scheduler);
return task;
2020-04-27 16:59:52 +00:00
}
public virtual void IndexAll()
{
return;
2020-04-28 15:11:10 +00:00
}
public void ReIndex()
{
2020-10-12 19:39:23 +00:00
Indexer.ReIndex();
2020-04-28 15:11:10 +00:00
}
2020-05-06 11:32:26 +00:00
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<string>(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;
}
}
2020-11-24 10:15:11 +00:00
public async Task<bool> SupportAsync(T t)
{
return await FactoryIndexerCommon.CheckStateAsync();
}
2020-01-24 13:07:51 +00:00
}
2020-10-19 15:53:15 +00:00
[Scope]
2020-01-24 13:07:51 +00:00
public class FactoryIndexer
{
2020-09-28 15:42:03 +00:00
private static readonly ICache cache = AscCache.Memory;
2020-05-06 11:32:26 +00:00
2020-08-12 09:58:08 +00:00
private FactoryIndexerHelper FactoryIndexerHelper { get; }
2020-04-29 14:11:31 +00:00
internal ILifetimeScope Builder { get; set; }
2020-02-17 08:58:14 +00:00
internal static bool Init { get; set; }
public ILog Log { get; }
2020-08-12 09:58:08 +00:00
private Client Client { get; }
private CoreBaseSettings CoreBaseSettings { get; }
2020-04-29 14:11:31 +00:00
public FactoryIndexer(
2020-05-06 11:32:26 +00:00
ILifetimeScope container,
FactoryIndexerHelper factoryIndexerHelper,
2020-04-29 14:11:31 +00:00
Client client,
IOptionsMonitor<ILog> options,
2020-01-24 13:07:51 +00:00
CoreBaseSettings coreBaseSettings)
2020-02-19 08:37:52 +00:00
{
2020-11-02 16:27:08 +00:00
Builder = container;
2020-05-06 11:32:26 +00:00
FactoryIndexerHelper = factoryIndexerHelper;
2020-02-19 08:37:52 +00:00
Client = client;
CoreBaseSettings = coreBaseSettings;
2020-01-24 13:07:51 +00:00
try
2020-02-17 08:58:14 +00:00
{
Log = options.Get("ASC.Indexer");
2020-01-24 13:07:51 +00:00
if (container != null)
{
Builder = container;
Init = true;
}
}
catch (Exception e)
2020-02-17 08:58:14 +00:00
{
2020-01-24 13:07:51 +00:00
Log.Fatal("FactoryIndexer", e);
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
}
public bool CheckState(bool cacheState = true)
{
if (!Init) return false;
const string key = "elasticsearch";
if (cacheState)
{
var cacheValue = cache.Get<string>(key);
if (!string.IsNullOrEmpty(cacheValue))
{
return Convert.ToBoolean(cacheValue);
}
}
var cacheTime = DateTime.UtcNow.AddMinutes(15);
try
{
var result = Client.Instance.Ping(new PingRequest());
var isValid = result.IsValid;
Log.DebugFormat("CheckState ping {0}", result.DebugInformation);
2020-11-24 10:15:11 +00:00
if (cacheState)
{
cache.Insert(key, isValid.ToString(CultureInfo.InvariantCulture).ToLower(), cacheTime);
}
return isValid;
}
catch (Exception e)
{
if (cacheState)
{
cache.Insert(key, "false", cacheTime);
}
Log.Error("Ping false", e);
return false;
}
}
public async Task<bool> CheckStateAsync(bool cacheState = true)
{
if (!Init) return false;
const string key = "elasticsearch";
if (cacheState)
{
var cacheValue = cache.Get<string>(key);
if (!string.IsNullOrEmpty(cacheValue))
{
return Convert.ToBoolean(cacheValue);
}
}
var cacheTime = DateTime.UtcNow.AddMinutes(15);
try
{
var result = await Client.Instance.PingAsync(new PingRequest());
var isValid = result.IsValid;
Log.DebugFormat("CheckState ping {0}", result.DebugInformation);
2020-01-24 13:07:51 +00:00
if (cacheState)
{
cache.Insert(key, isValid.ToString(CultureInfo.InvariantCulture).ToLower(), cacheTime);
}
return isValid;
}
catch (Exception e)
{
if (cacheState)
{
cache.Insert(key, "false", cacheTime);
}
Log.Error("Ping false", e);
return false;
}
}
public object GetState(TenantUtil tenantUtil)
{
2020-02-17 08:58:14 +00:00
var indices = CoreBaseSettings.Standalone ?
2020-04-23 17:24:56 +00:00
Client.Instance.Cat.Indices(new CatIndicesRequest { SortByColumns = new[] { "index" } }).Records.Select(r => new
2020-01-24 13:07:51 +00:00
{
2020-02-17 08:58:14 +00:00
r.Index,
r.DocsCount,
2020-01-24 13:07:51 +00:00
r.StoreSize
2020-04-23 17:24:56 +00:00
}) :
2020-01-24 13:07:51 +00:00
null;
State state = null;
if (CoreBaseSettings.Standalone)
2020-05-06 11:32:26 +00:00
{
state = new State
{
Indexing = FactoryIndexerHelper.Indexing,
LastIndexed = FactoryIndexerHelper.LastIndexed != DateTime.MinValue ? FactoryIndexerHelper.LastIndexed : default(DateTime?)
};
2020-01-24 13:07:51 +00:00
if (state.LastIndexed.HasValue)
{
state.LastIndexed = tenantUtil.DateTimeFromUtc(state.LastIndexed.Value);
}
}
return new
{
state,
2020-05-06 11:32:26 +00:00
indices,
2020-01-24 13:07:51 +00:00
status = CheckState()
};
}
public void Reindex(string name)
{
if (!CoreBaseSettings.Standalone) return;
var generic = typeof(BaseIndexer<>);
2020-04-29 14:11:31 +00:00
var indexers = Builder.Resolve<IEnumerable<IFactoryIndexer>>()
2020-02-17 08:58:14 +00:00
.Where(r => string.IsNullOrEmpty(name) || r.IndexName == name)
2020-04-29 14:11:31 +00:00
.Select(r => (IFactoryIndexer)Activator.CreateInstance(generic.MakeGenericType(r.GetType()), r));
2020-02-17 08:58:14 +00:00
2020-01-24 13:07:51 +00:00
foreach (var indexer in indexers)
2020-02-17 08:58:14 +00:00
{
2020-01-24 13:07:51 +00:00
indexer.ReIndex();
}
}
2020-02-17 08:58:14 +00:00
}
2020-01-24 13:07:51 +00:00
}