iamaleksey commented on code in PR #3506:
URL: https://github.com/apache/cassandra/pull/3506#discussion_r1756860467


##########
src/java/org/apache/cassandra/journal/Compactor.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.journal;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.cassandra.concurrent.ScheduledExecutorPlus;
+import org.apache.cassandra.concurrent.Shutdownable;
+import org.apache.cassandra.io.util.File;
+
+import static 
org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory;
+
+final class Compactor<K, V> implements Runnable, Shutdownable
+{
+    private final Journal<K, V> journal;
+    private final SegmentCompactor<K, V> segmentCompactor;
+    private final ScheduledExecutorPlus executor;
+
+    Compactor(Journal<K, V> journal, SegmentCompactor<K, V> segmentCompactor)
+    {
+        this.executor = executorFactory().scheduled(false, journal.name + 
"-compactor");
+        this.journal = journal;
+        this.segmentCompactor = segmentCompactor;
+    }
+
+    void start()
+    {
+        if (journal.params.enableCompaction())
+        {
+            executor.scheduleWithFixedDelay(this,
+                                            
journal.params.compactionPeriodMillis(),
+                                            
journal.params.compactionPeriodMillis(),
+                                            TimeUnit.MILLISECONDS);
+        }
+    }
+
+    @Override
+    public void run()
+    {
+        List<StaticSegment<K, V>> toCompact = new ArrayList<>();
+        journal.segments().selectStatic(toCompact);
+        if (toCompact.size() < 2)
+            return;
+
+        try
+        {
+            Collection<StaticSegment<K, V>> compacted = 
segmentCompactor.compact(toCompact, journal.keySupport);
+            journal.replaceCompactedSegments(toCompact, compacted);
+
+            for (StaticSegment<K, V> segment : toCompact)
+            {
+                segment.waitUntilUnreferenced();
+                segment.close();
+                for (Component component : Component.values())

Review Comment:
   This logic should be encapsulated in `StaticSegment` I think.



##########
src/java/org/apache/cassandra/service/accord/AccordSegmentCompactor.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.service.accord;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.PriorityQueue;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import accord.utils.Invariants;
+import org.apache.cassandra.db.ColumnFamilyStore;
+import org.apache.cassandra.db.Keyspace;
+import org.apache.cassandra.db.SerializationHeader;
+import org.apache.cassandra.db.compaction.OperationType;
+import org.apache.cassandra.db.lifecycle.LifecycleTransaction;
+import org.apache.cassandra.db.partitions.PartitionUpdate;
+import org.apache.cassandra.db.rows.EncodingStats;
+import org.apache.cassandra.io.sstable.format.SSTableReader;
+import org.apache.cassandra.io.sstable.format.SSTableWriter;
+import org.apache.cassandra.io.sstable.format.big.BigTableWriter;
+import org.apache.cassandra.io.sstable.metadata.MetadataCollector;
+import org.apache.cassandra.journal.KeySupport;
+import org.apache.cassandra.journal.SegmentCompactor;
+import org.apache.cassandra.journal.StaticSegment;
+
+/**
+ * Segment compactor: takes static segments and compacts them into a single 
SSTable.
+ */
+public class AccordSegmentCompactor<K, V> implements SegmentCompactor<K, V>

Review Comment:
   We arguably don't need to have this class parametrised? The test can use 
`JournalKey` without any issues?



##########
src/java/org/apache/cassandra/journal/KeySupport.java:
##########
@@ -37,6 +38,16 @@ public interface KeySupport<K> extends Comparator<K>
 
     void serialize(K key, DataOutputPlus out, int userVersion) throws 
IOException;
 
+    default ByteBuffer serialize(K key, int userVersion) throws IOException

Review Comment:
   The journal doesn't need this.. `AccordSegmentCompactor` is the only user, 
and it should arguably just use `JournalKey` class directly (and not be 
parametrised, see the comment these).



##########
src/java/org/apache/cassandra/journal/Journal.java:
##########
@@ -728,21 +591,16 @@ private void closeAllSegments()
      *
      * @return a subset of segments with references to them
      */
-    ReferencedSegments<K, V> selectAndReference(Iterable<K> ids)
+    protected ReferencedSegments<K, V> selectAndReference(K id)

Review Comment:
   Can reduce visibility back to package-private.



##########
src/java/org/apache/cassandra/journal/OnDiskIndex.java:
##########
@@ -282,54 +282,64 @@ public long[] lookUpAll(K id)
         return all;
     }
 
-    public IndexIterator<K> iterator()
+    IndexIterator<K> iterator()

Review Comment:
   I think we only have one implementation of `IndexIterator` 
(`IndexIteratorImpl`), so I would consider getting rid of the interface and 
refer to the class directly. But not a huge deal either way.



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