|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cassandra.spark.data; |
| 21 | + |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import org.apache.cassandra.spark.data.partitioner.ConsistencyLevel; |
| 26 | + |
| 27 | +/** |
| 28 | + * Dynamic {@link Sizing} implementation that uses table size, minimum number of replicas, maximum partition size, |
| 29 | + * and available Spark cores to determine the effective number of executor cores to use during the spark job execution. |
| 30 | + * |
| 31 | + * <p>This class is typically used when the table size is relatively small (few GBs). When reading small datasets, |
| 32 | + * this class will allocate a limited number of resources to read the table. This in turn helps reduce the cost of |
| 33 | + * coordinating a large number of executor cores when the dataset does not justify using the entire spark cluster |
| 34 | + * for reading. |
| 35 | + */ |
| 36 | +public class DynamicSizing implements Sizing |
| 37 | +{ |
| 38 | + private static final Logger LOGGER = LoggerFactory.getLogger(DynamicSizing.class); |
| 39 | + |
| 40 | + private final ReplicationFactor replicationFactor; |
| 41 | + private final int maxPartitionSize; |
| 42 | + private final int availableCores; |
| 43 | + private final String keyspace; |
| 44 | + private final String table; |
| 45 | + private final String dc; |
| 46 | + private final TableSizeProvider tableSizeProvider; |
| 47 | + private final ConsistencyLevel consistencyLevel; |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructs a new Sizing object. |
| 51 | + * |
| 52 | + * @param tableSizeProvider the table size provider |
| 53 | + * @param consistencyLevel the consistency level for the read operation |
| 54 | + * @param replicationFactor the replication factor for the keyspace |
| 55 | + * @param keyspace the Cassandra keyspace |
| 56 | + * @param table the Cassandra table |
| 57 | + * @param datacenter the Cassandra datacenter |
| 58 | + * @param maxPartitionSize the maximum partition size desired |
| 59 | + * @param availableCores the maximum number of cores available |
| 60 | + */ |
| 61 | + public DynamicSizing(TableSizeProvider tableSizeProvider, |
| 62 | + ConsistencyLevel consistencyLevel, |
| 63 | + ReplicationFactor replicationFactor, |
| 64 | + String keyspace, |
| 65 | + String table, |
| 66 | + String datacenter, |
| 67 | + int maxPartitionSize, |
| 68 | + int availableCores) |
| 69 | + { |
| 70 | + this.tableSizeProvider = tableSizeProvider; |
| 71 | + this.consistencyLevel = consistencyLevel; |
| 72 | + this.replicationFactor = replicationFactor; |
| 73 | + this.keyspace = keyspace; |
| 74 | + this.table = table; |
| 75 | + this.dc = datacenter; |
| 76 | + this.maxPartitionSize = maxPartitionSize; |
| 77 | + this.availableCores = availableCores; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the effective number of cores to be used during the spark execution. |
| 82 | + * The value is calculated by getting the table size * the number of replicas |
| 83 | + * we will use to read the data and then dividing it by the maximum partition |
| 84 | + * size in GB. For example, assume we have a table with 7.25 GB of data, and |
| 85 | + * assume a maximum partition size of 2.5 GB. Also, assume that a consistency |
| 86 | + * level of {@code LOCAL_QUORUM} and replication factor of 3. The number of |
| 87 | + * cores is calculated by the following formula: |
| 88 | + * |
| 89 | + * <pre> |
| 90 | + * totalTableSize * minReplicas |
| 91 | + * effectiveNumberOfCores = Math.ceil( --------------------------------- ) |
| 92 | + * maxPartitionSize |
| 93 | + * </pre> |
| 94 | + * |
| 95 | + * <p>In the example above, we have: |
| 96 | + * |
| 97 | + * <pre> |
| 98 | + * 7.25 GB * 2 |
| 99 | + * effectiveNumberOfCores = --------------- = 5.8 ~> 6 cores |
| 100 | + * 2.5 GB |
| 101 | + * </pre> |
| 102 | + * |
| 103 | + * <p>This method is guaranteed to return at least 1 core and at most {@code availableCores} |
| 104 | + * |
| 105 | + * @return the effective number of cores to be used during the spark execution |
| 106 | + */ |
| 107 | + @Override |
| 108 | + public int getEffectiveNumberOfCores() |
| 109 | + { |
| 110 | + double tableSizeInGiB = ((double) tableSizeProvider.tableSizeInBytes(keyspace, table, dc) |
| 111 | + / (double) (1024 /* KiB */ * 1024 /* MiB */ * 1024 /* GiB */)); |
| 112 | + double minReplicas = consistencyLevel.blockFor(replicationFactor, dc); |
| 113 | + |
| 114 | + // Guarantee at least one core and at most availableCores |
| 115 | + int effectiveNumberOfCores = Math.min(Math.max(1, (int) Math.ceil(tableSizeInGiB * minReplicas / maxPartitionSize)), availableCores); |
| 116 | + |
| 117 | + LOGGER.info("Using Dynamic Sizing. tableSize {}GiB, minReplicas {}, maxPartitionSize {}GiB, availableCores {}, effectiveNumberOfCores {}", |
| 118 | + tableSizeInGiB, minReplicas, maxPartitionSize, availableCores, effectiveNumberOfCores); |
| 119 | + |
| 120 | + return effectiveNumberOfCores; |
| 121 | + } |
| 122 | +} |
0 commit comments