Skip to content

Commit

Permalink
[BEAM-14405] Fix NPE when ProjectID is not specified in a template ex…
Browse files Browse the repository at this point in the history
…ecution

PR apache#16547 added handling for null ProjectIDs on command line execution,
but did not handle the possibility of null ProjectIDs in template
execution. PR apache#17094 added nullness checks in MonitoringInfoMetricNames,
which then triggered NPEs if the ProjectID was not specified during a
template execution.
  • Loading branch information
nielm committed May 4, 2022
1 parent 0daef62 commit c6962d4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public void setup() throws Exception {
spannerAccessor = SpannerAccessor.getOrCreate(config);
projectId =
this.config.getProjectId() == null
|| this.config.getProjectId().get() == null
|| this.config.getProjectId().get().isEmpty()
? SpannerOptions.getDefaultProjectId()
: this.config.getProjectId().get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,8 @@ public void setup() {

projectId =
this.spannerConfig.getProjectId() == null
|| this.spannerConfig.getProjectId().get() == null
|| this.spannerConfig.getProjectId().get().isEmpty()
? SpannerOptions.getDefaultProjectId()
: this.spannerConfig.getProjectId().get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ public void runReadTestWithDefaultProject() throws Exception {
.withServiceFactory(serviceFactory));
}

@Test
public void runReadTestWithNullProject() throws Exception {
runReadTest(
SpannerConfig.create()
.withProjectId((String) null)
.withInstanceId("123")
.withDatabaseId("aaa")
.withServiceFactory(serviceFactory));
}

private void runReadTest(SpannerConfig spannerConfig) throws Exception {
Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,22 @@ public void singleMutationPipelineNoProjectId() throws Exception {
verifyBatches(batch(m(2L)));
}

@Test
public void singleMutationPipelineNullProjectId() throws Exception {
Mutation mutation = m(2L);
PCollection<Mutation> mutations = pipeline.apply(Create.of(mutation));

mutations.apply(
SpannerIO.write()
.withProjectId((String) null)
.withInstanceId("test-instance")
.withDatabaseId("test-database")
.withServiceFactory(serviceFactory));
pipeline.run();

verifyBatches(batch(m(2L)));
}

@Test
public void singleMutationGroupPipeline() throws Exception {
PCollection<MutationGroup> mutations =
Expand Down

0 comments on commit c6962d4

Please sign in to comment.