arjunashok commented on code in PR #17:
URL: 
https://github.com/apache/cassandra-analytics/pull/17#discussion_r1419760670


##########
cassandra-analytics-integration-tests/src/test/java/org/apache/cassandra/analytics/ResiliencyTestBase.java:
##########
@@ -0,0 +1,384 @@
+/*
+ * 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.analytics;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Range;
+
+import com.datastax.driver.core.ConsistencyLevel;
+import 
o.a.c.analytics.sidecar.shaded.testing.adapters.base.StorageJmxOperations;
+import o.a.c.analytics.sidecar.shaded.testing.common.JmxClient;
+import o.a.c.analytics.sidecar.shaded.testing.common.data.QualifiedTableName;
+import org.apache.cassandra.distributed.UpgradeableCluster;
+import org.apache.cassandra.distributed.api.IInstanceConfig;
+import org.apache.cassandra.distributed.api.IUpgradeableInstance;
+import org.apache.cassandra.distributed.api.Row;
+import org.apache.cassandra.distributed.api.SimpleQueryResult;
+import org.apache.cassandra.distributed.api.TokenSupplier;
+import org.apache.cassandra.sidecar.testing.IntegrationTestBase;
+import org.apache.cassandra.spark.KryoRegister;
+import org.apache.cassandra.spark.bulkwriter.BulkSparkConf;
+import org.apache.cassandra.spark.bulkwriter.DecoratedKey;
+import org.apache.cassandra.spark.bulkwriter.Tokenizer;
+import org.apache.cassandra.spark.common.schema.ColumnType;
+import org.apache.cassandra.spark.common.schema.ColumnTypes;
+import org.apache.cassandra.testing.CassandraIntegrationTest;
+import org.apache.cassandra.testing.ConfigurableCassandraTestContext;
+import org.apache.spark.SparkConf;
+import org.apache.spark.sql.DataFrameWriter;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.RowFactory;
+import org.apache.spark.sql.SQLContext;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.types.StructType;
+import scala.Tuple2;
+
+import static junit.framework.TestCase.assertTrue;
+import static 
org.apache.cassandra.distributed.shared.NetworkTopology.dcAndRack;
+import static 
org.apache.cassandra.distributed.shared.NetworkTopology.networkTopology;
+import static org.apache.spark.sql.types.DataTypes.IntegerType;
+import static org.apache.spark.sql.types.DataTypes.StringType;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Base class for resiliency tests. Contains helper methods for data 
generation and validation
+ */
+public abstract class ResiliencyTestBase extends IntegrationTestBase
+{
+    private static final String createTableStmt = "create table if not exists 
%s (id int, course text, marks int, primary key (id));";
+    protected static final String retrieveRows = "select * from " + 
TEST_KEYSPACE + ".%s";
+    public static final int rowCount = 1000;
+
+    public QualifiedTableName initializeSchema()
+    {
+        return initializeSchema(ImmutableMap.of("datacenter1", 1));
+    }
+
+    public QualifiedTableName initializeSchema(Map<String, Integer> rf)
+    {
+        createTestKeyspace(rf);
+        return createTestTable(createTableStmt);
+    }
+
+    public SparkConf generateSparkConf()
+    {
+        SparkConf sparkConf = new SparkConf()
+                              .setAppName("Integration test Spark Cassandra 
Bulk Reader Job")
+                              .set("spark.serializer", 
"org.apache.spark.serializer.KryoSerializer")
+                              .set("spark.master", "local[8,4]");
+        BulkSparkConf.setupSparkConf(sparkConf, true);
+        KryoRegister.setup(sparkConf);
+        return sparkConf;
+    }
+
+    public SparkSession generateSparkSession(SparkConf sparkConf)
+    {
+        return SparkSession.builder()
+                           .config(sparkConf)
+                           .getOrCreate();
+    }
+
+    public Set<String> getDataForRange(Range<BigInteger> range)
+    {
+        // Iterate through all data entries; filter only entries that belong 
to range; convert to strings
+        return generateExpectedData().stream()
+                   .filter(t -> range.contains(t._1().getToken()))
+                   .map(t -> t._2()[0] + ":" + t._2()[1] + ":" + t._2()[2])
+                   .collect(Collectors.toSet());
+    }
+
+    public List<Tuple2<DecoratedKey, Object[]>> generateExpectedData()
+    {
+        // "create table if not exists %s (id int, course text, marks int, 
primary key (id));";
+        List<ColumnType<?>> columnTypes = Arrays.asList(ColumnTypes.INT);
+        Tokenizer tokenizer = new Tokenizer(Arrays.asList(0),
+                                            Arrays.asList("id"),
+                                            columnTypes,
+                                            true
+        );
+        return IntStream.range(0, rowCount).mapToObj(recordNum -> {
+            Object[] columns = new Object[]
+                               {
+                               recordNum, "course" + recordNum, recordNum
+                               };
+            return Tuple2.apply(tokenizer.getDecoratedKey(columns), columns);
+        }).collect(Collectors.toList());
+    }
+
+    public Map<IUpgradeableInstance, Set<String>> 
getInstanceData(List<IUpgradeableInstance> instances,
+                                                                  boolean 
isPending)
+    {
+
+        return instances.stream().collect(Collectors.toMap(Function.identity(),
+                                                           i -> 
filterTokenRangeData(getRangesForInstance(i, isPending))));
+    }
+
+    public Set<String> filterTokenRangeData(List<Range<BigInteger>> ranges)
+    {
+        return ranges.stream()
+                 .map(r -> getDataForRange(r))
+                 .flatMap(Collection::stream)
+                 .collect(Collectors.toSet());
+    }
+
+    private List<Range<BigInteger>> getRangesForInstance(IUpgradeableInstance 
instance, boolean isPending)
+    {
+        IInstanceConfig config = instance.config();
+        JmxClient client = JmxClient.builder()
+                                    
.host(config.broadcastAddress().getAddress().getHostAddress())
+                                    .port(config.jmxPort())
+                                    .build();
+        StorageJmxOperations ss = client.proxy(StorageJmxOperations.class, 
"org.apache.cassandra.db:type=StorageService");
+
+        Map<List<String>, List<String>> ranges = isPending ? 
ss.getPendingRangeToEndpointWithPortMap(TEST_KEYSPACE)
+                                                           : 
ss.getRangeToEndpointWithPortMap(TEST_KEYSPACE);
+
+        // filter ranges that belong to the instance
+        return ranges.entrySet()
+                            .stream()
+                            .filter(e -> 
e.getValue().contains(instance.broadcastAddress().getAddress().getHostAddress()
+                                                               + ":" + 
instance.broadcastAddress().getPort()))
+                            .map(e -> unwrapRanges(e.getKey()))
+                            .flatMap(Collection::stream)
+                            .collect(Collectors.toList());

Review Comment:
   Addressed



-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to