From 622cbe194b6d4e7b1ba55ddc41f1e54b901f8a60 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 21 May 2025 15:15:06 +0200 Subject: [PATCH 1/3] Fix NRE in LinkIndexProvider while adding a new branch to existing repository --- .../LinkIndexProvider.cs | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs b/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs index d6965ebc7..cc7c3949f 100644 --- a/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs +++ b/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs @@ -2,6 +2,7 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information +using System.Net; using Amazon.Lambda.Core; using Amazon.S3; using Amazon.S3.Model; @@ -36,27 +37,40 @@ private async Task GetLinkIndex() return _linkIndex; } - public async Task UpdateLinkIndexEntry(LinkRegistryEntry linkRegistryEntry) + public async Task UpdateLinkIndexEntry(LinkRegistryEntry newEntry) { _linkIndex ??= await GetLinkIndex(); - if (_linkIndex.Repositories.TryGetValue(linkRegistryEntry.Repository, out var existingEntry)) + var repository = newEntry.Repository; + var branch = newEntry.Branch; + // repository already exists in links.json + if (_linkIndex.Repositories.TryGetValue(repository, out var existingRepositoryEntry)) { - var newEntryIsNewer = DateTime.Compare(linkRegistryEntry.UpdatedAt, existingEntry[linkRegistryEntry.Branch].UpdatedAt) > 0; - if (newEntryIsNewer) + // The branch already exists in the repository entry + if (existingRepositoryEntry.TryGetValue(branch, out var existingBranchEntry)) { - existingEntry[linkRegistryEntry.Branch] = linkRegistryEntry; - logger.LogInformation("Updated existing entry for {repository}@{branch}", linkRegistryEntry.Repository, linkRegistryEntry.Branch); + if (newEntry.UpdatedAt > existingBranchEntry.UpdatedAt) + { + existingRepositoryEntry[branch] = newEntry; + logger.LogInformation("Updated existing entry for {repository}@{branch}", repository, branch); + } + else + logger.LogInformation("Skipping update for {repository}@{branch} because the existing entry is newer or equal", repository, branch); } + // branch does not exist in the repository entry else - logger.LogInformation("Skipping update for {repository}@{branch} because the existing entry is newer", linkRegistryEntry.Repository, linkRegistryEntry.Branch); + { + existingRepositoryEntry[branch] = newEntry; + logger.LogInformation("Added new entry '{repository}@{branch}' to existing entry for '{repository}'", repository, branch, repository); + } } + // onboarding new repository else { - _linkIndex.Repositories.Add(linkRegistryEntry.Repository, new Dictionary + _linkIndex.Repositories.Add(repository, new Dictionary { - { linkRegistryEntry.Branch, linkRegistryEntry } + { branch, newEntry } }); - logger.LogInformation("Added new entry for {repository}@{branch}", linkRegistryEntry.Repository, linkRegistryEntry.Branch); + logger.LogInformation("Added new entry for {repository}@{branch}", repository, branch); } } @@ -74,7 +88,11 @@ public async Task Save() ContentType = "application/json", IfMatch = _etag // Only update if the ETag matches. Meaning the object has not been changed in the meantime. }; - _ = await s3Client.PutObjectAsync(putObjectRequest); - logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key); + var putReponse = await s3Client.PutObjectAsync(putObjectRequest); + if (putReponse.HttpStatusCode == HttpStatusCode.OK) + logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key); + else + // TODO retry? + logger.LogError("Unable to save index to s3://{bucketName}/{key}", bucketName, key); } } From 53b1d57893d1c1c14f8203f96ae7fce3dc6a2809 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 21 May 2025 15:30:17 +0200 Subject: [PATCH 2/3] Throw exception when save goes wrong so its put back on the queue --- src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs b/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs index cc7c3949f..e92576a81 100644 --- a/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs +++ b/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs @@ -92,7 +92,9 @@ public async Task Save() if (putReponse.HttpStatusCode == HttpStatusCode.OK) logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key); else - // TODO retry? + { logger.LogError("Unable to save index to s3://{bucketName}/{key}", bucketName, key); + throw new Exception($"Unable to save index to s3://{bucketName}/{key}"); + } } } From 495c8a5e705ee9995f612cefcb13e0911063763c Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 21 May 2025 15:30:38 +0200 Subject: [PATCH 3/3] fix typo --- src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs b/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs index e92576a81..4cd5780cd 100644 --- a/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs +++ b/src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs @@ -88,8 +88,8 @@ public async Task Save() ContentType = "application/json", IfMatch = _etag // Only update if the ETag matches. Meaning the object has not been changed in the meantime. }; - var putReponse = await s3Client.PutObjectAsync(putObjectRequest); - if (putReponse.HttpStatusCode == HttpStatusCode.OK) + var putResponse = await s3Client.PutObjectAsync(putObjectRequest); + if (putResponse.HttpStatusCode == HttpStatusCode.OK) logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key); else {