-
Notifications
You must be signed in to change notification settings - Fork 127
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
Do not swallow errors in simple query. Fixing transaction descriptors. Read envchanges correctly. #105
Do not swallow errors in simple query. Fixing transaction descriptors. Read envchanges correctly. #105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
use crate::{ | ||
tds::{codec::read_varchar, lcid_to_encoding, sortid_to_encoding}, | ||
tds::{lcid_to_encoding, sortid_to_encoding}, | ||
Error, SqlReadBytes, | ||
}; | ||
use byteorder::{LittleEndian, ReadBytesExt}; | ||
use encoding::Encoding; | ||
use fmt::Debug; | ||
use futures::io::AsyncReadExt; | ||
use std::io::Cursor; | ||
use std::io::Read; | ||
use std::{convert::TryFrom, fmt}; | ||
|
||
uint_enum! { | ||
|
@@ -107,10 +110,10 @@ pub enum TokenEnvChange { | |
old: CollationInfo, | ||
new: CollationInfo, | ||
}, | ||
BeginTransaction(u64), | ||
CommitTransaction(u64), | ||
RollbackTransaction(u64), | ||
DefectTransaction(u64), | ||
BeginTransaction([u8; 8]), | ||
CommitTransaction, | ||
RollbackTransaction, | ||
DefectTransaction, | ||
Routing { | ||
host: String, | ||
port: u16, | ||
|
@@ -132,9 +135,9 @@ impl fmt::Display for TokenEnvChange { | |
write!(f, "SQL collation change from {} to {}", old, new) | ||
} | ||
Self::BeginTransaction(_) => write!(f, "Begin transaction"), | ||
Self::CommitTransaction(_) => write!(f, "Commit transaction"), | ||
Self::RollbackTransaction(_) => write!(f, "Rollback transaction"), | ||
Self::DefectTransaction(_) => write!(f, "Defect transaction"), | ||
Self::CommitTransaction => write!(f, "Commit transaction"), | ||
Self::RollbackTransaction => write!(f, "Rollback transaction"), | ||
Self::DefectTransaction => write!(f, "Defect transaction"), | ||
Self::Routing { host, port } => write!( | ||
f, | ||
"Server requested routing to a new address: {}:{}", | ||
|
@@ -151,82 +154,117 @@ impl TokenEnvChange { | |
where | ||
R: SqlReadBytes + Unpin, | ||
{ | ||
let _len = src.read_u16_le().await?; | ||
let ty_byte = src.read_u8().await?; | ||
let len = src.read_u16_le().await? as usize; | ||
|
||
// We read all the bytes now, due to whatever environment change tokens | ||
// we read, they might contain padding zeroes in the end we must | ||
// discard. | ||
let mut bytes = vec![0; len]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one place where read-buf will come in handy — free performance improvement when it's stable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, except we don't luckily read env tokens that often, and they're all quite short. Still excited about that RFC. |
||
src.read_exact(&mut bytes[0..len]).await?; | ||
|
||
let mut buf = Cursor::new(bytes); | ||
let ty_byte = buf.read_u8()?; | ||
|
||
let ty = EnvChangeTy::try_from(ty_byte) | ||
.map_err(|_| Error::Protocol(format!("invalid envchange type {:x}", ty_byte).into()))?; | ||
|
||
let token = match ty { | ||
EnvChangeTy::Database => { | ||
let len = src.read_u8().await?; | ||
let new_value = read_varchar(src, len).await?; | ||
let len = buf.read_u8()? as usize; | ||
let mut bytes = vec![0; len]; | ||
|
||
for i in 0..len { | ||
bytes[i] = buf.read_u16::<LittleEndian>()?; | ||
} | ||
|
||
let len = src.read_u8().await?; | ||
let old_value = read_varchar(src, len).await?; | ||
let new_value = String::from_utf16(&bytes[..])?; | ||
|
||
let len = buf.read_u8()? as usize; | ||
let mut bytes = vec![0; len]; | ||
|
||
for i in 0..len { | ||
bytes[i] = buf.read_u16::<LittleEndian>()?; | ||
} | ||
|
||
let old_value = String::from_utf16(&bytes[..])?; | ||
|
||
TokenEnvChange::Database(new_value, old_value) | ||
} | ||
EnvChangeTy::PacketSize => { | ||
let len = src.read_u8().await?; | ||
let new_value = read_varchar(src, len).await?; | ||
let len = buf.read_u8()? as usize; | ||
let mut bytes = vec![0; len]; | ||
|
||
for i in 0..len { | ||
bytes[i] = buf.read_u16::<LittleEndian>()?; | ||
} | ||
|
||
let new_value = String::from_utf16(&bytes[..])?; | ||
|
||
let len = buf.read_u8()? as usize; | ||
let mut bytes = vec![0; len]; | ||
|
||
for i in 0..len { | ||
bytes[i] = buf.read_u16::<LittleEndian>()?; | ||
} | ||
|
||
let len = src.read_u8().await?; | ||
let old_value = read_varchar(src, len).await?; | ||
let old_value = String::from_utf16(&bytes[..])?; | ||
|
||
TokenEnvChange::PacketSize(new_value.parse()?, old_value.parse()?) | ||
} | ||
EnvChangeTy::SqlCollation => { | ||
let len = src.read_u8().await? as usize; | ||
let len = buf.read_u8()? as usize; | ||
let mut new_value = vec![0; len]; | ||
src.read_exact(&mut new_value[0..len]).await?; | ||
buf.read_exact(&mut new_value[0..len])?; | ||
|
||
let len = src.read_u8().await? as usize; | ||
let len = buf.read_u8()? as usize; | ||
let mut old_value = vec![0; len]; | ||
src.read_exact(&mut old_value[0..len]).await?; | ||
buf.read_exact(&mut old_value[0..len])?; | ||
|
||
TokenEnvChange::SqlCollation { | ||
new: CollationInfo::new(new_value.as_slice()), | ||
old: CollationInfo::new(old_value.as_slice()), | ||
} | ||
} | ||
EnvChangeTy::BeginTransaction | EnvChangeTy::EnlistDTCTransaction => { | ||
src.read_u8().await?; | ||
let desc = src.read_u64_le().await?; | ||
let len = buf.read_u8()?; | ||
assert!(len == 8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if this is really needed. The JDBC code is extremely careful asserting all the things. The TDS standard dictates how the descriptor is always 8 bytes. And the only implementation is the SQL Server from Microsoft, which I highly doubt would send 9 bytes descriptor every now and then... |
||
|
||
let mut desc = [0; 8]; | ||
buf.read_exact(&mut desc)?; | ||
|
||
TokenEnvChange::BeginTransaction(desc) | ||
} | ||
EnvChangeTy::CommitTransaction => { | ||
src.read_u8().await?; | ||
let desc = src.read_u64_le().await?; | ||
TokenEnvChange::CommitTransaction(desc) | ||
} | ||
EnvChangeTy::RollbackTransaction => { | ||
src.read_u8().await?; | ||
let desc = src.read_u64_le().await?; | ||
TokenEnvChange::RollbackTransaction(desc) | ||
} | ||
EnvChangeTy::DefectTransaction => { | ||
src.read_u8().await?; | ||
let desc = src.read_u64_le().await?; | ||
TokenEnvChange::DefectTransaction(desc) | ||
} | ||
|
||
EnvChangeTy::CommitTransaction => TokenEnvChange::CommitTransaction, | ||
EnvChangeTy::RollbackTransaction => TokenEnvChange::RollbackTransaction, | ||
EnvChangeTy::DefectTransaction => TokenEnvChange::DefectTransaction, | ||
|
||
EnvChangeTy::Routing => { | ||
src.read_u16_le().await?; // routing data value length | ||
src.read_u8().await?; // routing protocol, always 0 (tcp) | ||
buf.read_u16::<LittleEndian>()?; // routing data value length | ||
buf.read_u8()?; // routing protocol, always 0 (tcp) | ||
|
||
let port = buf.read_u16::<LittleEndian>()?; | ||
|
||
let port = src.read_u16_le().await?; | ||
let len = buf.read_u16::<LittleEndian>()? as usize; // hostname string length | ||
let mut bytes = vec![0; len]; | ||
|
||
let len = src.read_u16_le().await?; // hostname string length | ||
let host = read_varchar(src, len).await?; | ||
for i in 0..len { | ||
bytes[i] = buf.read_u16::<LittleEndian>()?; | ||
} | ||
|
||
// ??? but needed... | ||
src.read_u16_le().await?; | ||
let host = String::from_utf16(&bytes[..])?; | ||
|
||
TokenEnvChange::Routing { host, port } | ||
} | ||
EnvChangeTy::RTLS => { | ||
let len = src.read_u8().await?; | ||
let mirror_name = read_varchar(src, len).await?; | ||
let len = buf.read_u8()? as usize; | ||
let mut bytes = vec![0; len]; | ||
|
||
for i in 0..len { | ||
bytes[i] = buf.read_u16::<LittleEndian>()?; | ||
} | ||
|
||
let mirror_name = String::from_utf16(&bytes[..])?; | ||
|
||
TokenEnvChange::ChangeMirror(mirror_name) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,17 +66,16 @@ impl<'a> QueryStream<'a> { | |
|
||
return Ok(()); | ||
} | ||
Some(ReceivedToken::DoneInProc(_)) | Some(ReceivedToken::DoneProc(_)) => { | ||
return Ok(()); | ||
} | ||
Some(ReceivedToken::Done(done)) if done.has_more() => return Ok(()), | ||
_ => { | ||
if self.columns().is_none() { | ||
Some(ReceivedToken::DoneInProc(done)) | ||
| Some(ReceivedToken::DoneProc(done)) | ||
| Some(ReceivedToken::Done(done)) => { | ||
if !done.has_more() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I understand this correctly, this is the part about swallowed errors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. We must go through the whole stream always. We might get any of the done tokens, but they have a bitflag set if there's more data to be come. |
||
self.state = QueryStreamState::Done; | ||
} | ||
|
||
return Ok(()); | ||
} | ||
_ => return Ok(()), | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that change motivated by endianness concerns? I can see we write that with
put_slice()
instead ofput_u64_le()
now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just do whatever the JDBC driver does. We always just write bytes to the message header, so there is no reason to convert that to an arbitrary number.