/// <summary> /// 执行 /// </summary> public void Exec() { try { //记录一下对当前相关数据表的版本号 var startVerstions = GetVerstions(); int threadCount = _indexConfig.ThreadCount; _log.Info(string.Format("{0}:开始创建索引器...", _indexType)); IIndexer indexer = _indexerContainer.ResolveIndexer(_indexType, _indexConfig.PageSize, _es); if (indexer.IsPaping) { _log.Info(string.Format("{0}:开始索引分页数据...", _indexType)); } else { _log.Info(string.Format("{0}:开始索数据...", _indexType)); threadCount = 1; } Action action = indexer.HandleData; var st = new Stopwatch(); st.Start(); var factory = new TaskFactory(); var taskArray = new Task[threadCount]; for (Int32 i = 0; i < threadCount; i++) { taskArray[i] = factory.StartNew(action); } factory.ContinueWhenAll(taskArray, tks => { //错误信息 if (Error(tks)) { _log.Info(string.Format("{0}:开始切换别名:{1}\r\n", _indexType, _indexConfig.Alias)); //将别名切换到新索引 _es.SetAlias(_indexConfig.Alias); _log.Info(string.Format("{0}:切换别名完成:{1}\r\n", _indexType, _indexConfig.Alias)); //记录一下运行结束时的版本号 var endVerstions = GetVerstions(); CheckStartEndVenstions(startVerstions, endVerstions); _log.Info(string.Format("{0}:运行正常结束,耗时{1}毫秒...\r\n", _indexType, st.ElapsedMilliseconds)); } else { _log.Info(string.Format("{0}:运行过程中有错误\r\n", _indexType)); //未成功时删除掉已创建索引 _es.DeleteIndex(tempIndexName); _log.Info(string.Format("{0}:运行过程中有错误已结束,耗时{1}毫秒...\r\n", _indexType, st.ElapsedMilliseconds)); } tks.ToList().ForEach(n => n.Dispose()); }).Wait(); } catch { //有异常删除已创建的索引 _es.DeleteIndex(tempIndexName); throw; } }
/// <summary> /// 索引处理 /// </summary> /// <param name="type">索引类型</param> /// <param name="log"></param> public DataInit(string type, ILog log) { _log = log; _indexType = type; _indexConfig = IndexConfigHelper.IndexConfig(type); if (_indexConfig == null) { throw new Exception(string.Format("{0}:未找到对应索引类型的配置信息!", _indexType)); } _log.Info(string.Format("{0}:开始操作索引...", type)); tempIndexName = _indexConfig.IndexName + DateTime.Now.ToString("yyyyMMddHHmmss"); //es操作对象 _es = new EsHandle(tempIndexName, _log); //创建一个索引 _es.CreateIndex(_indexConfig.Replicas, _indexConfig.Shards); try { _log.Info(string.Format("{0}:索引操作完成...", type)); //索引器容器 _indexerContainer = new IndexerContainer(_log); _log.Info(string.Format("{0}:开始索引数据...", type)); } catch { //有异常删除已创建的索引 _es.DeleteIndex(tempIndexName); throw; } }