From 3ed73256d592769d6042b24101801a92914d6b85 Mon Sep 17 00:00:00 2001 From: Olav Loite Date: Tue, 27 Aug 2019 15:38:04 +0200 Subject: [PATCH] remove tracing implementation from tests OpenCensus added a finalizer that checks that all spans have been closed in the latest version. Using tracer implementations in combination with tests therefore spams the logs with a lot of warnings, as much test code is deliberately only executing part of for example a transaction. --- .../google-cloud-spanner/pom.xml | 6 -- .../com/google/cloud/spanner/SessionPool.java | 9 +++ .../google/cloud/spanner/SessionPoolTest.java | 57 +++---------------- 3 files changed, 17 insertions(+), 55 deletions(-) diff --git a/google-cloud-clients/google-cloud-spanner/pom.xml b/google-cloud-clients/google-cloud-spanner/pom.xml index 947899622b76..3aa8603e565f 100644 --- a/google-cloud-clients/google-cloud-spanner/pom.xml +++ b/google-cloud-clients/google-cloud-spanner/pom.xml @@ -145,12 +145,6 @@ testlib test - - io.opencensus - opencensus-impl - ${opencensus.version} - runtime - io.opencensus opencensus-contrib-grpc-util diff --git a/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java b/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java index c4ac70e17c15..343fba8f1c68 100644 --- a/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java +++ b/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java @@ -49,6 +49,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -838,6 +839,7 @@ private PooledSession take() throws SpannerException { SessionOrError s = pollUninterruptiblyWithTimeout(currentTimeout); if (s == null) { // Set the status to DEADLINE_EXCEEDED and retry. + numWaiterTimeouts.incrementAndGet(); tracer.getCurrentSpan().setStatus(Status.DEADLINE_EXCEEDED); currentTimeout = Math.min(currentTimeout * 2, MAX_SESSION_WAIT_TIMEOUT); } else { @@ -1065,6 +1067,8 @@ private void replenishPool() { @GuardedBy("lock") private int maxSessionsInUse = 0; + private AtomicLong numWaiterTimeouts = new AtomicLong(); + @GuardedBy("lock") private final Set allSessions = new HashSet<>(); @@ -1123,6 +1127,11 @@ int getNumberOfAvailableWritePreparedSessions() { return writePreparedSessions.size(); } + @VisibleForTesting + long getNumWaiterTimeouts() { + return numWaiterTimeouts.get(); + } + private void initPool() { synchronized (lock) { poolMaintainer.init(); diff --git a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java index 353de74c109d..10320218c504 100644 --- a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java +++ b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java @@ -39,6 +39,7 @@ import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.cloud.spanner.spi.v1.SpannerRpc.ResultStreamConsumer; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; import com.google.common.util.concurrent.Uninterruptibles; import com.google.protobuf.ByteString; import com.google.spanner.v1.CommitRequest; @@ -46,15 +47,6 @@ import com.google.spanner.v1.ExecuteSqlRequest; import com.google.spanner.v1.ResultSetStats; import com.google.spanner.v1.RollbackRequest; -import io.opencensus.trace.Span; -import io.opencensus.trace.Status; -import io.opencensus.trace.Tracer; -import io.opencensus.trace.Tracing; -import io.opencensus.trace.config.TraceConfig; -import io.opencensus.trace.config.TraceParams; -import io.opencensus.trace.export.SpanData; -import io.opencensus.trace.export.SpanExporter.Handler; -import io.opencensus.trace.samplers.Samplers; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -581,32 +573,6 @@ public void keepAlive() throws Exception { @Test public void blockAndTimeoutOnPoolExhaustion() throws InterruptedException, ExecutionException { - if (minSessions != 0) { - // Only execute for minSessions == 0 as we need to setup and shutdown a mock sampler during - // the test case. A second run would not work as there is no way to restart the sampler. - return; - } - // Setup a dummy trace handler. - final AtomicInteger deadlineExceededCount = new AtomicInteger(); - Handler handler = - new Handler() { - @Override - public void export(Collection spanDataList) { - for (SpanData sd : spanDataList) { - if (sd.getStatus() == Status.DEADLINE_EXCEEDED - && sd.getName().equals(SessionPool.WAIT_FOR_SESSION)) { - deadlineExceededCount.incrementAndGet(); - } - } - } - }; - Tracing.getExportComponent() - .getSpanExporter() - .registerHandler(SessionPoolTest.class.getName(), handler); - TraceConfig traceConfig = Tracing.getTraceConfig(); - TraceParams activeTraceParams = traceConfig.getActiveTraceParams(); - traceConfig.updateActiveTraceParams( - activeTraceParams.toBuilder().setSampler(Samplers.alwaysSample()).build()); // Create a session pool with max 1 session and a low timeout for waiting for a session. options = SessionPoolOptions.newBuilder() @@ -618,20 +584,22 @@ public void export(Collection spanDataList) { when(client.createSession(db)).thenReturn(mockSession); pool = createPool(); - Tracer tracer = Tracing.getTracer(); - Span span = tracer.spanBuilder("RunTest").startSpan(); // Try to take a read or a read/write session. These requests should block. for (Boolean write : new Boolean[] {true, false}) { // Take the only session that can be in the pool. Session checkedOutSession = pool.getReadSession(); final Boolean finWrite = write; ExecutorService executor = Executors.newFixedThreadPool(1); + // Setup a flag that will indicate when the thread will start waiting for a session to prevent + // flaky fails if it takes some time before the thread is started. + final SettableFuture waitingForSession = SettableFuture.create(); Future fut = executor.submit( new Callable() { @Override public Void call() throws Exception { Session session; + waitingForSession.set(Boolean.TRUE); if (finWrite) { session = pool.getReadWriteSession(); } else { @@ -642,6 +610,8 @@ public Void call() throws Exception { } }); try { + // Wait until the background thread is actually waiting for a session. + waitingForSession.get(); fut.get(80L, TimeUnit.MILLISECONDS); fail("missing expected timeout exception"); } catch (TimeoutException e) { @@ -657,18 +627,7 @@ public Void call() throws Exception { Session session = pool.getReadSession(); assertThat(session).isNotNull(); session.close(); - span.end(); - Tracing.getExportComponent().shutdown(); - // Verify that we got a DEADLINE_EXCEEDED span for both read and read/write session. - // There might be more than 1 for each request, depending on the execution speed of - // the environment. - if (!isNoopExportComponent()) { - assertThat(deadlineExceededCount.get()).isAtLeast(2); - } - } - - private boolean isNoopExportComponent() { - return Tracing.getExportComponent().getClass().getName().contains("Noop"); + assertThat(pool.getNumWaiterTimeouts()).isAtLeast(2L); } @Test