-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
SparkSessionOperation.cs
161 lines (133 loc) · 6.1 KB
/
SparkSessionOperation.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
// 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.StartCreateSparkSession(SparkSessionOptions, bool?, CancellationToken)"/>,
/// <see cref="SparkSessionClient.StartCreateSparkSessionAsync(SparkSessionOptions, bool?, CancellationToken)"/>,
/// </summary>
public class SparkSessionOperation : Operation<SparkSession>
{
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 session.
/// </summary>
private SparkSession _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 created Spark session.
/// </summary>
private int _sessionId;
/// <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 => _sessionId.ToString(CultureInfo.InvariantCulture);
/// <summary>
/// Gets the <see cref="SparkSession"/>.
/// 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="SparkSession"/> immediately but may take time to the session to be ready.
/// </remarks>
public override SparkSession Value => OperationHelpers.GetValue(ref _value);
/// <summary>
/// Initializes a new instance of the <see cref="SparkSessionOperation"/> class.
/// </summary>
/// <param name="sessionId">The ID of the Spark session.</param>
/// <param name="client">The client used to check for completion.</param>
public SparkSessionOperation(int sessionId, SparkSessionClient client)
{
_sessionId = sessionId;
_client = client;
}
internal SparkSessionOperation(SparkSessionClient client, ClientDiagnostics diagnostics, Response<SparkSession> response)
: this(response.Value.Id, client)
{
_rawResponse = response.GetRawResponse();
_diagnostics = diagnostics;
}
/// <summary> Initializes a new instance of <see cref="SparkSessionOperation" /> for mocking. </summary>
protected SparkSessionOperation() {}
/// <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<SparkSession>> WaitForCompletionAsync(CancellationToken cancellationToken = default) =>
this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken);
/// <inheritdoc />
public override ValueTask<Response<SparkSession>> 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<SparkSession> update = async
? await _client.GetSparkSessionAsync(_sessionId, true, cancellationToken).ConfigureAwait(false)
: _client.GetSparkSession(_sessionId, true, 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(LivyStates livyState)
{
if (livyState == LivyStates.Error || livyState == LivyStates.Dead || livyState == LivyStates.Success || livyState == LivyStates.Killed || livyState == LivyStates.Idle)
{
return true;
}
return false;
}
}
}