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

[Issue4484] Support specifying command timeout while using the database loader #5288

Merged
merged 9 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -75,6 +75,10 @@ public DbCommand Command
{
_command = Connection.CreateCommand();
_command.CommandText = _source.CommandText;
if (_source.CommandTimeOut.HasValue)
{
_command.CommandTimeout = _source.CommandTimeOut.Value;
}
}
return _command;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ public DatabaseSource(DbProviderFactory providerFactory, string connectionString
CommandText = commandText;
}

/// <summary>Creates a new instance of the <see cref="DatabaseSource" /> class.</summary>
/// <param name="providerFactory">The factory used to create the <see cref="DbConnection"/>..</param>
/// <param name="connectionString">The string used to open the connection.</param>
/// <param name="commandText">The text command to run against the data source.</param>
/// <param name="commandTimeOut">The timeout(in seconds) for database command.</param>
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText, int commandTimeOut)
wangyems marked this conversation as resolved.
Show resolved Hide resolved
{
wangyems marked this conversation as resolved.
Show resolved Hide resolved
Contracts.CheckValue(providerFactory, nameof(providerFactory));
Contracts.CheckValue(connectionString, nameof(connectionString));
Contracts.CheckValue(commandText, nameof(commandText));

ProviderFactory = providerFactory;
ConnectionString = connectionString;
CommandText = commandText;
CommandTimeOut = commandTimeOut;
}

/// <summary>Gets the timeout for database command.</summary>
public int? CommandTimeOut { get; }
wangyems marked this conversation as resolved.
Show resolved Hide resolved

wangyems marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>Gets the text command to run against the data source.</summary>
public string CommandText { get; }

Expand Down
32 changes: 31 additions & 1 deletion test/Microsoft.ML.Tests/DatabaseLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public DatabaseLoaderTests(ITestOutputHelper output)

[LightGBMFact]
public void IrisLightGbm()
{
DatabaseSource dbs = GetIrisDatabaseSource("SELECT * FROM {0}");
IrisLightGbmImpl(dbs);
}

[LightGBMFact]
public void IrisLightGbmWithTimeOut()
{
DatabaseSource dbs = GetIrisDatabaseSourceWithTimeOut("WAITFOR DELAY '00:00:01'; SELECT * FROM {0}", 1);
var ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => IrisLightGbmImpl(dbs));
Assert.Contains("Timeout expired", ex.InnerException.Message);
wangyems marked this conversation as resolved.
Show resolved Hide resolved
}

private void IrisLightGbmImpl(DatabaseSource dbs)
{
var mlContext = new MLContext(seed: 1);

Expand All @@ -41,7 +55,7 @@ public void IrisLightGbm()

var loader = mlContext.Data.CreateDatabaseLoader(loaderColumns);

var trainingData = loader.Load(GetIrisDatabaseSource("SELECT * FROM {0}"));
var trainingData = loader.Load(dbs);

IEstimator<ITransformer> pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
.Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
Expand Down Expand Up @@ -225,6 +239,22 @@ private DatabaseSource GetIrisDatabaseSource(string command)
String.Format(command, TestDatasets.irisDbSQLite.trainFilename));
}

private DatabaseSource GetIrisDatabaseSourceWithTimeOut(string command, int commandTimeOut)
wangyems marked this conversation as resolved.
Show resolved Hide resolved
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return new DatabaseSource(
SqlClientFactory.Instance,
GetMSSQLConnectionString(TestDatasets.irisDb.name),
String.Format(command, $@"""{TestDatasets.irisDb.trainFilename}"""),
commandTimeOut);
else
return new DatabaseSource(
SQLiteFactory.Instance,
GetSQLiteConnectionString(TestDatasets.irisDbSQLite.name),
String.Format(command, TestDatasets.irisDbSQLite.trainFilename),
commandTimeOut);
}

private string GetMSSQLConnectionString(string databaseName)
{
var databaseFile = Path.GetFullPath(Path.Combine("TestDatabases", $"{databaseName}.mdf"));
Expand Down