Skip to content

Commit

Permalink
Re instantiate custom deserialisation for nodes hot threads (#3786)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebrain authored Jun 3, 2019
1 parent 6ffb24f commit 1fa7ffe
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ public InitializerMethod(CsharpNames names, string link, string summary) : base(

private bool IsCatMethod => CsharpNames.Namespace == "Cat";

private bool IsCatHelpMethod => CsharpNames.Namespace == "Cat" && MethodName == "Help";
private bool IsCatHelpMethod => IsCatMethod && MethodName == "Help";

public string DispatchMethod => IsCatHelpMethod ? "DoCatHelp" : IsCatMethod ? "DoCat" : "DoRequest";
private bool IsNodesHotThreadsMethod => CsharpNames.Namespace == "Nodes" && MethodName == "HotThreads";

public string DispatchGenerics => IsCatMethod
public string DispatchMethod => IsNodesHotThreadsMethod ? "DoNodesHotThreads" : IsCatHelpMethod ? "DoCatHelp" : IsCatMethod ? "DoCat" : "DoRequest";

public string DispatchGenerics => IsNodesHotThreadsMethod ? "" : IsCatMethod
? $"<{ArgumentType},{CsharpNames.ParametersName},{CsharpNames.RequestName.Replace("Request", "Record")}>"
: $"<{ArgumentType},{ResponseName}>";

public string DispatchParameters => IsCatMethod ? "request" : "request, request.RequestParameters";

public string DispatchParameters => IsCatMethod || IsNodesHotThreadsMethod
? "request"
: "request, request.RequestParameters";
}
}
4 changes: 2 additions & 2 deletions src/Nest/ElasticClient.Nodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ internal NodesNamespace(ElasticClient client): base(client)
/// <para> </para>
/// <a href = "http://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-nodes-hot-threads.html">http://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-nodes-hot-threads.html</a>
/// </summary>
public NodesHotThreadsResponse HotThreads(INodesHotThreadsRequest request) => DoRequest<INodesHotThreadsRequest, NodesHotThreadsResponse>(request, request.RequestParameters);
public NodesHotThreadsResponse HotThreads(INodesHotThreadsRequest request) => DoNodesHotThreads(request);
/// <summary>
/// <c>GET</c> request to the <c>nodes.hot_threads</c> API, read more about this API online:
/// <para> </para>
/// <a href = "http://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-nodes-hot-threads.html">http://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-nodes-hot-threads.html</a>
/// </summary>
public Task<NodesHotThreadsResponse> HotThreadsAsync(INodesHotThreadsRequest request, CancellationToken ct = default) => DoRequestAsync<INodesHotThreadsRequest, NodesHotThreadsResponse>(request, request.RequestParameters, ct);
public Task<NodesHotThreadsResponse> HotThreadsAsync(INodesHotThreadsRequest request, CancellationToken ct = default) => DoNodesHotThreadsAsync(request, ct);
/// <summary>
/// <c>GET</c> request to the <c>nodes.info</c> API, read more about this API online:
/// <para> </para>
Expand Down
57 changes: 56 additions & 1 deletion src/Nest/ElasticClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Elasticsearch.Net;
Expand Down Expand Up @@ -69,6 +71,48 @@ private CatResponse<TCatRecord> DeserializeCatHelpResponse<TCatRecord>(IApiCallD
return catResponse;
}

//::: {Dragonfly}{lvtIV72sRIWBGik7ulbuaw}{127.0.0.1}{127.0.0.1:9300}
private static readonly Regex NodeRegex = new Regex(@"^\s\{(?<name>.+?)\}\{(?<id>.+?)\}(?<hosts>.+)\n");

private static NodesHotThreadsResponse DeserializeNodesHotThreadsResponse(IApiCallDetails response, Stream stream)
{
using (stream)
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
var plainTextResponse = sr.ReadToEnd();

// If the response doesn't start with :::, which is the pattern that delimits
// each node section in the response, then the response format isn't recognized.
// Just return an empty response object. This is especially useful when unit
// testing against an in-memory connection where you won't get a real response.
if (!plainTextResponse.StartsWith(":::", StringComparison.Ordinal))
return new NodesHotThreadsResponse();

var sections = plainTextResponse.Split(new string[] { ":::" }, StringSplitOptions.RemoveEmptyEntries);
var info =
from section in sections
select section.Split(new string[] { "\n \n" }, StringSplitOptions.None)
into sectionLines
where sectionLines.Length > 0
let nodeLine = sectionLines.FirstOrDefault()
where nodeLine != null
let matches = NodeRegex.Match(nodeLine)
where matches.Success
let node = matches.Groups["name"].Value
let nodeId = matches.Groups["id"].Value
let hosts = matches.Groups["hosts"].Value.Split(new[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries)
let threads = sectionLines.Skip(1).Take(sectionLines.Length - 1).ToList()
select new HotThreadInformation
{
NodeName = node,
NodeId = nodeId,
Threads = threads,
Hosts = hosts
};
return new NodesHotThreadsResponse(info.ToList());
}
}

protected CatResponse<TCatRecord> DoCat<TRequest, TParams, TCatRecord>(TRequest request)
where TCatRecord : ICatRecord
where TParams : RequestParameters<TParams>, new()
Expand All @@ -87,7 +131,6 @@ protected Task<CatResponse<TCatRecord>> DoCatAsync<TRequest, TParams, TCatRecord
return DoRequestAsync<TRequest, CatResponse<TCatRecord>>(request, request.RequestParameters, ct, r => ElasticClient.ForceJson(r));
}


protected CatResponse<CatHelpRecord> DoCatHelp<TRequest, TParams, TCatRecord>(TRequest request)
where TParams : RequestParameters<TParams>, new()
where TRequest : class, IRequest<TParams>
Expand All @@ -103,6 +146,18 @@ protected Task<CatResponse<CatHelpRecord>> DoCatHelpAsync<TRequest, TParams, TCa
request.RequestParameters.DeserializationOverride = DeserializeCatHelpResponse<CatHelpRecord>;
return DoRequestAsync<TRequest, CatResponse<CatHelpRecord>>(request, request.RequestParameters, ct, r => ElasticClient.ForceJson(r));
}

protected NodesHotThreadsResponse DoNodesHotThreads(INodesHotThreadsRequest request)
{
request.RequestParameters.DeserializationOverride = DeserializeNodesHotThreadsResponse;
return DoRequest<INodesHotThreadsRequest, NodesHotThreadsResponse>(request, request.RequestParameters);
}

protected Task<NodesHotThreadsResponse> DoNodesHotThreadsAsync(INodesHotThreadsRequest request, CancellationToken ct)
{
request.RequestParameters.DeserializationOverride = DeserializeNodesHotThreadsResponse;
return DoRequestAsync<INodesHotThreadsRequest, NodesHotThreadsResponse>(request, request.RequestParameters, ct);
}
}
/// <summary>
/// ElasticClient is NEST's strongly typed client which exposes fully mapped Elasticsearch endpoints
Expand Down

0 comments on commit 1fa7ffe

Please sign in to comment.