b923 commented on a change in pull request #9759: [FLINK-14169][history-server] 
Cleanup expired jobs from history server
URL: https://github.com/apache/flink/pull/9759#discussion_r337104935
 
 

 ##########
 File path: 
flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/history/HistoryServerTest.java
 ##########
 @@ -117,45 +124,145 @@ public void testHistoryServerIntegration() throws 
Exception {
                for (int x = 0; x < numJobs; x++) {
                        runJob();
                }
+               final int numLegacyJobs = 1;
                createLegacyArchive(jmDirectory.toPath());
 
-               CountDownLatch numExpectedArchivedJobs = new 
CountDownLatch(numJobs + 1);
+               waitForArchivesCreation(numJobs + numLegacyJobs);
 
-               Configuration historyServerConfig = new Configuration();
-               
historyServerConfig.setString(HistoryServerOptions.HISTORY_SERVER_ARCHIVE_DIRS, 
jmDirectory.toURI().toString());
-               
historyServerConfig.setString(HistoryServerOptions.HISTORY_SERVER_WEB_DIR, 
hsDirectory.getAbsolutePath());
-               
historyServerConfig.setLong(HistoryServerOptions.HISTORY_SERVER_ARCHIVE_REFRESH_INTERVAL,
 100L);
+               CountDownLatch numExpectedArchivedJobs = new 
CountDownLatch(numJobs + numLegacyJobs);
 
-               
historyServerConfig.setInteger(HistoryServerOptions.HISTORY_SERVER_WEB_PORT, 0);
+               Configuration historyServerConfig = 
createTestConfiguration(false);
 
-               // the job is archived asynchronously after env.execute() 
returns
-               File[] archives = jmDirectory.listFiles();
-               while (archives == null || archives.length != numJobs + 1) {
-                       Thread.sleep(50);
-                       archives = jmDirectory.listFiles();
+               HistoryServer hs =
+               new HistoryServer(
+                       historyServerConfig,
+                       (event) -> {
+                               if (event.getType() == 
HistoryServerArchiveFetcher.ArchiveEventType.CREATED) {
+                                       numExpectedArchivedJobs.countDown();
+                               }
+                       });
+
+               try {
+                       hs.start();
+                       String baseUrl = "http://localhost:"; + hs.getWebPort();
+                       numExpectedArchivedJobs.await(10L, TimeUnit.SECONDS);
+
+                       ObjectMapper mapper = new ObjectMapper();
+                       Assert.assertEquals(numJobs + numLegacyJobs, 
getJobsOverview(baseUrl, mapper).getJobs().size());
+
+               } finally {
+                       hs.stop();
+               }
+       }
+
+       @Test
+       public void testCleanExpiredJob() throws Exception {
+               runArchiveExpirationTest(true);
+       }
+
+       @Test
+       public void testRemainExpiredJob() throws Exception {
+               runArchiveExpirationTest(false);
+       }
+
+       private void runArchiveExpirationTest(boolean cleanupExpiredJobs) 
throws Exception {
+               int numExpiredJobs = cleanupExpiredJobs ? 1 : 0;
+               int numJobs = 3;
+               List<String> jobIDs = new ArrayList<>();
+               for (int x = 0; x < numJobs; x++) {
+                       jobIDs.add(runJob());
                }
+               waitForArchivesCreation(numJobs);
+
+               CountDownLatch numExpectedArchivedJobs = new 
CountDownLatch(numJobs);
+               CountDownLatch numExpectedExpiredJobs = new 
CountDownLatch(numExpiredJobs);
+
+               Configuration historyServerConfig = 
createTestConfiguration(cleanupExpiredJobs);
+
+               HistoryServer hs =
+                       new HistoryServer(
+                               historyServerConfig,
+                               (event) -> {
+                                       switch (event.getType()){
+                                               case CREATED:
+                                                       
numExpectedArchivedJobs.countDown();
+                                                       break;
+                                               case DELETED:
+                                                       
numExpectedExpiredJobs.countDown();
+                                                       break;
+                                       }
+                               });
 
-               HistoryServer hs = new HistoryServer(historyServerConfig, 
numExpectedArchivedJobs);
                try {
                        hs.start();
+                       // check that expected count of archives exists
                        String baseUrl = "http://localhost:"; + hs.getWebPort();
                        numExpectedArchivedJobs.await(10L, TimeUnit.SECONDS);
 
                        ObjectMapper mapper = new ObjectMapper();
-                       String response = getFromHTTP(baseUrl + 
JobsOverviewHeaders.URL);
-                       MultipleJobsDetails overview = 
mapper.readValue(response, MultipleJobsDetails.class);
+                       Assert.assertEquals(numJobs, getJobsOverview(baseUrl, 
mapper).getJobs().size());
+
+                       String jobIDToDelete = jobIDs.get(0);
+                       verifyArchiveFilesExistence(jobIDToDelete, true);
+
+                       // delete one archive from jm
+                       URI resolve = 
jmDirectory.toURI().resolve(jobIDToDelete);
+                       new File(resolve).delete();
+
+                       numExpectedExpiredJobs.await(10L, TimeUnit.SECONDS);
+
+                       // check that archive presence in hs
+                       Assert.assertEquals(numJobs - numExpiredJobs, 
getJobsOverview(baseUrl, mapper).getJobs().size());
+                       verifyArchiveFilesExistence(jobIDToDelete, 
!cleanupExpiredJobs);
 
-                       Assert.assertEquals(numJobs + 1, 
overview.getJobs().size());
                } finally {
                        hs.stop();
                }
        }
 
-       private static void runJob() throws Exception {
+       private void verifyArchiveFilesExistence(String jobID, boolean 
expectArchiveFilesExist) {
+               List<File> archiveFiles = Arrays.asList(
+                       new 
File(hsDirectory.toURI().resolve(String.format("overviews/%s.json", jobID))),
+                       new 
File(hsDirectory.toURI().resolve(String.format("jobs/%s", jobID))),
+                       new 
File(hsDirectory.toURI().resolve(String.format("jobs/%s.json", jobID))));
+
+               archiveFiles.forEach(
+                       archiveFile -> Assert.assertEquals(
+                               String.format("Existence of file or directory 
%s", archiveFile.getPath()),
+                               archiveFile.exists(), expectArchiveFilesExist));
+       }
+
+       private void waitForArchivesCreation(int numJobs) throws 
InterruptedException {
+               // the job is archived asynchronously after env.execute() 
returns
+               File[] archives = jmDirectory.listFiles();
+               while (archives == null || archives.length != numJobs) {
+                       Thread.sleep(50);
+                       archives = jmDirectory.listFiles();
+               }
+       }
+
+       private Configuration createTestConfiguration(boolean 
cleanupExpiredJobs) {
+               Configuration historyServerConfig = new Configuration();
+               
historyServerConfig.setString(HistoryServerOptions.HISTORY_SERVER_ARCHIVE_DIRS, 
jmDirectory.toURI().toString());
+               
historyServerConfig.setString(HistoryServerOptions.HISTORY_SERVER_WEB_DIR, 
hsDirectory.getAbsolutePath());
+               
historyServerConfig.setLong(HistoryServerOptions.HISTORY_SERVER_ARCHIVE_REFRESH_INTERVAL,
 100L);
+
+               
historyServerConfig.setBoolean(HistoryServerOptions.HISTORY_SERVER_CLEANUP_EXPIRED_JOBS,
 cleanupExpiredJobs);
+
+               
historyServerConfig.setInteger(HistoryServerOptions.HISTORY_SERVER_WEB_PORT, 0);
+               return historyServerConfig;
+       }
+
+       private MultipleJobsDetails getJobsOverview(String baseUrl, 
ObjectMapper mapper) throws Exception {
 
 Review comment:
   Good point. Constant created and method refactored. Thanks

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to