|
| 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.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.Spark.Interop.Ipc; |
| 8 | + |
| 9 | +namespace Microsoft.Spark.Sql.Streaming |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A class to manage all the <see cref="StreamingQuery"/> active |
| 13 | + /// in a <see cref="SparkSession"/>. |
| 14 | + /// </summary> |
| 15 | + public sealed class StreamingQueryManager : IJvmObjectReferenceProvider |
| 16 | + { |
| 17 | + private readonly JvmObjectReference _jvmObject; |
| 18 | + |
| 19 | + internal StreamingQueryManager(JvmObjectReference jvmObject) => _jvmObject = jvmObject; |
| 20 | + |
| 21 | + JvmObjectReference IJvmObjectReferenceProvider.Reference => _jvmObject; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Returns a list of active queries associated with this SQLContext. |
| 25 | + /// </summary> |
| 26 | + /// <returns>Active queries associated with this SQLContext.</returns> |
| 27 | + public IEnumerable<StreamingQuery> Active() => |
| 28 | + ((JvmObjectReference[])_jvmObject.Invoke("active")) |
| 29 | + .Select(sq => new StreamingQuery(sq)); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Returns an active query from this SQLContext or throws exception if an active |
| 33 | + /// query with this name doesn't exist. |
| 34 | + /// </summary> |
| 35 | + /// <param name="id">Id of the <see cref="StreamingQuery"/>.</param> |
| 36 | + /// <returns> |
| 37 | + /// <see cref="StreamingQuery"/> if there is an active query with the given id. |
| 38 | + /// </returns> |
| 39 | + public StreamingQuery Get(string id) => |
| 40 | + new StreamingQuery((JvmObjectReference)_jvmObject.Invoke("get", id)); |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Wait until any of the queries on the associated SQLContext has terminated since the |
| 44 | + /// creation of the context, or since <see cref="ResetTerminated"/> was called. If any |
| 45 | + /// query was terminated with an exception, then the exception will be thrown. |
| 46 | + /// |
| 47 | + /// If a query has terminated, then subsequent calls to <see cref="AwaitAnyTermination()"/> |
| 48 | + /// will either return immediately (if the query was terminated by |
| 49 | + /// <see cref="StreamingQuery.Stop"/>), or throw the exception immediately (if the query |
| 50 | + /// was terminated with exception). Use <see cref="ResetTerminated"/> to clear past |
| 51 | + /// terminations and wait for new terminations. |
| 52 | + /// |
| 53 | + /// In the case where multiple queries have terminated since <see cref="ResetTerminated"/> |
| 54 | + /// was called, if any query has terminated with exception, then |
| 55 | + /// <see cref="AwaitAnyTermination()"/> will throw any of the exception. For correctly |
| 56 | + /// documenting exceptions across multiple queries, users need to stop all of them after |
| 57 | + /// any of them terminates with exception, and then check the |
| 58 | + /// <see cref="StreamingQuery.Exception"/> for each query. |
| 59 | + /// |
| 60 | + /// Throws StreamingQueryException on the JVM if any query has terminated with an |
| 61 | + /// exception. |
| 62 | + /// </summary> |
| 63 | + public void AwaitAnyTermination() => _jvmObject.Invoke("awaitAnyTermination"); |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Wait until any of the queries on the associated SQLContext has terminated since the |
| 67 | + /// creation of the context, or since <see cref="ResetTerminated"/> was called. If any |
| 68 | + /// query was terminated with an exception, then the exception will be thrown. |
| 69 | + /// |
| 70 | + /// If a query has terminated, then subsequent calls to <see cref="AwaitAnyTermination()"/> |
| 71 | + /// will either return immediately (if the query was terminated by |
| 72 | + /// <see cref="StreamingQuery.Stop"/>), or throw the exception immediately (if the query |
| 73 | + /// was terminated with exception). Use <see cref="ResetTerminated"/> to clear past |
| 74 | + /// terminations and wait for new terminations. |
| 75 | + /// |
| 76 | + /// In the case where multiple queries have terminated since <see cref="ResetTerminated"/> |
| 77 | + /// was called, if any query has terminated with exception, then |
| 78 | + /// <see cref="AwaitAnyTermination()"/> will throw any of the exception. For correctly |
| 79 | + /// documenting exceptions across multiple queries, users need to stop all of them after |
| 80 | + /// any of them terminates with exception, and then check the |
| 81 | + /// <see cref="StreamingQuery.Exception"/> for each query. |
| 82 | + /// |
| 83 | + /// Throws StreamingQueryException on the JVM if any query has terminated with an |
| 84 | + /// exception. |
| 85 | + /// </summary> |
| 86 | + /// <param name="timeoutMs"> |
| 87 | + /// Milliseconds to wait for query to terminate. Returns whether the query has terminated |
| 88 | + /// or not. |
| 89 | + /// </param> |
| 90 | + public void AwaitAnyTermination(long timeoutMs) => |
| 91 | + _jvmObject.Invoke("awaitAnyTermination", timeoutMs); |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Forget about past terminated queries so that <see cref="AwaitAnyTermination()"/> can be |
| 95 | + /// used again to wait for new terminations |
| 96 | + /// </summary> |
| 97 | + public void ResetTerminated() => _jvmObject.Invoke("resetTerminated"); |
| 98 | + } |
| 99 | +} |
0 commit comments