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 8 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,7 @@ public DbCommand Command
{
_command = Connection.CreateCommand();
_command.CommandText = _source.CommandText;
_command.CommandTimeout = _source.CommandTimeoutInSeconds;
}
return _command;
}
Expand Down
18 changes: 17 additions & 1 deletion src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,37 @@ namespace Microsoft.ML.Data
/// <summary>Exposes the data required for opening a database for reading.</summary>
public sealed class DatabaseSource
{
public static readonly int DefaultCommandTimeoutInSeconds = 30;
wangyems marked this conversation as resolved.
Show resolved Hide resolved
/// <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>
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText)
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText) :
this(providerFactory, connectionString, commandText, DefaultCommandTimeoutInSeconds)
{
}

/// <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="commandTimeoutInSeconds">The timeout(in seconds) for database command.</param>
public DatabaseSource(DbProviderFactory providerFactory, string connectionString, string commandText, int commandTimeoutInSeconds)
{
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));
Contracts.CheckUserArg(commandTimeoutInSeconds >= 0, nameof(commandTimeoutInSeconds));

ProviderFactory = providerFactory;
ConnectionString = connectionString;
CommandText = commandText;
CommandTimeoutInSeconds = commandTimeoutInSeconds;
}

/// <summary>Gets the timeout for database command.</summary>
public int CommandTimeoutInSeconds { get; }

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
26 changes: 22 additions & 4 deletions test/Microsoft.ML.Tests/DatabaseLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public DatabaseLoaderTests(ITestOutputHelper output)

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

[LightGBMFact]
public void IrisLightGbmWithTimeout()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) //sqlite does not have built-in command for sleep
return;
DatabaseSource dbs = GetIrisDatabaseSource("WAITFOR DELAY '00:00:01'; SELECT * FROM {0}", 1);
var ex = Assert.Throws<System.Reflection.TargetInvocationException>(() => IrisLightGbmImpl(dbs));
Assert.Contains("Timeout", ex.InnerException.Message);
}

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

Expand All @@ -41,7 +57,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 @@ -211,18 +227,20 @@ public void IrisSdcaMaximumEntropy()
/// SQLite database is used on Linux and MacOS builds.
/// </summary>
/// <returns>Return the appropiate Iris DatabaseSource according to build OS.</returns>
private DatabaseSource GetIrisDatabaseSource(string command)
private DatabaseSource GetIrisDatabaseSource(string command, int commandTimeoutInSeconds = 30)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return new DatabaseSource(
SqlClientFactory.Instance,
GetMSSQLConnectionString(TestDatasets.irisDb.name),
String.Format(command, $@"""{TestDatasets.irisDb.trainFilename}"""));
String.Format(command, $@"""{TestDatasets.irisDb.trainFilename}"""),
commandTimeoutInSeconds);
else
return new DatabaseSource(
SQLiteFactory.Instance,
GetSQLiteConnectionString(TestDatasets.irisDbSQLite.name),
String.Format(command, TestDatasets.irisDbSQLite.trainFilename));
String.Format(command, TestDatasets.irisDbSQLite.trainFilename),
commandTimeoutInSeconds);
}

private string GetMSSQLConnectionString(string databaseName)
Expand Down