NimzyMaina opened a new issue, #32509:
URL: https://github.com/apache/beam/issues/32509
### What happened?
The method used to check whether a table exists on spanner can in some
scenarios always return `false` as there is no ability to specify the
`table_catalog` and `table_schema`. In my case, these fields are always
populated in the `information_schema.tables` view.
The first time I run the application, it runs fine as it creates the
metadata table. The problem arrises when I restart the application & specify
the same metadata table. It tries to recreate the table but it already exists
resulting in a Spanner Exception.
```
Caused by: com.google.cloud.spanner.SpannerException: FAILED_PRECONDITION:
Operation with name
"projects/<project>/instances/<instance>/databases/<db>/operations/..." failed
with status = GrpcStatusCode{transportCode=FAILED_PRECONDITION} and message =
Duplicate name in schema: java_metadata_2.
```
[Link to tableExists
code](https://github.com/apache/beam/blob/75a463790d7912c475939f8c63d62ffba2002b0a/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dao/PartitionMetadataDao.java#L82)
```java
/**
* Checks whether the metadata table already exists in the database.
*
* @return true if the table exists, false if the table does not exist.
*/
public boolean tableExists() {
final String checkTableExistsStmt =
"SELECT t.table_name FROM information_schema.tables AS t "
+ "WHERE t.table_catalog = '' AND "
+ "t.table_schema = '' AND "
+ "t.table_name = '"
+ metadataTableName
+ "'";
try (ResultSet queryResultSet =
databaseClient
.singleUseReadOnlyTransaction()
.executeQuery(Statement.of(checkTableExistsStmt))) {
return queryResultSet.next();
}
}
```
[Link to Where it is
used](https://github.com/apache/beam/blob/75a463790d7912c475939f8c63d62ffba2002b0a/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dofn/InitializeDoFn.java#L66)
```java
@ProcessElement
public void processElement(OutputReceiver<PartitionMetadata> receiver) {
PartitionMetadataDao partitionMetadataDao =
daoFactory.getPartitionMetadataDao();
if (!partitionMetadataDao.tableExists()) { // <-- Fails at this point on
restart
daoFactory.getPartitionMetadataAdminDao().createPartitionMetadataTable();
createFakeParentPartition();
}
final PartitionMetadata initialPartition =
Optional.ofNullable(partitionMetadataDao.getPartition(InitialPartition.PARTITION_TOKEN))
.map(mapperFactory.partitionMetadataMapper()::from)
.orElseThrow(
() -> new IllegalStateException("Initial partition not found
in metadata table."));
receiver.output(initialPartition);
}
```
This results in a scenario where a change stream consumer **cannot recover**
from a restart.
## Suggested solution
```java
/**
* Checks whether the metadata table already exists in the database.
*
* @return true if the table exists, false if the table does not exist.
*/
public boolean tableExists() {
final String checkTableExistsStmt =
"SELECT t.table_name FROM information_schema.tables AS t "
+ "WHERE t.table_name = '" + metadataTableName + "'";
try (ResultSet queryResultSet =
databaseClient
.singleUseReadOnlyTransaction()
.executeQuery(Statement.of(checkTableExistsStmt))) {
return queryResultSet.next();
}
}
```
### Issue Priority
Priority: 1 (data loss / total loss of function)
### Issue Components
- [ ] Component: Python SDK
- [X] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [X] Component: IO connector
- [ ] Component: Beam YAML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Samza Runner
- [ ] Component: Twister2 Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]