Skip to content

Commit ef20f9e

Browse files
committed
chore: refactor based on pr comments
1 parent 4e9d6ae commit ef20f9e

7 files changed

Lines changed: 39 additions & 42 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowResultSet.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
2828
import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch;
2929
import com.google.cloud.bigquery.storage.v1.ArrowSchema;
30-
import io.opentelemetry.api.trace.Span;
31-
import io.opentelemetry.context.Context;
3230
import io.opentelemetry.context.Scope;
3331
import java.io.IOException;
3432
import java.math.BigDecimal;
@@ -239,32 +237,31 @@ public boolean next() throws SQLException {
239237
|| this.currentBatchRowIndex == (this.vectorSchemaRoot.getRowCount() - 1)) {
240238
/* Start of iteration or we have exhausted the current batch */
241239
// Advance the cursor. Potentially blocking operation.
242-
BigQueryArrowBatchWrapper batchWrapper;
243-
try (Scope scope = Context.current().with(Span.wrap(originalSpanContext)).makeCurrent()) {
244-
batchWrapper = this.buffer.take();
245-
}
246-
if (batchWrapper.getException() != null) {
247-
throw new BigQueryJdbcRuntimeException(batchWrapper.getException());
248-
}
249-
if (batchWrapper.isLast()) {
250-
/* Marks the end of the records */
251-
if (this.vectorSchemaRoot != null) {
252-
// IMP: To avoid memory leak: clear vectorSchemaRoot as it still holds
253-
// the last batch
254-
this.vectorSchemaRoot.clear();
240+
try (Scope scope = makeOriginalContextCurrent()) {
241+
BigQueryArrowBatchWrapper batchWrapper = this.buffer.take();
242+
if (batchWrapper.getException() != null) {
243+
throw new BigQueryJdbcRuntimeException(batchWrapper.getException());
244+
}
245+
if (batchWrapper.isLast()) {
246+
/* Marks the end of the records */
247+
if (this.vectorSchemaRoot != null) {
248+
// IMP: To avoid memory leak: clear vectorSchemaRoot as it still holds
249+
// the last batch
250+
this.vectorSchemaRoot.clear();
251+
}
252+
this.hasReachedEnd = true;
253+
this.rowCount++;
254+
return false;
255255
}
256-
this.hasReachedEnd = true;
256+
// Valid batch, process it
257+
ArrowRecordBatch arrowBatch = batchWrapper.getCurrentArrowBatch();
258+
// Populates vectorSchemaRoot
259+
this.arrowDeserializer.deserializeArrowBatch(arrowBatch);
260+
// Pointing to the first row in this fresh batch
261+
this.currentBatchRowIndex = 0;
257262
this.rowCount++;
258-
return false;
263+
return true;
259264
}
260-
// Valid batch, process it
261-
ArrowRecordBatch arrowBatch = batchWrapper.getCurrentArrowBatch();
262-
// Populates vectorSchemaRoot
263-
this.arrowDeserializer.deserializeArrowBatch(arrowBatch);
264-
// Pointing to the first row in this fresh batch
265-
this.currentBatchRowIndex = 0;
266-
this.rowCount++;
267-
return true;
268265
}
269266
// There are rows left in the current batch.
270267
else if (this.currentBatchRowIndex < this.vectorSchemaRoot.getRowCount()) {

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.google.cloud.bigquery.exception.BigQueryJdbcCoercionNotFoundException;
3030
import io.opentelemetry.api.trace.Span;
3131
import io.opentelemetry.api.trace.SpanContext;
32+
import io.opentelemetry.context.Context;
33+
import io.opentelemetry.context.Scope;
3234
import java.io.InputStream;
3335
import java.io.Reader;
3436
import java.io.StringReader;
@@ -72,6 +74,10 @@ protected BigQueryBaseResultSet(
7274
this.originalSpanContext = Span.current().getSpanContext();
7375
}
7476

77+
protected Scope makeOriginalContextCurrent() {
78+
return Context.current().with(Span.wrap(this.originalSpanContext)).makeCurrent();
79+
}
80+
7581
public QueryStatistics getQueryStatistics() {
7682
if (queryStatistics != null) {
7783
return queryStatistics;

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ private ResultSet getTablesImpl(
17451745
final String catalogParam = effectiveCatalog;
17461746
final String schemaParam = effectiveSchemaPattern;
17471747

1748-
Tracer tracer = BigQueryJdbcOpenTelemetry.getSafeTracer(this.connection);
1748+
Tracer tracer = this.connection.getTracer();
17491749
SpanContext parentSpanContext = Span.current().getSpanContext();
17501750
Runnable tableFetcher =
17511751
() -> {
@@ -2139,7 +2139,7 @@ private ResultSet getColumnsImpl(
21392139
final String catalogParam = effectiveCatalog;
21402140
final String schemaParam = effectiveSchemaPattern;
21412141

2142-
Tracer tracer = BigQueryJdbcOpenTelemetry.getSafeTracer(this.connection);
2142+
Tracer tracer = this.connection.getTracer();
21432143
SpanContext parentSpanContext = Span.current().getSpanContext();
21442144
Runnable columnFetcher =
21452145
() -> {
@@ -3699,7 +3699,7 @@ private ResultSet getSchemasImpl(String catalog, String schemaPattern) throws SQ
36993699
final List<FieldValueList> collectedResults = Collections.synchronizedList(new ArrayList<>());
37003700
final String catalogParam = catalog;
37013701

3702-
Tracer tracer = BigQueryJdbcOpenTelemetry.getSafeTracer(this.connection);
3702+
Tracer tracer = this.connection.getTracer();
37033703
SpanContext parentSpanContext = Span.current().getSpanContext();
37043704
Runnable schemaFetcher =
37053705
() -> {
@@ -5377,7 +5377,7 @@ private interface TracedMetadataOperation<T> {
53775377

53785378
private <T> T withTracing(String spanName, TracedMetadataOperation<T> operation)
53795379
throws SQLException {
5380-
Tracer tracer = BigQueryJdbcOpenTelemetry.getSafeTracer(this.connection);
5380+
Tracer tracer = this.connection.getTracer();
53815381
Span span = tracer.spanBuilder(spanName).startSpan();
53825382
try (Scope scope = span.makeCurrent()) {
53835383
return operation.run();

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOpenTelemetry.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,4 @@ public static OpenTelemetry getOpenTelemetry(
4949
public static Tracer getTracer(OpenTelemetry openTelemetry) {
5050
return openTelemetry.getTracer(INSTRUMENTATION_SCOPE_NAME);
5151
}
52-
53-
/** Gets a Tracer for the JDBC driver, fallback to noop if connection has no tracer. */
54-
public static Tracer getSafeTracer(BigQueryConnection connection) {
55-
if (connection != null && connection.getTracer() != null) {
56-
return connection.getTracer();
57-
}
58-
return getOpenTelemetry(false, false, null).getTracer(INSTRUMENTATION_SCOPE_NAME);
59-
}
6052
}

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJsonResultSet.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import com.google.cloud.bigquery.FieldValue.Attribute;
2626
import com.google.cloud.bigquery.Schema;
2727
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
28-
import io.opentelemetry.api.trace.Span;
29-
import io.opentelemetry.context.Context;
3028
import io.opentelemetry.context.Scope;
3129
import java.sql.ResultSet;
3230
import java.sql.SQLException;
@@ -161,7 +159,7 @@ public boolean next() throws SQLException {
161159
}
162160
try {
163161
// Advance the cursor,Potentially blocking operation
164-
try (Scope scope = Context.current().with(Span.wrap(originalSpanContext)).makeCurrent()) {
162+
try (Scope scope = makeOriginalContextCurrent()) {
165163
this.cursor = this.buffer.take();
166164
}
167165
if (this.cursor.getException() != null) {

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ private interface TracedOperation<T> {
15421542
}
15431543

15441544
private <T> T withTracing(String spanName, TracedOperation<T> operation) throws SQLException {
1545-
Tracer tracer = BigQueryJdbcOpenTelemetry.getSafeTracer(this.connection);
1545+
Tracer tracer = this.connection.getTracer();
15461546
Span span = tracer.spanBuilder(spanName).startSpan();
15471547
try (Scope scope = span.makeCurrent()) {
15481548
return operation.run(span);
@@ -1561,7 +1561,7 @@ private void fetchNextPages(
15611561
BlockingQueue<Tuple<TableResult, Boolean>> rpcResponseQueue,
15621562
BlockingQueue<BigQueryFieldValueListWrapper> bigQueryFieldValueListWrapperBlockingQueue,
15631563
TableResult result) {
1564-
Tracer tracer = BigQueryJdbcOpenTelemetry.getSafeTracer(this.connection);
1564+
Tracer tracer = this.connection.getTracer();
15651565
SpanContext parentSpanContext = Span.current().getSpanContext();
15661566
String currentPageToken = firstPageToken;
15671567
TableResult currentResults = result;

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryStatementTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.google.cloud.bigquery.storage.v1.ReadSession;
5353
import com.google.common.collect.ImmutableList;
5454
import com.google.common.collect.Maps;
55+
import io.opentelemetry.api.OpenTelemetry;
5556
import java.io.IOException;
5657
import java.sql.ResultSet;
5758
import java.sql.SQLException;
@@ -127,6 +128,9 @@ private Job getJobMock(
127128
@BeforeEach
128129
public void setUp() throws IOException, SQLException {
129130
bigQueryConnection = mock(BigQueryConnection.class);
131+
doReturn(OpenTelemetry.noop().getTracer(BigQueryJdbcOpenTelemetry.INSTRUMENTATION_SCOPE_NAME))
132+
.when(bigQueryConnection)
133+
.getTracer();
130134
rpcFactoryMock = mock(BigQueryRpcFactory.class);
131135
bigquery = mock(BigQuery.class);
132136
bigQueryConnection.bigQuery = bigquery;

0 commit comments

Comments
 (0)