Search in sources :

Example 1 with SupervisorUtils

use of org.apache.storm.daemon.supervisor.SupervisorUtils in project storm by apache.

the class WorkerLogsTest method testIdentifyWorkerLogDirs.

/**
 * Build up workerid-workerlogdir map for the old workers' dirs.
 */
@Test
public void testIdentifyWorkerLogDirs() throws Exception {
    try (TmpPath testDir = new TmpPath()) {
        Path port1Dir = Files.createDirectories(testDir.getFile().toPath().resolve("workers-artifacts/topo1/port1"));
        Path metaFile = Files.createFile(testDir.getFile().toPath().resolve("worker.yaml"));
        String expId = "id12345";
        SortedSet<Path> expected = new TreeSet<>();
        expected.add(port1Dir);
        SupervisorUtils mockedSupervisorUtils = mock(SupervisorUtils.class);
        SupervisorUtils.setInstance(mockedSupervisorUtils);
        Map<String, Object> stormConf = Utils.readStormConfig();
        WorkerLogs workerLogs = new WorkerLogs(stormConf, port1Dir, new StormMetricsRegistry()) {

            @Override
            public Optional<Path> getMetadataFileForWorkerLogDir(Path logDir) throws IOException {
                return Optional.of(metaFile);
            }

            @Override
            public String getWorkerIdFromMetadataFile(Path metaFile) {
                return expId;
            }
        };
        when(mockedSupervisorUtils.readWorkerHeartbeatsImpl(anyMap())).thenReturn(null);
        assertEquals(expected, workerLogs.getLogDirs(Collections.singleton(port1Dir), (wid) -> true));
    } finally {
        SupervisorUtils.resetInstance();
    }
}
Also used : TmpPath(org.apache.storm.testing.TmpPath) Path(java.nio.file.Path) SortedSet(java.util.SortedSet) Files(java.nio.file.Files) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) IOException(java.io.IOException) SupervisorUtils(org.apache.storm.daemon.supervisor.SupervisorUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Utils(org.apache.storm.utils.Utils) File(java.io.File) TreeSet(java.util.TreeSet) Map(java.util.Map) Optional(java.util.Optional) TmpPath(org.apache.storm.testing.TmpPath) Path(java.nio.file.Path) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) TreeSet(java.util.TreeSet) TmpPath(org.apache.storm.testing.TmpPath) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) SupervisorUtils(org.apache.storm.daemon.supervisor.SupervisorUtils) Test(org.junit.Test)

Example 2 with SupervisorUtils

use of org.apache.storm.daemon.supervisor.SupervisorUtils in project storm by apache.

the class LogCleanerTest method testGetDeadWorkerDirs.

/**
 * return directories for workers that are not alive.
 */
@Test
public void testGetDeadWorkerDirs() throws Exception {
    Map<String, Object> stormConf = Utils.readStormConfig();
    stormConf.put(SUPERVISOR_WORKER_TIMEOUT_SECS, 5);
    LSWorkerHeartbeat hb = new LSWorkerHeartbeat();
    hb.set_time_secs(1);
    Map<String, LSWorkerHeartbeat> idToHb = Collections.singletonMap("42", hb);
    int nowSecs = 2;
    try (TmpPath testDir = new TmpPath()) {
        Path unexpectedDir1 = createDir(testDir.getFile().toPath(), "dir1");
        Path expectedDir2 = createDir(testDir.getFile().toPath(), "dir2");
        Path expectedDir3 = createDir(testDir.getFile().toPath(), "dir3");
        Set<Path> logDirs = Sets.newSet(unexpectedDir1, expectedDir2, expectedDir3);
        SupervisorUtils mockedSupervisorUtils = mock(SupervisorUtils.class);
        SupervisorUtils.setInstance(mockedSupervisorUtils);
        Map<String, Object> conf = Utils.readStormConfig();
        StormMetricsRegistry metricRegistry = new StormMetricsRegistry();
        WorkerLogs stubbedWorkerLogs = new WorkerLogs(conf, Paths.get(""), metricRegistry) {

            @Override
            public SortedSet<Path> getLogDirs(Set<Path> logDirs, Predicate<String> predicate) {
                TreeSet<Path> ret = new TreeSet<>();
                if (predicate.test("42")) {
                    ret.add(unexpectedDir1);
                }
                if (predicate.test("007")) {
                    ret.add(expectedDir2);
                }
                if (predicate.test("")) {
                    ret.add(expectedDir3);
                }
                return ret;
            }
        };
        LogCleaner logCleaner = new LogCleaner(conf, stubbedWorkerLogs, new DirectoryCleaner(metricRegistry), null, metricRegistry);
        when(mockedSupervisorUtils.readWorkerHeartbeatsImpl(anyMap())).thenReturn(idToHb);
        assertEquals(Sets.newSet(expectedDir2, expectedDir3), logCleaner.getDeadWorkerDirs(nowSecs, logDirs));
    } finally {
        SupervisorUtils.resetInstance();
    }
}
Also used : Path(java.nio.file.Path) TmpPath(org.apache.storm.testing.TmpPath) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) Set(java.util.Set) TmpPath(org.apache.storm.testing.TmpPath) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) Predicate(java.util.function.Predicate) LSWorkerHeartbeat(org.apache.storm.generated.LSWorkerHeartbeat) TreeSet(java.util.TreeSet) SupervisorUtils(org.apache.storm.daemon.supervisor.SupervisorUtils) Test(org.junit.jupiter.api.Test)

Aggregations

Path (java.nio.file.Path)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 SupervisorUtils (org.apache.storm.daemon.supervisor.SupervisorUtils)2 StormMetricsRegistry (org.apache.storm.metric.StormMetricsRegistry)2 TmpPath (org.apache.storm.testing.TmpPath)2 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 LSWorkerHeartbeat (org.apache.storm.generated.LSWorkerHeartbeat)1 Utils (org.apache.storm.utils.Utils)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)1