ntminer 5 years ago
parent
commit
0b45349d5d

+ 8 - 3
src/NTMinerServer/Core/Redis/IGpuNameRedis.cs

@@ -4,8 +4,13 @@ using System.Threading.Tasks;
 
 namespace NTMiner.Core.Redis {
     public interface IGpuNameRedis {
-        Task<List<GpuName>> GetAllAsync();
-        Task SetAsync(GpuName gpuName);
-        Task SetAsync(List<GpuName> gpuNames);
+        Task<List<GpuName>> GetAllRawAsync();
+        /// <summary>
+        /// 系统自动维护GpuNameSet时走批量接口,可以认为这个接口是用于人工添加新上市的Gpu的。
+        /// </summary>
+        /// <param name="gpuName"></param>
+        /// <returns></returns>
+        Task SetRawAsync(GpuName gpuName);
+        Task SetRawAsync(List<GpuName> gpuNames);
     }
 }

+ 7 - 7
src/NTMinerServer/Core/Redis/Impl/GpuNameRedis.cs

@@ -6,16 +6,16 @@ using System.Threading.Tasks;
 
 namespace NTMiner.Core.Redis.Impl {
     public class GpuNameRedis : IGpuNameRedis {
-        protected const string _redisKeyGpuNameByClientId = "speedDatas.GpuNameByClientId";// 根据ClientId索引GpuName对象的json
+        private const string _redisKeyGpuNamesRaw = "gpuNames.Raw";
 
         protected readonly ConnectionMultiplexer _connection;
         public GpuNameRedis(ConnectionMultiplexer connection) {
             _connection = connection;
         }
 
-        public Task<List<GpuName>> GetAllAsync() {
+        public Task<List<GpuName>> GetAllRawAsync() {
             var db = _connection.GetDatabase();
-            return db.HashGetAllAsync(_redisKeyGpuNameByClientId).ContinueWith(t => {
+            return db.HashGetAllAsync(_redisKeyGpuNamesRaw).ContinueWith(t => {
                 List<GpuName> list = new List<GpuName>();
                 foreach (var item in t.Result) {
                     if (item.Value.HasValue) {
@@ -29,22 +29,22 @@ namespace NTMiner.Core.Redis.Impl {
             });
         }
 
-        public Task SetAsync(GpuName gpuName) {
+        public Task SetRawAsync(GpuName gpuName) {
             // 忽略显存小于2G的卡
             if (gpuName == null || !gpuName.IsValid()) {
                 return TaskEx.CompletedTask;
             }
             var db = _connection.GetDatabase();
-            return db.HashSetAsync(_redisKeyGpuNameByClientId, gpuName.ToString(), VirtualRoot.JsonSerializer.Serialize(gpuName));
+            return db.HashSetAsync(_redisKeyGpuNamesRaw, gpuName.ToString(), VirtualRoot.JsonSerializer.Serialize(gpuName));
         }
 
-        public Task SetAsync(List<GpuName> gpuNames) {
+        public Task SetRawAsync(List<GpuName> gpuNames) {
             if (gpuNames == null || gpuNames.Count == 0) {
                 return TaskEx.CompletedTask;
             }
             gpuNames = gpuNames.Where(a => a.IsValid()).ToList();
             var db = _connection.GetDatabase();
-            return db.HashSetAsync(_redisKeyGpuNameByClientId, gpuNames.Select(a => new HashEntry(a.ToString(), VirtualRoot.JsonSerializer.Serialize(a))).ToArray());
+            return db.HashSetAsync(_redisKeyGpuNamesRaw, gpuNames.Select(a => new HashEntry(a.ToString(), VirtualRoot.JsonSerializer.Serialize(a))).ToArray());
         }
     }
 }

+ 1 - 1
src/WebApiServer/Core/IGpuNameSet.cs → src/WebApiServer/Core/IGpuNameRawSet.cs

@@ -2,7 +2,7 @@
 using System.Collections.Generic;
 
 namespace NTMiner.Core {
-    public interface IGpuNameSet {
+    public interface IGpuNameRawSet {
         /// <summary>
         /// 该集合的成员是异步从redis中加载数据初始化的,所以有了这个IsReadied属性。
         /// </summary>

+ 1 - 1
src/WebApiServer/Core/Impl/ClientDataSet.cs

@@ -130,7 +130,7 @@ namespace NTMiner.Core.Impl {
                 _speedDataRedis.SetAsync(new SpeedData(speedDto, DateTime.Now));
             }
             foreach (var gpuSpeedData in speedDto.GpuTable) {
-                WebApiRoot.GpuNameSet.Add(gpuSpeedData.Name, gpuSpeedData.TotalMemory);
+                WebApiRoot.GpuNameRawSet.Add(gpuSpeedData.Name, gpuSpeedData.TotalMemory);
             }
             ClientData clientData = GetByClientId(speedDto.ClientId);
             if (clientData == null) {

+ 20 - 5
src/WebApiServer/Core/Impl/GpuNameSet.cs → src/WebApiServer/Core/Impl/GpuNameRawSet.cs

@@ -1,17 +1,19 @@
 using NTMiner.Core.Gpus;
 using NTMiner.Core.Redis;
 using System.Collections.Generic;
+using System.Linq;
 
 namespace NTMiner.Core.Impl {
-    public class GpuNameSet : IGpuNameSet {
+    public class GpuNameRawSet : IGpuNameRawSet {
         private readonly HashSet<GpuName> _hashSet = new HashSet<GpuName>();
+        private readonly HashSet<GpuName> _toSaves = new HashSet<GpuName>();
 
         public bool IsReadied {
             get; private set;
         }
 
-        public GpuNameSet(IGpuNameRedis userRedis) {
-            userRedis.GetAllAsync().ContinueWith(t => {
+        public GpuNameRawSet(IGpuNameRedis gpuNameRedis) {
+            gpuNameRedis.GetAllRawAsync().ContinueWith(t => {
                 foreach (var item in t.Result) {
                     _hashSet.Add(item);
                 }
@@ -19,6 +21,12 @@ namespace NTMiner.Core.Impl {
                 Write.UserOk("Gpu名称集就绪");
                 VirtualRoot.RaiseEvent(new GpuNameSetInitedEvent());
             });
+            VirtualRoot.AddEventPath<Per1MinuteEvent>("周期将新发现的GpuName持久化到redis", LogEnum.DevConsole, action: message => {
+                if (_toSaves.Count != 0) {
+                    gpuNameRedis.SetRawAsync(_toSaves.ToList());
+                    _toSaves.Clear();
+                }
+            }, this.GetType());
         }
 
         /// <summary>
@@ -27,13 +35,20 @@ namespace NTMiner.Core.Impl {
         /// <param name="gpuName"></param>
         /// <param name="gpuTotalMemory"></param>
         public void Add(string gpuName, ulong gpuTotalMemory) {
+            if (!IsReadied) {
+                return;
+            }
             if (string.IsNullOrEmpty(gpuName) || !GpuName.IsValidTotalMemory(gpuTotalMemory)) {
                 return;
             }
-            _hashSet.Add(new GpuName {
+            var item = new GpuName {
                 Name = gpuName,
                 TotalMemory = gpuTotalMemory
-            });
+            };
+            bool isNew = _hashSet.Add(item);
+            if (isNew) {
+                _toSaves.Add(item);
+            }
         }
 
         public IEnumerable<IGpuName> AsEnumerable() {

+ 2 - 2
src/WebApiServer/WebApiRoot.cs

@@ -76,7 +76,7 @@ namespace NTMiner {
                         NTMinerWalletSet = new NTMinerWalletSet();
                         ClientDataSet clientDataSet = new ClientDataSet(minerRedis, speedDataRedis, minerClientMqSender);
                         ClientDataSet = clientDataSet;
-                        GpuNameSet = new GpuNameSet(gpuNameRedist);
+                        GpuNameRawSet = new GpuNameRawSet(gpuNameRedist);
                         CoinSnapshotSet = new CoinSnapshotSet(clientDataSet);
                         MineWorkSet = new UserMineWorkSet();
                         MinerGroupSet = new UserMinerGroupSet();
@@ -164,7 +164,7 @@ namespace NTMiner {
 
         public static IClientDataSet ClientDataSet { get; private set; }
 
-        public static IGpuNameSet GpuNameSet { get; private set; }
+        public static IGpuNameRawSet GpuNameRawSet { get; private set; }
 
         public static ICoinSnapshotSet CoinSnapshotSet { get; private set; }
 

+ 2 - 2
src/WebApiServer/WebApiServer.csproj

@@ -96,8 +96,8 @@
     </Compile>
     <Compile Include="Controllers\ClientDataBinaryController.cs" />
     <Compile Include="Controllers\ReportBinaryController.cs" />
-    <Compile Include="Core\IGpuNameSet.cs" />
-    <Compile Include="Core\Impl\GpuNameSet.cs" />
+    <Compile Include="Core\IGpuNameRawSet.cs" />
+    <Compile Include="Core\Impl\GpuNameRawSet.cs" />
     <Compile Include="Role\AdminAttribute.cs" />
     <Compile Include="Role\UserAttribute.cs" />
     <Compile Include="Controllers\ApiControllerBase.cs" />