From e459692975f76b662859f6a72a0756e468df6476 Mon Sep 17 00:00:00 2001 From: Zachary Pinto Date: Fri, 10 Nov 2023 08:56:13 -0800 Subject: [PATCH] Change canCompleteSwap and completeSwapIfPossible to return json with kv pair for result of check or attempt to complete swap. (#2697) --- .../resources/helix/PerInstanceAccessor.java | 12 ++++------- .../rest/server/TestPerInstanceAccessor.java | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/PerInstanceAccessor.java b/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/PerInstanceAccessor.java index b920f66ce8..b1b4f82642 100644 --- a/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/PerInstanceAccessor.java +++ b/helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/PerInstanceAccessor.java @@ -436,15 +436,11 @@ public Response updateInstance(@PathParam("clusterId") String clusterId, admin.setInstanceOperation(clusterId, instanceName, state); break; case canCompleteSwap: - if (!admin.canCompleteSwap(clusterId, instanceName)) { - return badRequest("Swap is not ready to be completed!"); - } - break; + return OK(OBJECT_MAPPER.writeValueAsString( + Map.of("successful", admin.canCompleteSwap(clusterId, instanceName)))); case completeSwapIfPossible: - if (!admin.completeSwapIfPossible(clusterId, instanceName)) { - return badRequest("Swap is not ready to be completed!"); - } - break; + return OK(OBJECT_MAPPER.writeValueAsString( + Map.of("successful", admin.completeSwapIfPossible(clusterId, instanceName)))); case addInstanceTag: if (!validInstance(node, instanceName)) { return badRequest("Instance names are not match!"); diff --git a/helix-rest/src/test/java/org/apache/helix/rest/server/TestPerInstanceAccessor.java b/helix-rest/src/test/java/org/apache/helix/rest/server/TestPerInstanceAccessor.java index 273019bd3f..a8675202a3 100644 --- a/helix-rest/src/test/java/org/apache/helix/rest/server/TestPerInstanceAccessor.java +++ b/helix-rest/src/test/java/org/apache/helix/rest/server/TestPerInstanceAccessor.java @@ -501,6 +501,26 @@ public void updateInstance() throws IOException { instanceConfig = _configAccessor.getInstanceConfig(CLUSTER_NAME, INSTANCE_NAME); Assert.assertEquals( instanceConfig.getInstanceOperation(), ""); + + // test canCompleteSwap + Response canCompleteSwapResponse = + new JerseyUriRequestBuilder("clusters/{}/instances/{}?command=canCompleteSwap").format( + CLUSTER_NAME, INSTANCE_NAME).post(this, entity); + Assert.assertEquals(canCompleteSwapResponse.getStatus(), Response.Status.OK.getStatusCode()); + Map responseMap = + OBJECT_MAPPER.readValue(canCompleteSwapResponse.readEntity(String.class), Map.class); + Assert.assertFalse((boolean) responseMap.get("successful")); + + // test completeSwapIfPossible + Response completeSwapIfPossibleResponse = new JerseyUriRequestBuilder( + "clusters/{}/instances/{}?command=completeSwapIfPossible").format(CLUSTER_NAME, + INSTANCE_NAME).post(this, entity); + Assert.assertEquals(completeSwapIfPossibleResponse.getStatus(), + Response.Status.OK.getStatusCode()); + responseMap = + OBJECT_MAPPER.readValue(completeSwapIfPossibleResponse.readEntity(String.class), Map.class); + Assert.assertFalse((boolean) responseMap.get("successful")); + System.out.println("End test :" + TestHelper.getTestMethodName()); }