-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Token Range to Endpoints endpoint (#377)
- Loading branch information
1 parent
b7b6cf4
commit 3727fab
Showing
12 changed files
with
444 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
management-api-server/src/main/java/com/datastax/mgmtapi/resources/v2/TokenResourcesV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Please see the included license file for details. | ||
*/ | ||
package com.datastax.mgmtapi.resources.v2; | ||
|
||
import com.datastax.mgmtapi.ManagementApplication; | ||
import com.datastax.mgmtapi.resources.common.BaseResources; | ||
import com.datastax.mgmtapi.resources.helpers.ResponseTools; | ||
import com.datastax.mgmtapi.resources.v2.models.TokenRangeToEndpointResponse; | ||
import com.datastax.mgmtapi.resources.v2.models.TokenRangeToEndpoints; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/api/v2/tokens") | ||
public class TokenResourcesV2 extends BaseResources { | ||
|
||
public TokenResourcesV2(ManagementApplication application) { | ||
super(application); | ||
} | ||
|
||
@GET | ||
@Path("/rangetoendpoint") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Operation( | ||
summary = "Retrieve a mapping of Token ranges to endpoints", | ||
operationId = "getRangeToEndpointMapV2") | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "Token range retrieval was successful", | ||
content = | ||
@Content( | ||
mediaType = MediaType.APPLICATION_JSON, | ||
schema = @Schema(implementation = TokenRangeToEndpointResponse.class))) | ||
@ApiResponse( | ||
responseCode = "404", | ||
description = "Keyspace not found", | ||
content = | ||
@Content( | ||
mediaType = MediaType.TEXT_PLAIN, | ||
schema = @Schema(implementation = String.class), | ||
examples = @ExampleObject(value = "keyspace not found"))) | ||
public Response getRangeToEndpointMap(@QueryParam(value = "keyspaceName") String keyspaceName) { | ||
return handle( | ||
() -> { | ||
if (keyspaceName != null && !keyspaceExists(keyspaceName)) { | ||
return Response.status(Response.Status.NOT_FOUND).entity("keyspace not found").build(); | ||
} | ||
|
||
Map<List<String>, List<String>> map = | ||
(Map<List<String>, List<String>>) | ||
ResponseTools.getSingleRowResponse( | ||
app.dbUnixSocketFile, | ||
app.cqlService, | ||
"CALL NodeOps.getRangeToEndpointMap(?)", | ||
keyspaceName); | ||
return Response.ok(convert(map)).build(); | ||
}); | ||
} | ||
|
||
private TokenRangeToEndpointResponse convert(Map<List<String>, List<String>> map) { | ||
List<TokenRangeToEndpoints> rangesToEndpoints = new ArrayList<>(map.size()); | ||
map.entrySet() | ||
.forEach( | ||
(Map.Entry<List<String>, List<String>> e) -> { | ||
rangesToEndpoints.add( | ||
new TokenRangeToEndpoints(convertRanges(e.getKey()), e.getValue())); | ||
}); | ||
return new TokenRangeToEndpointResponse(rangesToEndpoints); | ||
} | ||
|
||
private List<Long> convertRanges(List<String> range) { | ||
// each Range should be exactly 2 strings: start, end | ||
assert range.size() == 2; | ||
List<Long> tokenRange = Arrays.asList(Long.valueOf(range.get(0)), Long.parseLong(range.get(1))); | ||
return tokenRange; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
.../src/main/java/com/datastax/mgmtapi/resources/v2/models/TokenRangeToEndpointResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Please see the included license file for details. | ||
*/ | ||
package com.datastax.mgmtapi.resources.v2.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class TokenRangeToEndpointResponse { | ||
|
||
@JsonProperty(value = "token_range_to_endpoints", required = true) | ||
public final List<TokenRangeToEndpoints> tokenRangeToEndpoints; | ||
|
||
@JsonCreator | ||
public TokenRangeToEndpointResponse( | ||
@JsonProperty(value = "token_range_to_endpoints", required = true) | ||
List<TokenRangeToEndpoints> list) { | ||
this.tokenRangeToEndpoints = list; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 83 * Objects.hashCode(tokenRangeToEndpoints); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
TokenRangeToEndpointResponse other = (TokenRangeToEndpointResponse) obj; | ||
return Objects.equals(this.tokenRangeToEndpoints, other.tokenRangeToEndpoints); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
try { | ||
return new ObjectMapper().writeValueAsString(this); | ||
} catch (JsonProcessingException e) { | ||
return String.format("Unable to format TokenRangeToEndpointResponse (%s)", e.getMessage()); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...-server/src/main/java/com/datastax/mgmtapi/resources/v2/models/TokenRangeToEndpoints.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Please see the included license file for details. | ||
*/ | ||
package com.datastax.mgmtapi.resources.v2.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class TokenRangeToEndpoints { | ||
|
||
@JsonProperty(value = "tokens", required = true) | ||
public final List<Long> tokens; | ||
|
||
@JsonProperty(value = "endpoints", required = true) | ||
public final List<String> endpoints; | ||
|
||
@JsonCreator | ||
public TokenRangeToEndpoints( | ||
@JsonProperty(value = "tokens", required = true) List<Long> tokens, | ||
@JsonProperty(value = "endpoints", required = true) List<String> endpoints) { | ||
this.tokens = tokens; | ||
this.endpoints = endpoints; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
TokenRangeToEndpoints other = (TokenRangeToEndpoints) obj; | ||
if (!Objects.equals(this.tokens, other.tokens)) { | ||
return false; | ||
} | ||
return Objects.equals(this.endpoints, other.endpoints); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 83 * Objects.hashCode(this.tokens) * Objects.hashCode(this.endpoints); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
try { | ||
return new ObjectMapper().writeValueAsString(this); | ||
} catch (JsonProcessingException e) { | ||
return String.format("Unable to format TokenRangeToEndpoints (%s)", e.getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.