Skip to content

Commit

Permalink
Reflect review
Browse files Browse the repository at this point in the history
  • Loading branch information
imasahiro committed Mar 7, 2018
1 parent 8977fed commit 983b437
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 54 deletions.
Expand Up @@ -24,6 +24,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.endpoint.DynamicEndpointGroup;
import com.linecorp.armeria.client.endpoint.EndpointGroup;
Expand All @@ -34,79 +36,65 @@
/**
* A CentralDogma based {@link EndpointGroup} implementation. This {@link EndpointGroup} retrieves the list of
* {@link Endpoint}s from a route file served by CentralDogma, and update the list when upstream data changes.
* Route file could be json file or normal text file.
* <p>
* <p>In below example, json file with below content will be served as route file:
* <p>
* Route file could be JSON file or normal text file.
*
* <p>For example, the following JSON file will be served as a route file:
* <pre>{@code
* [
* "host1:port1",
* "host2:port2",
* "host3:port3"
* ]
* }
* </pre>
* <p>
* <p>The route file could be retrieve as {@link EndpointGroup} using below code:
* <p>
* }</pre>
*
* <p>The route file could be retrieved as an {@link EndpointGroup} using the following code:
* <pre>{@code
* CentralDogmaEndpointGroup<JsonNode> endpointGroup = CentralDogmaEndpointGroup.ofJsonFile(
* centralDogma, "myProject", "myRepo",
* CentralDogmaCodec.DEFAULT_JSON_CODEC,
* Query.ofJsonPath("/route.json")
* )
* endpointGroup.endpoints();
* }
* </pre>
* }</pre>
*
* @param <T> Type of CentralDomgma file (could be JsonNode or String)
* @param <T> Type of CentralDomgma file (could be {@link JsonNode} or {@link String})
*/
public final class CentralDogmaEndpointGroup<T> extends DynamicEndpointGroup {
private static final Logger logger = LoggerFactory.getLogger(CentralDogmaEndpointGroup.class);
private static final long WATCH_INITIALIZATION_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(10);

private final Watcher<T> instanceListWatcher;
private final EndpointListCodec<T> endpointCodec;
private final EndpointListDecoder<T> endpointCodec;

/**
* Creates a new {@link CentralDogmaEndpointGroup}.
*
* @param endpointCodec A {@link EndpointListCodec}
* @param watcher A {@link Watcher}
* @param endpointCodec A {@link EndpointListDecoder}
*/
public static <T> CentralDogmaEndpointGroup<T> ofWatcher(EndpointListCodec<T> endpointCodec,
Watcher<T> watcher) {
public static <T> CentralDogmaEndpointGroup<T> ofWatcher(Watcher<T> watcher,
EndpointListDecoder<T> endpointCodec) {
return new CentralDogmaEndpointGroup<>(watcher, endpointCodec);
}

/**
* Creates a new {@link CentralDogmaEndpointGroup}.
* @param uri an uri of CentralDogma server
* @param dogmaProject CentralDogma project name
* @param dogmaRepo CentralDogma repository name
* @param dogmaCodec A {@link CentralDogmaCodec}
* @param waitTime a {@code long} define how long we should wait for initial result
* @param waitUnit a {@link TimeUnit} define unit of wait time
* @throws CentralDogmaEndpointException if couldn't get initial result from CentralDogma server
*/

/**
* Creates a new {@link CentralDogmaEndpointGroup}.
*
* @param centralDogma A {@link CentralDogma}
* @param endpointCodec A {@link EndpointListCodec}
* @param projectName CentralDogma project name
* @param repositoryName CentralDogma repository name
* @param query A {@link Query} to route file
* @param endpointCodec An {@link EndpointListDecoder}
*/
public static <T> CentralDogmaEndpointGroup<T> of(CentralDogma centralDogma,
EndpointListCodec<T> endpointCodec,
String projectName, String repositoryName,
Query<T> query) {
return ofWatcher(endpointCodec, centralDogma.fileWatcher(projectName, repositoryName, query));
Query<T> query,
EndpointListDecoder<T> endpointCodec
) {
return ofWatcher(centralDogma.fileWatcher(projectName, repositoryName, query), endpointCodec);
}

private CentralDogmaEndpointGroup(Watcher<T> instanceListWatcher, EndpointListCodec<T> endpointCodec) {
private CentralDogmaEndpointGroup(Watcher<T> instanceListWatcher, EndpointListDecoder<T> endpointCodec) {
this.instanceListWatcher = requireNonNull(instanceListWatcher, "instanceListWatcher");
this.endpointCodec = requireNonNull(endpointCodec, "endpointCodec");
registerWatcher();
Expand Down
Expand Up @@ -25,7 +25,7 @@
import com.linecorp.armeria.client.Endpoint;

final class EndpointListCodecUtils {
static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().disable(FAIL_ON_UNKNOWN_PROPERTIES);
static final ObjectMapper objectMapper = new ObjectMapper().disable(FAIL_ON_UNKNOWN_PROPERTIES);

private EndpointListCodecUtils() {}

Expand Down
Expand Up @@ -24,12 +24,12 @@
/**
* Decode and encode between CentralDogma upstream file content and list of {@link Endpoint}s.
*
* @param <T> Type of CentralDomgma file (could be JsonNode or String)
* @param <T> Type of CentralDomgma file (could be {@link JsonNode} or {@link String})
*/
public interface EndpointListCodec<T> {
public interface EndpointListDecoder<T> {

/**
* Default {@link EndpointListCodec} implementation for JsonNode object.
* Default {@link EndpointListDecoder} implementation for JsonNode object.
* Retrive object must be a json array (which has format as "[ \"segment1\", \"segment2\" ]"
* Each segment represents an endpoint whose format is
* {@code <host>[:<port_number>[:weight]]}, such as:
Expand All @@ -41,10 +41,10 @@ public interface EndpointListCodec<T> {
* </ul>
* Note that the port number must be specified when you want to specify the weight.
*/
EndpointListCodec<JsonNode> DEFAULT_JSON_CODEC = new DefaultCentralDogmaJsonCodec();
EndpointListDecoder<JsonNode> JSON = new JsonEndpointListDecoder();

/**
* Default {@link EndpointListCodec} implementation for String object.
* Default {@link EndpointListDecoder} implementation for String object.
* Retrive object must be a string which is a list of segments separated by newline.
* Each segment represents an endpoint whose format is
* {@code <host>[:<port_number>[:weight]]}, such as:
Expand All @@ -56,7 +56,7 @@ public interface EndpointListCodec<T> {
* </ul>
* Note that the port number must be specified when you want to specify the weight.
*/
EndpointListCodec<String> DEFAULT_STRING_CODEC = new DefaultCentralDogmaTextCodec();
EndpointListDecoder<String> TEXT = new TextEndpointListDecoder();

/**
* Decodes an object into a set of {@link Endpoint}s.
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
package com.linecorp.centraldogma.client.armeria;

import static com.linecorp.centraldogma.client.armeria.EndpointListCodecUtils.OBJECT_MAPPER;
import static com.linecorp.centraldogma.client.armeria.EndpointListCodecUtils.objectMapper;
import static com.linecorp.centraldogma.client.armeria.EndpointListCodecUtils.convertToEndpointList;

import java.io.IOException;
Expand All @@ -26,13 +26,13 @@

import com.linecorp.armeria.client.Endpoint;

public final class DefaultCentralDogmaJsonCodec implements EndpointListCodec<JsonNode> {
final class JsonEndpointListDecoder implements EndpointListDecoder<JsonNode> {
@Override
public List<Endpoint> decode(JsonNode node) {
final List<String> endpoints;
try {
endpoints = OBJECT_MAPPER.readValue(node.traverse(),
new TypeReference<List<String>>() {});
endpoints = objectMapper.readValue(node.traverse(),
new TypeReference<List<String>>() {});
} catch (IOException e) {
throw new IllegalArgumentException("invalid format: " + node);
}
Expand Down
Expand Up @@ -15,25 +15,21 @@
*/
package com.linecorp.centraldogma.client.armeria;

import static com.linecorp.centraldogma.client.armeria.EndpointListCodecUtils.OBJECT_MAPPER;
import static com.linecorp.centraldogma.client.armeria.EndpointListCodecUtils.convertToEndpointList;

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Splitter;

import com.linecorp.armeria.client.Endpoint;

public final class DefaultCentralDogmaTextCodec implements EndpointListCodec<String> {
final class TextEndpointListDecoder implements EndpointListDecoder<String> {
private static final Splitter NEWLINE_SPLITTER = Splitter.on(System.getProperty("line.separator", "\n"))
.omitEmptyStrings()
.trimResults();

@Override
public List<Endpoint> decode(String object) {
final List<String> endpoints;
try {
endpoints = OBJECT_MAPPER.readValue(object, new TypeReference<List<String>>() {});
} catch (IOException e) {
throw new IllegalArgumentException("invalid format: " + object);
}
return convertToEndpointList(endpoints);
return convertToEndpointList(NEWLINE_SPLITTER.splitToList(object));
}
}

0 comments on commit 983b437

Please sign in to comment.