scwhittle commented on code in PR #32689:
URL: https://github.com/apache/beam/pull/32689#discussion_r1806481969
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dao/PartitionMetadataAdminDao.java:
##########
@@ -129,7 +123,7 @@ public void createPartitionMetadataTable() {
// Literals need be added around literals to preserve casing.
ddl.add(
"CREATE TABLE \""
- + tableName
+ + names.getTableName()
Review Comment:
does this method fail if the tables/indexes already exist?
IIUC the issue correctly this is failing currently because the index has the
same name across pipelines and the creation is failing. Do we also have to
worry about a single pipeline failing if it created the tables but Dataflow
retries the work? It seems we might want ot use create if it doesn't exist.
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dao/PartitionMetadataTableNamesTest.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.gcp.spanner.changestreams.dao;
+
+import static
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataTableNames.MAX_NAME_LENGTH;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class PartitionMetadataTableNamesTest {
+ @Test
+ public void testGeneratePartitionMetadataNamesRemovesHyphens() {
+ PartitionMetadataTableNames names =
+ PartitionMetadataTableNames.generateRandom("my-database-id-12345");
Review Comment:
generate twice and verify the names are different?
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dao/PartitionMetadataTableNamesTest.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.gcp.spanner.changestreams.dao;
+
+import static
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataTableNames.MAX_NAME_LENGTH;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class PartitionMetadataTableNamesTest {
+ @Test
+ public void testGeneratePartitionMetadataNamesRemovesHyphens() {
+ PartitionMetadataTableNames names =
+ PartitionMetadataTableNames.generateRandom("my-database-id-12345");
+ assertFalse(names.getTableName().contains("-"));
+ assertFalse(names.getWatermarkIndexName().contains("-"));
+ assertFalse(names.getCreatedAtIndexName().contains("-"));
+ }
+
+ @Test
+ public void testGeneratePartitionMetadataNamesIsShorterThan64Characters() {
+ PartitionMetadataTableNames names =
+ PartitionMetadataTableNames.generateRandom(
+
"my-database-id-larger-than-maximum-length-1234567890-1234567890-1234567890");
+ assertTrue(names.getTableName().length() <= MAX_NAME_LENGTH);
+ assertTrue(names.getWatermarkIndexName().length() <= MAX_NAME_LENGTH);
+ assertTrue(names.getCreatedAtIndexName().length() <= MAX_NAME_LENGTH);
+
+ names = PartitionMetadataTableNames.generateRandom("d");
+ assertTrue(names.getTableName().length() <= MAX_NAME_LENGTH);
+ assertTrue(names.getWatermarkIndexName().length() <= MAX_NAME_LENGTH);
+ assertTrue(names.getCreatedAtIndexName().length() <= MAX_NAME_LENGTH);
+ }
+
+ @Test
+ public void testPartitionMetadataNamesFromExistingTable() {
+ PartitionMetadataTableNames names =
+ PartitionMetadataTableNames.fromExistingTable("databaseid", "mytable");
+
+ assertEquals("mytable", names.getTableName());
+ assertFalse(names.getWatermarkIndexName().contains("-"));
Review Comment:
generate names from existing table twice and verify the indexes are
different?
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/dao/PartitionMetadataTableNames.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.gcp.spanner.changestreams.dao;
+
+import java.io.Serializable;
+import java.util.Objects;
+import java.util.UUID;
+import javax.annotation.Nullable;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
+
+/**
+ * Configuration for a partition metadata table. It encapsulates the name of
the metadata table and
+ * indexes.
+ */
+public class PartitionMetadataTableNames implements Serializable {
+
+ private static final long serialVersionUID = 8848098877671834584L;
+
+ /** PostgreSQL max table and index length is 63 bytes. */
+ @VisibleForTesting static final int MAX_NAME_LENGTH = 63;
+
+ private static final String PARTITION_METADATA_TABLE_NAME_FORMAT =
"Metadata_%s_%s";
+ private static final String WATERMARK_INDEX_NAME_FORMAT =
"WatermarkIdx_%s_%s";
+ private static final String CREATED_AT_START_TIMESTAMP_INDEX_NAME_FORMAT =
"CreatedAtIdx_%s_%s";
+
+ /**
+ * Generates a unique name for the partition metadata table and its indexes.
The table name will
+ * be in the form of {@code "Metadata_<databaseId>_<uuid>"}. The watermark
index will be in the
+ * form of {@code "WatermarkIdx_<databaseId>_<uuid>}. The createdAt / start
timestamp index will
+ * be in the form of {@code "CreatedAtIdx_<databaseId>_<uuid>}.
+ *
+ * @param databaseId The database id where the table will be created
+ * @return the unique generated names of the partition metadata ddl
+ */
+ public static PartitionMetadataTableNames generateRandom(String databaseId) {
+ UUID uuid = UUID.randomUUID();
+
+ String table = generateName(PARTITION_METADATA_TABLE_NAME_FORMAT,
databaseId, uuid);
+ String watermarkIndex = generateName(WATERMARK_INDEX_NAME_FORMAT,
databaseId, uuid);
+ String createdAtIndex =
+ generateName(CREATED_AT_START_TIMESTAMP_INDEX_NAME_FORMAT, databaseId,
uuid);
+
+ return new PartitionMetadataTableNames(table, watermarkIndex,
createdAtIndex);
+ }
+
+ /**
+ * Encapsulates an existing table name. Index names are generated, but will
only be used if the
+ * given table does not exist. The watermark index will be in the form of
{@code
Review Comment:
"existing table name and" "if the given table does not exist" is a little
confusing.
Maybe instead of existing table name it should be something like "selected
table name", and that table may or may not exist.
--
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]