-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from codota/boaz-rolling
Fix consecutive timeouts & restarts
- Loading branch information
Showing
9 changed files
with
172 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ | |
|
||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.util.concurrency.AppExecutorUtil; | ||
import com.tabnine.binary.exceptions.BinaryCannotRecoverException; | ||
import com.tabnine.binary.exceptions.TabNineDeadException; | ||
import com.tabnine.binary.exceptions.TooManyConsecutiveRestartsException; | ||
import com.tabnine.binary.exceptions.TabNineInvalidResponseException; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Queue; | ||
|
@@ -16,10 +17,10 @@ public class BinaryRequestFacade { | |
private final Queue<Future<?>> activeRequests = new ConcurrentLinkedQueue<>(); | ||
private final AtomicInteger consecutiveRestarts = new AtomicInteger(0); | ||
private final AtomicInteger consecutiveTimeouts = new AtomicInteger(0); | ||
private final TabNineGateway process; | ||
private final TabNineGateway tabNineGateway; | ||
|
||
public BinaryRequestFacade(TabNineGateway process) { | ||
this.process = process; | ||
public BinaryRequestFacade(TabNineGateway tabNineGateway) { | ||
this.tabNineGateway = tabNineGateway; | ||
} | ||
|
||
public <R extends BinaryResponse> R executeRequest(BinaryRequest<R> req) { | ||
|
@@ -28,34 +29,22 @@ public <R extends BinaryResponse> R executeRequest(BinaryRequest<R> req) { | |
|
||
@Nullable | ||
public <R extends BinaryResponse> R executeRequest(BinaryRequest<R> req, int timeoutMillis) { | ||
if (process.isStarting()) { | ||
if (tabNineGateway.isStarting()) { | ||
Logger.getInstance(getClass()).info("Can't get completions because TabNine process is not started yet."); | ||
|
||
return null; | ||
} | ||
|
||
try { | ||
Future<R> request = AppExecutorUtil.getAppExecutorService().submit(() -> { | ||
try { | ||
return process.request(req); | ||
} catch (TabNineDeadException e) { | ||
Logger.getInstance(getClass()).warn("TabNine is in invalid state, it is being restarted.", e); | ||
|
||
if (consecutiveRestarts.incrementAndGet() > CONSECUTIVE_RESTART_THRESHOLD) { | ||
Logger.getInstance(getClass()).error("Tabnine is not able to function properly. Contact [email protected]", new TooManyConsecutiveRestartsException()); | ||
consecutiveRestarts.set(0); | ||
} else { | ||
restartBinary(); | ||
} | ||
|
||
return null; | ||
} finally { | ||
activeRequests.removeIf(Future::isDone); | ||
} | ||
}); | ||
|
||
activeRequests.add(request); | ||
|
||
return request.get(timeoutMillis, TimeUnit.MILLISECONDS); | ||
Future<R> requestFuture = AppExecutorUtil.getAppExecutorService().submit(() -> executeBoundlessRequest(req)); | ||
|
||
activeRequests.add(requestFuture); | ||
|
||
R result = requestFuture.get(timeoutMillis, TimeUnit.MILLISECONDS); | ||
|
||
consecutiveTimeouts.set(0); | ||
|
||
return result; | ||
} catch (TimeoutException e) { | ||
Logger.getInstance(getClass()).info("TabNine's response timed out."); | ||
|
||
|
@@ -64,18 +53,52 @@ public <R extends BinaryResponse> R executeRequest(BinaryRequest<R> req, int tim | |
consecutiveTimeouts.set(0); | ||
restartBinary(); | ||
} | ||
} catch (ExecutionException e) { | ||
if(e.getCause().getCause() instanceof BinaryCannotRecoverException) { | ||
throw (BinaryCannotRecoverException) e.getCause().getCause(); | ||
} | ||
|
||
Logger.getInstance(getClass()).warn("TabNine's threw an unknown error during request.", e); | ||
} catch (CancellationException e) { | ||
// This is ok. Nothing needs to be done. | ||
} catch (Throwable t) { | ||
Logger.getInstance(getClass()).error("TabNine's threw an unknown error.", t); | ||
} catch (Exception e) { | ||
Logger.getInstance(getClass()).error("TabNine's threw an unknown error.", e); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Nullable | ||
private <R extends BinaryResponse> R executeBoundlessRequest(BinaryRequest<R> req) { | ||
try { | ||
R result = tabNineGateway.request(req); | ||
|
||
consecutiveRestarts.set(0); | ||
|
||
return result; | ||
} catch (TabNineDeadException e) { | ||
Logger.getInstance(getClass()).warn("TabNine is in invalid state, it is being restarted.", e); | ||
|
||
if (consecutiveRestarts.incrementAndGet() > CONSECUTIVE_RESTART_THRESHOLD) { | ||
// NOTICE: In the production version of IntelliJ, logging an error kills the plugin. So this is similar to exit(1); | ||
Logger.getInstance(getClass()).error("Tabnine is not able to function properly. Contact [email protected]", new BinaryCannotRecoverException()); | ||
} else { | ||
restartBinary(); | ||
} | ||
|
||
return null; | ||
} catch (TabNineInvalidResponseException e) { | ||
Logger.getInstance(getClass()).warn(e); | ||
|
||
return null; | ||
} finally { | ||
activeRequests.removeIf(Future::isDone); | ||
} | ||
} | ||
|
||
private void restartBinary() { | ||
this.activeRequests.forEach(f -> f.cancel(true)); | ||
this.activeRequests.clear(); | ||
this.process.restart(); | ||
this.tabNineGateway.restart(); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/tabnine/binary/exceptions/BinaryCannotRecoverException.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,7 @@ | ||
package com.tabnine.binary.exceptions; | ||
|
||
public class BinaryCannotRecoverException extends RuntimeException { | ||
public BinaryCannotRecoverException() { | ||
super(); | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
src/main/java/com/tabnine/binary/exceptions/TooManyConsecutiveRestartsException.java
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.tabnine.testutils; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
import static com.tabnine.general.StaticConfig.CONSECUTIVE_RESTART_THRESHOLD; | ||
import static com.tabnine.general.StaticConfig.ILLEGAL_RESPONSE_THRESHOLD; | ||
import static com.tabnine.testutils.TestData.*; | ||
|
||
public class BadResultsUtils { | ||
@NotNull | ||
public static Stream<String> overThresholdBadResultsWithAGoodResultInBetween() { | ||
Stream<String> results = Stream.concat(enoughBadResultsToCauseARestart(), Stream.of(A_PREDICTION_RESULT)); | ||
|
||
for(int i = 0; i < CONSECUTIVE_RESTART_THRESHOLD; i++) { | ||
results = Stream.concat(results, enoughBadResultsToCauseARestart()); | ||
} | ||
|
||
return results; | ||
} | ||
@NotNull | ||
public static Stream<String> enoughBadResultsToCauseADeath() { | ||
Stream<String> results = enoughBadResultsToCauseARestart(); | ||
|
||
for(int i = 0; i < CONSECUTIVE_RESTART_THRESHOLD; i++) { | ||
results = Stream.concat(results, enoughBadResultsToCauseARestart()); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private static Stream<String> enoughBadResultsToCauseARestart() { | ||
return IntStream.range(0, ILLEGAL_RESPONSE_THRESHOLD + OVERFLOW).mapToObj(i -> INVALID_RESULT); | ||
} | ||
} |
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