Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TableServiceAsyncTest: Eliminate Compiler Overhead Variance; Close QueryCompiler's JavaFileManager #4808

Merged
merged 6 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -683,19 +683,51 @@ private void maybeCreateClass(String className, String code, String packageName,
}
}

private static volatile JavaCompiler JAVA_COMPILER = null;

private static JavaCompiler getJavaCompiler() {
JavaCompiler localCompiler;
if ((localCompiler = JAVA_COMPILER) == null) {
synchronized (QueryCompiler.class) {
if ((localCompiler = JAVA_COMPILER) == null) {
localCompiler = JAVA_COMPILER = ToolProvider.getSystemJavaCompiler();
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
if (localCompiler == null) {
throw new RuntimeException("No Java compiler provided - are you using a JRE instead of a JDK?");
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
}
return localCompiler;
}

/**
* While the JavaFileManager should be closed to clean up resources, using a singleton avoids repeated processing of
* the classpath, which is <b>very</b> expensive.
*/
private static volatile JavaFileManager JAVA_FILE_MANAGER = null;

private static JavaFileManager getJavaFileManager() {
JavaFileManager localManager;
if ((localManager = JAVA_FILE_MANAGER) == null) {
synchronized (QueryCompiler.class) {
if ((localManager = JAVA_FILE_MANAGER) == null) {
localManager = JAVA_FILE_MANAGER = getJavaCompiler().getStandardFileManager(null, null, null);
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
return localManager;
}

private void maybeCreateClassHelper(String fqClassName, String finalCode, String[] splitPackageName,
String rootPathAsString, String tempDirAsString) {
final StringWriter compilerOutput = new StringWriter();

final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new RuntimeException("No Java compiler provided - are you using a JRE instead of a JDK?");
}
final JavaCompiler compiler = getJavaCompiler();

final String classPathAsString = getClassPath() + File.pathSeparator + getJavaClassPath();
final List<String> compilerOptions = Arrays.asList("-d", tempDirAsString, "-cp", classPathAsString);

final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
final JavaFileManager fileManager = getJavaFileManager();

final boolean result = compiler.getTask(compilerOutput,
fileManager,
Expand Down Expand Up @@ -735,7 +767,7 @@ private void maybeCreateClassHelper(String fqClassName, String finalCode, String
* @return a Pair of success, and the compiler output
*/
private Pair<Boolean, String> tryCompile(File basePath, Collection<File> javaFiles) throws IOException {
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final JavaCompiler compiler = getJavaCompiler();
if (compiler == null) {
throw new RuntimeException("No Java compiler provided - are you using a JRE instead of a JDK?");
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
}
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

import io.deephaven.client.DeephavenSessionTestBase;
import io.deephaven.client.impl.TableService.TableHandleFuture;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.table.Table;
import io.deephaven.engine.testutil.TstUtils;
import io.deephaven.engine.util.TableTools;
import io.deephaven.qst.table.TableSpec;
import io.deephaven.util.SafeCloseable;
import org.junit.Test;

import java.time.Duration;
Expand All @@ -19,15 +24,15 @@
public class TableServiceAsyncTest extends DeephavenSessionTestBase {

private static final Duration GETTIME = Duration.ofSeconds(15);
private static final int CHAIN_OPS = 50;
private static final int CHAIN_OPS = 250;
private static final int CHAIN_ROWS = 1000;

@Test(timeout = 20000)
public void longChainAsyncExportOnlyLast() throws ExecutionException, InterruptedException, TimeoutException {
final List<TableSpec> longChain = createLongChain();
final TableSpec longChainLast = longChain.get(longChain.size() - 1);
try (final TableHandle handle = get(session.executeAsync(longChainLast))) {
checkSucceeded(handle);
checkSucceeded(handle, CHAIN_OPS);
}
}

Expand All @@ -36,9 +41,10 @@ public void longChainAsyncExportAll() throws ExecutionException, InterruptedExce
final List<TableSpec> longChain = createLongChain();
final List<? extends TableHandleFuture> futures = session.executeAsync(longChain);
try {
int chainLength = 0;
for (final TableHandleFuture future : futures) {
try (final TableHandle handle = get(future)) {
checkSucceeded(handle);
checkSucceeded(handle, ++chainLength);
}
}
} catch (final Throwable t) {
Expand All @@ -55,7 +61,7 @@ public void longChainAsyncExportAllCancelAllButLast()
// Cancel or close all but the last one
TableService.TableHandleFuture.cancelOrClose(futures.subList(0, futures.size() - 1), true);
try (final TableHandle lastHandle = get(futures.get(futures.size() - 1))) {
checkSucceeded(lastHandle);
checkSucceeded(lastHandle, CHAIN_OPS);
}
}

Expand All @@ -68,7 +74,7 @@ public void immediatelyCompletedFromCachedTableServices()
try (final TableHandle ignored = get(tableService.executeAsync(longChainLast))) {
for (int i = 0; i < 1000; ++i) {
try (final TableHandle handle = get(tableService.executeAsync(longChainLast))) {
checkSucceeded(handle);
checkSucceeded(handle, CHAIN_OPS);
}
}
}
Expand All @@ -79,22 +85,30 @@ private static TableHandle get(TableHandleFuture future)
return future.getOrCancel(GETTIME);
}

private static void checkSucceeded(TableHandle x) {
private void checkSucceeded(TableHandle x, int chainLength) {
assertThat(x.isSuccessful()).isTrue();
try (final SafeCloseable ignored = getExecutionContext().open()) {
final Table result = getSession(session.getCurrentToken()).<Table>getExport(x.exportId().id()).get();
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
ExecutionContext.getContext().getQueryScope().putParam("ChainLength", chainLength);
final Table expected = TableTools.emptyTable(CHAIN_ROWS).update("Current = ii - 1 + ChainLength");
rcaudy marked this conversation as resolved.
Show resolved Hide resolved
TstUtils.assertTableEquals(expected, result);
}
}

private static List<TableSpec> createLongChain() {
return createLongChain(CHAIN_OPS, CHAIN_ROWS);
}

private static List<TableSpec> createLongChain(int numColumns, int numRows) {
final List<TableSpec> longChain = new ArrayList<>(numColumns);
for (int i = 0; i < numColumns; ++i) {
private static List<TableSpec> createLongChain(int chainLength, int numRows) {
final List<TableSpec> longChain = new ArrayList<>(chainLength);
for (int i = 0; i < chainLength; ++i) {
if (i == 0) {
longChain.add(TableSpec.empty(numRows).view("I_0=ii"));
longChain.add(TableSpec.empty(numRows).view("Current = ii"));
} else {
final TableSpec prev = longChain.get(i - 1);
longChain.add(prev.updateView("I_" + i + " = 1 + I_" + (i - 1)));
// Note: it's important that this formula is constant with respect to "i", otherwise we'll spend a lot
// of time compiling formulas
longChain.add(prev.updateView("Current = 1 + Current"));
}
}
return longChain;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.deephaven.client.impl;

import com.google.common.annotations.VisibleForTesting;
import io.grpc.CallCredentials;
import io.grpc.CallOptions;
import io.grpc.Channel;
Expand All @@ -14,6 +15,7 @@

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Executor;

import static io.deephaven.client.impl.Authentication.AUTHORIZATION_HEADER;
Expand Down Expand Up @@ -56,6 +58,11 @@ public void setBearerToken(String bearerToken) {
}
}

@VisibleForTesting
UUID getCurrentToken() {
return UUID.fromString(bearerToken);
}

private void handleMetadata(Metadata metadata) {
parseBearerToken(metadata).ifPresent(BearerHandler.this::setBearerToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
*/
package io.deephaven.client.impl;

import com.google.common.annotations.VisibleForTesting;
import io.deephaven.proto.DeephavenChannel;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -22,6 +24,12 @@ public interface Session
@Override
void close();

/**
* Returns the current auth token.
*/
@VisibleForTesting
UUID getCurrentToken();
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved
nbauernfeind marked this conversation as resolved.
Show resolved Hide resolved

/**
* Closes the session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -132,6 +133,11 @@ private ExportStates newExportStates() {
return new ExportStates(this, bearerChannel.session(), bearerChannel.table(), exportTicketCreator);
}

@Override
public UUID getCurrentToken() {
return bearerHandler.getCurrentToken();
}

@Override
public TableService newStatefulTableService() {
return new TableServiceImpl(newExportStates());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dagger.BindsInstance;
import dagger.Component;
import io.deephaven.client.ClientDefaultsModule;
import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.context.TestExecutionContext;
import io.deephaven.engine.liveness.LivenessScope;
import io.deephaven.engine.liveness.LivenessScopeStack;
Expand All @@ -23,11 +24,13 @@
import io.deephaven.server.plugin.js.JsPluginNoopConsumerModule;
import io.deephaven.server.runner.scheduler.SchedulerDelegatingImplModule;
import io.deephaven.server.session.ObfuscatingErrorTransformerModule;
import io.deephaven.server.session.SessionState;
import io.deephaven.server.util.Scheduler;
import io.deephaven.util.SafeCloseable;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.testing.GrpcCleanupRule;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -39,6 +42,7 @@
import java.io.PrintStream;
import java.time.Duration;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -85,7 +89,8 @@ interface Builder {
@Rule
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();

private SafeCloseable executionContext;
private ExecutionContext executionContext;
private SafeCloseable executionContextCloseable;

private LogBuffer logBuffer;
private SafeCloseable scopeCloseable;
Expand Down Expand Up @@ -127,7 +132,8 @@ public void setUp() throws Exception {
.injectFields(this);

final PeriodicUpdateGraph updateGraph = server.getUpdateGraph().cast();
executionContext = TestExecutionContext.createForUnitTests().withUpdateGraph(updateGraph).open();
executionContext = TestExecutionContext.createForUnitTests().withUpdateGraph(updateGraph);
executionContextCloseable = executionContext.open();
if (updateGraph.isUnitTestModeAllowed()) {
updateGraph.enableUnitTestMode();
updateGraph.resetForUnitTests(false);
Expand All @@ -153,7 +159,7 @@ public void tearDown() throws Exception {
if (updateGraph.isUnitTestModeAllowed()) {
updateGraph.resetForUnitTests(true);
}
executionContext.close();
executionContextCloseable.close();

scheduler.shutdown();
}
Expand All @@ -170,6 +176,14 @@ public ScriptSession getScriptSession() {
return scriptSessionProvider.get();
}

public SessionState getSession(@NotNull final UUID token) {
return server.sessionService().getSessionForToken(token);
}

public ExecutionContext getExecutionContext() {
return executionContext;
}

/**
* The session token expiration
*
Expand Down
Loading