ifesdjeen commented on a change in pull request #1382:
URL: https://github.com/apache/cassandra/pull/1382#discussion_r800756382



##########
File path: 
test/distributed/org/apache/cassandra/distributed/fuzz/SSTableGenerator.java
##########
@@ -0,0 +1,381 @@
+/*
+ * 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.fuzz;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+
+import harry.core.Run;
+import harry.ddl.SchemaSpec;
+import harry.model.OpSelectors;
+import harry.operations.Relation;
+import harry.operations.Query;
+import harry.operations.QueryGenerator;
+import harry.util.BitSet;
+import org.apache.cassandra.cql3.AbstractMarker;
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.cql3.QueryOptions;
+import org.apache.cassandra.cql3.SingleColumnRelation;
+import org.apache.cassandra.cql3.VariableSpecifications;
+import org.apache.cassandra.cql3.WhereClause;
+import org.apache.cassandra.cql3.restrictions.StatementRestrictions;
+import org.apache.cassandra.cql3.statements.Bound;
+import org.apache.cassandra.cql3.statements.DeleteStatement;
+import org.apache.cassandra.cql3.statements.StatementType;
+import org.apache.cassandra.db.ClusteringBound;
+import org.apache.cassandra.db.ColumnFamilyStore;
+import org.apache.cassandra.db.ConsistencyLevel;
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.db.DeletionTime;
+import org.apache.cassandra.db.Mutation;
+import org.apache.cassandra.db.RangeTombstone;
+import org.apache.cassandra.db.RowUpdateBuilder;
+import org.apache.cassandra.db.Slices;
+import org.apache.cassandra.db.marshal.ByteBufferAccessor;
+import org.apache.cassandra.db.marshal.CompositeType;
+import org.apache.cassandra.db.partitions.PartitionUpdate;
+import org.apache.cassandra.io.sstable.format.SSTableReader;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.utils.FBUtilities;
+
+import static harry.generators.DataGenerators.UNSET_VALUE;
+
+public class SSTableGenerator
+{
+    protected final SchemaSpec schema;
+    protected final OpSelectors.DescriptorSelector descriptorSelector;
+    protected final OpSelectors.MonotonicClock clock;
+    protected final ColumnFamilyStore store;
+    protected final TableMetadata metadata;
+    protected final QueryGenerator rangeSelector;
+
+    private final Set<SSTableReader> sstables = new HashSet<>();
+
+    private long lts = 0;
+
+    public SSTableGenerator(Run run,
+                            ColumnFamilyStore store)
+    {
+        this.schema = run.schemaSpec;
+        this.descriptorSelector = run.descriptorSelector;
+        this.clock = run.clock;
+        this.store = store;
+        // We assume metadata can not change over the lifetime of sstable 
generator
+        this.metadata = store.metadata.get();
+        this.rangeSelector = new QueryGenerator(schema, run.pdSelector, 
descriptorSelector, run.rng);
+        store.disableAutoCompaction();
+    }
+
+    public Collection<SSTableReader> gen(int rows)
+    {
+        return gen(rows, 0);
+    }
+
+    public Collection<SSTableReader> gen(int rows, int level)
+    {
+        mark();
+        for (int i = 0; i < rows; i++)
+        {
+            long current = lts++;
+            write(current, current, current, current, true).applyUnsafe();
+            if (schema.staticColumns != null)
+                writeStatic(current, current, current, current, 
true).applyUnsafe();
+        }
+
+        return flush(level);
+    }
+
+    public void mark()
+    {
+        sstables.clear();
+        sstables.addAll(store.getLiveSSTables());
+    }
+
+    public Collection<SSTableReader> flush(int level)
+    {
+        store.forceBlockingFlush();
+        sstables.removeAll(store.getLiveSSTables());
+
+        for (SSTableReader reader : sstables)
+        {
+            if (reader.getSSTableLevel() != reader.getSSTableLevel())
+            {
+                try
+                {
+                    
reader.descriptor.getMetadataSerializer().mutateLevel(reader.descriptor, level);
+                    reader.reloadSSTableMetadata();
+                }
+                catch (IOException e)
+                {
+                    throw new RuntimeException("Can't mutate level", e);
+                }
+            }
+        }
+
+        Set<SSTableReader> ret = new HashSet<>(sstables);
+        mark();
+        return ret;
+    }
+
+    public Mutation write(long lts, long pd, long cd, long opId, boolean 
withRowMarker)

Review comment:
       I really like the idea, but unfortuately I don't see a way to make this 
operation cheap. My only hope is that people writing tests will almost never be 
exposed to these ambiguous IDs, so hopefully this won't cause any problems. 
Maybe we should try to use Kotlin for some parts and make this possible. I'd 
suggest opening a separate ticket for this.
   




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