You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "frankgh (via GitHub)" <gi...@apache.org> on 2023/09/07 12:20:37 UTC
[GitHub] [cassandra] frankgh opened a new pull request, #2675: CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc…
frankgh opened a new pull request, #2675:
URL: https://github.com/apache/cassandra/pull/2675
…hema.instance object
In this commit the `org.apache.cassandra.io.sstable.CQLSSTableWriter#build` method synchronizes on the `Schema.instance` object (instead of the `CQLSSTableWriter.class`) to prevent concurrent schema operations to fail when the offline tool also updates the schema.
For example, a table creation operation, which modifies the keyspaces tables metadata, might end up missing the update when a concurrent call to the `CQLSSTableWriter#build` method is accessing the singleton Schema instance.
In 5.0, the synchronization on Schema.instance is no longer needed. This is only an issue in the 4.0 branch
[CASSANDRA-18317](https://issues.apache.org/jira/browse/CASSANDRA-18317)
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc… [cassandra]
Posted by "frankgh (via GitHub)" <gi...@apache.org>.
frankgh commented on PR #2675:
URL: https://github.com/apache/cassandra/pull/2675#issuecomment-1783369138
Closed via https://github.com/apache/cassandra/commit/26c374da4f03e4a6b64e414805cd92f3eb0a36c6
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc… [cassandra]
Posted by "frankgh (via GitHub)" <gi...@apache.org>.
frankgh commented on code in PR #2675:
URL: https://github.com/apache/cassandra/pull/2675#discussion_r1350657297
##########
test/unit/org/apache/cassandra/io/sstable/CQLSSTableWriterConcurrencyTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.cassandra.io.sstable;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.dht.Murmur3Partitioner;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.schema.Schema;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests modifications to the Schema in the {@link CQLSSTableWriter} class while other Schema modifications are
+ * occurring concurrently
+ */
+public class CQLSSTableWriterConcurrencyTest extends CQLTester
+{
+ private static final Logger logger = LoggerFactory.getLogger(CQLSSTableWriterTest.class);
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpClass()
+ {
+ CQLTester.setUpClass();
+ }
+
+ @Test
+ public void testConcurrentSchemaModification() throws InterruptedException, IOException
+ {
+ String schema = "CREATE TABLE %s ("
+ + " k int PRIMARY KEY,"
+ + " v1 text,"
+ + " v2 int"
+ + ")";
+
+ int nThreads = 20;
+ ExecutorService pool = Executors.newFixedThreadPool(nThreads);
+ CountDownLatch latch = new CountDownLatch(nThreads);
+ AtomicInteger errorCount = new AtomicInteger();
+
+ // Prepare all the variables required for the test
+ String[] tableNames = new String[nThreads];
+ String[] fullQueries = new String[nThreads];
+ String[] insertStatements = new String[nThreads];
+ File[] dataDirs = new File[nThreads];
+ String baseDataDir = tempFolder.newFolder().getAbsolutePath();
+
+ for (int i = 0; i < nThreads; i++)
+ {
+ tableNames[i] = String.format("table_%02d", i);
+ fullQueries[i] = String.format(schema, KEYSPACE + '.' + tableNames[i]);
+ logger.info(fullQueries[i]);
+
+ if (i % 2 != 0)
+ {
+ // dataDir and insert statement are only needed for the CQLSSTableWriter class
+ dataDirs[i] = new File(Paths.get(baseDataDir, KEYSPACE, tableNames[i]));
+ assert dataDirs[i].tryCreateDirectories();
+ insertStatements[i] = String.format("INSERT INTO %s.%s (k, v1, v2) VALUES (?, ?, ?)", KEYSPACE, tableNames[i]);
+ }
+
+ final int finalI = i;
+ pool.submit(() -> {
+ try
+ {
+ latch.countDown();
+ latch.await();
+
+ // Invoke all schema modifications roughly at the same time
+ if (finalI % 2 == 0)
+ {
+ schemaChange(fullQueries[finalI]);
+ // If another thread modified the Schema without the proper synchronization, it's possible
+ // that the table metadata was swapped out and calling the Keyspace#getColumnFamilyStore
+ // method will produce an IllegalArgumentException
+ Schema.instance.getKeyspaceInstance(KEYSPACE).getColumnFamilyStore(tableNames[finalI]);
+ }
+ else
+ {
+ CQLSSTableWriter.builder()
+ .inDirectory(dataDirs[finalI])
+ .forTable(fullQueries[finalI])
+ .withPartitioner(Murmur3Partitioner.instance)
+ .using(insertStatements[finalI])
+ .build();
+ }
+ }
+ catch (Throwable throwable)
+ {
+ logger.error("Error while processing element number {}", finalI, throwable);
+ errorCount.incrementAndGet();
+ }
+ });
+ }
+
+ pool.shutdown();
+ assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
Review Comment:
pushed suggestion to all branches
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc… [cassandra]
Posted by "Maxwell-Guo (via GitHub)" <gi...@apache.org>.
Maxwell-Guo commented on code in PR #2675:
URL: https://github.com/apache/cassandra/pull/2675#discussion_r1351359139
##########
test/unit/org/apache/cassandra/io/sstable/CQLSSTableWriterConcurrencyTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.cassandra.io.sstable;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.dht.Murmur3Partitioner;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.schema.Schema;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests modifications to the Schema in the {@link CQLSSTableWriter} class while other Schema modifications are
+ * occurring concurrently
+ */
+public class CQLSSTableWriterConcurrencyTest extends CQLTester
+{
+ private static final Logger logger = LoggerFactory.getLogger(CQLSSTableWriterTest.class);
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpClass()
+ {
+ CQLTester.setUpClass();
+ }
+
+ @Test
+ public void testConcurrentSchemaModification() throws InterruptedException, IOException
+ {
+ String schema = "CREATE TABLE %s ("
+ + " k int PRIMARY KEY,"
+ + " v1 text,"
+ + " v2 int"
+ + ")";
+
+ int nThreads = 20;
+ ExecutorService pool = Executors.newFixedThreadPool(nThreads);
+ CountDownLatch latch = new CountDownLatch(nThreads);
+ AtomicInteger errorCount = new AtomicInteger();
+
+ // Prepare all the variables required for the test
+ String[] tableNames = new String[nThreads];
+ String[] fullQueries = new String[nThreads];
+ String[] insertStatements = new String[nThreads];
+ File[] dataDirs = new File[nThreads];
+ String baseDataDir = tempFolder.newFolder().getAbsolutePath();
+
+ for (int i = 0; i < nThreads; i++)
+ {
+ tableNames[i] = String.format("table_%02d", i);
+ fullQueries[i] = String.format(schema, KEYSPACE + '.' + tableNames[i]);
+ logger.info(fullQueries[i]);
+
+ if (i % 2 != 0)
+ {
+ // dataDir and insert statement are only needed for the CQLSSTableWriter class
+ dataDirs[i] = new File(Paths.get(baseDataDir, KEYSPACE, tableNames[i]));
+ assert dataDirs[i].tryCreateDirectories();
+ insertStatements[i] = String.format("INSERT INTO %s.%s (k, v1, v2) VALUES (?, ?, ?)", KEYSPACE, tableNames[i]);
+ }
+
+ final int finalI = i;
+ pool.submit(() -> {
+ try
+ {
+ latch.countDown();
+ latch.await();
+
+ // Invoke all schema modifications roughly at the same time
+ if (finalI % 2 == 0)
+ {
+ schemaChange(fullQueries[finalI]);
+ // If another thread modified the Schema without the proper synchronization, it's possible
+ // that the table metadata was swapped out and calling the Keyspace#getColumnFamilyStore
+ // method will produce an IllegalArgumentException
+ Schema.instance.getKeyspaceInstance(KEYSPACE).getColumnFamilyStore(tableNames[finalI]);
+ }
+ else
+ {
+ CQLSSTableWriter.builder()
+ .inDirectory(dataDirs[finalI])
+ .forTable(fullQueries[finalI])
+ .withPartitioner(Murmur3Partitioner.instance)
+ .using(insertStatements[finalI])
+ .build();
+ }
+ }
+ catch (Throwable throwable)
+ {
+ logger.error("Error while processing element number {}", finalI, throwable);
+ errorCount.incrementAndGet();
+ }
+ });
+ }
+
+ pool.shutdown();
+ assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
Review Comment:
+1
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc… [cassandra]
Posted by "frankgh (via GitHub)" <gi...@apache.org>.
frankgh closed pull request #2675: CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc…
URL: https://github.com/apache/cassandra/pull/2675
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
[GitHub] [cassandra] frankgh commented on a diff in pull request #2675: CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc…
Posted by "frankgh (via GitHub)" <gi...@apache.org>.
frankgh commented on code in PR #2675:
URL: https://github.com/apache/cassandra/pull/2675#discussion_r1318835446
##########
test/unit/org/apache/cassandra/io/sstable/CQLSSTableWriterConcurrencyTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.cassandra.io.sstable;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.dht.Murmur3Partitioner;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.schema.Schema;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests modifications to the Schema in the {@link CQLSSTableWriter} class while other Schema modifications are
+ * occurring concurrently
+ */
+public class CQLSSTableWriterConcurrencyTest extends CQLTester
+{
+ private static final Logger logger = LoggerFactory.getLogger(CQLSSTableWriterTest.class);
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpClass()
+ {
+ CQLTester.setUpClass();
+ }
+
+ @Test
+ public void testConcurrentSchemaModification() throws InterruptedException, IOException
+ {
+ String schema = "CREATE TABLE %s ("
+ + " k int PRIMARY KEY,"
+ + " v1 text,"
+ + " v2 int"
+ + ")";
+
+ int nThreads = 20;
+ ExecutorService pool = Executors.newFixedThreadPool(nThreads);
+ CountDownLatch latch = new CountDownLatch(nThreads);
+ AtomicInteger errorCount = new AtomicInteger();
+
+ // Prepare all the variables required for the test
+ String[] tableNames = new String[nThreads];
+ String[] fullQueries = new String[nThreads];
+ String[] insertStatements = new String[nThreads];
+ File[] dataDirs = new File[nThreads];
+ String baseDataDir = tempFolder.newFolder().getAbsolutePath();
+
+ for (int i = 0; i < nThreads; i++)
+ {
+ tableNames[i] = String.format("table_%02d", i);
+ fullQueries[i] = String.format(schema, KEYSPACE + '.' + tableNames[i]);
+ logger.info(fullQueries[i]);
+
+ if (i % 2 != 0)
+ {
+ // dataDir and insert statement are only needed for the CQLSSTableWriter class
+ dataDirs[i] = new File(Paths.get(baseDataDir, KEYSPACE, tableNames[i]));
+ assert dataDirs[i].tryCreateDirectories();
+ insertStatements[i] = String.format("INSERT INTO %s.%s (k, v1, v2) VALUES (?, ?, ?)", KEYSPACE, tableNames[i]);
+ }
+
+ final int finalI = i;
+ pool.submit(() -> {
+ try
+ {
+ latch.countDown();
+ latch.await();
+
+ // Invoke all schema modifications roughly at the same time
+ if (finalI % 2 == 0)
+ {
+ schemaChange(fullQueries[finalI]);
+ // If another thread modified the Schema without the proper synchronization, it's possible
+ // that the table metadata was swapped out and calling the Keyspace#getColumnFamilyStore
+ // method will produce an IllegalArgumentException
+ Schema.instance.getKeyspaceInstance(KEYSPACE).getColumnFamilyStore(tableNames[finalI]);
+ }
+ else
+ {
+ CQLSSTableWriter.builder()
+ .inDirectory(dataDirs[finalI])
+ .forTable(fullQueries[finalI])
+ .withPartitioner(Murmur3Partitioner.instance)
+ .using(insertStatements[finalI])
+ .build();
+ }
+ }
+ catch (Throwable throwable)
+ {
+ logger.error("Error while processing element number {}", finalI, throwable);
+ errorCount.incrementAndGet();
+ }
+ });
+ }
+
+ pool.shutdown();
+ assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
Review Comment:
a GC in the test? the test will fail. What is an acceptable value?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc… [cassandra]
Posted by "yifan-c (via GitHub)" <gi...@apache.org>.
yifan-c commented on code in PR #2675:
URL: https://github.com/apache/cassandra/pull/2675#discussion_r1350612880
##########
test/unit/org/apache/cassandra/io/sstable/CQLSSTableWriterConcurrencyTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.cassandra.io.sstable;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.dht.Murmur3Partitioner;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.schema.Schema;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests modifications to the Schema in the {@link CQLSSTableWriter} class while other Schema modifications are
+ * occurring concurrently
+ */
+public class CQLSSTableWriterConcurrencyTest extends CQLTester
+{
+ private static final Logger logger = LoggerFactory.getLogger(CQLSSTableWriterTest.class);
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpClass()
+ {
+ CQLTester.setUpClass();
+ }
+
+ @Test
+ public void testConcurrentSchemaModification() throws InterruptedException, IOException
+ {
+ String schema = "CREATE TABLE %s ("
+ + " k int PRIMARY KEY,"
+ + " v1 text,"
+ + " v2 int"
+ + ")";
+
+ int nThreads = 20;
+ ExecutorService pool = Executors.newFixedThreadPool(nThreads);
+ CountDownLatch latch = new CountDownLatch(nThreads);
+ AtomicInteger errorCount = new AtomicInteger();
+
+ // Prepare all the variables required for the test
+ String[] tableNames = new String[nThreads];
+ String[] fullQueries = new String[nThreads];
+ String[] insertStatements = new String[nThreads];
+ File[] dataDirs = new File[nThreads];
+ String baseDataDir = tempFolder.newFolder().getAbsolutePath();
+
+ for (int i = 0; i < nThreads; i++)
+ {
+ tableNames[i] = String.format("table_%02d", i);
+ fullQueries[i] = String.format(schema, KEYSPACE + '.' + tableNames[i]);
+ logger.info(fullQueries[i]);
+
+ if (i % 2 != 0)
+ {
+ // dataDir and insert statement are only needed for the CQLSSTableWriter class
+ dataDirs[i] = new File(Paths.get(baseDataDir, KEYSPACE, tableNames[i]));
+ assert dataDirs[i].tryCreateDirectories();
+ insertStatements[i] = String.format("INSERT INTO %s.%s (k, v1, v2) VALUES (?, ?, ?)", KEYSPACE, tableNames[i]);
+ }
+
+ final int finalI = i;
+ pool.submit(() -> {
+ try
+ {
+ latch.countDown();
+ latch.await();
+
+ // Invoke all schema modifications roughly at the same time
+ if (finalI % 2 == 0)
+ {
+ schemaChange(fullQueries[finalI]);
+ // If another thread modified the Schema without the proper synchronization, it's possible
+ // that the table metadata was swapped out and calling the Keyspace#getColumnFamilyStore
+ // method will produce an IllegalArgumentException
+ Schema.instance.getKeyspaceInstance(KEYSPACE).getColumnFamilyStore(tableNames[finalI]);
+ }
+ else
+ {
+ CQLSSTableWriter.builder()
+ .inDirectory(dataDirs[finalI])
+ .forTable(fullQueries[finalI])
+ .withPartitioner(Murmur3Partitioner.instance)
+ .using(insertStatements[finalI])
+ .build();
+ }
+ }
+ catch (Throwable throwable)
+ {
+ logger.error("Error while processing element number {}", finalI, throwable);
+ errorCount.incrementAndGet();
+ }
+ });
+ }
+
+ pool.shutdown();
+ assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
Review Comment:
Timeout of closing the executor pool may not qualify as a failure condition of the test. How about logging a warning message and let it go?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
[GitHub] [cassandra] Maxwell-Guo commented on a diff in pull request #2675: CASSANDRA-18317 Properly synchronize CQLSSTableWriter#build on the Sc…
Posted by "Maxwell-Guo (via GitHub)" <gi...@apache.org>.
Maxwell-Guo commented on code in PR #2675:
URL: https://github.com/apache/cassandra/pull/2675#discussion_r1318715458
##########
test/unit/org/apache/cassandra/io/sstable/CQLSSTableWriterConcurrencyTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.cassandra.io.sstable;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.dht.Murmur3Partitioner;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.schema.Schema;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests modifications to the Schema in the {@link CQLSSTableWriter} class while other Schema modifications are
+ * occurring concurrently
+ */
+public class CQLSSTableWriterConcurrencyTest extends CQLTester
+{
+ private static final Logger logger = LoggerFactory.getLogger(CQLSSTableWriterTest.class);
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @BeforeClass
+ public static void setUpClass()
+ {
+ CQLTester.setUpClass();
+ }
+
+ @Test
+ public void testConcurrentSchemaModification() throws InterruptedException, IOException
+ {
+ String schema = "CREATE TABLE %s ("
+ + " k int PRIMARY KEY,"
+ + " v1 text,"
+ + " v2 int"
+ + ")";
+
+ int nThreads = 20;
+ ExecutorService pool = Executors.newFixedThreadPool(nThreads);
+ CountDownLatch latch = new CountDownLatch(nThreads);
+ AtomicInteger errorCount = new AtomicInteger();
+
+ // Prepare all the variables required for the test
+ String[] tableNames = new String[nThreads];
+ String[] fullQueries = new String[nThreads];
+ String[] insertStatements = new String[nThreads];
+ File[] dataDirs = new File[nThreads];
+ String baseDataDir = tempFolder.newFolder().getAbsolutePath();
+
+ for (int i = 0; i < nThreads; i++)
+ {
+ tableNames[i] = String.format("table_%02d", i);
+ fullQueries[i] = String.format(schema, KEYSPACE + '.' + tableNames[i]);
+ logger.info(fullQueries[i]);
+
+ if (i % 2 != 0)
+ {
+ // dataDir and insert statement are only needed for the CQLSSTableWriter class
+ dataDirs[i] = new File(Paths.get(baseDataDir, KEYSPACE, tableNames[i]));
+ assert dataDirs[i].tryCreateDirectories();
+ insertStatements[i] = String.format("INSERT INTO %s.%s (k, v1, v2) VALUES (?, ?, ?)", KEYSPACE, tableNames[i]);
+ }
+
+ final int finalI = i;
+ pool.submit(() -> {
+ try
+ {
+ latch.countDown();
+ latch.await();
+
+ // Invoke all schema modifications roughly at the same time
+ if (finalI % 2 == 0)
+ {
+ schemaChange(fullQueries[finalI]);
+ // If another thread modified the Schema without the proper synchronization, it's possible
+ // that the table metadata was swapped out and calling the Keyspace#getColumnFamilyStore
+ // method will produce an IllegalArgumentException
+ Schema.instance.getKeyspaceInstance(KEYSPACE).getColumnFamilyStore(tableNames[finalI]);
+ }
+ else
+ {
+ CQLSSTableWriter.builder()
+ .inDirectory(dataDirs[finalI])
+ .forTable(fullQueries[finalI])
+ .withPartitioner(Murmur3Partitioner.instance)
+ .using(insertStatements[finalI])
+ .build();
+ }
+ }
+ catch (Throwable throwable)
+ {
+ logger.error("Error while processing element number {}", finalI, throwable);
+ errorCount.incrementAndGet();
+ }
+ });
+ }
+
+ pool.shutdown();
+ assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
Review Comment:
what about if ut just occurs a gc and cost more than one minute and this just faild?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org