CompressEnginer2.cs 8.1 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    {

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

        private ManualResetEvent closedEvent;

        private Thread mCompressThread;

        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;

51
        private int mLastDataRegionId;
52 53 54 55 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

        #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 已提交
87 88
            CompressUnitManager2.Manager.Init();

cdy816's avatar
cdy816 已提交
89
            mHisTagService = ServiceLocator.Locator.Resolve<IHisEngine2>();
90
            CompressMemory2.TagCountPerMemory = TagCountOneFile;
cdy816's avatar
cdy816 已提交
91

92 93 94 95 96 97 98 99
            foreach (var vm in mTargetMemorys)
            {
                vm.Value.Dispose();
            }
            mTargetMemorys.Clear();

            var histag = mHisTagService.ListAllTags();
            //计算数据区域个数
100
            mLastDataRegionId = -1;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            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);
            }
        }

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        /// <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);
            }
        }

147 148 149 150 151 152
        /// <summary>
        /// 
        /// </summary>
        public void Start()
        {
            mIsClosed = false;
cdy816's avatar
cdy816 已提交
153
            //Init();
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            resetEvent = new ManualResetEvent(false);
            closedEvent = new ManualResetEvent(false);
            mCompressThread = new Thread(ThreadPro);
            mCompressThread.IsBackground = true;
            mCompressThread.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop()
        {
            mIsClosed = true;
            resetEvent.Set();
            closedEvent.WaitOne();

            resetEvent.Dispose();
            closedEvent.Dispose();
        }

        /// <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();
        }

189 190


191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckIsBusy()
        {
            foreach(var vv in mTargetMemorys)
            {
                if(vv.Value.IsBusy())
                {
                    return true;
                }
            }
            return false;
        }

207 208 209 210 211 212 213 214 215 216 217 218 219
        /// <summary>
        /// 等待空闲
        /// </summary>
        public void WaitForReady()
        {
            while (CheckIsBusy())
            {
                Thread.Sleep(10);
            }
        }



220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        /// <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);
                    });

255 256
                    sm.Clear();
                  //  ServiceLocator.Locator.Resolve<IHisEngine2>().ClearMemoryHisData(sm);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                    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 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        /// <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;
        }

294 295 296 297 298 299 300 301
        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...

    }
}