|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Concurrent; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Threading; |
| 9 | +using Microsoft.Spark.Services; |
| 10 | + |
| 11 | +namespace Microsoft.Spark.Interop.Ipc |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// In .NET for Apache Spark, we maintain a 1-to-1 mapping between .NET application threads |
| 15 | + /// and corresponding JVM threads. When a .NET thread calls a Spark API, that call is executed |
| 16 | + /// by its corresponding JVM thread. This functionality allows for multithreaded applications |
| 17 | + /// with thread-local variables. |
| 18 | + /// |
| 19 | + /// This class keeps track of the .NET application thread lifecycle. When a .NET application |
| 20 | + /// thread is no longer alive, this class submits an rmThread command to the JVM backend to |
| 21 | + /// dispose of its corresponding JVM thread. All methods are thread-safe. |
| 22 | + /// </summary> |
| 23 | + internal class JvmThreadPoolGC : IDisposable |
| 24 | + { |
| 25 | + private readonly ILoggerService _loggerService; |
| 26 | + private readonly IJvmBridge _jvmBridge; |
| 27 | + private readonly TimeSpan _threadGCInterval; |
| 28 | + private readonly ConcurrentDictionary<int, Thread> _activeThreads; |
| 29 | + |
| 30 | + private readonly object _activeThreadGCTimerLock; |
| 31 | + private Timer _activeThreadGCTimer; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Construct the JvmThreadPoolGC. |
| 35 | + /// </summary> |
| 36 | + /// <param name="loggerService">Logger service.</param> |
| 37 | + /// <param name="jvmBridge">The JvmBridge used to call JVM methods.</param> |
| 38 | + /// <param name="threadGCInterval">The interval to GC finished threads.</param> |
| 39 | + public JvmThreadPoolGC(ILoggerService loggerService, IJvmBridge jvmBridge, TimeSpan threadGCInterval) |
| 40 | + { |
| 41 | + _loggerService = loggerService; |
| 42 | + _jvmBridge = jvmBridge; |
| 43 | + _threadGCInterval = threadGCInterval; |
| 44 | + _activeThreads = new ConcurrentDictionary<int, Thread>(); |
| 45 | + |
| 46 | + _activeThreadGCTimerLock = new object(); |
| 47 | + _activeThreadGCTimer = null; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Dispose of the GC timer and run a final round of thread GC. |
| 52 | + /// </summary> |
| 53 | + public void Dispose() |
| 54 | + { |
| 55 | + lock (_activeThreadGCTimerLock) |
| 56 | + { |
| 57 | + if (_activeThreadGCTimer != null) |
| 58 | + { |
| 59 | + _activeThreadGCTimer.Dispose(); |
| 60 | + _activeThreadGCTimer = null; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + GCThreads(); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Try to start monitoring a thread. |
| 69 | + /// </summary> |
| 70 | + /// <param name="thread">The thread to add.</param> |
| 71 | + /// <returns>True if success, false if already added.</returns> |
| 72 | + public bool TryAddThread(Thread thread) |
| 73 | + { |
| 74 | + bool returnValue = _activeThreads.TryAdd(thread.ManagedThreadId, thread); |
| 75 | + |
| 76 | + // Initialize the GC timer if necessary. |
| 77 | + if (_activeThreadGCTimer == null) |
| 78 | + { |
| 79 | + lock (_activeThreadGCTimerLock) |
| 80 | + { |
| 81 | + if (_activeThreadGCTimer == null && _activeThreads.Count > 0) |
| 82 | + { |
| 83 | + _activeThreadGCTimer = new Timer( |
| 84 | + (state) => GCThreads(), |
| 85 | + null, |
| 86 | + _threadGCInterval, |
| 87 | + _threadGCInterval); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return returnValue; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Try to remove a thread from the pool. If the removal is successful, then the |
| 97 | + /// corresponding JVM thread will also be disposed. |
| 98 | + /// </summary> |
| 99 | + /// <param name="threadId">The ID of the thread to remove.</param> |
| 100 | + /// <returns>True if success, false if the thread cannot be found.</returns> |
| 101 | + private bool TryDisposeJvmThread(int threadId) |
| 102 | + { |
| 103 | + if (_activeThreads.TryRemove(threadId, out _)) |
| 104 | + { |
| 105 | + // _activeThreads does not have ownership of the threads on the .NET side. This |
| 106 | + // class does not need to call Join() on the .NET Thread. However, this class is |
| 107 | + // responsible for sending the rmThread command to the JVM to trigger disposal |
| 108 | + // of the corresponding JVM thread. |
| 109 | + if ((bool)_jvmBridge.CallStaticJavaMethod("DotnetHandler", "rmThread", threadId)) |
| 110 | + { |
| 111 | + _loggerService.LogDebug($"GC'd JVM thread {threadId}."); |
| 112 | + return true; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + _loggerService.LogWarn( |
| 117 | + $"rmThread returned false for JVM thread {threadId}. " + |
| 118 | + $"Either thread does not exist or has already been GC'd."); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Remove any threads that are no longer active. |
| 127 | + /// </summary> |
| 128 | + private void GCThreads() |
| 129 | + { |
| 130 | + foreach (KeyValuePair<int, Thread> kvp in _activeThreads) |
| 131 | + { |
| 132 | + if (!kvp.Value.IsAlive) |
| 133 | + { |
| 134 | + TryDisposeJvmThread(kvp.Key); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + lock (_activeThreadGCTimerLock) |
| 139 | + { |
| 140 | + // Dispose of the timer if there are no threads to monitor. |
| 141 | + if (_activeThreadGCTimer != null && _activeThreads.IsEmpty) |
| 142 | + { |
| 143 | + _activeThreadGCTimer.Dispose(); |
| 144 | + _activeThreadGCTimer = null; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments