You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2019/09/06 09:01:18 UTC

[lucenenet] 08/21: BUG: Lucene.Net.Benchmark.ByTask.Tasks.WriteLineDocTaskTest.TestMultiThreaded(): Added lock to synchronize file writes. Also refactored test to utilize the ThreadClass error handling and added extra asserts for help future debugging efforts.

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 2a1b6d16afa7eff8513109749907fd99ca3a1d21
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Tue Sep 3 08:36:30 2019 +0700

    BUG: Lucene.Net.Benchmark.ByTask.Tasks.WriteLineDocTaskTest.TestMultiThreaded(): Added lock to synchronize file writes. Also refactored test to utilize the ThreadClass error handling and added extra asserts for help future debugging efforts.
---
 .../ByTask/Tasks/WriteLineDocTask.cs               | 18 ++++++++---
 .../ByTask/Tasks/WriteLineDocTaskTest.cs           | 36 ++++++++--------------
 2 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/Lucene.Net.Benchmark/ByTask/Tasks/WriteLineDocTask.cs b/src/Lucene.Net.Benchmark/ByTask/Tasks/WriteLineDocTask.cs
index 8e3fccf..5b5885c 100644
--- a/src/Lucene.Net.Benchmark/ByTask/Tasks/WriteLineDocTask.cs
+++ b/src/Lucene.Net.Benchmark/ByTask/Tasks/WriteLineDocTask.cs
@@ -88,6 +88,8 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
         private readonly bool[] sufficientFields;
         private readonly bool checkSufficientFields;
 
+        private readonly object lineFileLock = new object(); // LUCENENET specific - lock to ensure writes don't collide for this instance
+
 
         public WriteLineDocTask(PerfRunData runData)
             : base(runData)
@@ -157,7 +159,10 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
             {
                 sb.Append(SEP).Append(f);
             }
-            @out.WriteLine(sb.ToString());
+            lock (lineFileLock) // LUCENENET specific - lock to ensure writes don't collide for this instance
+            {
+                @out.WriteLine(sb.ToString());
+            }
         }
 
         protected override string GetLogMessage(int recsCount)
@@ -194,9 +199,12 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
             }
             if (sufficient)
             {
-                sb.Length = sb.Length - 1; // remove redundant last separator
-                                           // lineFileOut is a PrintWriter, which synchronizes internally in println.
-                LineFileOut(doc).WriteLine(sb.ToString());
+                sb.Length--; // remove redundant last separator
+                // lineFileOut is a PrintWriter, which synchronizes internally in println.
+                lock (lineFileLock) // LUCENENET specific - lock to ensure writes don't collide for this instance
+                {
+                    LineFileOut(doc).WriteLine(sb.ToString());
+                }
             }
 
             return 1;
@@ -215,6 +223,8 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
         {
             if (disposing)
             {
+                threadBuffer.Dispose(); // LUCENENET specific: ThreadLocal is disposable
+                threadNormalizer.Dispose(); // LUCENENET specific: ThreadLocal is disposable
                 lineFileOut.Dispose();
             }
             base.Dispose(disposing);
diff --git a/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/WriteLineDocTaskTest.cs b/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/WriteLineDocTaskTest.cs
index d97dbbb..c6e626c 100644
--- a/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/WriteLineDocTaskTest.cs
+++ b/src/Lucene.Net.Tests.Benchmark/ByTask/Tasks/WriteLineDocTaskTest.cs
@@ -203,7 +203,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
 
         internal static void assertHeaderLine(String line)
         {
-            assertTrue("First line should be a header line", line.StartsWith(WriteLineDocTask.FIELDS_HEADER_INDICATOR, StringComparison.Ordinal));
+            assertTrue("First line should be a header line", line != null && line.StartsWith(WriteLineDocTask.FIELDS_HEADER_INDICATOR, StringComparison.Ordinal));
         }
 
         /* Tests WriteLineDocTask with a bzip2 format. */
@@ -370,10 +370,10 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
         private class ThreadAnonymousHelper : ThreadClass
         {
             private readonly WriteLineDocTask wldt;
-            internal Exception Exception { get; private set; }
             public ThreadAnonymousHelper(string name, WriteLineDocTask wldt)
                 : base(name)
             {
+                this.IsDebug = true;
                 this.wldt = wldt;
             }
 
@@ -385,8 +385,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
                 }
                 catch (Exception e)
                 {
-                    //throw new Exception(e.ToString(), e);
-                    this.Exception = e;
+                    throw new Exception(e.ToString(), e);
                 }
             }
         }
@@ -396,31 +395,21 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
         {
             FileInfo file = new FileInfo(Path.Combine(getWorkDir().FullName, "one-line"));
             PerfRunData runData = createPerfRunData(file, false, typeof(ThreadingDocMaker).AssemblyQualifiedName);
-            WriteLineDocTask wldt = new WriteLineDocTask(runData);
             ThreadClass[] threads = new ThreadClass[10];
-            for (int i = 0; i < threads.Length; i++)
-            {
-                threads[i] = new ThreadAnonymousHelper("t" + i, wldt);
-            }
-
-            foreach (ThreadClass t in threads) t.Start();
-            foreach (ThreadClass t in threads) t.Join();
-
-            wldt.Dispose();
-
-            // LUCENENET specific - need to transfer any exception that occurred back to this thread
-            foreach (ThreadClass t in threads)
+            using (WriteLineDocTask wldt = new WriteLineDocTask(runData))
             {
-                var thread = t as ThreadAnonymousHelper;
-
-                if (thread?.Exception != null)
+                for (int i = 0; i < threads.Length; i++)
                 {
-                    throw thread.Exception;
+                    threads[i] = new ThreadAnonymousHelper("t" + i, wldt);
                 }
-            }
+
+                foreach (ThreadClass t in threads) t.Start();
+                foreach (ThreadClass t in threads) t.Join();
+
+            } // wldt.Dispose();
 
             ISet<String> ids = new HashSet<string>();
-            TextReader br = new StreamReader(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
+            TextReader br = new StreamReader(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.None), Encoding.UTF8);
             try
             {
                 String line = br.ReadLine();
@@ -428,6 +417,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Tasks
                 for (int i = 0; i < threads.Length; i++)
                 {
                     line = br.ReadLine();
+                    assertNotNull($"line for index {i.ToString()} is missing", line); // LUCENENET specific - ensure the line is there before splitting
                     String[] parts = line.Split(WriteLineDocTask.SEP).TrimEnd();
                     assertEquals(line, 3, parts.Length);
                     // check that all thread names written are the same in the same line