You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2020/09/01 00:48:42 UTC

[GitHub] [cassandra] dcapwell commented on a change in pull request #642: Cassandra 15861 lock trunk

dcapwell commented on a change in pull request #642:
URL: https://github.com/apache/cassandra/pull/642#discussion_r480474987



##########
File path: src/java/org/apache/cassandra/db/streaming/CassandraOutgoingFile.java
##########
@@ -154,29 +143,38 @@ public UUID getPendingRepair()
         return ref.get().getPendingRepair();
     }
 
-    public int getManifestSize()
-    {
-        return manifest.components().size();
-    }
-
     @Override
     public void write(StreamSession session, DataOutputStreamPlus out, int version) throws IOException
     {
+        boolean streamEntirely = shouldStreamEntireSSTable && out instanceof AsyncStreamingOutputPlus;
         SSTableReader sstable = ref.get();
-        CassandraStreamHeader.serializer.serialize(header, out, version);
-        out.flush();
 
-        if (shouldStreamEntireSSTable && out instanceof AsyncStreamingOutputPlus)
+        if (streamEntirely)
         {
-            CassandraEntireSSTableStreamWriter writer = new CassandraEntireSSTableStreamWriter(sstable, session, manifest);
-            writer.write((AsyncStreamingOutputPlus) out);
+            // Acquire lock to avoid concurrent sstable component mutation becaue of stats update or index summary

Review comment:
       `s/becaue/because/`

##########
File path: src/java/org/apache/cassandra/db/streaming/ComponentContext.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.db.streaming;
+
+import com.google.common.collect.ImmutableSet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.io.sstable.Component;
+import org.apache.cassandra.io.sstable.Descriptor;
+import org.apache.cassandra.io.util.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Mutable SSTable components and their hardlinks to avoid concurrent sstable component modification
+ * during entire-sstable-streaming.
+ */
+public class ComponentContext implements AutoCloseable
+{
+    private static final Logger logger = LoggerFactory.getLogger(ComponentContext.class);
+
+    private static final Set<Component> MUTABLE_COMPONENTS = ImmutableSet.of(Component.STATS, Component.SUMMARY);
+
+    private final Map<Component, File> hardLinks;
+    private final ComponentManifest manifest;
+
+    private ComponentContext(Map<Component, File> hardLinks, ComponentManifest manifest)
+    {
+        this.hardLinks = hardLinks;
+        this.manifest = manifest;
+    }
+
+    public static ComponentContext create(Descriptor descriptor)
+    {
+        LinkedHashMap<Component, File> hardLinks = new LinkedHashMap<>(1);
+
+        for (Component component : MUTABLE_COMPONENTS)
+        {
+            File file = new File(descriptor.filenameFor(component));
+            if (!file.exists())
+                continue;
+
+            File hardlink = new File(descriptor.tmpFilenameForStreaming(component));
+            FileUtils.createHardLink(file, hardlink);
+            hardLinks.put(component, hardlink);
+        }
+
+        return new ComponentContext(hardLinks, ComponentManifest.create(descriptor));
+    }
+
+    public ComponentManifest manifest()
+    {
+        return manifest;
+    }
+
+    /**
+     * @return file channel to be streamed, either original component or hardlinked component.
+     */
+    public FileChannel channel(Descriptor descriptor, Component component, long size) throws IOException
+    {
+        String toTransfer = hardLinks.containsKey(component) ? hardLinks.get(component).getPath() : descriptor.filenameFor(component);
+        @SuppressWarnings("resource") // file channel will be closed by Caller
+        FileChannel channel = new RandomAccessFile(toTransfer, "r").getChannel();
+
+        assert size == channel.size() : String.format("Entire sstable streaming expects %s file size to be %s but got %s.",
+                                                      component, size, channel.size());
+        return channel;
+    }
+
+    @Override
+    public void close()
+    {
+        Throwable accumulate = null;
+        for (File file : hardLinks.values())
+            accumulate = FileUtils.deleteWithConfirm(file, accumulate);
+
+        hardLinks.clear();
+
+        if (accumulate != null)
+            logger.warn("Failed to remove hard link files: {}", accumulate.getMessage());

Review comment:
       should be `logger.warn("Failed to remove hard link files", accumulate);`

##########
File path: src/java/org/apache/cassandra/db/streaming/ComponentContext.java
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.db.streaming;
+
+import com.google.common.collect.ImmutableSet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.io.sstable.Component;
+import org.apache.cassandra.io.sstable.Descriptor;
+import org.apache.cassandra.io.util.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Mutable SSTable components and their hardlinks to avoid concurrent sstable component modification
+ * during entire-sstable-streaming.
+ */
+public class ComponentContext implements AutoCloseable
+{
+    private static final Logger logger = LoggerFactory.getLogger(ComponentContext.class);
+
+    private static final Set<Component> MUTABLE_COMPONENTS = ImmutableSet.of(Component.STATS, Component.SUMMARY);
+
+    private final Map<Component, File> hardLinks;
+    private final ComponentManifest manifest;
+
+    private ComponentContext(Map<Component, File> hardLinks, ComponentManifest manifest)
+    {
+        this.hardLinks = hardLinks;
+        this.manifest = manifest;
+    }
+
+    public static ComponentContext create(Descriptor descriptor)
+    {
+        LinkedHashMap<Component, File> hardLinks = new LinkedHashMap<>(1);

Review comment:
       any reason to use `LinkedHashMap` rather than `HashMap`?  this allocates a bit more memory and don't see a need for order (we delete files in the same order we created them, but don't think we require that property)




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

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