0

虽然我用 MongoDB 进行了几次写测试,但我发现有时 .Net 程序永远不会完成。它创建一些测试数据,然后将它们与并行循环插入到 mongodb 中。日志在 mongod 和 c#-driver 中激活。插入时我看了mongostat。#connections 随着时间的推移而增加,因此 #inserts/sec。在接近 160 个连接时, mongostat 的 qr 列再也不会变为零了。剩余的插入发生,但 .Net 程序永远不会完成。如果我在 mongo-console 中查询插入文档的数量,则会丢失一些。

如果我将 c#-driver 配置为 minPoolSize = 30 和 maxPoolSize = 200,问题会更频繁地发生。

我在 mongodb-win32-x86_64-2.4.4 和 mongodb-win32-x86_64-2.4.4 版本中尝试了 MongoDB。它发生在 Windows 7 和 Windows 8 上。作为 C# 驱动程序,我尝试了 1.6.0.4624、1.7.0.4714 和 1.8.2

代码如下所示:

public class WriteDocuments
{
    public MongoDAL target;

    public WriteDocuments(MongoDBConfiguration config)
    {
        target = new MongoDAL(config);

        target.DropCollection();

        target.DropAllIndexes();
        target.EnsureIndexes(config.Indexes);
    }

    public override void WriteData(List<BooleanDatenpunktDTO> data)
    {
        Stopwatch sw = new Stopwatch();

        try
        {
            sw.Start();
            Parallel.For(0, data.Count, i =>
            {
                target.AddSignalAsDocument(data[i].Pfad, data[i].Wert.ToString(), data[i].Zeitstempel);
            });
            sw.Stop();
        }
        catch (MongoDALException exc)
        {
            throw new Exception(exc.Message);
        }
    }
}

public class MongoDAL
{
    private MongoServer _Server;
    public MongoServer Server
    {
        get { return _Server; }
        set { _Server = value; }
    }

    public MongoDatabase CurrentDB;

    const string DATA_COLLECTIONNAME = "Data";

    MongoCollection DataCollection;

    public MongoDAL(MongoDBConfiguration config)
    {
        // Create ConnectionString
        MongoConnectionStringBuilder connectionStringBuilder = new MongoConnectionStringBuilder();
        connectionStringBuilder.Server = new MongoServerAddress(config.Mongod_IPAddress, config.Mongod_Port);
        connectionStringBuilder.DatabaseName = config.DatabaseName;
        connectionStringBuilder.Journal = config.Journaling;

        if (config.MinPoolSize != -1) connectionStringBuilder.MinConnectionPoolSize = config.MinPoolSize;
        if (config.MaxPoolSize != -1) connectionStringBuilder.MaxConnectionPoolSize = config.MaxPoolSize;

        string s = connectionStringBuilder.ToString();
        MongoClient client = new MongoClient(connectionStringBuilder.ToString()); 
        Server = client.GetServer(); 
        CurrentDB = GetDatabase(config.DatabaseName);

        DataCollection = GetCollection(DATA_COLLECTIONNAME);
    }

    ~MongoDAL() { }

    #region General

    public void DropCollection()
    {
        CurrentDB.DropCollection(DATA_COLLECTIONNAME);
    }

    public void DropAllIndexes()
    {
        DataCollection.DropAllIndexes();
    }

    public void EnsureIndexes(params string[] keyNames)
    {
        DataCollection.EnsureIndex(new IndexKeysBuilder().Ascending(keyNames));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="databaseName"></param>
    /// <returns>A new or existing instance of MongoDatabase.</returns>
    private MongoDatabase GetDatabase(string databaseName)
    {
        return Server.GetDatabase(databaseName);
    }

    /// <summary>
    /// Get the collection specified
    /// </summary>
    /// <param name="collectionName">Name of the collection</param>
    /// <returns>A MongoCollection</returns>
    private MongoCollection GetCollection(string collectionName)
    {
        return CurrentDB.GetCollection<BsonDocument>(collectionName);
    }

    #endregion


    public void AddData(string path, string value, DateTime timestamp, DataQuality quality = DataQuality.Automatic)
    {
        ArchivedData dp = new ArchivedData(path, value, timestamp, quality);
        dp._id = ObjectId.GenerateNewId();

        try
        {
            WriteConcernResult writeResult = DataCollection.Insert(dp);
        }
        catch (MongoException exc)
        {
            throw new MongoDALException("Fehler beim Archivieren der Daten", exc);
        }
    }
}

之后命令db.currentOp()输出:

> db.currentOp()
{
    "inprog" : [
            {
                    "opid" : 3452652,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64295",
                    "desc" : "conn21",
                    "connectionId" : 21,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3452638,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64287",
                    "desc" : "conn12",
                    "connectionId" : 12,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456594,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64316",
                    "desc" : "conn36",
                    "connectionId" : 36,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3455804,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64264",
                    "desc" : "conn8",
                    "connectionId" : 8,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456501,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64342",
                    "desc" : "conn56",
                    "connectionId" : 56,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3451700,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64337",
                    "desc" : "conn50",
                    "connectionId" : 50,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3447483,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64398",
                    "desc" : "conn112",
                    "connectionId" : 112,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3451060,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64377",
                    "desc" : "conn91",
                    "connectionId" : 91,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456626,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64340",
                    "desc" : "conn54",
                    "connectionId" : 54,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456348,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64345",
                    "desc" : "conn60",
                    "connectionId" : 60,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456540,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64382",
                    "desc" : "conn96",
                    "connectionId" : 96,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3451482,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64385",
                    "desc" : "conn98",
                    "connectionId" : 98,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456338,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64426",
                    "desc" : "conn140",
                    "connectionId" : 140,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456189,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64418",
                    "desc" : "conn132",
                    "connectionId" : 132,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3439322,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64410",
                    "desc" : "conn124",
                    "connectionId" : 124,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456623,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64411",
                    "desc" : "conn125",
                    "connectionId" : 125,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3451553,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64414",
                    "desc" : "conn128",
                    "connectionId" : 128,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3448524,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64401",
                    "desc" : "conn115",
                    "connectionId" : 115,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3455241,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64436",
                    "desc" : "conn153",
                    "connectionId" : 153,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3453702,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64444",
                    "desc" : "conn159",
                    "connectionId" : 159,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456652,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64438",
                    "desc" : "conn150",
                    "connectionId" : 150,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3453517,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64431",
                    "desc" : "conn145",
                    "connectionId" : 145,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3443315,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64455",
                    "desc" : "conn169",
                    "connectionId" : 169,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3455479,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64448",
                    "desc" : "conn162",
                    "connectionId" : 162,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3456370,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64494",
                    "desc" : "conn204",
                    "connectionId" : 204,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3455771,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64459",
                    "desc" : "conn173",
                    "connectionId" : 173,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3449858,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64476",
                    "desc" : "conn190",
                    "connectionId" : 190,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            {
                    "opid" : 3451259,
                    "active" : false,
                    "op" : "insert",
                    "ns" : "",
                    "insert" : {

                    },
                    "client" : "127.0.0.1:64463",
                    "desc" : "conn177",
                    "connectionId" : 177,
                    "locks" : {
                            "^HypeArchiv_4" : "W"
                    },
                    "waitingForLock" : true,
                    "numYields" : 0,
                    "lockStats" : {
                            "timeLockedMicros" : {

                            },
                            "timeAcquiringMicros" : {

                            }
                    }
            },
            ...
    ]
}
4

0 回答 0