-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2241] improvement(server): Introduce storage flush operation timeou…
…t cancel to avoid disk hang (#2247) ### What changes were proposed in this pull request? Introduce storage flush operation timeout cancel to avoid disk hang ### Why are the changes needed? For #2241 ### Does this PR introduce _any_ user-facing change? Yes. `rss.server.storage.flushOptionTimeoutSec` is introduced, the default value = -1 means this timeout cancel will not be activated by default ### How was this patch tested? unit tests and existing tests. --------- Co-authored-by: Junfan Zhang <[email protected]>
- Loading branch information
Showing
4 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
common/src/main/java/org/apache/uniffle/common/future/CompletableFutureUtils.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,72 @@ | ||
/* | ||
* 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.uniffle.common.future; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.apache.uniffle.common.exception.RssException; | ||
import org.apache.uniffle.common.util.ThreadUtils; | ||
|
||
public class CompletableFutureUtils { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CompletableFutureUtils.class); | ||
|
||
public static <T> T withTimeoutCancel(Supplier<T> supplier, long timeoutMills) throws Exception { | ||
return withTimeoutCancel(supplier, timeoutMills, ""); | ||
} | ||
|
||
public static <T> T withTimeoutCancel( | ||
Supplier<T> supplier, long timeoutMills, String operationName) throws Exception { | ||
CompletableFuture<T> future = | ||
CompletableFuture.supplyAsync( | ||
supplier, | ||
Executors.newSingleThreadExecutor(ThreadUtils.getThreadFactory(operationName))); | ||
future.exceptionally( | ||
throwable -> { | ||
throw new RssException(throwable); | ||
}); | ||
|
||
CompletableFuture<T> extended = | ||
CompletableFutureExtension.orTimeout(future, timeoutMills, TimeUnit.MILLISECONDS); | ||
try { | ||
return extended.get(); | ||
} catch (Exception e) { | ||
if (e instanceof ExecutionException) { | ||
Throwable internalThrowable = e.getCause(); | ||
if (internalThrowable instanceof TimeoutException) { | ||
LOGGER.error( | ||
"The operation of [{}] haven't finished in the {}(millis). Drop this execution!", | ||
operationName, | ||
timeoutMills); | ||
throw new TimeoutException(); | ||
} | ||
if (internalThrowable instanceof Exception) { | ||
throw (Exception) internalThrowable; | ||
} | ||
} | ||
throw e; | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
common/src/test/java/org/apache/uniffle/common/future/CompletableFutureUtilsTest.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,78 @@ | ||
/* | ||
* 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.uniffle.common.future; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.apache.uniffle.common.exception.RssException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class CompletableFutureUtilsTest { | ||
|
||
@Test | ||
public void timeoutTest() { | ||
// case1: legal operation | ||
Supplier<Integer> supplier = () -> 1; | ||
try { | ||
int result = CompletableFutureUtils.withTimeoutCancel(supplier, 100); | ||
assertEquals(1, result); | ||
} catch (Exception e) { | ||
fail(); | ||
} | ||
|
||
// case2: illegal | ||
supplier = | ||
() -> { | ||
try { | ||
Thread.sleep(100000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return 10; | ||
}; | ||
try { | ||
int result = CompletableFutureUtils.withTimeoutCancel(supplier, 100); | ||
fail(); | ||
} catch (Exception e) { | ||
if (!(e instanceof TimeoutException)) { | ||
fail(); | ||
} | ||
} | ||
|
||
// case3: fast fail when internal supplier throw exception | ||
supplier = | ||
() -> { | ||
throw new RssException("Hello"); | ||
}; | ||
try { | ||
int result = CompletableFutureUtils.withTimeoutCancel(supplier, 100); | ||
fail(); | ||
} catch (Exception e) { | ||
if (e instanceof RssException) { | ||
// ignore | ||
} else { | ||
fail(); | ||
} | ||
} | ||
} | ||
} |
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