diff options
author | Harald Musum <musum@yahoo-inc.com> | 2017-01-30 22:37:43 +0100 |
---|---|---|
committer | Harald Musum <musum@yahoo-inc.com> | 2017-01-30 22:37:43 +0100 |
commit | bd72d93970d0011dca67635169a94ff7818b51f9 (patch) | |
tree | 2068eb1602f07d8995d2fa61ba73c40bc66dbba7 /configserver | |
parent | 2e3f8c69badca74caf3b4efc05ccc577e62363e1 (diff) |
Reformat and move around code, no functional changes
Diffstat (limited to 'configserver')
2 files changed, 105 insertions, 104 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java index 675af7ff706..5027c00007f 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java @@ -54,6 +54,97 @@ public class ApplicationConvergenceChecker extends AbstractComponent { this.stateApiFactory = stateApiFactory; } + public HttpResponse listConfigConvergence(Application application, URI uri) { + final JSONObject answer = new JSONObject(); + final JSONArray nodes = new JSONArray(); + final ModelConfig config; + try { + config = application.getConfig(ModelConfig.class, ""); + } catch (IOException e) { + throw new RuntimeException("failed on get config model", e); + } + config.hosts() + .forEach(host -> { + host.services().stream() + .filter(service -> serviceTypes.contains(service.type())) + .forEach(service -> { + Optional<Integer> statePort = getStatePort(service); + if (statePort.isPresent()) { + JSONObject hostNode = new JSONObject(); + try { + hostNode.put("host", host.name()); + hostNode.put("port", statePort.get()); + hostNode.put("url", uri.toString() + "/" + host.name() + ":" + statePort.get()); + hostNode.put("type", service.type()); + + } catch (JSONException e) { + throw new RuntimeException(e); + } + nodes.put(hostNode); + } + }); + }); + try { + answer.put("services", nodes); + JSONObject debug = new JSONObject(); + debug.put("wantedVersion", application.getApplicationGeneration()); + answer.put("debug", debug); + answer.put("url", uri.toString()); + return new JsonHttpResponse(200, answer); + } catch (JSONException e) { + try { + answer.put("error", e.getMessage()); + } catch (JSONException e1) { + throw new RuntimeException("Failed while creating error message ", e1); + } + return new JsonHttpResponse(500, answer); + } + } + + public HttpResponse nodeConvergenceCheck(Application application, String hostFromRequest, URI uri) { + JSONObject answer = new JSONObject(); + JSONObject debug = new JSONObject(); + try { + answer.put("url", uri); + debug.put("wantedGeneration", application.getApplicationGeneration()); + debug.put("host", hostFromRequest); + + if (!hostInApplication(application, hostFromRequest)) { + debug.put("problem", "Host:port (service) no longer part of application, refetch list of services."); + answer.put("debug", debug); + return new JsonHttpResponse(410, answer); + } + final long generation = getServiceGeneration(URI.create("http://" + hostFromRequest)); + debug.put("currentGeneration", generation); + answer.put("debug", debug); + answer.put("converged", generation >= application.getApplicationGeneration()); + return new JsonHttpResponse(200, answer); + } catch (JSONException | ProcessingException e) { + try { + answer.put("error", e.getMessage()); + } catch (JSONException e1) { + throw new RuntimeException("Fail while creating error message ", e1); + } + return new JsonHttpResponse(500, answer); + } + } + + @Override + public void deconstruct() { + client.close(); + } + + @Path(statePath) + public interface StateApi { + @Path(configSubPath) + @GET + JsonNode config(); + } + + public interface StateApiFactory { + StateApi createStateApi(Client client, URI serviceUri); + } + private Optional<Integer> getStatePort(ModelConfig.Hosts.Services service) { return service.ports().stream() .filter(port -> port.tags().contains("state")) .map(ModelConfig.Hosts.Services.Ports::number) @@ -74,11 +165,6 @@ public class ApplicationConvergenceChecker extends AbstractComponent { return generationFromContainerState(state.config()); } - @Override - public void deconstruct() { - client.close(); - } - private boolean hostInApplication(Application application, String hostPort) { final ModelConfig config; try { @@ -101,34 +187,6 @@ public class ApplicationConvergenceChecker extends AbstractComponent { return false; } - public HttpResponse nodeConvergenceCheck(Application application, String hostFromRequest, URI uri) { - JSONObject answer = new JSONObject(); - JSONObject debug = new JSONObject(); - try { - answer.put("url", uri); - debug.put("wantedGeneration", application.getApplicationGeneration()); - debug.put("host", hostFromRequest); - - if (!hostInApplication(application, hostFromRequest)) { - debug.put("problem", "Host:port (service) no longer part of application, refetch list of services."); - answer.put("debug", debug); - return new JsonHttpResponse(410, answer); - } - final long generation = getServiceGeneration(URI.create("http://" + hostFromRequest)); - debug.put("currentGeneration", generation); - answer.put("debug", debug); - answer.put("converged", generation >= application.getApplicationGeneration()); - return new JsonHttpResponse(200, answer); - } catch (JSONException | ProcessingException e) { - try { - answer.put("error", e.getMessage()); - } catch (JSONException e1) { - throw new RuntimeException("Fail while creating error message ", e1); - } - return new JsonHttpResponse(500, answer); - } - } - private static class JsonHttpResponse extends HttpResponse { private final JSONObject answer; @@ -149,61 +207,4 @@ public class ApplicationConvergenceChecker extends AbstractComponent { } } - public HttpResponse listConfigConvergence(Application application, URI uri) { - final JSONObject answer = new JSONObject(); - final JSONArray nodes = new JSONArray(); - final ModelConfig config; - try { - config = application.getConfig(ModelConfig.class, ""); - } catch (IOException e) { - throw new RuntimeException("failed on get config model", e); - } - config.hosts().stream() - .forEach(host -> { - host.services().stream() - .filter(service -> serviceTypes.contains(service.type())) - .forEach(service -> { - Optional<Integer> statePort = getStatePort(service); - if (statePort.isPresent()) { - JSONObject hostNode = new JSONObject(); - try { - hostNode.put("host", host.name()); - hostNode.put("port", statePort.get()); - hostNode.put("url", uri.toString() + "/" + host.name() + ":" + statePort.get()); - hostNode.put("type", service.type()); - - } catch (JSONException e) { - throw new RuntimeException(e); - } - nodes.put(hostNode); - } - }); - }); - try { - answer.put("services", nodes); - JSONObject debug = new JSONObject(); - debug.put("wantedVersion", application.getApplicationGeneration()); - answer.put("debug", debug); - answer.put("url", uri.toString()); - return new JsonHttpResponse(200, answer); - } catch (JSONException e) { - try { - answer.put("error", e.getMessage()); - } catch (JSONException e1) { - throw new RuntimeException("Failed while creating error message ", e1); - } - return new JsonHttpResponse(500, answer); - } - } - - @Path(statePath) - public interface StateApi { - @Path(configSubPath) - @GET - JsonNode config(); - } - - public interface StateApiFactory { - StateApi createStateApi(Client client, URI serviceUri); - } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java index 21d0ec970ce..c383ddb198c 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java @@ -57,19 +57,6 @@ public class ApplicationConvergenceCheckerTest { application = new Application(mockModel, new ServerCache(), 3, Version.fromIntValues(0, 0, 0), MetricUpdater.createTestUpdater(), appId); } - private void assertJsonResponseEquals(HttpResponse httpResponse, String expected) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - httpResponse.render(out); - String response = out.toString(StandardCharsets.UTF_8.name()); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonResponse = mapper.readTree(response); - JsonNode jsonExpected = mapper.readTree(expected); - if (jsonExpected.equals(jsonResponse)) { - return; - } - fail("Not equal, response is '" + response + "' expected '"+ expected + "'"); - } - @Test public void converge() throws IOException, SAXException { ApplicationConvergenceChecker checker = new ApplicationConvergenceChecker((client, serviceUri) -> () -> string2json("{\"config\":{\"generation\":3}}")); @@ -98,6 +85,19 @@ public class ApplicationConvergenceCheckerTest { "\"url\":\"http://foo:234/serviceconvergence\"}"); } + private void assertJsonResponseEquals(HttpResponse httpResponse, String expected) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + httpResponse.render(out); + String response = out.toString(StandardCharsets.UTF_8.name()); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonResponse = mapper.readTree(response); + JsonNode jsonExpected = mapper.readTree(expected); + if (jsonExpected.equals(jsonResponse)) { + return; + } + fail("Not equal, response is '" + response + "' expected '"+ expected + "'"); + } + private JsonNode string2json(String data) { try { return mapper.readTree(data); @@ -108,7 +108,7 @@ public class ApplicationConvergenceCheckerTest { private static class MockModel implements Model { private final int statePort; - public MockModel(int statePort) { + MockModel(int statePort) { this.statePort = statePort; } |