Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting Event counter in .Net core #719

Merged
merged 17 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ internal DbConnectionInternal CreateNonPooledConnection(DbConnection owningConne
DbConnectionInternal newConnection = CreateConnection(connectionOptions, poolKey, poolGroupProviderInfo, null, owningConnection, userOptions);
if (null != newConnection)
{
SqlClientEventSource.Log.HardConnectRequest();
karinazhou marked this conversation as resolved.
Show resolved Hide resolved
newConnection.MakeNonPooledObject(owningConnection);
}
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.CreateNonPooledConnection|RES|CPOOL> {0}, Non-pooled database connection created.", ObjectID);
Expand All @@ -138,6 +139,7 @@ internal DbConnectionInternal CreatePooledConnection(DbConnectionPool pool, DbCo
DbConnectionInternal newConnection = CreateConnection(options, poolKey, poolGroupProviderInfo, pool, owningObject, userOptions);
if (null != newConnection)
{
SqlClientEventSource.Log.HardConnectRequest();
karinazhou marked this conversation as resolved.
Show resolved Hide resolved
newConnection.MakePooledConnection(pool);
}
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.CreatePooledConnection|RES|CPOOL> {0}, Pooled database connection created.", ObjectID);
Expand Down Expand Up @@ -281,6 +283,7 @@ internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnectionPoolKey key, D

// lock prevents race condition with PruneConnectionPoolGroups
newConnectionPoolGroups.Add(key, newConnectionPoolGroup);
SqlClientEventSource.Log.ActiveConnectionPoolGroupRequest();
connectionPoolGroup = newConnectionPoolGroup;
_connectionPoolGroups = newConnectionPoolGroups;
}
Expand Down Expand Up @@ -324,6 +327,7 @@ private void PruneConnectionPoolGroups(object state)
{
_poolsToRelease.Remove(pool);
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePool={1}", ObjectID, pool.ObjectID);
SqlClientEventSource.Log.InactiveConnectionPoolRequest(false);
}
}
}
Expand All @@ -348,6 +352,7 @@ private void PruneConnectionPoolGroups(object state)
{
_poolGroupsToRelease.Remove(poolGroup);
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePoolGroup={1}", ObjectID, poolGroup.ObjectID);
SqlClientEventSource.Log.InactiveConnectionPoolGroupRequest(false);
}
}
}
Expand All @@ -373,6 +378,7 @@ private void PruneConnectionPoolGroups(object state)
// otherwise process entry which may move it from active to idle
if (entry.Value.Prune())
{ // may add entries to _poolsToRelease
SqlClientEventSource.Log.ActiveConnectionPoolGroupRequest(false);
karinazhou marked this conversation as resolved.
Show resolved Hide resolved
QueuePoolGroupForRelease(entry.Value);
}
else
Expand Down Expand Up @@ -405,6 +411,7 @@ internal void QueuePoolForRelease(DbConnectionPool pool, bool clearing)
}
_poolsToRelease.Add(pool);
}
SqlClientEventSource.Log.InactiveConnectionPoolRequest();
}

internal void QueuePoolGroupForRelease(DbConnectionPoolGroup poolGroup)
Expand All @@ -416,6 +423,7 @@ internal void QueuePoolGroupForRelease(DbConnectionPoolGroup poolGroup)
{
_poolGroupsToRelease.Add(poolGroup);
}
SqlClientEventSource.Log.InactiveConnectionPoolGroupRequest();
}

virtual protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ internal void DeactivateConnection()
int activateCount = Interlocked.Decrement(ref _activateCount);
#endif // DEBUG

SqlClientEventSource.Log.ActiveConnectionRequest(false);

if (!_connectionIsDoomed && Pool.UseLoadBalancing)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ internal int Clear()
if (pool != null)
{
DbConnectionFactory connectionFactory = pool.ConnectionFactory;
SqlClientEventSource.Log.ActiveConnectionPoolRequest(false);
connectionFactory.QueuePoolForRelease(pool, true);
}
}
Expand Down Expand Up @@ -187,6 +188,7 @@ internal DbConnectionPool GetConnectionPool(DbConnectionFactory connectionFactor
newPool.Startup(); // must start pool before usage
bool addResult = _poolCollection.TryAdd(currentIdentity, newPool);
Debug.Assert(addResult, "No other pool with current identity should exist at this point");
SqlClientEventSource.Log.ActiveConnectionPoolRequest();
pool = newPool;
}
else
Expand Down Expand Up @@ -264,6 +266,7 @@ internal bool Prune()
DbConnectionFactory connectionFactory = pool.ConnectionFactory;

connectionFactory.QueuePoolForRelease(pool, false);
SqlClientEventSource.Log.ActiveConnectionPoolRequest(false);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@
<Compile Include="Microsoft\Data\SqlClient\EnclaveProviderBase.cs" />
<Compile Include="Microsoft\Data\SqlClient\EnclaveSessionCache.cs" />
</ItemGroup>
<!-- netcoreapp 2.1 & netstandard 2.0 -->
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND ('$(TargetFramework)' == 'netcoreapp2.1' OR '$(TargetFramework)' == 'netstandard2.0')">
<Compile Include="Microsoft\Data\SqlClient\SqlClientEventSource.NetCoreApp2.cs" />
</ItemGroup>
<!-- netcoreapp 3.1 & netstandard 2.1 and above -->
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(TargetFramework)' != 'netcoreapp2.1' AND '$(TargetFramework)' != 'netstandard2.0'">
<Compile Include="Microsoft\Data\SqlClient\SqlClientEventSource.NetCoreApp3.cs" />
DavoudEshtehari marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(TargetGroup)' == 'netcoreapp'">
<Compile Include="Microsoft\Data\Common\DbConnectionStringCommon.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPool.NetCoreApp.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ internal bool TryGetConnection(DbConnection owningConnection, TaskCompletionSour
}

connection = CreateNonPooledConnection(owningConnection, poolGroup, userOptions);
SqlClientEventSource.Log.NonPooledConnectionRequest();
}
else
{
Expand Down Expand Up @@ -209,6 +210,10 @@ private static void TryGetConnectionCompletedContinuation(Task<DbConnectionInter
task.Result.DoomThisConnection();
task.Result.Dispose();
}
else
{
SqlClientEventSource.Log.NonPooledConnectionRequest();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ internal void ActivateConnection(Transaction transaction)
#endif // DEBUG

Activate(transaction);
SqlClientEventSource.Log.ActiveConnectionRequest();
}

internal virtual void CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
Expand Down Expand Up @@ -296,6 +297,7 @@ internal virtual void CloseConnection(DbConnection owningObject, DbConnectionFac
else
{
Deactivate(); // ensure we de-activate non-pooled connections, or the data readers and transactions may not get cleaned up...
SqlClientEventSource.Log.HardDisconnectRequest();

// To prevent an endless recursion, we need to clear
// the owning object before we call dispose so that
Expand All @@ -312,6 +314,7 @@ internal virtual void CloseConnection(DbConnection owningObject, DbConnectionFac
}
else
{
SqlClientEventSource.Log.NonPooledConnectionRequest(false);
Dispose();
}
}
Expand Down Expand Up @@ -370,6 +373,7 @@ virtual internal void DelegatedTransactionEnded()
// once and for all, or the server will have fits about us
// leaving connections open until the client-side GC kicks
// in.
SqlClientEventSource.Log.NonPooledConnectionRequest(false);
Dispose();
}
// When _pooledCount is 0, the connection is a pooled connection
Expand Down Expand Up @@ -482,6 +486,7 @@ internal void SetInStasis()
{
_isInStasis = true;
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.SetInStasis|RES|CPOOL> {0}, Non-Pooled Connection has Delegated Transaction, waiting to Dispose.", ObjectID);
SqlClientEventSource.Log.StasisConnectionRequest();
}

private void TerminateStasis(bool returningToPool)
Expand All @@ -494,6 +499,7 @@ private void TerminateStasis(bool returningToPool)
{
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.TerminateStasis|RES|CPOOL> {0}, Delegated Transaction has ended, connection is closed/leaked. Disposing.", ObjectID);
}
SqlClientEventSource.Log.StasisConnectionRequest(false);
_isInStasis = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ internal void PutTransactedObject(Transaction transaction, DbConnectionInternal
}
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.TransactedConnectionPool.PutTransactedObject|RES|CPOOL> {0}, Transaction {1}, Connection {2}, Added.", ObjectID, transaction.GetHashCode(), transactedObject.ObjectID);
}
SqlClientEventSource.Log.FreeConnectionRequest();
}

internal void TransactionEnded(Transaction transaction, DbConnectionInternal transactedObject)
Expand Down Expand Up @@ -293,6 +294,7 @@ internal void TransactionEnded(Transaction transaction, DbConnectionInternal tra
// connections, we'll put it back...
if (0 <= entry)
{
SqlClientEventSource.Log.FreeConnectionRequest();
DavoudEshtehari marked this conversation as resolved.
Show resolved Hide resolved
Pool.PutObjectFromTransactedPool(transactedObject);
}
}
Expand Down Expand Up @@ -600,6 +602,7 @@ private void CleanupCallback(object state)
{
Debug.Assert(obj != null, "null connection is not expected");
// If we obtained one from the old stack, destroy it.
SqlClientEventSource.Log.FreeConnectionRequest(false);

// Transaction roots must survive even aging out (TxEnd event will clean them up).
bool shouldDestroy = true;
Expand Down Expand Up @@ -696,11 +699,13 @@ internal void Clear()
while (_stackNew.TryPop(out obj))
{
Debug.Assert(obj != null, "null connection is not expected");
SqlClientEventSource.Log.FreeConnectionRequest(false);
DestroyObject(obj);
}
while (_stackOld.TryPop(out obj))
{
Debug.Assert(obj != null, "null connection is not expected");
SqlClientEventSource.Log.FreeConnectionRequest(false);
DestroyObject(obj);
}

Expand Down Expand Up @@ -742,6 +747,7 @@ private DbConnectionInternal CreateObject(DbConnection owningObject, DbConnectio
}
_objectList.Add(newObj);
_totalObjects = _objectList.Count;
SqlClientEventSource.Log.PooledConnectionRequest();
}

// If the old connection belonged to another pool, we need to remove it from that
Expand Down Expand Up @@ -967,9 +973,11 @@ internal void DestroyObject(DbConnectionInternal obj)
if (removed)
{
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.DestroyObject|RES|CPOOL> {0}, Connection {1}, Removed from pool.", ObjectID, obj.ObjectID);
SqlClientEventSource.Log.PooledConnectionRequest(false);
}
obj.Dispose();
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.DestroyObject|RES|CPOOL> {0}, Connection {1}, Disposed.", ObjectID, obj.ObjectID);
SqlClientEventSource.Log.HardDisconnectRequest();
}
}

Expand Down Expand Up @@ -1301,6 +1309,7 @@ private bool TryGetConnection(DbConnection owningObject, uint waitForMultipleObj
}

connection = obj;
SqlClientEventSource.Log.SoftConnectRequest();
return true;
}

Expand Down Expand Up @@ -1337,6 +1346,7 @@ internal DbConnectionInternal ReplaceConnection(DbConnection owningObject, DbCon

if (newConnection != null)
{
SqlClientEventSource.Log.SoftConnectRequest();
PrepareConnection(owningObject, newConnection, oldConnection.EnlistedTransaction);
oldConnection.PrepareForReplaceConnection();
oldConnection.DeactivateConnection();
Expand Down Expand Up @@ -1374,6 +1384,7 @@ private DbConnectionInternal GetFromGeneralPool()
if (null != obj)
{
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetFromGeneralPool|RES|CPOOL> {0}, Connection {1}, Popped from general pool.", ObjectID, obj.ObjectID);
SqlClientEventSource.Log.FreeConnectionRequest(false);
}
return (obj);
}
Expand All @@ -1390,6 +1401,7 @@ private DbConnectionInternal GetFromTransactedPool(out Transaction transaction)
if (null != obj)
{
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetFromTransactedPool|RES|CPOOL> {0}, Connection {1}, Popped from transacted pool.", ObjectID, obj.ObjectID);
SqlClientEventSource.Log.FreeConnectionRequest(false);

if (obj.IsTransactionRoot)
{
Expand Down Expand Up @@ -1544,12 +1556,13 @@ internal void PutNewObject(DbConnectionInternal obj)

_stackNew.Push(obj);
_waitHandles.PoolSemaphore.Release(1);
SqlClientEventSource.Log.FreeConnectionRequest();
}

internal void PutObject(DbConnectionInternal obj, object owningObject)
{
Debug.Assert(null != obj, "null obj?");

SqlClientEventSource.Log.SoftDisconnectRequest();

// Once a connection is closing (which is the state that we're in at
// this point in time) you cannot delegate a transaction to or enlist
Expand Down Expand Up @@ -1662,6 +1675,8 @@ private bool ReclaimEmancipatedObjects()
{
DbConnectionInternal obj = reclaimedObjects[i];
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.ReclaimEmancipatedObjects|RES|CPOOL> {0}, Connection {1}, Reclaiming.", ObjectID, obj.ObjectID);
SqlClientEventSource.Log.ReclaimedConnectionRequest();

emancipatedObjectFound = true;

obj.DetachCurrentTransactionIfEnded();
Expand Down
Loading