-
Notifications
You must be signed in to change notification settings - Fork 216
feat(connector): Add warning log for MV read permission fallback #1402
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
Draft
cjac
wants to merge
1
commit into
GoogleCloudDataproc:master
Choose a base branch
from
LLC-Technologies-Collier:feature/mv-permission-logging
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -38,3 +38,8 @@ AdAdHocITSuite.scala | |
|
||
# toolchains file | ||
toolchains.xml | ||
|
||
# Emacs | ||
*~ | ||
\#*\# | ||
.\#* |
This file contains hidden or 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 hidden or 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 hidden or 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
139 changes: 139 additions & 0 deletions
139
...ery/src/test/java/com/google/cloud/spark/bigquery/integration/MaterializedViewReadIT.java
This file contains hidden or 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,139 @@ | ||
/* | ||
* Copyright 2025 Google LLC and Contributors | ||
cjac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* Licensed 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 com.google.cloud.spark.bigquery.integration; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.bigquery.JobInfo; | ||
import com.google.cloud.bigquery.QueryJobConfiguration; | ||
import com.google.cloud.bigquery.TableId; | ||
import com.google.cloud.spark.bigquery.ReadSessionCreator; | ||
import com.google.cloud.spark.bigquery.connector.common.integration.IntegrationTestUtils; | ||
import com.google.cloud.spark.bigquery.connector.common.integration.SparkBigQueryIntegrationTestBase; | ||
import java.io.StringWriter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.SimpleLayout; | ||
import org.apache.log4j.WriterAppender; | ||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.Row; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class MaterializedViewReadIT extends SparkBigQueryIntegrationTestBase { | ||
|
||
private final Logger logger = Logger.getLogger(ReadSessionCreator.class); | ||
private WriterAppender appender; | ||
private StringWriter stringWriter; | ||
private String testDataset; | ||
private String mvName; | ||
private TableId sourceTableId; | ||
private String testServiceAccount; | ||
|
||
@Before | ||
public void setUp() throws InterruptedException { | ||
// Set up a custom log appender to capture logs | ||
stringWriter = new StringWriter(); | ||
appender = new WriterAppender(new SimpleLayout(), stringWriter); | ||
appender.setThreshold(Level.WARN); | ||
logger.addAppender(appender); | ||
|
||
// Create a dedicated service account for this test | ||
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
assertThat(projectId).isNotNull(); | ||
testServiceAccount = | ||
String.format( | ||
"mv-test-%s@%s.iam.gserviceaccount.com", | ||
UUID.randomUUID().toString().substring(0, 8), projectId); | ||
IntegrationTestUtils.createServiceAccount(testServiceAccount); | ||
|
||
// Create a temporary dataset and grant the new SA the BQ User role | ||
// This provides permissions to run jobs (for the fallback) but not to read table data directly | ||
testDataset = "mv_read_it_" + System.nanoTime(); | ||
IntegrationTestUtils.createDataset(testDataset); | ||
IntegrationTestUtils.grantServiceAccountRole( | ||
testServiceAccount, "roles/bigquery.user", testDataset); | ||
IntegrationTestUtils.grantServiceAccountRole( | ||
testServiceAccount, "roles/bigquery.metadataViewer", testDataset); | ||
|
||
// Create a source table | ||
sourceTableId = TableId.of(testDataset, "source_table_" + System.nanoTime()); | ||
IntegrationTestUtils.createTable(sourceTableId, "name:string, value:integer", "name,value"); | ||
|
||
// Create a Materialized View | ||
mvName = "test_mv_" + System.nanoTime(); | ||
String createMvSql = | ||
String.format( | ||
"CREATE MATERIALIZED VIEW `%s.%s` AS SELECT name, value FROM `%s.%s`", | ||
testDataset, mvName, testDataset, sourceTableId.getTable()); | ||
QueryJobConfiguration createMvJob = QueryJobConfiguration.newBuilder(createMvSql).build(); | ||
bigquery.create(JobInfo.of(createMvJob)).waitFor(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
logger.removeAppender(appender); | ||
try { | ||
if (testDataset != null) { | ||
IntegrationTestUtils.deleteDatasetAndTables(testDataset); | ||
} | ||
} finally { | ||
if (testServiceAccount != null) { | ||
IntegrationTestUtils.deleteServiceAccount(testServiceAccount); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadMaterializedView_lackingPermission_logsWarningAndFallsBack() { | ||
// This test confirms that when reading a Materialized View with a service account | ||
// that lacks `bigquery.tables.getData` permission, the connector: | ||
// 1. Logs a specific WARN message indicating the permission issue and the fallback. | ||
// 2. Successfully reads the data by falling back to materializing the view's query. | ||
|
||
// Arrange: Use the dedicated service account with insufficient permissions for a direct read. | ||
String mvToRead = testDataset + "." + mvName; | ||
|
||
// Act: Read the materialized view, impersonating the test service account | ||
Dataset<Row> df = | ||
spark | ||
.read() | ||
.format("bigquery") | ||
.option("viewsEnabled", "true") // Required to read any kind of view | ||
.option("impersonationServiceAccount", testServiceAccount) | ||
.load(mvToRead); | ||
|
||
List<Row> result = df.collectAsList(); | ||
|
||
// Assert | ||
// 1. Assert that the read was successful via fallback | ||
assertThat(result).hasSize(2); | ||
List<String> names = result.stream().map(row -> row.getString(0)).collect(Collectors.toList()); | ||
assertThat(names).containsExactlyElementsIn(Arrays.asList("name1", "name2")); | ||
|
||
// 2. Assert that the specific warning was logged | ||
String logOutput = stringWriter.toString(); | ||
assertThat(logOutput).contains("Failed to initiate a direct read from Materialized View"); | ||
assertThat(logOutput) | ||
.contains("The service account likely lacks 'bigquery.tables.getData' permission"); | ||
assertThat(logOutput).contains("Falling back to re-executing the view's underlying query"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.