/
SparkStatementOperation.cs
163 lines (135 loc) · 6.4 KB
/
SparkStatementOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Analytics.Synapse.Spark.Models;
namespace Azure.Analytics.Synapse.Spark
{
/// <summary>
/// A long-running operation for:
/// <see cref="SparkSessionClient.StartCreateSparkStatementAsync(int, SparkStatementOptions, CancellationToken)"/>,
/// <see cref="SparkSessionClient.StartCreateSparkStatement(int, SparkStatementOptions, CancellationToken)"/>,
/// </summary>
public class SparkStatementOperation : Operation<SparkStatement>
{
private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(5);
/// <summary>
/// Provides tools for exception creation in case of failure.
/// </summary>
private readonly ClientDiagnostics _diagnostics;
/// <summary>
/// The client used to check for completion.
/// </summary>
private readonly SparkSessionClient _client;
/// <summary>
/// Whether the operation has completed.
/// </summary>
private bool _hasCompleted;
/// <summary>
/// Gets the created Spark statement.
/// </summary>
private SparkStatement _value;
/// <summary>
/// Raw HTTP response.
/// </summary>
private Response _rawResponse;
/// <summary>
/// <c>true</c> if the long-running operation has a value. Otherwise, <c>false</c>.
/// </summary>
private bool _hasValue;
/// <summary>
/// Gets the Id of the Spark session where the statement executes.
/// </summary>
private int _sessionId;
/// <summary>
/// Gets the Id of the created Spark statement.
/// </summary>
private int _statementId;
/// <summary>
/// Gets a value indicating whether the operation has completed.
/// </summary>
public override bool HasCompleted => _hasCompleted;
/// <inheritdoc/>
public override bool HasValue => _hasValue;
/// <inheritdoc/>
public override string Id => _statementId.ToString(CultureInfo.InvariantCulture);
/// <summary>
/// Gets the <see cref="SparkStatement"/>.
/// You should await <see cref="WaitForCompletionAsync(CancellationToken)"/> before attempting to use session in this pending state.
/// </summary>
/// <remarks>
/// Azure Synapse will return a <see cref="SparkStatement"/> immediately but may take time to the statement to be ready.
/// </remarks>
public override SparkStatement Value => OperationHelpers.GetValue(ref _value);
/// <summary>
/// Initializes a new instance of the <see cref="SparkStatementOperation"/> class.
/// </summary>
/// <param name="sessionId">The ID of the Spark session where the statement executes.</param>
/// <param name="statementId">The ID of the Spark statement.</param>
/// <param name="client">The client used to check for completion.</param>
public SparkStatementOperation(int sessionId, int statementId, SparkSessionClient client)
{
_sessionId = sessionId;
_statementId = statementId;
_client = client;
}
internal SparkStatementOperation(SparkSessionClient client, ClientDiagnostics diagnostics, Response<SparkStatement> response, int sessionId)
: this(sessionId, response.Value.Id, client)
{
_rawResponse = response.GetRawResponse();
_diagnostics = diagnostics;
}
/// <summary> Initializes a new instance of <see cref="SparkStatementOperation" /> for mocking. </summary>
protected SparkStatementOperation() {}
/// <inheritdoc/>
public override Response GetRawResponse() => _rawResponse;
/// <inheritdoc/>
public override Response UpdateStatus(CancellationToken cancellationToken = default) => UpdateStatusAsync(false, cancellationToken).EnsureCompleted();
/// <inheritdoc/>
public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) => await UpdateStatusAsync(true, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public override ValueTask<Response<SparkStatement>> WaitForCompletionAsync(CancellationToken cancellationToken = default) =>
this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken);
/// <inheritdoc />
public override ValueTask<Response<SparkStatement>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken) =>
this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
private async ValueTask<Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
{
if (!_hasCompleted)
{
using DiagnosticScope? scope = _diagnostics?.CreateScope($"{nameof(SparkSessionOperation)}.{nameof(UpdateStatus)}");
scope?.Start();
try
{
// Get the latest status
Response<SparkStatement> update = async
? await _client.GetSparkStatementAsync(_sessionId, _statementId, cancellationToken).ConfigureAwait(false)
: _client.GetSparkStatement(_sessionId, _statementId, cancellationToken);
// Check if the operation is no longer running
_hasCompleted = IsJobComplete(update.Value.State.Value);
if (_hasCompleted)
{
_hasValue = true;
_value = update.Value;
}
// Update raw response
_rawResponse = update.GetRawResponse();
}
catch (Exception e)
{
scope?.Failed(e);
throw;
}
}
return GetRawResponse();
}
private static bool IsJobComplete(LivyStatementStates livyState)
{
return livyState == LivyStatementStates.Available || livyState == LivyStatementStates.Error || livyState == LivyStatementStates.Cancelled;
}
}
}