@VisibleForTesting String urlToMatchCenteredInLogPageDaemonFile(byte[] needle, Path canonicalPath, int offset, Integer port) throws UnknownHostException { final String host = Utils.hostname(); final Path truncatedFilePath = truncatePathToLastElements(canonicalPath, 1); Map<String, Object> parameters = new HashMap<>(); parameters.put("file", truncatedFilePath.toString()); parameters.put("start", Math.max(0, offset - (LogviewerConstant.DEFAULT_BYTES_PER_PAGE / 2) - (needle.length / -2))); parameters.put("length", LogviewerConstant.DEFAULT_BYTES_PER_PAGE); return UrlBuilder.build(String.format(this.scheme + "://%s:%d/api/v1/daemonlog", host, port), parameters); }
@VisibleForTesting String urlToMatchCenteredInLogPage(byte[] needle, Path canonicalPath, int offset, Integer port) throws UnknownHostException { final String host = Utils.hostname(); final Path truncatedFilePath = truncatePathToLastElements(canonicalPath, 3); Map<String, Object> parameters = new HashMap<>(); parameters.put("file", truncatedFilePath.toString()); parameters.put("start", Math.max(0, offset - (LogviewerConstant.DEFAULT_BYTES_PER_PAGE / 2) - (needle.length / -2))); parameters.put("length", LogviewerConstant.DEFAULT_BYTES_PER_PAGE); return UrlBuilder.build(String.format(this.scheme + "://%s:%d/api/v1/log", host, port), parameters); }
@Override public void report(Throwable error) { LOG.error("Error", error); if (Time.deltaSecs(intervalStartTime.get()) > errorIntervalSecs) { intervalErrors.set(0); intervalStartTime.set(Time.currentTimeSecs()); } if (intervalErrors.incrementAndGet() <= maxPerInterval) { try { stormClusterState.reportError(stormId, componentId, Utils.hostname(), workerTopologyContext.getThisWorkerPort().longValue(), error); } catch (UnknownHostException e) { throw Utils.wrapInRuntime(e); } } } }
String hostname = Utils.hostname();
@Test public void testReturnsZeroMatchesForUnseenPattern() throws UnknownHostException, InvalidRequestException { Utils prevUtils = null; try { Utils mockedUtil = mock(Utils.class); prevUtils = Utils.setInstance(mockedUtil); String pattern = "Not There"; when(mockedUtil.hostname()).thenReturn(expectedHost); final File file = new File(String.join(File.separator, "src", "test", "resources"), "test-worker.log.test"); Map<String, Object> expected = new HashMap<>(); expected.put("isDaemon", "no"); expected.put("searchString", pattern); expected.put("startByteOffset", 0); expected.put("matches", Collections.emptyList()); LogviewerLogSearchHandler handler = getSearchHandlerWithPort(expectedPort); Map<String, Object> searchResult = handler.substringSearch(file, pattern); assertEquals(expected, searchResult); } finally { Utils.setInstance(prevUtils); } }
/** * Send a heartbeat to local supervisor first to check if supervisor is ok for heartbeating. */ private void heartbeatToMasterIfLocalbeatFail(LSWorkerHeartbeat lsWorkerHeartbeat) { if (ConfigUtils.isLocalMode(this.conf)) { return; } //In distributed mode, send heartbeat directly to master if local supervisor goes down. SupervisorWorkerHeartbeat workerHeartbeat = new SupervisorWorkerHeartbeat(lsWorkerHeartbeat.get_topology_id(), lsWorkerHeartbeat.get_executors(), lsWorkerHeartbeat.get_time_secs()); try (SupervisorClient client = SupervisorClient.getConfiguredClient(conf, Utils.hostname(), supervisorPort)) { client.getClient().sendSupervisorWorkerHeartbeat(workerHeartbeat); } catch (Exception tr1) { //If any error/exception thrown, report directly to nimbus. LOG.warn("Exception when send heartbeat to local supervisor", tr1.getMessage()); try (NimbusClient nimbusClient = NimbusClient.getConfiguredClient(conf)) { nimbusClient.getClient().sendSupervisorWorkerHeartbeat(workerHeartbeat); } catch (Exception tr2) { //if any error/exception thrown, just ignore. LOG.error("Exception when send heartbeat to master", tr2.getMessage()); } } }
@Test public void testLogviewerLinkCentersTheMatchInThePageDaemon() throws UnknownHostException { String expectedFname = "foobar.log"; LogviewerLogSearchHandler handler = getSearchHandlerWithPort(expectedPort); Utils prevUtils = null; try { Utils mockedUtil = mock(Utils.class); prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost); String actualUrl = handler.urlToMatchCenteredInLogPageDaemonFile(new byte[42], new File(expectedFname).toPath(), 27526, 8888); assertEquals("http://" + expectedHost + ":" + expectedPort + "/api/v1/daemonlog?file=" + expectedFname + "&start=1947&length=" + LogviewerConstant.DEFAULT_BYTES_PER_PAGE, actualUrl); } finally { Utils.setInstance(prevUtils); } }
@Test public void testLogviewerLinkCentersTheMatchInThePage() throws UnknownHostException { String expectedFname = "foobar.log"; LogviewerLogSearchHandler handler = getSearchHandlerWithPort(expectedPort); Utils prevUtils = null; try { Utils mockedUtil = mock(Utils.class); prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost); String actualUrl = handler.urlToMatchCenteredInLogPage(new byte[42], new File(expectedFname).toPath(), 27526, 8888); assertEquals("http://" + expectedHost + ":" + expectedPort + "/api/v1/log?file=" + expectedFname + "&start=1947&length=" + LogviewerConstant.DEFAULT_BYTES_PER_PAGE, actualUrl); } finally { Utils.setInstance(prevUtils); } }
@Test public void testAreallySmallLogDaemonFile() throws InvalidRequestException, UnknownHostException { Utils prevUtils = null; try { Utils mockedUtil = mock(Utils.class); prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost); final File file = new File(String.join(File.separator, "src", "test", "resources"), "small-worker.log.test"); Map<String, Object> expected = new HashMap<>(); expected.put("isDaemon", "yes"); expected.put("searchString", pattern); expected.put("startByteOffset", 0); List<Map<String, Object>> matches = new ArrayList<>(); matches.add(buildMatchData(7, "000000 ", " 000000\n", pattern, "/api/v1/daemonlog?file=" + file.getName() + "&start=0&length=51200" )); expected.put("matches", matches); LogviewerLogSearchHandler handler = getSearchHandlerWithPort(expectedPort); Map<String, Object> searchResult = handler.substringSearchDaemonLog(file, pattern); assertEquals(expected, searchResult); } finally { Utils.setInstance(prevUtils); } }
private Assignment getLocalAssignment(Map<String, Object> conf, IStormClusterState stormClusterState, String topologyId) { if (!ConfigUtils.isLocalMode(conf)) { try (SupervisorClient supervisorClient = SupervisorClient.getConfiguredClient(conf, Utils.hostname(), supervisorPort)) { Assignment assignment = supervisorClient.getClient().getLocalAssignmentForStorm(topologyId); return assignment; } catch (Throwable tr1) { //if any error/exception thrown, fetch it from zookeeper return stormClusterState.remoteAssignmentInfo(topologyId, null); } } else { return stormClusterState.remoteAssignmentInfo(topologyId, null); } }
prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost);
this.hostname = Utils.hostname(); } catch (UnknownHostException ignored) { this.hostname = "";
@Test public void testAreallySmallLogFile() throws Exception { Utils prevUtils = null; try { Utils mockedUtil = mock(Utils.class); prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost); final File file = new File(String.join(File.separator, "src", "test", "resources"), "small-worker.log.test"); Map<String, Object> expected = new HashMap<>(); expected.put("isDaemon", "no"); expected.put("searchString", pattern); expected.put("startByteOffset", 0); List<Map<String, Object>> matches = new ArrayList<>(); matches.add(buildMatchData(7, "000000 ", " 000000\n", pattern, "/api/v1/log?file=test" + encodedFileSeparator() + "resources" + encodedFileSeparator() + file.getName() + "&start=0&length=51200" )); expected.put("matches", matches); LogviewerLogSearchHandler handler = getSearchHandlerWithPort(expectedPort); Map<String, Object> searchResult = handler.substringSearch(file, pattern); assertEquals(expected, searchResult); } finally { Utils.setInstance(prevUtils); } }
prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost);
prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost);
prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost);
this.hostName = Utils.hostname(); } catch (UnknownHostException e) { throw Utils.wrapInRuntime(e);
prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost);
prevUtils = Utils.setInstance(mockedUtil); when(mockedUtil.hostname()).thenReturn(expectedHost);
this.hostName = Utils.hostname(); } catch (UnknownHostException e) { throw Utils.wrapInRuntime(e);