Skip to content

Commit

Permalink
fix nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
stasput committed Nov 18, 2024
1 parent 9adfc5a commit 87710a3
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

namespace SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Api;

Expand All @@ -8,5 +10,5 @@ public class RtqMonitoringSearchResults
public long TotalCount { get; set; }

[JsonPropertyName("taskMetas")]
public RtqMonitoringTaskMeta[] TaskMetas { get; set; }
public RtqMonitoringTaskMeta[] TaskMetas { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

using SkbKontur.Cassandra.DistributedTaskQueue.Cassandra.Entities;
using SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Json;
Expand All @@ -8,10 +10,10 @@ namespace SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Api;
public class RtqMonitoringTaskMeta
{
[JsonPropertyName("name")]
public string Name { get; set; }
public string Name { get; set; } = null!;

[JsonPropertyName("id")]
public string Id { get; set; }
public string Id { get; set; } = null!;

[JsonPropertyName("ticks")]
[JsonConverter(typeof(LongToStringConverter))]
Expand Down Expand Up @@ -50,7 +52,7 @@ public class RtqMonitoringTaskMeta
public TaskState State { get; set; }

[JsonPropertyName("taskActions")]
public TaskActions TaskActions { get; set; }
public TaskActions? TaskActions { get; set; }

[JsonPropertyName("attempts")]
public int Attempts { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

using SkbKontur.Cassandra.DistributedTaskQueue.Handling;
using SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Json;
Expand All @@ -10,15 +12,15 @@ public class RtqMonitoringTaskModel
public static RtqMonitoringTaskModel Empty => new();

[JsonPropertyName("taskMeta")]
public RtqMonitoringTaskMeta TaskMeta { get; set; }
public RtqMonitoringTaskMeta TaskMeta { get; set; } = null!;

[JsonPropertyName("taskData")]
[JsonConverter(typeof(TaskDataJsonSerializer))]
public IRtqTaskData TaskData { get; set; }
public IRtqTaskData TaskData { get; set; } = null!;

[JsonPropertyName("childTaskIds")]
public string[] ChildTaskIds { get; set; }
public string[] ChildTaskIds { get; set; } = null!;

[JsonPropertyName("exceptionInfos")]
public string[] ExceptionInfos { get; set; }
public string[] ExceptionInfos { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

using SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Json;
using SkbKontur.Cassandra.TimeBasedUuid;
Expand All @@ -9,9 +11,9 @@ public class TimestampRange
{
[JsonPropertyName("lowerBound")]
[JsonConverter(typeof(TimestampJsonConverter))]
public Timestamp LowerBound { get; set; }
public Timestamp LowerBound { get; set; } = null!;

[JsonPropertyName("upperBound")]
[JsonConverter(typeof(TimestampJsonConverter))]
public Timestamp UpperBound { get; set; }
public Timestamp UpperBound { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Linq;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -26,7 +28,7 @@ public static void DieIfBulkRequestFailed(this StringResponse response)
{
response.EnsureSuccess();
var bulkResponse = JsonSerializer.Deserialize<BulkResponse>(response.Body);
if (bulkResponse.HasErrors)
if (bulkResponse!.HasErrors)
{
var innerExceptions = bulkResponse
.Items
Expand All @@ -39,7 +41,7 @@ public static void DieIfBulkRequestFailed(this StringResponse response)
}
}

private static string ExtendErrorMessageWithElasticInfo<T>(this T response, string errorMessage)
private static string ExtendErrorMessageWithElasticInfo<T>(this T? response, string errorMessage)
where T : IElasticsearchResponse
{
var fullErrorMessage = new StringBuilder($"ElasticSearch error: '{errorMessage}'").AppendLine();
Expand All @@ -58,7 +60,7 @@ private static string ExtendErrorMessageWithElasticInfo<T>(this T response, stri
return fullErrorMessage.ToString();
}

private static Exception CreateExceptionIfError(ResponseBase responseItem, int requestNumber)
private static Exception? CreateExceptionIfError(ResponseBase responseItem, int requestNumber)
{
return responseItem.Status >= 400 && responseItem.Status < 600
? new InvalidOperationException($"Request number #{requestNumber} failed: '{responseItem.ToPrettyJson()}'")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

namespace SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.ElasticsearchClientExtensions.Responses.Search;

internal class HitInfo
{
[JsonPropertyName("_id")]
public string Id { get; set; }
public string Id { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

using SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Json;

Expand All @@ -11,5 +13,5 @@ internal class HitsCollection
public long TotalCount { get; set; }

[JsonPropertyName("hits")]
public HitInfo[] Hits { get; set; }
public HitInfo[] Hits { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Text.Json.Serialization;
#nullable enable

using System.Text.Json.Serialization;

namespace SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.ElasticsearchClientExtensions.Responses.Search;

internal class SearchResponse
{
[JsonPropertyName("hits")]
public HitsCollection Hits { get; set; }
public HitsCollection Hits { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -67,22 +69,22 @@ private void IndexMetas(TaskMetaInformation[] batch)
{
var taskDatas = perfGraphiteReporter.ReportTiming("ReadTaskDatas", () => taskDataStorage.Read(batch));
var taskExceptionInfos = perfGraphiteReporter.ReportTiming("ReadTaskExceptionInfos", () => taskExceptionInfoStorage.Read(batch));
var enrichedBatch = new ( /*[NotNull]*/ TaskMetaInformation TaskMeta, /*[NotNull, ItemNotNull]*/ TaskExceptionInfo[] TaskExceptionInfos, /*[CanBeNull]*/ object TaskData)[batch.Length];
var enrichedBatch = new ( TaskMetaInformation TaskMeta, TaskExceptionInfo[] TaskExceptionInfos, object? TaskData)[batch.Length];
for (var i = 0; i < batch.Length; i++)
{
var taskMeta = batch[i];
object taskDataObj = null;
object? taskDataObj = null;
if (taskDatas.TryGetValue(taskMeta.Id, out var taskData))
{
if (taskDataRegistry.TryGetTaskType(taskMeta.Name, out var taskType))
taskDataObj = TryDeserializeTaskData(taskType, taskData, taskMeta);
taskDataObj = TryDeserializeTaskData(taskType!, taskData, taskMeta);
}
enrichedBatch[i] = (taskMeta, taskExceptionInfos[taskMeta.Id], taskDataObj);
}
perfGraphiteReporter.ReportTiming("IndexBatch", () => IndexBatch(enrichedBatch));
}

private void IndexBatch((TaskMetaInformation TaskMeta, TaskExceptionInfo[] TaskExceptionInfos, object TaskData)[] batch)
private void IndexBatch((TaskMetaInformation TaskMeta, TaskExceptionInfo[] TaskExceptionInfos, object? TaskData)[] batch)
{
logger.Info("IndexBatch: {BatchLength} tasks", new {BatchLength = batch.Length});
var payload = new string[batch.Length * 2];
Expand Down Expand Up @@ -127,7 +129,7 @@ private object TryDeserializeTaskData(Type taskType, byte[] taskData, TaskMetaIn
}
}

private static object BuildTaskIndexedInfo(TaskMetaInformation taskMeta, TaskExceptionInfo[] taskExceptionInfos, object taskData)
private static object BuildTaskIndexedInfo(TaskMetaInformation taskMeta, TaskExceptionInfo[] taskExceptionInfos, object? taskData)
{
var executionDurationTicks = taskMeta.ExecutionDurationTicks;
var meta = new MetaIndexedInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json;
#nullable enable

using System.Text.Json;
using System.Text.Json.Serialization;

namespace SkbKontur.Cassandra.DistributedTaskQueue.Monitoring.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Text.Json;

using Elasticsearch.Net;
Expand Down Expand Up @@ -28,7 +30,7 @@ public string GetDescription()
return $"RtqElasticsearchOffsetStorage with IndexName: {elasticIndexName}, BladeKey: {bladeKey}";
}

public void Write(string newOffset)
public void Write(string? newOffset)
{
var payload = new OffsetStorageElement {Offset = newOffset};
var postData = PostData.String(JsonSerializer.Serialize(payload));
Expand All @@ -41,7 +43,7 @@ public void Write(string newOffset)
#pragma warning restore CS0618
}

public string Read()
public string? Read()

Check warning on line 46 in Cassandra.DistributedTaskQueue.Monitoring/Storage/Writing/RtqElasticsearchOffsetStorage.cs

View workflow job for this annotation

GitHub Actions / test (6.8.20)

Nullability of reference types in return type of 'string? RtqElasticsearchOffsetStorage.Read()' doesn't match implicitly implemented member 'string IOffsetStorage<string>.Read()' (possibly because of nullability attributes).

Check warning on line 46 in Cassandra.DistributedTaskQueue.Monitoring/Storage/Writing/RtqElasticsearchOffsetStorage.cs

View workflow job for this annotation

GitHub Actions / test (7.17.4)

Nullability of reference types in return type of 'string? RtqElasticsearchOffsetStorage.Read()' doesn't match implicitly implemented member 'string IOffsetStorage<string>.Read()' (possibly because of nullability attributes).
{
var stringResponse = elasticsearchClient.UseElastic7
? elasticsearchClient.Get<StringResponse>(elasticIndexName, bladeKey, allowNotFoundStatusCode).EnsureSuccess()
Expand All @@ -55,7 +57,7 @@ public string Read()
if (elasticResponse?.Source == null || !elasticResponse.Found)
return GetDefaultOffset();

return elasticResponse.Source.Offset;
return elasticResponse.Source?.Offset;
}

private string GetDefaultOffset()
Expand All @@ -78,6 +80,6 @@ private string GetDefaultOffset()

private class OffsetStorageElement
{
public string Offset { get; set; }
public string? Offset { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
#nullable enable

using JetBrains.Annotations;
using System.Collections.Generic;

using System.Text.Json.Serialization;

Expand All @@ -12,20 +12,20 @@ public TaskIndexedInfo()
{
}

public TaskIndexedInfo([NotNull] MetaIndexedInfo meta, [NotNull] string exceptionInfo, [CanBeNull] object data)
public TaskIndexedInfo(MetaIndexedInfo meta, string exceptionInfo, object? data)
{
Meta = meta;
ExceptionInfo = exceptionInfo;
if (data != null)
Data = new Dictionary<string, object> {{meta.Name, data}};
}

public MetaIndexedInfo Meta { get; set; }
public MetaIndexedInfo Meta { get; set; } = null!;

[JsonConverter(typeof(TruncateLongStringsConverter2K))]
public string ExceptionInfo { get; set; }
public string ExceptionInfo { get; set; } = null!;

// NOTE! Using TaskTypeName->TaskData dictionary here to avoid type conflicts between fields with the same name in different TaskData contracts
// since we must index all TaskData types into single elasticsearch mapping type (see https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html)
public Dictionary<string, object> Data { get; set; }
public Dictionary<string, object> Data { get; set; } = null!;
}

0 comments on commit 87710a3

Please sign in to comment.