adelapena commented on code in PR #2649:
URL: https://github.com/apache/cassandra/pull/2649#discussion_r1318377083


##########
test/distributed/org/apache/cassandra/distributed/test/guardrails/GuardrailBulkLoadEnabledTest.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.distributed.test.guardrails;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.db.Keyspace;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.distributed.Cluster;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.service.StorageService;
+import org.apache.cassandra.tools.BulkLoader;
+import org.apache.cassandra.tools.ToolRunner;
+
+import static com.google.common.collect.Lists.transform;
+import static java.util.stream.Collectors.toList;
+import static org.apache.cassandra.db.ColumnFamilyStore.FlushReason.UNIT_TESTS;
+import static org.apache.cassandra.distributed.api.Feature.GOSSIP;
+import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL;
+import static org.apache.cassandra.distributed.api.Feature.NETWORK;
+import static org.apache.cassandra.distributed.shared.AssertUtils.assertRows;
+import static org.apache.cassandra.distributed.shared.AssertUtils.row;
+import static org.apache.cassandra.distributed.test.ExecUtil.rethrow;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests the guardrails for bulk load, {@link Guardrails#bulkLoadEnabled}.
+ */
+public class GuardrailBulkLoadEnabledTest extends GuardrailTester
+{
+    private static Path tempDir;
+    private static Cluster CLUSTER;
+    static String NODES;
+    static int NATIVE_PORT;
+    static int STORAGE_PORT;
+
+    public static Path tempDir() throws Exception
+    {
+        return Files.createTempDirectory("GuardrailBulkLoadEnabledTest");
+    }
+
+    @Override
+    protected Cluster getCluster()
+    {
+        return CLUSTER;
+    }
+
+    @BeforeClass
+    public static void setupCluster() throws Exception
+    {
+        tempDir = tempDir();
+        CLUSTER = init(Cluster.build(1).withConfig(c -> 
c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP)).start());
+        NODES = CLUSTER.get(1).config().broadcastAddress().getHostString();
+        NATIVE_PORT = 
CLUSTER.get(1).callOnInstance(DatabaseDescriptor::getNativeTransportPort);
+        STORAGE_PORT = 
CLUSTER.get(1).callOnInstance(DatabaseDescriptor::getStoragePort);
+    }
+
+    @AfterClass
+    public static void teardownCluster()
+    {
+        if (CLUSTER != null)
+            CLUSTER.close();
+
+        for (File f : new File(tempDir).tryList())
+        {
+            f.tryDelete();
+        }
+    }
+
+    @Test
+    public void bulkLoaderEnabled() throws Throwable
+    {
+        File sstablesToUpload = prepareSstablesForUpload();
+        // bulk load SSTables work as expected
+        ToolRunner.ToolResult tool = loadData(sstablesToUpload);
+        tool.assertOnCleanExit();
+        assertTrue(tool.getStdout().contains("Summary statistics"));
+        assertRows(CLUSTER.get(1).executeInternal("SELECT count(*) FROM 
bulk_load_tables.test"), row(22L));
+
+        // truncate table
+        truncateGeneratedTables();
+        assertRows(CLUSTER.get(1).executeInternal("SELECT * FROM 
bulk_load_tables.test"), EMPTY_ROWS);
+
+        // Disable bulk load, stream should fail and no data should be loaded
+        CLUSTER.get(1).runOnInstance(() -> 
Guardrails.instance.setBulkLoadEnabled(false));
+        long mark = CLUSTER.get(1).logs().mark();
+        tool = loadData(sstablesToUpload);
+
+        CLUSTER.get(1).logs().watchFor(mark, "Bulk load of SSTables is not 
allowed");
+        tool.asserts().failure().errorContains("Stream failed");
+        assertRows(CLUSTER.get(1).executeInternal("SELECT * FROM 
bulk_load_tables.test"), EMPTY_ROWS);
+
+        // Enable bulk load again, data should be loaded successfully
+        CLUSTER.get(1).runOnInstance(() -> 
Guardrails.instance.setBulkLoadEnabled(true));
+        tool = loadData(sstablesToUpload);
+        tool.assertOnCleanExit();
+        assertTrue(tool.getStdout().contains("Summary statistics"));
+        assertRows(CLUSTER.get(1).executeInternal("SELECT count(*) FROM 
bulk_load_tables.test"), row(22L));
+    }
+
+    private static ToolRunner.ToolResult loadData(File sstablesToUpload)
+    {
+        return ToolRunner.invokeClass(BulkLoader.class,
+                                      "--nodes", NODES,
+                                      "--port", Integer.toString(NATIVE_PORT),
+                                      "--storage-port", 
Integer.toString(STORAGE_PORT),
+                                      sstablesToUpload.absolutePath());
+    }
+
+    private static File prepareSstablesForUpload() throws IOException
+    {
+        generateSSTables();
+        File sstableDir = copySStablesFromDataDir("test");
+        truncateGeneratedTables();
+        return sstableDir;
+    }
+
+    private static File copySStablesFromDataDir(String table) throws 
IOException
+    {
+        File cfDir = new File(tempDir +  File.pathSeparator() + 
"bulk_load_tables" + File.pathSeparator() + table);
+        cfDir.tryCreateDirectories();
+        List<String> keyspaceDirPaths = CLUSTER.get(1).callOnInstance(
+        () -> Keyspace.open("bulk_load_tables")
+                      .getColumnFamilyStore(table)
+                      .getDirectories()
+                      .getCFDirectories()
+                      .stream()
+                      .map(File::absolutePath)
+                      .collect(toList())
+        );
+        for (File srcDir : transform(keyspaceDirPaths, (p) -> new File(p)))

Review Comment:
   ```suggestion
           for (File srcDir : transform(keyspaceDirPaths, File::new))
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to