From 99597f5fec4ff8ff085f94be60d394598e920c25 Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Thu, 22 Aug 2024 21:48:32 +0200 Subject: [PATCH] Check for null in method instead of scattered all around the code --- .../config/server/ApplicationRepository.java | 26 +++++-------------- .../server/http/v2/ApplicationHandler.java | 4 --- .../http/v2/ApplicationHandlerTest.java | 2 +- 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index 4f3898af5527..0f1f569b6c0b 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -586,7 +586,6 @@ static void checkIfActiveIsNewerThanSessionToBeActivated(long sessionId, long cu */ public boolean delete(ApplicationId applicationId) { Tenant tenant = getTenant(applicationId); - if (tenant == null) return false; TenantApplications tenantApplications = tenant.getApplicationRepo(); NestedTransaction transaction = new NestedTransaction(); @@ -716,7 +715,10 @@ public ApplicationFile getApplicationFileFromSession(TenantName tenantName, long } public Tenant getTenant(ApplicationId applicationId) { - return tenantRepository.getTenant(applicationId.tenant()); + var tenant = tenantRepository.getTenant(applicationId.tenant()); + if (tenant == null) throw new NotFoundException("Tenant '" + applicationId.tenant() + "' not found"); + + return tenant; } Application getApplication(ApplicationId applicationId) { @@ -725,8 +727,6 @@ Application getApplication(ApplicationId applicationId) { private Application getApplication(ApplicationId applicationId, Optional version) { Tenant tenant = getTenant(applicationId); - if (tenant == null) throw new NotFoundException("Tenant '" + applicationId.tenant() + "' not found"); - Optional activeApplicationVersions = tenant.getSessionRepository().activeApplicationVersions(applicationId); if (activeApplicationVersions.isEmpty()) throw new NotFoundException("Unknown application id '" + applicationId + "'"); @@ -905,15 +905,11 @@ public Activation activate(Session session, ApplicationId applicationId, Tenant * @return the active session, or null if there is no active session for the given application id. */ public Optional getActiveSession(ApplicationId applicationId) { - Tenant tenant = getTenant(applicationId); - if (tenant == null) throw new IllegalArgumentException("Could not find any tenant for '" + applicationId + "'"); - return getActiveSession(tenant, applicationId); + return getActiveSession(getTenant(applicationId), applicationId); } public long getSessionIdForApplication(ApplicationId applicationId) { Tenant tenant = getTenant(applicationId); - if (tenant == null) - throw new NotFoundException("Tenant '" + applicationId.tenant() + "' not found"); if (! tenant.getApplicationRepo().exists(applicationId)) throw new NotFoundException("Unknown application id '" + applicationId + "'"); @@ -1037,11 +1033,7 @@ public ApplicationMetaData getMetadataFromLocalSession(Tenant tenant, long sessi } private ApplicationCuratorDatabase requireDatabase(ApplicationId id) { - Tenant tenant = getTenant(id); - if (tenant == null) - throw new NotFoundException("Tenant '" + id.tenant().value() + "' not found"); - - return tenant.getApplicationRepo().database(); + return getTenant(id).getApplicationRepo().database(); } public ApplicationReindexing getReindexing(ApplicationId id) { @@ -1050,11 +1042,7 @@ public ApplicationReindexing getReindexing(ApplicationId id) { } public void modifyReindexing(ApplicationId id, UnaryOperator modifications) { - Tenant tenant = getTenant(id); - if (tenant == null) - throw new NotFoundException("Tenant '" + id.tenant().value() + "' not found"); - - tenant.getApplicationRepo().database().modifyReindexing(id, ApplicationReindexing.empty(), modifications); + getTenant(id).getApplicationRepo().database().modifyReindexing(id, ApplicationReindexing.empty(), modifications); } public PendingRestarts getPendingRestarts(ApplicationId id) { diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java index 75d94434303a..314d16dd29d1 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java @@ -348,10 +348,6 @@ private HttpResponse enableReindexing(ApplicationId applicationId) { } private HttpResponse getReindexingStatus(ApplicationId applicationId) { - Tenant tenant = applicationRepository.getTenant(applicationId); - if (tenant == null) - throw new NotFoundException("Tenant '" + applicationId.tenant().value() + "' not found"); - try { Map> documentTypes = getActiveModelOrThrow(applicationId).documentTypesByCluster(); ApplicationReindexing reindexing = applicationRepository.getReindexing(applicationId); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java index 01ddceb33a26..fe573ab5c2f2 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java @@ -787,7 +787,7 @@ private void assertNotAllowed(Method method) throws IOException { private void deleteAndAssertOKResponseMocked(ApplicationId applicationId, boolean fullAppIdInUrl) throws IOException { Tenant tenant = applicationRepository.getTenant(applicationId); - long sessionId = tenant.getApplicationRepo().requireActiveSessionOf(applicationId); + tenant.getApplicationRepo().requireActiveSessionOf(applicationId); deleteAndAssertResponse(applicationId, Zone.defaultZone(), Response.Status.OK, null, fullAppIdInUrl); }