|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.cassandra.db; |
| 18 | + |
| 19 | +import javax.annotation.concurrent.NotThreadSafe; |
| 20 | + |
| 21 | +import org.apache.cassandra.db.monitoring.Monitorable; |
| 22 | +import org.apache.cassandra.db.partitions.UnfilteredPartitionIterator; |
| 23 | +import org.apache.cassandra.db.rows.RangeTombstoneMarker; |
| 24 | +import org.apache.cassandra.db.rows.Row; |
| 25 | +import org.apache.cassandra.db.rows.UnfilteredRowIterator; |
| 26 | +import org.apache.cassandra.db.transform.Transformation; |
| 27 | +import org.apache.cassandra.index.Index; |
| 28 | + |
| 29 | +/** |
| 30 | + * A custom {@link Monitorable.ExecutionInfo} implementation for {@link ReadCommand}, to be used unless there is an |
| 31 | + * {@link Index.Searcher} with its own custom implementation. |
| 32 | + * </p> |
| 33 | + * It holds and prints the number of partitions, rows and tombstones fetched and returned by the command. |
| 34 | + * </p> |
| 35 | + * Deleted partitions are considered as a partition tombstone. |
| 36 | + * Deleted rows and range tombstone markers are considered as row tombstones. |
| 37 | + * </p> |
| 38 | + * Instances of this class are meant to be updated by a single thread (the read command itself) and read by any number |
| 39 | + * of threads (the read command's monitoring task). |
| 40 | + */ |
| 41 | +@NotThreadSafe |
| 42 | +class ReadCommandExecutionInfo implements Monitorable.ExecutionInfo |
| 43 | +{ |
| 44 | + private volatile long partitionsFetched = 0; |
| 45 | + private volatile long partitionsReturned = 0; |
| 46 | + private volatile long partitionTombstones = 0; |
| 47 | + private volatile long rowsFetched = 0; |
| 48 | + private volatile long rowsReturned = 0; |
| 49 | + private volatile long rowTombstones = 0; |
| 50 | + |
| 51 | + /** |
| 52 | + * Counts the number of fetched partitions and rows in the specified iterator. |
| 53 | + * |
| 54 | + * @param partitions the iterator of fetched partitions to count |
| 55 | + * @param nowInSec the command's time in seconds, used to evaluate whether a partition/row is alive |
| 56 | + * @return the same iterator |
| 57 | + */ |
| 58 | + UnfilteredPartitionIterator countFetched(UnfilteredPartitionIterator partitions, int nowInSec) |
| 59 | + { |
| 60 | + Transformation<UnfilteredRowIterator> rowCounter = new Transformation<>() { |
| 61 | + @Override |
| 62 | + protected Row applyToRow(Row row) |
| 63 | + { |
| 64 | + if (row.hasLiveData(nowInSec, false)) |
| 65 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 66 | + rowsFetched++; |
| 67 | + return row; |
| 68 | + } |
| 69 | + }; |
| 70 | + return Transformation.apply(partitions, new Transformation<>() { |
| 71 | + @Override |
| 72 | + protected UnfilteredRowIterator applyToPartition(UnfilteredRowIterator partition) |
| 73 | + { |
| 74 | + if (!partition.partitionLevelDeletion().deletes(nowInSec)) |
| 75 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 76 | + partitionsFetched++; |
| 77 | + return Transformation.apply(partition, rowCounter); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Counts the number of fetched partitions, rows and tombstones in the specified iterator. |
| 84 | + * |
| 85 | + * @param partitions the iterator of returned partitions to count |
| 86 | + * @param nowInSec the command's time in seconds, used to evaluate whether a partition/row is alive |
| 87 | + * @return the same iterator |
| 88 | + */ |
| 89 | + UnfilteredPartitionIterator countReturned(UnfilteredPartitionIterator partitions, int nowInSec) |
| 90 | + { |
| 91 | + Transformation<UnfilteredRowIterator> rowCounter = new Transformation<>() { |
| 92 | + @Override |
| 93 | + protected Row applyToRow(Row row) |
| 94 | + { |
| 95 | + if (row.hasLiveData(nowInSec, false)) |
| 96 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 97 | + rowsReturned++; |
| 98 | + else |
| 99 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 100 | + rowTombstones++; |
| 101 | + return row; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected RangeTombstoneMarker applyToMarker(RangeTombstoneMarker marker) |
| 106 | + { |
| 107 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 108 | + rowTombstones++; |
| 109 | + return marker; |
| 110 | + } |
| 111 | + }; |
| 112 | + return Transformation.apply(partitions, new Transformation<>() { |
| 113 | + @Override |
| 114 | + protected UnfilteredRowIterator applyToPartition(UnfilteredRowIterator partition) |
| 115 | + { |
| 116 | + if (partition.partitionLevelDeletion().deletes(nowInSec)) |
| 117 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 118 | + partitionTombstones++; |
| 119 | + else |
| 120 | + // noinspection NonAtomicOperationOnVolatileField (single writer thread) |
| 121 | + partitionsReturned++; |
| 122 | + return Transformation.apply(partition, rowCounter); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String toLogString(boolean unique) |
| 129 | + { |
| 130 | + StringBuilder sb = new StringBuilder("\n"); |
| 131 | + sb.append(INDENT); |
| 132 | + sb.append(unique ? "Fetched/returned/tombstones:" : "Slowest fetched/returned/tombstones:"); |
| 133 | + append(sb, "partitions", |
| 134 | + partitionsFetched, |
| 135 | + partitionsReturned, |
| 136 | + partitionTombstones); |
| 137 | + append(sb, "rows", |
| 138 | + rowsFetched, |
| 139 | + rowsReturned, |
| 140 | + rowTombstones); |
| 141 | + return sb.toString(); |
| 142 | + } |
| 143 | + |
| 144 | + private static void append(StringBuilder sb, String name, long fetched, long returned, long tombstones) |
| 145 | + { |
| 146 | + sb.append('\n') |
| 147 | + .append(DOUBLE_INDENT) |
| 148 | + .append(name) |
| 149 | + .append(": ") |
| 150 | + .append(fetched) |
| 151 | + .append('/') |
| 152 | + .append(returned) |
| 153 | + .append('/') |
| 154 | + .append(tombstones); |
| 155 | + } |
| 156 | +} |
0 commit comments