Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: DtmClient add query single trans global status/detail #88

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions samples/DtmSample/Controllers/MsgTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,29 @@

return Ok(TransResponse.BuildSucceedResponse());
}

/// <summary>
/// query
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("query")]
public async Task<IActionResult> Query(string gid, CancellationToken cancellationToken)

Check warning on line 286 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.Query(string, CancellationToken)' (but other parameters do)

Check warning on line 286 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.Query(string, CancellationToken)' (but other parameters do)

Check warning on line 286 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on ubuntu-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.Query(string, CancellationToken)' (but other parameters do)

Check warning on line 286 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on ubuntu-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.Query(string, CancellationToken)' (but other parameters do)
{
TransGlobal trans = await _dtmClient.Query(gid, cancellationToken);
return Ok(trans);
}

/// <summary>
/// query status
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("query-status")]
public async Task<IActionResult> QueryStatus(string gid, CancellationToken cancellationToken)

Check warning on line 298 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.QueryStatus(string, CancellationToken)' (but other parameters do)

Check warning on line 298 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on windows-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.QueryStatus(string, CancellationToken)' (but other parameters do)

Check warning on line 298 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on ubuntu-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.QueryStatus(string, CancellationToken)' (but other parameters do)

Check warning on line 298 in samples/DtmSample/Controllers/MsgTestController.cs

View workflow job for this annotation

GitHub Actions / build on ubuntu-latest

Parameter 'gid' has no matching param tag in the XML comment for 'MsgTestController.QueryStatus(string, CancellationToken)' (but other parameters do)
{
string status = await _dtmClient.QueryStatus(gid, cancellationToken);
return Ok(status);
}
}
}
5 changes: 5 additions & 0 deletions src/Dtmcli/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ internal static class Request
internal const string BRANCH_COMPENSATE = "compensate";

internal const string URL_NewGid = "/api/dtmsvr/newGid";

/// <summary>
/// query single
/// </summary>
internal const string URL_Query = "/api/dtmsvr/query";
}
}
}
45 changes: 43 additions & 2 deletions src/Dtmcli/DtmClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DtmCommon;
using System;
using DtmCommon;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Net.Http;
Expand Down Expand Up @@ -46,7 +47,7 @@ public HttpClient GetHttpClient(string name)
{
return _httpClientFactory.CreateClient(name);
}

public async Task<HttpResponseMessage> PrepareWorkflow(TransBase tb, CancellationToken cancellationToken)
{
var url = string.Concat(_dtmOptions.DtmUrl.TrimEnd(Slash), Constant.Request.URLBASE_PREFIX, "prepareWorkflow");
Expand Down Expand Up @@ -133,6 +134,46 @@ public TransBase TransBaseFromQuery(Microsoft.AspNetCore.Http.IQueryCollection q
}
#endif

/// <summary>
/// Query single global transaction
/// </summary>
/// <param name="gid">global id</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<TransGlobal> Query(string gid, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(gid)) throw new ArgumentNullException(nameof(gid));

var url = string.Concat(_dtmOptions.DtmUrl.TrimEnd(Slash), Constant.Request.URL_Query, $"?gid={gid}");
var client = _httpClientFactory.CreateClient(Constant.DtmClientHttpName);
var response = await client.GetAsync(url, cancellationToken).ConfigureAwait(false);
var dtmContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
DtmImp.Utils.CheckStatus(response.StatusCode, dtmContent);
return JsonSerializer.Deserialize<TransGlobal>(dtmContent, _jsonOptions);
}

/// <summary>
/// Query single global transaction status
/// </summary>
/// <param name="gid"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<string> QueryStatus(string gid, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(gid)) throw new ArgumentNullException(nameof(gid));

var url = string.Concat(_dtmOptions.DtmUrl.TrimEnd(Slash), Constant.Request.URL_Query, $"?gid={gid}");
var client = _httpClientFactory.CreateClient(Constant.DtmClientHttpName);
var response = await client.GetAsync(url, cancellationToken).ConfigureAwait(false);
var dtmContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
DtmImp.Utils.CheckStatus(response.StatusCode, dtmContent);
var graph = JsonSerializer.Deserialize<TransGlobalForStatus>(dtmContent, _jsonOptions);
return graph.Transaction == null
? string.Empty
: graph.Transaction.Status;
}

public class DtmGid
{
[JsonPropertyName("gid")]
Expand Down
4 changes: 4 additions & 0 deletions src/Dtmcli/IDtmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ public interface IDtmClient
#if NET5_0_OR_GREATER
TransBase TransBaseFromQuery(Microsoft.AspNetCore.Http.IQueryCollection query);
#endif

Task<TransGlobal> Query(string gid, CancellationToken cancellationToken);

Task<string> QueryStatus(string gid, CancellationToken cancellationToken);
}
}
89 changes: 89 additions & 0 deletions src/Dtmcli/TransGlobal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Dtmcli;

/// <summary>
/// query status only
/// </summary>
internal class TransGlobalForStatus
{
[JsonPropertyName("transaction")] public DtmTransactionForStatus Transaction { get; set; }

public class DtmTransactionForStatus
{
[JsonPropertyName("status")] public string Status { get; set; }
}
}

// convert from json to c# code, json sample: saga succeed http (dtm-labs/dtm/qs/main.go)
public class TransGlobal
{
[JsonPropertyName("branches")] public List<DtmBranch> Branches { get; set; }

[JsonPropertyName("transaction")] public DtmTransaction Transaction { get; set; }

public class DtmTransaction
{
[JsonPropertyName("id")] public int Id { get; set; }

[JsonPropertyName("create_time")] public DateTimeOffset CreateTime { get; set; }

[JsonPropertyName("update_time")] public DateTimeOffset UpdateTime { get; set; }

[JsonPropertyName("gid")] public string Gid { get; set; }

[JsonPropertyName("trans_type")] public string TransType { get; set; }

[JsonPropertyName("steps")] public List<TransactionStep> Steps { get; set; }

[JsonPropertyName("payloads")] public List<string> Payloads { get; set; }

[JsonPropertyName("status")] public string Status { get; set; }

// [JsonPropertyName("query_prepared")] public string QueryPrepared { get; set; }

[JsonPropertyName("protocol")] public string Protocol { get; set; }

[JsonPropertyName("finish_time")] public DateTimeOffset FinishTime { get; set; }

[JsonPropertyName("options")] public string Options { get; set; }

[JsonPropertyName("next_cron_interval")]
public int NextCronInterval { get; set; }

[JsonPropertyName("next_cron_time")] public DateTimeOffset NextCronTime { get; set; }

// [JsonPropertyName("wait_result")] public bool WaitResult { get; set; }

[JsonPropertyName("concurrent")] public bool Concurrent { get; set; }
}

public class DtmBranch
{
[JsonPropertyName("id")] public int Id { get; set; }

[JsonPropertyName("create_time")] public DateTimeOffset CreateTime { get; set; }

[JsonPropertyName("update_time")] public DateTimeOffset UpdateTime { get; set; }

[JsonPropertyName("gid")] public string Gid { get; set; }

[JsonPropertyName("url")] public string Url { get; set; }

[JsonPropertyName("bin_data")] public string BinData { get; set; }

[JsonPropertyName("branch_id")] public string BranchId { get; set; }

[JsonPropertyName("op")] public string Op { get; set; }

[JsonPropertyName("status")] public string Status { get; set; }
}

public class TransactionStep
{
[JsonPropertyName("action")] public string Action { get; set; }
[JsonPropertyName("Compensate")] public string Compensate { get; set; }
}
}
Loading
Loading