-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-22867 The ForkJoinPool in CleanerChore will spawn thousands of …
…threads in our cluster with thousands table
- Loading branch information
Showing
3 changed files
with
158 additions
and
46 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
hbase-server/src/main/java/org/apache/hadoop/hbase/io/util/ConcurrentDirVisitor.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,110 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.hadoop.hbase.io.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
|
||
import org.apache.hadoop.fs.FileStatus; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.hbase.DaemonThreadFactory; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
@InterfaceAudience.Private | ||
public class ConcurrentDirVisitor { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ConcurrentDirVisitor.class); | ||
|
||
public interface Visitor { | ||
boolean visit(FileStatus targets); | ||
} | ||
|
||
private final FileSystem fs; | ||
private final ThreadPoolExecutor pool; | ||
|
||
public ConcurrentDirVisitor(FileSystem fs, ThreadPoolExecutor pool) { | ||
this.fs = fs; | ||
this.pool = pool; | ||
} | ||
|
||
public ConcurrentDirVisitor(FileSystem fs, int poolSize, String threadName) { | ||
this.fs = fs; | ||
this.pool = new ThreadPoolExecutor(poolSize, poolSize, 500, TimeUnit.SECONDS, | ||
new LinkedBlockingQueue<>(), | ||
new DaemonThreadFactory("dir-visitor-(" + threadName + ")" + "-pool")); | ||
} | ||
|
||
private CompletableFuture<Boolean> submit(Supplier<Boolean> supplier) { | ||
return CompletableFuture.supplyAsync(supplier, pool); | ||
} | ||
|
||
public CompletableFuture<Boolean> visit(FileStatus target, Visitor visitor) { | ||
if (target == null) { | ||
throw new IllegalArgumentException("The target FileStatus shouldn't be null"); | ||
} | ||
if (target.isFile()) { | ||
return this.submit(() -> visitor.visit(target)); | ||
} else if (target.isDirectory()) { | ||
return this.submit(new DirTask(fs, target, visitor)); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private class DirTask implements Supplier<Boolean> { | ||
private final FileSystem fs; | ||
private final FileStatus dir; | ||
private final Visitor visitor; | ||
|
||
public DirTask(FileSystem fs, FileStatus dir, Visitor visitor) { | ||
Preconditions.checkArgument(dir.isDirectory(), "Must be a directory"); | ||
this.fs = fs; | ||
this.dir = dir; | ||
this.visitor = visitor; | ||
} | ||
|
||
@Override | ||
public Boolean get() { | ||
try { | ||
FileStatus[] subFiles = fs.listStatus(dir.getPath()); | ||
if (subFiles == null || subFiles.length == 0) { | ||
LOG.debug("No sub-file or sub-directory under directory: {}", dir.getPath()); | ||
return true; | ||
} | ||
List<CompletableFuture<Boolean>> futures = new ArrayList<>(); | ||
for (int i = 0; i < subFiles.length; i++) { | ||
futures.add(visit(subFiles[i], visitor)); | ||
} | ||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(); | ||
return true; | ||
} catch (Exception e) { | ||
LOG.warn("Encountered error when visit directory: " + dir.getPath(), e); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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