Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fedd23c
Update ResourceManagerUtils.cs
mstfbl Mar 13, 2020
85f10af
Added TestDownloadFromLocal
mstfbl Mar 16, 2020
63b3f33
Added TestDownloadError
mstfbl Mar 16, 2020
e16b7d6
Revert "Added TestDownloadError"
mstfbl Mar 16, 2020
2caf810
Edit EnsureResourceAsync and its dependencies
mstfbl Mar 16, 2020
6e05a87
Edited TestDownloadFromLocal and re-added TestDownloadError()
mstfbl Mar 16, 2020
69b9827
Disabling TestDownloadFromLocal and TestDownloadError
mstfbl Mar 16, 2020
6e5b246
Edits
mstfbl Mar 16, 2020
cd56549
Re-activated TestDownloadError and TestDownloadFromLocal
mstfbl Mar 16, 2020
2c4d22e
Edits, added 5 min timeout, and debugging requested url
mstfbl Mar 16, 2020
2f67666
Removed timeouts, and re-added Resource download tests in separate un…
mstfbl Mar 17, 2020
8bf03c8
Edits
mstfbl Mar 17, 2020
fd3c7e6
Removed hardcode "microsoft.com" check for HTTP Status Code
mstfbl Mar 18, 2020
bc8b065
Update ResourceManagerUtils.cs
mstfbl Mar 18, 2020
95514c4
Edits for reviews, removing hardcodings of status codes
mstfbl Mar 18, 2020
93b5454
Removing paranthesis from one-liner if statement
mstfbl Mar 18, 2020
a54c7e0
Update TestResourceDownload.cs
mstfbl Mar 18, 2020
b8d5094
Update TestResourceDownload.cs
mstfbl Mar 18, 2020
38fc48f
Nit fix + test case fixes
mstfbl Mar 18, 2020
666d328
Update ResourceManagerUtils.cs
mstfbl Mar 18, 2020
a9e1b5d
Update ResourceManagerUtils.cs
mstfbl Mar 18, 2020
d460db7
Update ResourceManagerUtils.cs
mstfbl Mar 18, 2020
ed3c6fc
Update ResourceManagerUtils.cs
mstfbl Mar 19, 2020
d7b43ed
Added checking for the host of the download absoluteURL euqaling "aka…
mstfbl Mar 24, 2020
d9cdc07
Edit TestResourceDownload
mstfbl Mar 24, 2020
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
54 changes: 29 additions & 25 deletions src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ public async Task<ResourceDownloadResults> EnsureResourceAsync(IHostEnvironment
await DownloadFromUrlWithRetryAsync(env, ch, absoluteUrl.AbsoluteUri, fileName, timeout, filePath), absoluteUrl.AbsoluteUri);
}

private async Task<string> DownloadFromUrlWithRetryAsync(IHostEnvironment env, IChannel ch, string url, string fileName,
int timeout, string filePath, int retryTimes = 5)
private async Task<string> DownloadFromUrlWithRetryAsync(IHostEnvironment env, IChannel ch, string url, string fileName, int timeout, string filePath, int retryTimes = 5)
Comment thread
mstfbl marked this conversation as resolved.
Outdated
{
var downloadResult = "";

Expand All @@ -125,8 +124,12 @@ private async Task<string> DownloadFromUrlWithRetryAsync(IHostEnvironment env, I
if (string.IsNullOrEmpty(thisDownloadResult))
Comment thread
mstfbl marked this conversation as resolved.
return thisDownloadResult;
else
{
Comment thread
mstfbl marked this conversation as resolved.
Outdated
downloadResult += thisDownloadResult + @"\n";

// Do not retry if the URL does not exist
if (thisDownloadResult.Contains("does not exist"))
Comment thread
mstfbl marked this conversation as resolved.
Outdated
return downloadResult;
}
await Task.Delay(10 * 1000);
}

Expand All @@ -136,7 +139,7 @@ private async Task<string> DownloadFromUrlWithRetryAsync(IHostEnvironment env, I
/// <returns>Returns the error message if an error occurred, null if download was successful.</returns>
private async Task<string> DownloadFromUrlAsync(IHostEnvironment env, IChannel ch, string url, string fileName, int timeout, string filePath)
{
using (var webClient = new WebClient())
using (var webClient = new WebClientResponseUri())
using (var downloadCancel = new CancellationTokenSource())
{
bool deleteNeeded = false;
Expand All @@ -160,27 +163,8 @@ private async Task<string> DownloadFromUrlAsync(IHostEnvironment env, IChannel c
deleteNeeded = true;
return (await t).Message;
}

return CheckValidDownload(ch, filePath, url, ref deleteNeeded);
}
}

private static string CheckValidDownload(IChannel ch, string filePath, string url, ref bool deleteNeeded)
{
// If the relative url does not exist, aka.ms redirects to www.microsoft.com. Make sure this did not happen.
// If the file is big then it is definitely not the redirect.
var info = new FileInfo(filePath);
if (info.Length > 4096)
return null;
Comment thread
mstfbl marked this conversation as resolved.
string error = null;
using (var r = new StreamReader(filePath))
{
var text = r.ReadToEnd();
if (text.Contains("<head>") && text.Contains("<body>") && text.Contains("microsoft.com"))
error = $"The url '{url}' does not exist. Url was redirected to www.microsoft.com.";
}
deleteNeeded = error != null;
return error;
}

private static void TryDelete(IChannel ch, string filePath, bool warn = true)
Expand Down Expand Up @@ -252,7 +236,7 @@ private static string GetFilePath(IChannel ch, string fileName, string dir, out
return filePath;
}

private Exception DownloadResource(IHostEnvironment env, IChannel ch, WebClient webClient, Uri uri, string path, string fileName, CancellationToken ct)
private Exception DownloadResource(IHostEnvironment env, IChannel ch, WebClientResponseUri webClient, Uri uri, string path, string fileName, CancellationToken ct)
{
if (File.Exists(path))
return null;
Expand All @@ -269,12 +253,19 @@ private Exception DownloadResource(IHostEnvironment env, IChannel ch, WebClient
string tempPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), "temp-resource-" + guid.ToString()));
try
{
Console.WriteLine(String.Format("Trying to view file through the url: {0}", uri.ToString()));
Comment thread
mstfbl marked this conversation as resolved.
Outdated
using (var s = webClient.OpenRead(uri))
using (var fh = env.CreateOutputFile(tempPath))
using (var ws = fh.CreateWriteStream())
{
var headers = webClient.ResponseHeaders.GetValues("Content-Length");
if (Utils.Size(headers) == 0 || !long.TryParse(headers[0], out var size))
var requestUri = webClient.ResponseUri;
if (requestUri.Host == "www.microsoft.com" && requestUri.ToString().Length < 60)
{
return new ArgumentException($"The url '{uri}' does not exist. Url was redirected to '{requestUri}'.");
}

if (!long.TryParse(headers[0], out var size))
size = 10000000;

long printFreq = (long)(size / 10.0);
Expand Down Expand Up @@ -328,4 +319,17 @@ public static ResourceDownloadResults GetErrorMessage(out string errorMessage, p
private static extern int chmod(string pathname, int mode);
#pragma warning restore IDE1006
}

public class WebClientResponseUri : WebClient
{
public Uri ResponseUri { get; private set; }

protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = null;
response = base.GetWebResponse(request);
Comment thread
mstfbl marked this conversation as resolved.
Outdated
ResponseUri = response.ResponseUri;
return response;
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.ML.TensorFlow/TensorflowUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ internal static void DownloadIfNeeded(IHostEnvironment env, string url, string d
using (var ch = env.Start("Ensuring meta files are present."))
{
var ensureModel = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, url, fileName, dir, timeout);
ensureModel.Wait();
ensureModel.Wait(5 * 60 * 1000); // 5 minute timeout period
Comment thread
mstfbl marked this conversation as resolved.
Outdated
var errorResult = ResourceManagerUtils.GetErrorMessage(out var errorMessage, ensureModel.Result);
if (errorResult != null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Transforms/Text/WordEmbeddingsExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ private string EnsureModelFile(IHostEnvironment env, out int linesToSkip, WordEm
{
string dir = kind == WordEmbeddingEstimator.PretrainedModelKind.SentimentSpecificWordEmbedding ? Path.Combine("Text", "Sswe") : "WordVectors";
var url = $"{dir}/{modelFileName}";
var ensureModel = ResourceManagerUtils.Instance.EnsureResourceAsync(Host, ch, url, modelFileName, dir, Timeout);
Comment thread
mstfbl marked this conversation as resolved.
ensureModel.Wait();
var ensureModel = ResourceManagerUtils.Instance.EnsureResourceAsync(env, ch, url, modelFileName, dir, Timeout);
ensureModel.Wait(5 * 60 * 1000); // 5 minute timeout period
Comment thread
mstfbl marked this conversation as resolved.
Outdated
var errorResult = ResourceManagerUtils.GetErrorMessage(out var errorMessage, ensureModel.Result);
if (errorResult != null)
{
Expand Down
Loading