-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hadoop: avoid create too many threads for multiple JuiceFileSystem in…
…stance (#3857) * hadoop: use static field for background thread to save thread usage update update clean refactor use BgTaskUtil to admin bg task clean code * add license * close emptier fs * only close when BgTaskUtil is gced * clean code * add static field for gc when sdk runs in multiple classloader * update * alter parameter name
- Loading branch information
1 parent
8227f24
commit 9881252
Showing
3 changed files
with
119 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
sdk/java/src/main/java/io/juicefs/utils/BgTaskUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* JuiceFS, Copyright 2023 Juicedata, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.juicefs.utils; | ||
|
||
import org.apache.hadoop.fs.FileSystem; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class BgTaskUtil { | ||
private static final Logger LOG = LoggerFactory.getLogger(BgTaskUtil.class); | ||
|
||
private static BgTaskUtil staticFieldForGc = new BgTaskUtil(); | ||
|
||
private BgTaskUtil() { | ||
} | ||
|
||
private static final ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2, r -> { | ||
Thread thread = new Thread(r, "Background Task"); | ||
thread.setDaemon(true); | ||
return thread; | ||
}); | ||
// use timer to run trash emptier because it will occupy a thread | ||
private static final List<Timer> timers = new ArrayList<>(); | ||
private static final List<FileSystem> fileSystems = new ArrayList<>(); | ||
private static Set<String> runningBgTask = new HashSet<>(); | ||
|
||
public static void startScheduleTask(String name, String type, Runnable task, long initialDelay, long period, TimeUnit unit) { | ||
synchronized (runningBgTask) { | ||
if (isRunning(name, type)) { | ||
return; | ||
} | ||
threadPool.scheduleAtFixedRate(() -> { | ||
try { | ||
task.run(); | ||
} catch (Exception e) { | ||
LOG.error("Background task failed", e); | ||
} | ||
}, initialDelay, period, unit); | ||
runningBgTask.add(genKey(name, type)); | ||
} | ||
} | ||
|
||
|
||
public static void startTrashEmptier(String name, String type, FileSystem fs, Runnable emptierTask, long delay) { | ||
synchronized (runningBgTask) { | ||
if (isRunning(name, type)) { | ||
return; | ||
} | ||
Timer timer = new Timer(); | ||
timer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
emptierTask.run(); | ||
} | ||
}, delay); | ||
runningBgTask.add(genKey(name, type)); | ||
timers.add(timer); | ||
fileSystems.add(fs); | ||
} | ||
} | ||
|
||
public static boolean isRunning(String name, String type) { | ||
synchronized (runningBgTask) { | ||
return runningBgTask.contains(genKey(name, type)); | ||
} | ||
} | ||
|
||
private static String genKey(String name, String type) { | ||
return name + "|" + type; | ||
} | ||
|
||
@Override | ||
protected void finalize() { | ||
threadPool.shutdownNow(); | ||
for (Timer timer : timers) { | ||
timer.cancel(); | ||
timer.purge(); | ||
} | ||
for (FileSystem fs : fileSystems) { | ||
try { | ||
fs.close(); | ||
} catch (IOException e) { | ||
LOG.warn("close trash emptier fs failed", e); | ||
} | ||
} | ||
} | ||
} |