Skip to content

Commit ae9dada

Browse files
authored
Code Merge | TdsParserStateObject alignment (#2073)
* Code Merge: Bring TdsParserStateObject closer together between netcore and netfx * Address feedback and remove excessive trace entry
1 parent a924df3 commit ae9dada

File tree

6 files changed

+154
-156
lines changed

6 files changed

+154
-156
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ internal bool TryInitialize(TdsParserStateObject stateObj, int columnsCount)
9898
return false;
9999
}
100100

101-
SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap received, column count = {1}", stateObj._objectID, columnsCount);
102-
SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, Null Bitmap length {1}, NBCROW bitmap data: {2}", stateObj._objectID, (ushort)_nullBitmap.Length, _nullBitmap);
101+
SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap received, column count = {1}", stateObj.ObjectID, columnsCount);
102+
SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap data. Null Bitmap {1}, Null bitmap length: {2}", stateObj.ObjectID, _nullBitmap, (ushort)_nullBitmap.Length);
103+
103104
return true;
104105
}
105106
}
@@ -484,7 +485,7 @@ internal bool TryReadChar(out char value)
484485

485486
AssertValidState();
486487
value = (char)((buffer[1] << 8) + buffer[0]);
487-
488+
488489
return true;
489490
}
490491

@@ -543,7 +544,6 @@ internal bool TryReadInt32(out int value)
543544
AssertValidState();
544545
value = (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) + buffer[0];
545546
return true;
546-
547547
}
548548

549549
// This method is safe to call when doing async without snapshot
@@ -564,7 +564,7 @@ internal bool TryReadInt64(out long value)
564564
// then use ReadByteArray since the logic is there to take care of that.
565565

566566
int bytesRead;
567-
if (!TryReadByteArray(_bTmp.AsSpan(_bTmpRead), 8 - _bTmpRead, out bytesRead))
567+
if (!TryReadByteArray(_bTmp.AsSpan(start: _bTmpRead), 8 - _bTmpRead, out bytesRead))
568568
{
569569
Debug.Assert(_bTmpRead + bytesRead <= 8, "Read more data than required");
570570
_bTmpRead += bytesRead;
@@ -642,7 +642,7 @@ internal bool TryReadUInt32(out uint value)
642642
// then use ReadByteArray since the logic is there to take care of that.
643643

644644
int bytesRead;
645-
if (!TryReadByteArray(_bTmp.AsSpan(_bTmpRead), 4 - _bTmpRead, out bytesRead))
645+
if (!TryReadByteArray(_bTmp.AsSpan(start: _bTmpRead), 4 - _bTmpRead, out bytesRead))
646646
{
647647
Debug.Assert(_bTmpRead + bytesRead <= 4, "Read more data than required");
648648
_bTmpRead += bytesRead;
@@ -1828,13 +1828,13 @@ public void ProcessSniPacket(PacketHandle packet, uint error)
18281828
}
18291829

18301830
SniReadStatisticsAndTracing();
1831-
1831+
SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.ReadNetworkPacketAsyncCallback | INFO | ADV | State Object Id {0}, Packet read. In Buffer {1}, In Bytes Read: {2}", ObjectID, _inBuff, (ushort)_inBytesRead);
18321832

18331833
AssertValidState();
18341834
}
18351835
else
18361836
{
1837-
throw SQL.ParsingError();
1837+
throw SQL.ParsingError(ParsingErrorState.ProcessSniPacketFailed);
18381838
}
18391839
}
18401840
}
@@ -1896,7 +1896,6 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
18961896
// to the outstanding GCRoot until AppDomain.Unload.
18971897
// We live with the above for the time being due to the constraints of the current
18981898
// reliability infrastructure provided by the CLR.
1899-
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.ReadAsyncCallback | Info | State Object Id {0}, received error {1} on idle connection", _objectID, (int)error);
19001899

19011900
TaskCompletionSource<object> source = _networkPacketTaskSource;
19021901
#if DEBUG
@@ -1919,7 +1918,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
19191918
Debug.Assert(CheckPacket(packet, source) && source != null, "AsyncResult null on callback");
19201919

19211920
if (_parser.MARSOn)
1922-
{
1921+
{
19231922
// Only take reset lock on MARS and Async.
19241923
CheckSetResetConnectionState(error, CallbackType.Read);
19251924
}
@@ -1928,10 +1927,10 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
19281927

19291928
// The timer thread may be unreliable under high contention scenarios. It cannot be
19301929
// assumed that the timeout has happened on the timer thread callback. Check the timeout
1931-
// synchrnously and then call OnTimeoutSync to force an atomic change of state.
1930+
// synchrnously and then call OnTimeoutSync to force an atomic change of state.
19321931
if (TimeoutHasExpired)
19331932
{
1934-
OnTimeoutSync(true);
1933+
OnTimeoutSync(asyncClose: true);
19351934
}
19361935

19371936
// try to change to the stopped state but only do so if currently in the running state
@@ -2125,6 +2124,13 @@ public void WriteAsyncCallback(IntPtr key, PacketHandle packet, uint sniError)
21252124
/////////////////////////////////////////
21262125
// Network/Packet Writing & Processing //
21272126
/////////////////////////////////////////
2127+
2128+
//
2129+
// Takes a secure string and offsets and saves them for a write latter when the information is written out to SNI Packet
2130+
// This method is provided to better handle the life cycle of the clear text of the secure string
2131+
// This method also ensures that the clear text is not held in the unpined managed buffer so that it avoids getting moved around by CLR garbage collector
2132+
// TdsParserStaticMethods.EncryptPassword operation is also done in the unmanaged buffer for the clear text later
2133+
//
21282134
internal void WriteSecureString(SecureString secureString)
21292135
{
21302136
Debug.Assert(_securePasswords[0] == null || _securePasswords[1] == null, "There are more than two secure passwords");
@@ -2363,11 +2369,11 @@ internal Task WritePacket(byte flushMode, bool canAccumulate = false)
23632369
// However, since we don't know the version prior to login Is2005OrNewer was always false prior to login
23642370
// So removing the Is2005OrNewer check causes issues since the login packet happens to meet the rest of the conditions below
23652371
// So we need to avoid this check prior to login completing
2366-
state == TdsParserState.OpenLoggedIn &&
2367-
!_bulkCopyOpperationInProgress && // ignore the condition checking for bulk copy
2368-
_outBytesUsed == (_outputHeaderLen + BitConverter.ToInt32(_outBuff, _outputHeaderLen))
2372+
state == TdsParserState.OpenLoggedIn
2373+
&& !_bulkCopyOpperationInProgress // ignore the condition checking for bulk copy
2374+
&& _outBytesUsed == (_outputHeaderLen + BitConverter.ToInt32(_outBuff, _outputHeaderLen))
23692375
&& _outputPacketCount == 0
2370-
|| _outBytesUsed == _outputHeaderLen
2376+
|| _outBytesUsed == _outputHeaderLen
23712377
&& _outputPacketCount == 0)
23722378
{
23732379
return null;
@@ -2420,6 +2426,7 @@ internal Task WritePacket(byte flushMode, bool canAccumulate = false)
24202426
// If we have been canceled, then ensure that we write the ATTN packet as well
24212427
task = AsyncHelper.CreateContinuationTask(task, CancelWritePacket);
24222428
}
2429+
24232430
return task;
24242431
}
24252432

@@ -2443,7 +2450,7 @@ private void CancelWritePacket()
24432450
}
24442451
}
24452452

2446-
#pragma warning disable 0420 // a reference to a volatile field will not be treated as volatile
2453+
#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile
24472454

24482455
private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccumulate, bool callerHasConnectionLock, bool asyncClose = false)
24492456
{
@@ -2456,9 +2463,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu
24562463

24572464
Task task = null;
24582465
_writeCompletionSource = null;
2459-
24602466
PacketHandle packetPointer = EmptyReadPacket;
2461-
24622467
bool sync = !_parser._asyncWrite;
24632468
if (sync && _asyncWriteCount > 0)
24642469
{ // for example, SendAttention while there are writes pending
@@ -2493,7 +2498,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu
24932498

24942499
if (sniError == TdsEnums.SNI_SUCCESS_IO_PENDING)
24952500
{
2496-
Debug.Assert(!sync, "Completion should be handled in SniManagedWwrapper");
2501+
Debug.Assert(!sync, "Completion should be handled in SniManagedWrapper");
24972502
Interlocked.Increment(ref _asyncWriteCount);
24982503
Debug.Assert(_asyncWriteCount >= 0);
24992504
if (!canAccumulate)
@@ -2639,7 +2644,7 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
26392644
}
26402645
}
26412646
#if DEBUG
2642-
}
2647+
}
26432648
#endif
26442649

26452650
SetTimeoutSeconds(AttentionTimeoutSeconds); // Initialize new attention timeout of 5 seconds.

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ private bool TryCleanPartialRead()
860860
}
861861

862862
#if DEBUG
863-
if (_stateObj._pendingData)
863+
if (_stateObj.HasPendingData)
864864
{
865865
byte token;
866866
if (!_stateObj.TryPeekByte(out token))
@@ -1067,7 +1067,7 @@ private bool TryCloseInternal(bool closeReader)
10671067
#else
10681068
{
10691069
#endif //DEBUG
1070-
if ((!_isClosed) && (parser != null) && (stateObj != null) && (stateObj._pendingData))
1070+
if ((!_isClosed) && (parser != null) && (stateObj != null) && (stateObj.HasPendingData))
10711071
{
10721072

10731073
// It is possible for this to be called during connection close on a
@@ -1333,7 +1333,7 @@ private bool TryConsumeMetaData()
13331333
{
13341334
// warning: Don't check the MetaData property within this function
13351335
// warning: as it will be a reentrant call
1336-
while (_parser != null && _stateObj != null && _stateObj._pendingData && !_metaDataConsumed)
1336+
while (_parser != null && _stateObj != null && _stateObj.HasPendingData && !_metaDataConsumed)
13371337
{
13381338
if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed)
13391339
{
@@ -3473,7 +3473,7 @@ private bool TryHasMoreResults(out bool moreResults)
34733473

34743474
Debug.Assert(null != _command, "unexpected null command from the data reader!");
34753475

3476-
while (_stateObj._pendingData)
3476+
while (_stateObj.HasPendingData)
34773477
{
34783478
byte token;
34793479
if (!_stateObj.TryPeekByte(out token))
@@ -3563,7 +3563,7 @@ private bool TryHasMoreRows(out bool moreRows)
35633563
moreRows = false;
35643564
return true;
35653565
}
3566-
if (_stateObj._pendingData)
3566+
if (_stateObj.HasPendingData)
35673567
{
35683568
// Consume error's, info's, done's on HasMoreRows, so user obtains error on Read.
35693569
// Previous bug where Read() would return false with error on the wire in the case
@@ -3620,7 +3620,7 @@ private bool TryHasMoreRows(out bool moreRows)
36203620
moreRows = false;
36213621
return false;
36223622
}
3623-
if (_stateObj._pendingData)
3623+
if (_stateObj.HasPendingData)
36243624
{
36253625
if (!_stateObj.TryPeekByte(out b))
36263626
{
@@ -3964,7 +3964,7 @@ private bool TryReadInternal(bool setTimeout, out bool more)
39643964
if (moreRows)
39653965
{
39663966
// read the row from the backend (unless it's an altrow were the marker is already inside the altrow ...)
3967-
while (_stateObj._pendingData)
3967+
while (_stateObj.HasPendingData)
39683968
{
39693969
if (_altRowStatus != ALTROWSTATUS.AltRow)
39703970
{
@@ -3996,7 +3996,7 @@ private bool TryReadInternal(bool setTimeout, out bool more)
39963996
}
39973997
}
39983998

3999-
if (!_stateObj._pendingData)
3999+
if (!_stateObj.HasPendingData)
40004000
{
40014001
if (!TryCloseInternal(false /*closeReader*/))
40024002
{
@@ -4020,7 +4020,7 @@ private bool TryReadInternal(bool setTimeout, out bool more)
40204020
{
40214021
// if we are in SingleRow mode, and we've read the first row,
40224022
// read the rest of the rows, if any
4023-
while (_stateObj._pendingData && !_sharedState._dataReady)
4023+
while (_stateObj.HasPendingData && !_sharedState._dataReady)
40244024
{
40254025
if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady))
40264026
{
@@ -4061,7 +4061,7 @@ private bool TryReadInternal(bool setTimeout, out bool more)
40614061
more = false;
40624062

40634063
#if DEBUG
4064-
if ((!_sharedState._dataReady) && (_stateObj._pendingData))
4064+
if ((!_sharedState._dataReady) && (_stateObj.HasPendingData))
40654065
{
40664066
byte token;
40674067
if (!_stateObj.TryPeekByte(out token))

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,11 +915,11 @@ override internal void ValidateConnectionForExecute(SqlCommand command)
915915
// or if MARS is off, then a datareader exists
916916
throw ADP.OpenReaderExists(parser.MARSOn); // MDAC 66411
917917
}
918-
else if (!parser.MARSOn && parser._physicalStateObj._pendingData)
918+
else if (!parser.MARSOn && parser._physicalStateObj.HasPendingData)
919919
{
920920
parser.DrainData(parser._physicalStateObj);
921921
}
922-
Debug.Assert(!parser._physicalStateObj._pendingData, "Should not have a busy physicalStateObject at this point!");
922+
Debug.Assert(!parser._physicalStateObj.HasPendingData, "Should not have a busy physicalStateObject at this point!");
923923

924924
parser.RollbackOrphanedAPITransactions();
925925
}
@@ -1078,7 +1078,7 @@ private void ResetConnection()
10781078
// obtains a clone.
10791079

10801080
Debug.Assert(!HasLocalTransactionFromAPI, "Upon ResetConnection SqlInternalConnectionTds has a currently ongoing local transaction.");
1081-
Debug.Assert(!_parser._physicalStateObj._pendingData, "Upon ResetConnection SqlInternalConnectionTds has pending data.");
1081+
Debug.Assert(!_parser._physicalStateObj.HasPendingData, "Upon ResetConnection SqlInternalConnectionTds has pending data.");
10821082

10831083
if (_fResetConnection)
10841084
{

0 commit comments

Comments
 (0)