CompressEnginer2.cs 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
//==============================================================
//  Copyright (C) 2019  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2019/12/27 18:45:02.
//  Version 1.0
//  种道洋
//==============================================================
using DBRuntime.His;
using System;
using System.Collections.Generic;
using System.Diagnostics;
13
using System.Linq;
14 15 16 17 18 19 20 21
using System.Text;
using System.Threading;

namespace Cdy.Tag
{
    /// <summary>
    /// 
    /// </summary>
cdy816's avatar
cdy816 已提交
22
    public class CompressEnginer2 : IDataCompress2,IDisposable
23 24 25 26 27 28 29 30
    {

        #region ... Variables  ...
        /// <summary>
        /// 
        /// </summary>
        private ManualResetEvent resetEvent;

31 32
        private ManualResetEvent mManualEvent;

33 34 35 36
        private ManualResetEvent closedEvent;

        private Thread mCompressThread;

37 38
        private Thread mManualCompressThread;

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        private bool mIsClosed = false;

        private HisDataMemoryBlockCollection mSourceMemory;


        /// <summary>
        /// 
        /// </summary>
        private Dictionary<int, CompressMemory2> mTargetMemorys = new Dictionary<int, CompressMemory2>();

        private DateTime mCurrentTime;

        private IHisEngine2 mHisTagService;

        //private long mTotalSize = 0;

55
        private int mLastDataRegionId;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...

        ///// <summary>
        ///// 
        ///// </summary>
        //public CompressEnginer2(long size)
        //{
        //    mTotalSize = size;
        //}

        #endregion ...Constructor...

        #region ... Properties ...

        /// <summary>
        /// 单个文件内变量的个数
        /// </summary>
        public int TagCountOneFile { get; set; } = 100000;

        #endregion ...Properties...

        #region ... Methods    ...

        /// <summary>
        /// 
        /// </summary>
        public void Init()
        {
cdy816's avatar
cdy816 已提交
91 92
            CompressUnitManager2.Manager.Init();

cdy816's avatar
cdy816 已提交
93
            mHisTagService = ServiceLocator.Locator.Resolve<IHisEngine2>();
94
            CompressMemory2.TagCountPerMemory = TagCountOneFile;
cdy816's avatar
cdy816 已提交
95

96 97 98 99 100 101 102 103
            foreach (var vm in mTargetMemorys)
            {
                vm.Value.Dispose();
            }
            mTargetMemorys.Clear();

            var histag = mHisTagService.ListAllTags();
            //计算数据区域个数
104
            mLastDataRegionId = -1;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            foreach (var vv in histag)
            {
                var id = vv.Id;
                var did = id / TagCountOneFile;
                if (mLastDataRegionId != did)
                {
                    mTargetMemorys.Add(did, new CompressMemory2() { Id = did,Name="CompressTarget"+did });
                    mLastDataRegionId = did;
                }
            }

            foreach (var vv in mTargetMemorys)
            {
                vv.Value.Init(ServiceLocator.Locator.Resolve<IHisEngine2>().CurrentMemory);
                LoggerService.Service.Info("CompressEnginer", "Cal CompressMemory memory size:" + vv.Value.Length / 1024.0 / 1024 + "M", ConsoleColor.Cyan);
            }
        }

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tags"></param>
        public void ReSizeTagCompress(List<HisTag> tags)
        {
            List<CompressMemory2> ctmp = new List<CompressMemory2>();
            ctmp.Add(mTargetMemorys.Last().Value);

            foreach (var vv in tags)
            {
                var id = vv.Id;
                var did = id / TagCountOneFile;
                if (mLastDataRegionId != did)
                {
                    var vvv = new CompressMemory2() { Id = did, Name = "CompressTarget" + did };
                    mTargetMemorys.Add(did, vvv);
                    mLastDataRegionId = did;
                    ctmp.Add(vvv);
                }
            }

            foreach (var vv in ctmp)
            {
                vv.ReInit(ServiceLocator.Locator.Resolve<IHisEngine2>().CurrentMemory);
            }
        }

151 152 153 154 155
        /// <summary>
        /// 
        /// </summary>
        public void Start()
        {
cdy816's avatar
cdy816 已提交
156
            LoggerService.Service.Info("CompressEnginer", "start to Start");
157
            mIsClosed = false;
cdy816's avatar
cdy816 已提交
158
            //Init();
159 160
            resetEvent = new ManualResetEvent(false);
            closedEvent = new ManualResetEvent(false);
161 162 163

            mManualEvent = new ManualResetEvent(false);

164 165 166
            mCompressThread = new Thread(ThreadPro);
            mCompressThread.IsBackground = true;
            mCompressThread.Start();
167 168 169 170

            mManualCompressThread = new Thread(ManualThreadPro);
            mManualCompressThread.IsBackground = true;
            mManualCompressThread.Start();
171 172 173 174 175 176 177
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop()
        {
cdy816's avatar
cdy816 已提交
178 179
            LoggerService.Service.Info("CompressEnginer", "start to stop");

180 181
            mIsClosed = true;
            resetEvent.Set();
182
            mManualEvent.Set();
183 184 185 186
            closedEvent.WaitOne();

            resetEvent.Dispose();
            closedEvent.Dispose();
cdy816's avatar
cdy816 已提交
187 188 189 190 191 192

            foreach(var vv in mTargetMemorys)
            {
                while (vv.Value.IsBusy())
                    vv.Value.DecRef();
            }
193 194 195

            while (mCompressThread.IsAlive) Thread.Sleep(1);
            while (mManualCompressThread.IsAlive) Thread.Sleep(1);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataMemory"></param>
        public void RequestToCompress(HisDataMemoryBlockCollection dataMemory)
        {
            mSourceMemory = dataMemory;
            mCurrentTime = mSourceMemory.CurrentDatetime;
            foreach(var vv in mTargetMemorys)
            {
                vv.Value.CurrentTime = mCurrentTime;
            }
            resetEvent.Set();
        }

213 214


215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckIsBusy()
        {
            foreach(var vv in mTargetMemorys)
            {
                if(vv.Value.IsBusy())
                {
                    return true;
                }
            }
            return false;
        }

231 232 233 234 235 236 237 238 239 240 241 242 243
        /// <summary>
        /// 等待空闲
        /// </summary>
        public void WaitForReady()
        {
            while (CheckIsBusy())
            {
                Thread.Sleep(10);
            }
        }



244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        /// <summary>
        /// 
        /// </summary>

        private void ThreadPro()
        {
            ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2);
            while (!mIsClosed)
            {
                try
                {
                    resetEvent.WaitOne();
                    resetEvent.Reset();
                    if (mIsClosed)
                        break;

                    //#if DEBUG 
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    LoggerService.Service.Info("Compress", "********开始执行压缩********", ConsoleColor.Blue);
                    //#endif
                    var sm = mSourceMemory;

                    while (CheckIsBusy())
                    {
                        LoggerService.Service.Warn("Compress", "压缩出现阻塞");
                        Thread.Sleep(500);
                    }

                    System.Threading.Tasks.Parallel.ForEach(mTargetMemorys, (mm) =>
                    {
                        ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2);
                        mm.Value.Compress(sm);
                    });

279 280
                    sm.Clear();
                  //  ServiceLocator.Locator.Resolve<IHisEngine2>().ClearMemoryHisData(sm);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                    sm.MakeMemoryNoBusy();

                    ServiceLocator.Locator.Resolve<IDataSerialize2>().RequestToSave();

                    //#if DEBUG
                    sw.Stop();
                    LoggerService.Service.Info("Compress", ">>>>>>>>>压缩完成>>>>>>>>>" + " ElapsedMilliseconds:" + sw.ElapsedMilliseconds, ConsoleColor.Blue);
                    //#endif
                }
                catch(Exception ex)
                {
                    LoggerService.Service.Erro("Compress", ex.Message+ex.StackTrace);
                }

            }
            closedEvent.Set();

            LoggerService.Service.Info("Compress", "压缩线程退出!");

        }

cdy816's avatar
cdy816 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {

            foreach (var vv in mTargetMemorys)
            {
                while (vv.Value.IsBusy()) Thread.Sleep(1);
                vv.Value.Dispose();
            }
            mTargetMemorys.Clear();
            mSourceMemory = null;
            mHisTagService = null;
        }

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public void RequestManualToCompress(ManualHisDataMemoryBlock data)
        {
            foreach (var vv in mTargetMemorys)
            {
                if (data.Id >= vv.Value.Id * TagCountOneFile && data.Id < (vv.Value.Id + 1) * TagCountOneFile)
                {
                    vv.Value.AddRequestManualToCompress(data);
                }
            }
            mManualEvent.Set();
        }

        /// <summary>
        /// 
        /// </summary>
        private void ManualThreadPro()
        {
            ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2);
            while (!mIsClosed)
            {
                mManualEvent.WaitOne();
                mManualEvent.Reset();
                if (mIsClosed)
                    break;

                foreach (var vv in mTargetMemorys.Values)
                {
                    vv.RequestManualToCompress();
                }

            }
        }

355 356 357 358 359 360 361 362
        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...

    }
}