Skip to content

Commit 6dd2038

Browse files
committed
[FLINK-38344][runtime-web] Fix the bug that The local files of the HistoryServer may risk never being deleted.
(cherry picked from commit 39a4628)
1 parent 9955378 commit 6dd2038

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/history/HistoryServer.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,7 @@ public HistoryServer(
197197
webRefreshIntervalMillis =
198198
config.get(HistoryServerOptions.HISTORY_SERVER_WEB_REFRESH_INTERVAL).toMillis();
199199

200-
String webDirectory = config.get(HistoryServerOptions.HISTORY_SERVER_WEB_DIR);
201-
if (webDirectory == null) {
202-
webDirectory =
203-
System.getProperty("java.io.tmpdir")
204-
+ File.separator
205-
+ "flink-web-history-"
206-
+ UUID.randomUUID();
207-
}
208-
webDir = new File(webDirectory);
200+
webDir = clearWebDir(config);
209201

210202
boolean cleanupExpiredArchives =
211203
config.get(HistoryServerOptions.HISTORY_SERVER_CLEANUP_EXPIRED_JOBS);
@@ -257,6 +249,34 @@ public HistoryServer(
257249
HistoryServer.this::stop, HistoryServer.class.getSimpleName(), LOG);
258250
}
259251

252+
private File clearWebDir(Configuration config) throws IOException {
253+
String webDirectory = config.get(HistoryServerOptions.HISTORY_SERVER_WEB_DIR);
254+
if (webDirectory == null) {
255+
webDirectory =
256+
System.getProperty("java.io.tmpdir")
257+
+ File.separator
258+
+ "flink-web-history-"
259+
+ UUID.randomUUID();
260+
}
261+
final File webDir = new File(webDirectory);
262+
LOG.info("Clear the web directory {}", webDir);
263+
if (webDir.exists() && webDir.isDirectory() && webDir.listFiles() != null) {
264+
// Reset the current working directory to eliminate the risk of local file leakage.
265+
// This is because when the current process is forcibly terminated by an external
266+
// command,
267+
// the hook methods for cleaning up local files will not be called.
268+
for (File subFile : webDir.listFiles()) {
269+
FileUtils.deleteFileOrDirectory(subFile);
270+
}
271+
}
272+
return webDir;
273+
}
274+
275+
@VisibleForTesting
276+
File getWebDir() {
277+
return webDir;
278+
}
279+
260280
@VisibleForTesting
261281
int getWebPort() {
262282
return netty.getServerPort();

flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/history/HistoryServerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,35 @@ void testRemainExpiredJob() throws Exception {
270270
runArchiveExpirationTest(false);
271271
}
272272

273+
@Test
274+
void testClearWebDir() throws Exception {
275+
// Test the path configured by 'historyserver.web.tmpdir' is clean.
276+
Configuration historyServerConfig =
277+
createTestConfiguration(
278+
HistoryServerOptions.HISTORY_SERVER_CLEANUP_EXPIRED_JOBS.defaultValue());
279+
historyServerConfig.set(
280+
HistoryServerOptions.HISTORY_SERVER_WEB_DIR, hsDirectory.toURI().toString());
281+
HistoryServer hs = new HistoryServer(historyServerConfig);
282+
assertInitializedHistoryServerWebDir(hs.getWebDir());
283+
284+
// Test the path configured by 'historyserver.web.tmpdir' is dirty.
285+
new File(hsDirectory.toURI() + "/dirtyEmptySubDir").mkdir();
286+
new File(hsDirectory.toURI() + "/dirtyEmptySubFile.json").createNewFile();
287+
new File(hsDirectory.toURI() + "/overviews/dirtyEmptySubDir").mkdir();
288+
new File(hsDirectory.toURI() + "/overviews/dirtyEmptySubFile.json").createNewFile();
289+
new File(hsDirectory.toURI() + "/jobs/dirtyEmptySubDir").mkdir();
290+
new File(hsDirectory.toURI() + "/jobs/dirtyEmptySubFile.json").createNewFile();
291+
hs = new HistoryServer(historyServerConfig);
292+
assertInitializedHistoryServerWebDir(hs.getWebDir());
293+
}
294+
295+
private void assertInitializedHistoryServerWebDir(File historyWebDir) {
296+
297+
assertThat(historyWebDir.list()).containsExactlyInAnyOrder("overviews", "jobs");
298+
assertThat(new File(historyWebDir, "overviews")).exists().isDirectory().isEmptyDirectory();
299+
assertThat(new File(historyWebDir, "jobs").list()).containsExactly("overview.json");
300+
}
301+
273302
private void runArchiveExpirationTest(boolean cleanupExpiredJobs) throws Exception {
274303
int numExpiredJobs = cleanupExpiredJobs ? 1 : 0;
275304
int numJobs = 3;

0 commit comments

Comments
 (0)