public static ProtobufSystemInfo.SystemInfo provide() { ProtobufSystemInfo.SystemInfo.Builder protobuf = ProtobufSystemInfo.SystemInfo.newBuilder(); if (instance != null) { instance.sections.forEach(section -> protobuf.addSections(section.toProtobuf())); } return protobuf.build(); } }
@Test public void serves_data_from_SystemInfoSections() throws Exception { NanoHTTPD.Response response = underTest.serve(createHttpSession(GET)); assertThat(response.getStatus()).isEqualTo(OK); ProtobufSystemInfo.SystemInfo systemInfo = ProtobufSystemInfo.SystemInfo.parseFrom(response.getData()); assertThat(systemInfo.getSectionsCount()).isEqualTo(2); assertThat(systemInfo.getSections(0).getName()).isEqualTo("state1"); assertThat(systemInfo.getSections(1).getName()).isEqualTo("state2"); }
private static void completeNodeInfo(DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer, Member member, NodeInfo nodeInfo) { Optional<ProtobufSystemInfo.SystemInfo> nodeAnswer = distributedAnswer.getAnswer(member); Optional<Exception> failure = distributedAnswer.getFailed(member); if (distributedAnswer.hasTimedOut(member)) { nodeInfo.setErrorMessage("Failed to retrieve information on time"); } else if (failure.isPresent()) { nodeInfo.setErrorMessage("Failed to retrieve information: " + failure.get().getMessage()); } else if (nodeAnswer.isPresent()) { nodeAnswer.get().getSectionsList().forEach(nodeInfo::addSection); } } }
@Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { if (session.getMethod() != NanoHTTPD.Method.GET) { return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); } ProtobufSystemInfo.SystemInfo.Builder infoBuilder = ProtobufSystemInfo.SystemInfo.newBuilder(); for (SystemInfoSection sectionProvider : sectionProviders) { ProtobufSystemInfo.Section section = sectionProvider.toProtobuf(); infoBuilder.addSections(section); } byte[] bytes = infoBuilder.build().toByteArray(); return newFixedLengthResponse(OK, PROTOBUF_MIME_TYPE, new ByteArrayInputStream(bytes), bytes.length); } }
private static void completeNodeInfo(DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer, Member member, NodeInfo nodeInfo) { Optional<ProtobufSystemInfo.SystemInfo> nodeAnswer = distributedAnswer.getAnswer(member); Optional<Exception> failure = distributedAnswer.getFailed(member); if (distributedAnswer.hasTimedOut(member)) { nodeInfo.setErrorMessage("Failed to retrieve information on time"); } else if (failure.isPresent()) { nodeInfo.setErrorMessage("Failed to retrieve information: " + failure.get().getMessage()); } else if (nodeAnswer.isPresent()) { nodeAnswer.get().getSectionsList().forEach(nodeInfo::addSection); } } }
@Override public Optional<ProtobufSystemInfo.SystemInfo> call(String url) throws Exception { byte[] protobuf = IOUtils.toByteArray(new URI(url)); return Optional.of(ProtobufSystemInfo.SystemInfo.parseFrom(protobuf)); } }
@Override public void write(JsonWriter json) { writeHealth(json); List<ProtobufSystemInfo.Section> sections = stream(systemInfoSections) .map(SystemInfoSection::toProtobuf) .collect(MoreCollectors.toArrayList()); ceHttpClient.retrieveSystemInfo() .ifPresent(ce -> sections.addAll(ce.getSectionsList())); writeSections(sections, json); writeTelemetry(json); }
public static ProtobufSystemInfo.SystemInfo provide() { ProtobufSystemInfo.SystemInfo.Builder protobuf = ProtobufSystemInfo.SystemInfo.newBuilder(); if (instance != null) { instance.sections.forEach(section -> protobuf.addSections(section.toProtobuf())); } return protobuf.build(); } }
@Test public void load_info_from_all_nodes() throws Exception { DistributedAnswer<SystemInfo> answer = new DistributedAnswer<>(); answer.setAnswer(newMember("foo"), SystemInfo.newBuilder().addSections(Section.newBuilder().build()).build()); answer.setTimedOut(newMember("bar")); answer.setFailed(newMember("baz"), new IOException("BOOM")); when(hzMember.call(any(DistributedCall.class), any(MemberSelector.class), anyLong())).thenReturn(answer); Collection<NodeInfo> nodes = underTest.load(); assertThat(nodes).hasSize(3); NodeInfo successfulNodeInfo = findNode(nodes, "foo"); assertThat(successfulNodeInfo.getName()).isEqualTo("foo"); assertThat(successfulNodeInfo.getHost()).hasValue(AN_ADDRESS.getHostAddress()); assertThat(successfulNodeInfo.getErrorMessage()).isEmpty(); assertThat(successfulNodeInfo.getSections()).hasSize(1); NodeInfo timedOutNodeInfo = findNode(nodes, "bar"); assertThat(timedOutNodeInfo.getName()).isEqualTo("bar"); assertThat(timedOutNodeInfo.getErrorMessage()).hasValue("Failed to retrieve information on time"); assertThat(timedOutNodeInfo.getSections()).isEmpty(); NodeInfo failedNodeInfo = findNode(nodes, "baz"); assertThat(failedNodeInfo.getName()).isEqualTo("baz"); assertThat(failedNodeInfo.getErrorMessage()).hasValue("Failed to retrieve information: BOOM"); assertThat(failedNodeInfo.getSections()).isEmpty(); }
@Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { if (session.getMethod() != NanoHTTPD.Method.GET) { return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); } ProtobufSystemInfo.SystemInfo.Builder infoBuilder = ProtobufSystemInfo.SystemInfo.newBuilder(); for (SystemInfoSection sectionProvider : sectionProviders) { ProtobufSystemInfo.Section section = sectionProvider.toProtobuf(); infoBuilder.addSections(section); } byte[] bytes = infoBuilder.build().toByteArray(); return newFixedLengthResponse(OK, PROTOBUF_MIME_TYPE, new ByteArrayInputStream(bytes), bytes.length); } }
@Test public void merge_sections() { ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{ new TestSystemInfoSection("foo"), new TestSystemInfoSection("bar")}); underTest.start(); List<ProtobufSystemInfo.Section> sections = ProcessInfoProvider.provide().getSectionsList(); assertThat(sections).extracting(ProtobufSystemInfo.Section::getName) .containsExactlyInAnyOrder("foo", "bar"); underTest.stop(); }
@Test public void remove_global_sections_from_results() { ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{ new TestGlobalSystemInfoSection("foo"), new TestSystemInfoSection("bar")}); underTest.start(); List<ProtobufSystemInfo.Section> sections = ProcessInfoProvider.provide().getSectionsList(); assertThat(sections).extracting(ProtobufSystemInfo.Section::getName).containsExactly("bar"); underTest.stop(); }
@Override public void write(JsonWriter json) { writeHealth(json); List<ProtobufSystemInfo.Section> sections = stream(systemInfoSections) .map(SystemInfoSection::toProtobuf) .collect(MoreCollectors.toArrayList()); ceHttpClient.retrieveSystemInfo() .ifPresent(ce -> sections.addAll(ce.getSectionsList())); writeSections(sections, json); writeTelemetry(json); }
@Test public void retrieveSystemInfo_get_information_if_process_is_up() { Buffer response = new Buffer(); response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray()); server.enqueue(new MockResponse().setBody(response)); // initialize registration of process setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE); Optional<ProtobufSystemInfo.SystemInfo> info = underTest.retrieveSystemInfo(); assertThat(info.get().getSectionsCount()).isEqualTo(0); }
@Override public Optional<ProtobufSystemInfo.SystemInfo> call(String url) throws Exception { byte[] protobuf = IOUtils.toByteArray(new URI(url)); return Optional.of(ProtobufSystemInfo.SystemInfo.parseFrom(protobuf)); } }
@Test public void empty_result_is_returned_if_not_started_yet() { ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{ new TestSystemInfoSection("foo"), new TestSystemInfoSection("bar")}); assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(0); underTest.start(); assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(2); underTest.stop(); assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(0); } }