smiklosovic commented on code in PR #3374:
URL: https://github.com/apache/cassandra/pull/3374#discussion_r1838597410


##########
test/distributed/org/apache/cassandra/fuzz/snapshots/SnapshotsTest.java:
##########
@@ -0,0 +1,1145 @@
+/*
+ * 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.fuzz.snapshots;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.UUID;
+import javax.management.openmbean.TabularData;
+
+import com.google.common.collect.MapDifference;
+import com.google.common.collect.MapDifference.ValueDifference;
+import com.google.common.collect.Maps;
+import org.junit.Test;
+
+import accord.utils.Property.StateOnlyCommand;
+import accord.utils.RandomSource;
+import org.apache.cassandra.distributed.Cluster;
+import org.apache.cassandra.distributed.api.Feature;
+import org.apache.cassandra.distributed.api.IInvokableInstance;
+import org.apache.cassandra.distributed.api.IIsolatedExecutor;
+import 
org.apache.cassandra.distributed.api.IIsolatedExecutor.SerializableFunction;
+import 
org.apache.cassandra.distributed.api.IIsolatedExecutor.SerializableTriConsumer;
+import org.apache.cassandra.distributed.api.NodeToolResult;
+import org.apache.cassandra.fuzz.snapshots.SnapshotsTest.State.TestSnapshot;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.Keyspaces;
+import org.apache.cassandra.schema.Schema;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.service.snapshot.SnapshotManager;
+import org.apache.cassandra.service.snapshot.TableSnapshot;
+import org.apache.cassandra.utils.Generators;
+import org.apache.cassandra.utils.Pair;
+import org.quicktheories.core.RandomnessSource;
+import org.quicktheories.impl.JavaRandom;
+
+import static accord.utils.Property.commands;
+import static accord.utils.Property.stateful;
+import static com.google.common.collect.Sets.difference;
+import static java.lang.String.format;
+import static java.util.UUID.randomUUID;
+import static 
org.apache.cassandra.fuzz.snapshots.SnapshotsTest.CreateKeyspace.createKeypaces;
+import static 
org.apache.cassandra.fuzz.snapshots.SnapshotsTest.CreateTable.createTables;
+import static org.junit.Assert.assertEquals;
+
+public class SnapshotsTest
+{
+    @Test
+    public void fuzzySnapshotsTest()
+    {
+        stateful()
+        .withExamples(1)
+        .withSteps(2000)
+        .withStepTimeout(Duration.ofMinutes(3))
+        .check(commands(() -> State::new)
+               .add(CreateKeyspace::new)
+               .add(DropKeyspace::new)
+               .add(CreateTable::new)
+               .add(DropTable::new)
+               .add(TruncateTable::new)
+               .add(TakeSnapshot::new)
+               .add(ClearSnapshot::new)
+               .add(ListSnapshots::new)
+               .destroyState(State::destroy)
+               .build());
+    }
+
+    public static abstract class AbstractCommand implements 
StateOnlyCommand<State>
+    {
+        protected final RandomSource rs;
+        protected final State state;
+
+        public AbstractCommand(RandomSource rs, State state)
+        {
+            this.rs = rs;
+            this.state = state;
+        }
+
+        public void executeQuery(String query)
+        {
+            state.getNode().executeInternal(query);
+        }
+
+        public IInvokableInstance getNode()
+        {
+            return state.getNode();
+        }
+    }
+
+    public static class ListSnapshots extends AbstractCommand
+    {
+        private final Pair<SnapshotsHolder, Map<String, String>> listingParams;
+
+        public ListSnapshots(RandomSource rs, State state)
+        {
+            super(rs, state);
+            listingParams = generateParams();
+        }
+
+        @Override
+        public void applyUnit(State state)
+        {
+            assertEquals(listingParams.left, 
categorize(list(listingParams.right)));
+        }
+
+        @Override
+        public String toString()
+        {
+            return "ListSnapshots{" +
+                   "params=" + listingParams +
+                   '}';
+        }
+
+        private Pair<SnapshotsHolder, Map<String, String>> generateParams()
+        {
+            Map<String, String> listingParams = new HashMap<>();
+            List<Integer> toShuffle = state.getShuffledListOfInts(4);
+
+            String keyspace = null;
+            for (int i = 0; i < 4; i++)
+            {
+                boolean picked = false;
+                switch (toShuffle.get(i))
+                {
+                    case 1:
+                        if (!state.truncatedSnapshots.isEmpty())
+                        {
+                            keyspace = 
state.rs.pick(state.truncatedSnapshots.keySet()).split("\\.")[0];
+                            picked = true;
+                        }
+                        break;
+                    case 2:
+                        if (!state.droppedSnapshots.isEmpty())
+                        {
+                            keyspace = 
state.rs.pick(state.droppedSnapshots).split("\\.")[0];
+                            picked = true;
+                        }
+                        break;
+                    case 3:
+                        if (!state.snapshots.isEmpty())
+                        {
+                            keyspace = 
state.rs.pick(state.snapshots).getKeyspaceName();
+                            picked = true;
+                        }
+                        break;
+                    default:
+                        // keyspace will be null so all snapshost will be 
listed
+                        picked = true;
+                        break;
+                }
+                if (picked)
+                    break;
+            }
+
+            // we need to populate expected snapshots after listing
+            SnapshotsHolder holder = new SnapshotsHolder();
+
+            if (keyspace == null)
+            {
+                holder.normal.addAll(state.snapshots);
+                holder.dropped.addAll(state.droppedSnapshots);
+                holder.truncated.putAll(state.truncatedSnapshots);
+            }
+            else
+            {
+                listingParams.put("keyspace", keyspace);
+                for (String s : state.droppedSnapshots)
+                    if (s.startsWith(keyspace + '.'))
+                        holder.dropped.add(s);
+
+                for (Map.Entry<String, Integer> entry : 
state.truncatedSnapshots.entrySet())
+                    if (entry.getKey().startsWith(keyspace + '.'))
+                        holder.truncated.put(entry.getKey(), entry.getValue());
+
+                for (TestSnapshot s : state.snapshots)
+                    if (s.getKeyspaceName().equals(keyspace))
+                        holder.normal.add(s);
+            }
+
+            return Pair.create(holder, listingParams);
+        }
+
+        private List<String> list(Map<String, String> parameters)
+        {
+            return getNode().applyOnInstance((SerializableFunction<Map<String, 
String>, List<String>>) (params) ->
+            {
+                Map<String, TabularData> listingResult = 
SnapshotManager.instance.listSnapshots(params);
+
+                List<String> snapshots = new ArrayList<>();
+                for (final Map.Entry<String, TabularData> snapshotDetail : 
listingResult.entrySet())
+                {
+                    Set<?> values = snapshotDetail.getValue().keySet();
+                    for (Object eachValue : values)
+                    {
+                        final List<?> value = (List<?>) eachValue;
+                        String tag = (String) value.get(0);
+                        String keyspace = (String) value.get(1);
+                        String table = (String) value.get(2);
+                        snapshots.add(format("%s.%s.%s", keyspace, table, 
tag));
+                    }
+                }
+
+                return snapshots;
+            }, parameters);
+        }
+
+        private SnapshotsHolder categorize(List<String> listedSnapshots)
+        {
+            SnapshotsHolder holder = new SnapshotsHolder();
+            for (String snapshot : listedSnapshots)
+            {
+                if (snapshot.contains("dropped-"))
+                {
+                    String[] split = snapshot.split("\\.");
+                    holder.dropped.add(format("%s.%s", split[0], split[1]));
+                }
+                else if (snapshot.contains("truncated-"))
+                {
+                    String[] split = snapshot.split("\\.");
+                    String ksTb = format("%s.%s", split[0], split[1]);
+                    holder.truncated.merge(ksTb, 1, Integer::sum);
+                }
+                else
+                {
+                    String[] split = snapshot.split("\\.");
+                    holder.normal.add(new TestSnapshot(split[0], split[1], 
split[2]));
+                }
+            }
+
+            return holder;
+        }
+
+        private static class SnapshotsHolder
+        {
+            Set<TestSnapshot> normal = new HashSet<>();
+            Set<String> dropped = new HashSet<>();
+            Map<String, Integer> truncated = new HashMap<>();
+
+            @Override
+            public String toString()
+            {
+                return "SnapshotsHolder{" +
+                       "normal=" + normal +
+                       ", dropped=" + dropped +
+                       ", truncated=" + truncated +
+                       '}';
+            }
+
+            @Override
+            public boolean equals(Object o)
+            {
+                if (this == o) return true;
+                if (o == null || getClass() != o.getClass()) return false;
+                SnapshotsHolder holder = (SnapshotsHolder) o;
+                return Objects.equals(normal, holder.normal) &&
+                       Objects.equals(dropped, holder.dropped) &&
+                       Objects.equals(truncated, holder.truncated);
+            }
+
+            @Override
+            public int hashCode()
+            {
+                return Objects.hash(normal, dropped, truncated);
+            }
+        }
+    }
+
+    public static class CreateKeyspace extends AbstractCommand
+    {
+        private static final String CREATE_KEYSPACE_QUERY =
+        "CREATE KEYSPACE %s WITH replication = { 'class' : 'SimpleStrategy', 
'replication_factor' : '1' }";
+
+        private final Set<String> keyspaceToCreate = new HashSet<>();
+
+        public CreateKeyspace(RandomSource rs, State state)
+        {
+            super(rs, state);
+            keyspaceToCreate.add(state.addRandomKeyspace());
+        }
+
+        @Override
+        public void applyUnit(State state)
+        {
+            createKeypaces(state);
+            assertEquals(state.schema.keySet(), state.getKeyspaces());
+            state.populate();
+        }
+
+        public static Set<String> createKeypaces(State state)
+        {
+            Set<String> existingKeyspaces = state.getKeyspaces();
+            Set<String> keyspacesToBe = state.schema.keySet();
+
+            Set<String> difference = difference(keyspacesToBe, 
existingKeyspaces);
+
+            for (String keyspaceToCreate : difference)
+                state.getNode().executeInternal(format(CREATE_KEYSPACE_QUERY, 
keyspaceToCreate));
+
+            return difference;

Review Comment:
   Yes, I may simplify, it is true that we always have just one keyspace to 
create. I probably just followed what the code was giving me and then it ended 
up in more "generic" way with multiple keyspaces ... 
   
   EDIT:
   
   well, I knot that `difference` method will give me a set of one keyspace, 
yes. That's because what I am doing - one keyspace at a time. So `for` loop is 
not necessary. But I like that I am making difference like that. It is just 
`difference` set will contain always just one entry.



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