Esempio n. 1
0
        //internal static List<MongodbServerStateInfo> GetState()
        //{
        //    lock (submitDataMemoryQueueServices)
        //    {
        //        var state = new List<MongodbServerStateInfo>()
        //        {
        //            new MongodbServerStateInfo
        //            {
        //                MemoryQueueServiceStates = submitDataMemoryQueueServices.ToDictionary(s=>s.Key, s=>s.Value.GetState()),
        //            }
        //        };
        //        return state;
        //    }
        //}

        private static MongoServer CreateMasterMongoServer(string typeName)
        {
            if (!masterServerCache.ContainsKey(typeName))
            {
                lock (masterServerCache)
                {
                    if (!masterServerCache.ContainsKey(typeName))
                    {
                        var config = MongodbServerConfiguration.GetMongodbServerConfigurationItem(typeName);
                        if (config == null)
                        {
                            LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Master", "CreateMasterMongoServer",
                                                        string.Format("没取到服务器配置项,参数:{0}", typeName));
                            return(null);
                        }
                        var server = MongodbServerConfiguration.GetMongodbServerUrl(config.MongodbServerUrlName);
                        if (server == null)
                        {
                            LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Master", "CreateMasterMongoServer",
                                                        string.Format("没取到服务器地址,参数:{0}", config.MongodbServerUrlName));
                            return(null);
                        }
                        masterServerCache.Add(typeName, MongoServer.Create(server.Master));
                    }
                }
            }
            return(masterServerCache[typeName]);
        }
Esempio n. 2
0
        public void SubmitData(IList <MongodbData> dataList, MongodbDatabaseDescription databaseDescription)
        {
            if (dataList == null || dataList.Count == 0)
            {
                return;
            }

            var typeFullName = dataList.First().TypeFullName;

            try
            {
                var config = MongodbServerConfiguration.GetMongodbServerConfigurationItem(typeFullName);
                if (config == null)
                {
                    LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Master", "SubmitData",
                                                string.Format("没取到服务器配置项,参数:{0}", typeFullName));
                    return;
                }

                if (!submitDataMemoryQueueServices.ContainsKey(typeFullName))
                {
                    lock (submitDataMemoryQueueServices)
                    {
                        if (!submitDataMemoryQueueServices.ContainsKey(typeFullName))
                        {
                            var memoryQueueService = LocalServiceLocator.GetService <IMemoryQueueService>();
                            memoryQueueService.Init(new MemoryQueueServiceConfiguration(string.Format("{0}_{1}", ServiceName, typeFullName), InternalSubmitData)
                            {
                                ConsumeErrorAction                   = config.ConsumeErrorAction,
                                ConsumeIntervalMilliseconds          = config.ConsumeIntervalMilliseconds,
                                ConsumeIntervalWhenErrorMilliseconds = config.ConsumeIntervalWhenErrorMilliseconds,
                                ConsumeItemCountInOneBatch           = config.ConsumeItemCountInOneBatch,
                                ConsumeThreadCount                   = config.ConsumeThreadCount,
                                MaxItemCount = config.MaxItemCount,
                                NotReachBatchCountConsumeAction = config.NotReachBatchCountConsumeAction,
                                ReachMaxItemCountAction         = config.ReachMaxItemCountAction,
                            });
                            submitDataMemoryQueueServices.Add(typeFullName, memoryQueueService);
                        }
                    }
                }

                if (databaseDescription != null)
                {
                    InitMongodbDatabaseDescription(typeFullName, databaseDescription);
                }

                if (config.SubmitToDatabase)
                {
                    submitDataMemoryQueueServices[typeFullName].EnqueueBatch(dataList);
                }
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Master", "SubmitData", ex.Message);
                throw;
            }
        }
Esempio n. 3
0
 public MongodbAdminConfigurationItem GetAdminConfigurationInternal(string username)
 {
     try
     {
         var config     = MongodbServerConfiguration.GetConfig().MongodbAdminConfigurationItems;
         var configItem = config.Values.FirstOrDefault(c => c.UserName == username);
         return(configItem);
     }
     catch (Exception ex)
     {
         LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Admin", "GetAdminConfigurationInternal", ex.ToString());
         throw;
     }
 }
Esempio n. 4
0
        private static MongodbServerUrl GetMongodbServerUrl(string typeName)
        {
            var config = MongodbServerConfiguration.GetMongodbServerConfigurationItem(typeName);

            if (config == null)
            {
                return(null);
            }
            var server = MongodbServerConfiguration.GetMongodbServerUrl(config.MongodbServerUrlName);

            if (server == null)
            {
                return(null);
            }
            return(server);
        }
Esempio n. 5
0
        public static void Init()
        {
            if (servers.Count == 0)
            {
                MemcachedClient = MemcachedClient.GetClient(MongodbServerConfiguration.GetConfig().MemcachedClusterName);

                BsonClassMap.RegisterClassMap <MongodbDatabaseDescription>(cm =>
                {
                    cm.AutoMap();
                    cm.SetIgnoreExtraElements(true);
                    cm.SetIdMember(cm.GetMemberMap("TypeFullName"));
                });

                BsonClassMap.RegisterClassMap <MongodbColumnDescription>(cm =>
                {
                    cm.AutoMap();
                    cm.SetIgnoreExtraElements(true);
                });

                BsonClassMap.RegisterClassMap <CollectionMetadata>(cm =>
                {
                    cm.AutoMap();
                    cm.SetIgnoreExtraElements(true);
                    cm.SetIdMember(cm.GetMemberMap("CollectionName"));
                });

                foreach (var url in MongodbServerConfiguration.GetConfig().MongodbServerUrls)
                {
                    var server = url.Value;
                    Maintainance(server);
                    var thread = new Thread(() =>
                    {
                        while (true)
                        {
                            Thread.Sleep(Math.Max(MongodbServerConfiguration.GetConfig().MaintainceIntervalMilliSeconds + 1, 30 * 1000));
                            Maintainance(server);
                        }
                    })
                    {
                        IsBackground = true,
                        Name         = string.Format("MongodbServerMaintainanceCenter_{0}", url.Key)
                    };
                    thread.Start();
                }
            }
        }
Esempio n. 6
0
        public MongodbAdminConfigurationItem GetAdminConfiguration(string username, string password)
        {
            if (string.IsNullOrWhiteSpace(username) || password == null)
            {
                return(null);
            }

            try
            {
                var config     = MongodbServerConfiguration.GetConfig().MongodbAdminConfigurationItems;
                var configItem = config.Values.FirstOrDefault(c => c.UserName == username && c.Password == password);
                if (configItem.MongodbAdminDatabaseConfigurationItems == null)
                {
                    LocalLoggingService.Error("MongodbAdminDatabaseConfigurationItems为空!!! {0} {1}", username, password);
                }
                return(configItem);
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Admin", "GetAdminConfiguration", ex.ToString());
                throw;
            }
        }
Esempio n. 7
0
 private static MongoServer CreateSlaveMongoServer(string typeName)
 {
     try
     {
         if (!slaveServerCache.ContainsKey(typeName))
         {
             lock (slaveServerCache)
             {
                 if (!slaveServerCache.ContainsKey(typeName))
                 {
                     var config = MongodbServerConfiguration.GetMongodbServerConfigurationItem(typeName);
                     if (config == null)
                     {
                         LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Slave", "CreateSlaveMongoServer",
                                                     string.Format("没取到服务器配置项,参数:{0}", typeName));
                         return(null);
                     }
                     var server = MongodbServerConfiguration.GetMongodbServerUrl(config.MongodbServerUrlName);
                     if (server == null)
                     {
                         LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Slave", "CreateSlaveMongoServer",
                                                     string.Format("没取到服务器地址,参数:{0}", config.MongodbServerUrlName));
                         return(null);
                     }
                     slaveServerCache.Add(typeName, MongoServer.Create(server.Slave));
                 }
             }
         }
         return(slaveServerCache[typeName]);
     }
     catch (Exception ex)
     {
         LocalLoggingService.Error("{0} {1} {2} {3} {4}", MongodbServerConfiguration.ModuleName, "MongodbServer_Slave", "CreateSlaveMongoServer",
                                   string.Format("创建服务器链接出错,参数:{0}", typeName), ex.Message);
         throw;
     }
 }
Esempio n. 8
0
 private int InternalGetDataCount(TimeSpan delay, string typeFullName, string databaseName, string tableName, string columnName, DateTime begin, DateTime end, IMongoQuery filterquery)
 {
     try
     {
         var count = MongodbServerMaintainceCenter.MemcachedClient.GetAndSetWhen <int?>(GetMemcachedKey(typeFullName, databaseName, tableName, columnName,
                                                                                                        begin.ToString("yyyyMMddHHmmss"), end.ToString("yyyyMMddHHmmss"), filterquery), () =>
         {
             var query      = Query.And(Query.LT(columnName, end).GTE(begin), filterquery);
             var server     = CreateSlaveMongoServer(typeFullName);
             var database   = server.GetDatabase(databaseName);
             var collection = database.GetCollection(tableName);
             var c          = Convert.ToInt32(collection.Count(query));
             //LocalLoggingService.Debug(string.Format("类型 {0} 数据库 {1} 表 {2} 列 {3} 开始 {4} 结束 {5} 条件 {6}  值 {7}", typeFullName, databaseName, tableName, columnName,
             //   begin.ToString("yyyyMMddHHmmss"), end.ToString("yyyyMMddHHmmss"), filterquery == null ? "" : filterquery.ToString(), c));
             return(c);
         }, TimeSpan.FromDays(20), () => MongodbServerConfiguration.GetConfig().EnableCache&& DateTime.Now > end + delay);
         return(count.Value);
     }
     catch (Exception ex)
     {
         LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServer_Statistics", "InternalGetDataCount", ex.Message);
         throw;
     }
 }