Skip to content

fix #1923 add support for the shard stores API #1938

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

Merged
merged 1 commit into from
Mar 22, 2016
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;
using Elasticsearch.Net;

namespace Nest
{
public partial interface IElasticClient
{
/// <summary>
/// Indices level stats provide statistics on different operations happening on an index. The API provides statistics on
/// the index level scope (though most stats can also be retrieved using node level scope).
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-stats.html
/// </summary>
/// <param name="selector">Optionaly further describe the indices stats operation</param>
IIndicesShardStoresResponse IndicesShardStores(Func<IndicesShardStoresDescriptor, IIndicesShardStoresRequest> selector = null);

/// <inheritdoc/>
IIndicesShardStoresResponse IndicesShardStores(IIndicesShardStoresRequest request);

/// <inheritdoc/>
Task<IIndicesShardStoresResponse> IndicesShardStoresAsync(Func<IndicesShardStoresDescriptor, IIndicesShardStoresRequest> selector = null);

/// <inheritdoc/>
Task<IIndicesShardStoresResponse> IndicesShardStoresAsync(IIndicesShardStoresRequest request);

}

public partial class ElasticClient
{
/// <inheritdoc/>
public IIndicesShardStoresResponse IndicesShardStores(Func<IndicesShardStoresDescriptor, IIndicesShardStoresRequest> selector = null) =>
this.IndicesShardStores(selector.InvokeOrDefault(new IndicesShardStoresDescriptor()));

/// <inheritdoc/>
public IIndicesShardStoresResponse IndicesShardStores(IIndicesShardStoresRequest request) =>
this.Dispatcher.Dispatch<IIndicesShardStoresRequest, IndicesShardStoresRequestParameters, IndicesShardStoresResponse>(
request,
(p, d) => this.LowLevelDispatch.IndicesShardStoresDispatch<IndicesShardStoresResponse>(p)
);

/// <inheritdoc/>
public Task<IIndicesShardStoresResponse> IndicesShardStoresAsync(Func<IndicesShardStoresDescriptor, IIndicesShardStoresRequest> selector = null) =>
this.IndicesShardStoresAsync(selector.InvokeOrDefault(new IndicesShardStoresDescriptor()));

/// <inheritdoc/>
public Task<IIndicesShardStoresResponse> IndicesShardStoresAsync(IIndicesShardStoresRequest request) =>
this.Dispatcher.DispatchAsync<IIndicesShardStoresRequest, IndicesShardStoresRequestParameters, IndicesShardStoresResponse, IIndicesShardStoresResponse>(
request,
(p, d) => this.LowLevelDispatch.IndicesShardStoresDispatchAsync<IndicesShardStoresResponse>(p)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;

namespace Nest
{
public partial interface IIndicesShardStoresRequest
{
IEnumerable<TypeName> Types { get; set; }
}

public partial class IndicesShardStoresRequest
{
private IEnumerable<TypeName> _types;
public IEnumerable<TypeName> Types
{
get { return _types; }
set
{
if (value.HasAny()) this.RequestState.RequestParameters.AddQueryString("types", value);
else this.RequestState.RequestParameters.RemoveQueryString("types");
this._types = value;
}
}
}

[DescriptorFor("IndicesShardStores")]
public partial class IndicesShardStoresDescriptor
{
private IEnumerable<TypeName> _types;
IEnumerable<TypeName> IIndicesShardStoresRequest.Types
{
get { return _types; }
set
{
if (value.HasAny()) this.RequestState.RequestParameters.AddQueryString("types", value);
else this.RequestState.RequestParameters.RemoveQueryString("types");
this._types = value;
}
}

//<summary>A comma-separated list of fields for `completion` metric (supports wildcards)</summary>
public IndicesShardStoresDescriptor Types(params TypeName[] types) =>
Assign(a => a.Types = types);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Nest
{
public interface IIndicesShardStoresResponse : IResponse
{
[JsonProperty(PropertyName = "indices")]
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
Dictionary<string, IndicesShardStores> Indices { get; set; }
}

[JsonObject]
public class IndicesShardStoresResponse : ResponseBase, IIndicesShardStoresResponse
{
public Dictionary<string, IndicesShardStores> Indices { get; set; }
}

public class IndicesShardStores
{
[JsonProperty(PropertyName = "shards")]
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
public Dictionary<string, ShardStoreWrapper> Shards { get; set; }
}

public class ShardStoreWrapper
{
public IEnumerable<ShardStore> Stores { get; set; }
}

[JsonConverter(typeof(ShardStoreJsonConverter))]
public class ShardStore
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("transport_address")]
public string TransportAddress { get; set; }

[JsonProperty("version")]
public long Version { get; set; }

[JsonProperty("store_exeption")]
public ShardStoreException StoreException { get; set; }

[JsonProperty("allocation")]
public ShardStoreAllocation Allocation { get; set; }

[JsonProperty("attributes")]
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
public Dictionary<string, object> Attributes { get; set; }
}

public class ShardStoreException
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("reason")]
public string Reason { get; set; }
}

public enum ShardStoreAllocation
{
[EnumMember(Value = "primary")]
Primary,
[EnumMember(Value = "replica")]
Replica,
[EnumMember(Value = "unused")]
Unused
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Nest
{
internal class ShardStoreJsonConverter : JsonConverter
{
private readonly VerbatimDictionaryKeysJsonConverter _dictionaryConverter =
new VerbatimDictionaryKeysJsonConverter();

private readonly PropertyJsonConverter _elasticTypeConverter = new PropertyJsonConverter();

public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanConvert(Type objectType) => objectType == typeof(IDictionary<string, IFieldMapping>);

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var r = new ShardStore();
JObject o = JObject.Load(reader);
var properties = o.Properties().ToListOrNullIfEmpty();
var id = properties.First();
properties.AddRange(id.Value.Value<JObject>().Properties());

r.Id = id.Name;

foreach (var p in properties)
{
switch(p.Name)
{
case "name":
r.Name = p.Value.Value<string>();
break;
case "transport_address":
r.TransportAddress = p.Value.Value<string>();
break;
case "version":
r.Version = p.Value.Value<long>();
break;
case "store_exception":
r.StoreException = p.Value.ToObject<ShardStoreException>();
break;
case "allocation":
r.Allocation = p.Value.ToObject<ShardStoreAllocation>();
break;
case "attributes":
r.Attributes = p.Value.ToObject<Dictionary<string, object>>();
break;
}
}
return r;
}
}
}
4 changes: 4 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@
<Compile Include="Indices\Monitoring\IndicesSegments\SegmentsResponse.cs" />
<Compile Include="Indices\Monitoring\IndicesSegments\ShardSegmentRouting.cs" />
<Compile Include="Indices\Monitoring\IndicesSegments\ShardsSegment.cs" />
<Compile Include="Indices\Monitoring\IndicesShardStores\ElasticClient-IndicesShardStores.cs" />
<Compile Include="Indices\Monitoring\IndicesShardStores\ShardStoreJsonConverter.cs" />
<Compile Include="Indices\Monitoring\IndicesShardStores\IndicesShardStoresRequest.cs" />
<Compile Include="Indices\Monitoring\IndicesShardStores\IndicesShardStoresResponse.cs" />
<Compile Include="Indices\Monitoring\IndicesStats\ElasticClient-IndicesStats.cs" />
<Compile Include="Indices\Monitoring\IndicesStats\IndexStats.cs" />
<Compile Include="Indices\Monitoring\IndicesStats\IndicesStats.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Framework;
using Tests.Framework.Integration;
using Xunit;

namespace Tests.Indices.Monitoring.IndicesShardStores
{
[Collection(IntegrationContext.OwnIndex)]
public class IndicesShardStoresApiTests : ApiIntegrationTestBase<IIndicesShardStoresResponse, IIndicesShardStoresRequest, IndicesShardStoresDescriptor, IndicesShardStoresRequest>
{
public IndicesShardStoresApiTests(OwnIndexCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private static string IndexWithUnassignedShards = "nest-" + RandomString();

protected override void BeforeAllCalls(IElasticClient client, IDictionary<ClientMethod, string> values)
{
client.CreateIndex(IndexWithUnassignedShards, s => s
.Settings(settings => settings
.NumberOfShards(1)
.NumberOfReplicas(2)
)
);
client.Index(new IndexRequest<object>(IndexWithUnassignedShards)
{
Document = new { x = 1 },
Refresh = true
});
}

protected override LazyResponses ClientUsage() => Calls(
fluent: (client, f) => client.IndicesShardStores(f),
fluentAsync: (client, f) => client.IndicesShardStoresAsync(f),
request: (client, r) => client.IndicesShardStores(r),
requestAsync: (client, r) => client.IndicesShardStoresAsync(r)
);
protected override IndicesShardStoresRequest Initializer =>
new IndicesShardStoresRequest(IndexWithUnassignedShards)
{
Status = new[] { "all" }
};
protected override Func<IndicesShardStoresDescriptor, IIndicesShardStoresRequest> Fluent => s =>
s.Index(IndexWithUnassignedShards)
.Status("all");

protected override bool ExpectIsValid => true;
protected override int ExpectStatusCode => 200;
protected override HttpMethod HttpMethod => HttpMethod.GET;
protected override string UrlPath => $"/{IndexWithUnassignedShards}/_shard_stores";

[I] public Task AssertResponse() => AssertOnAllResponses(r =>
{
r.Indices.Should().NotBeEmpty();
var indicesShardStore = r.Indices[IndexWithUnassignedShards];
indicesShardStore.Should().NotBeNull();
indicesShardStore.Shards.Should().NotBeEmpty().And.ContainKey("0");
var shardStoreWrapper = indicesShardStore.Shards["0"];
shardStoreWrapper.Stores.Should().NotBeNullOrEmpty();

var shardStore = shardStoreWrapper.Stores.First();
shardStore.Id.Should().NotBeNullOrWhiteSpace();
shardStore.Name.Should().NotBeNullOrWhiteSpace();
shardStore.TransportAddress.Should().NotBeNullOrWhiteSpace();
shardStore.Version.Should().BeGreaterThan(0);
shardStore.Allocation.Should().Be(ShardStoreAllocation.Primary);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Elasticsearch.Net;
using Nest;
using Tests.Framework;
using Tests.Framework.MockData;
using static Tests.Framework.UrlTester;

namespace Tests.Indices.Monitoring.IndicesShardStores
{
public class IndicesShardStoresUrlTests
{
[U] public async Task Urls()
{
await GET($"/_shard_stores")
.Fluent(c => c.IndicesShardStores())
.Request(c => c.IndicesShardStores(new IndicesShardStoresRequest()))
.FluentAsync(c => c.IndicesShardStoresAsync())
.RequestAsync(c => c.IndicesShardStoresAsync(new IndicesShardStoresRequest()))
;

var index = "index1,index2";
await GET($"/index1%2Cindex2/_shard_stores")
.Fluent(c => c.IndicesShardStores(s=>s.Index(index)))
.Request(c => c.IndicesShardStores(new IndicesShardStoresRequest(index)))
.FluentAsync(c => c.IndicesShardStoresAsync(s=>s.Index(index)))
.RequestAsync(c => c.IndicesShardStoresAsync(new IndicesShardStoresRequest(index)))
;

}
}
}
2 changes: 2 additions & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@
<Compile Include="Indices\StatusManagement\ForceMerge\ForceMergeUrlTests.cs" />
<Compile Include="Search\SearchTemplate\RenderSearchTemplate\RenderSearchTemplateApiTests.cs" />
<Compile Include="Search\SearchTemplate\RenderSearchTemplate\RenderSearchTemplateUrlTests.cs" />
<Compile Include="Indices\Monitoring\IndicesShardStores\IndicesShardStoresApiTests.cs" />
<Compile Include="Indices\Monitoring\IndicesShardStores\IndicesShardStoresUrlTests.cs" />
<None Include="index.asciidoc" />
<None Include="paket.references" />
<None Include="tests.yaml" />
Expand Down