|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution.streaming.state |
| 19 | + |
| 20 | +import org.apache.spark.internal.Logging |
| 21 | +import org.apache.spark.sql.catalyst.expressions.{Attribute, UnsafeRow} |
| 22 | +import org.apache.spark.sql.catalyst.expressions.codegen.{GenerateUnsafeProjection, GenerateUnsafeRowJoiner} |
| 23 | +import org.apache.spark.sql.types.StructType |
| 24 | + |
| 25 | +/** |
| 26 | + * Base trait for state manager purposed to be used from streaming aggregations. |
| 27 | + */ |
| 28 | +sealed trait StreamingAggregationStateManager extends Serializable { |
| 29 | + |
| 30 | + /** Extract columns consisting key from input row, and return the new row for key columns. */ |
| 31 | + def getKey(row: UnsafeRow): UnsafeRow |
| 32 | + |
| 33 | + /** Calculate schema for the value of state. The schema is mainly passed to the StateStoreRDD. */ |
| 34 | + def getStateValueSchema: StructType |
| 35 | + |
| 36 | + /** Get the current value of a non-null key from the target state store. */ |
| 37 | + def get(store: StateStore, key: UnsafeRow): UnsafeRow |
| 38 | + |
| 39 | + /** |
| 40 | + * Put a new value for a non-null key to the target state store. Note that key will be |
| 41 | + * extracted from the input row, and the key would be same as the result of getKey(inputRow). |
| 42 | + */ |
| 43 | + def put(store: StateStore, row: UnsafeRow): Unit |
| 44 | + |
| 45 | + /** |
| 46 | + * Commit all the updates that have been made to the target state store, and return the |
| 47 | + * new version. |
| 48 | + */ |
| 49 | + def commit(store: StateStore): Long |
| 50 | + |
| 51 | + /** Remove a single non-null key from the target state store. */ |
| 52 | + def remove(store: StateStore, key: UnsafeRow): Unit |
| 53 | + |
| 54 | + /** Return an iterator containing all the key-value pairs in target state store. */ |
| 55 | + def iterator(store: StateStore): Iterator[UnsafeRowPair] |
| 56 | + |
| 57 | + /** Return an iterator containing all the keys in target state store. */ |
| 58 | + def keys(store: StateStore): Iterator[UnsafeRow] |
| 59 | + |
| 60 | + /** Return an iterator containing all the values in target state store. */ |
| 61 | + def values(store: StateStore): Iterator[UnsafeRow] |
| 62 | +} |
| 63 | + |
| 64 | +object StreamingAggregationStateManager extends Logging { |
| 65 | + val supportedVersions = Seq(1, 2) |
| 66 | + val legacyVersion = 1 |
| 67 | + |
| 68 | + def createStateManager( |
| 69 | + keyExpressions: Seq[Attribute], |
| 70 | + inputRowAttributes: Seq[Attribute], |
| 71 | + stateFormatVersion: Int): StreamingAggregationStateManager = { |
| 72 | + stateFormatVersion match { |
| 73 | + case 1 => new StreamingAggregationStateManagerImplV1(keyExpressions, inputRowAttributes) |
| 74 | + case 2 => new StreamingAggregationStateManagerImplV2(keyExpressions, inputRowAttributes) |
| 75 | + case _ => throw new IllegalArgumentException(s"Version $stateFormatVersion is invalid") |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +abstract class StreamingAggregationStateManagerBaseImpl( |
| 81 | + protected val keyExpressions: Seq[Attribute], |
| 82 | + protected val inputRowAttributes: Seq[Attribute]) extends StreamingAggregationStateManager { |
| 83 | + |
| 84 | + @transient protected lazy val keyProjector = |
| 85 | + GenerateUnsafeProjection.generate(keyExpressions, inputRowAttributes) |
| 86 | + |
| 87 | + override def getKey(row: UnsafeRow): UnsafeRow = keyProjector(row) |
| 88 | + |
| 89 | + override def commit(store: StateStore): Long = store.commit() |
| 90 | + |
| 91 | + override def remove(store: StateStore, key: UnsafeRow): Unit = store.remove(key) |
| 92 | + |
| 93 | + override def keys(store: StateStore): Iterator[UnsafeRow] = { |
| 94 | + // discard and don't convert values to avoid computation |
| 95 | + store.getRange(None, None).map(_.key) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * The implementation of StreamingAggregationStateManager for state version 1. |
| 101 | + * In state version 1, the schema of key and value in state are follow: |
| 102 | + * |
| 103 | + * - key: Same as key expressions. |
| 104 | + * - value: Same as input row attributes. The schema of value contains key expressions as well. |
| 105 | + * |
| 106 | + * @param keyExpressions The attributes of keys. |
| 107 | + * @param inputRowAttributes The attributes of input row. |
| 108 | + */ |
| 109 | +class StreamingAggregationStateManagerImplV1( |
| 110 | + keyExpressions: Seq[Attribute], |
| 111 | + inputRowAttributes: Seq[Attribute]) |
| 112 | + extends StreamingAggregationStateManagerBaseImpl(keyExpressions, inputRowAttributes) { |
| 113 | + |
| 114 | + override def getStateValueSchema: StructType = inputRowAttributes.toStructType |
| 115 | + |
| 116 | + override def get(store: StateStore, key: UnsafeRow): UnsafeRow = { |
| 117 | + store.get(key) |
| 118 | + } |
| 119 | + |
| 120 | + override def put(store: StateStore, row: UnsafeRow): Unit = { |
| 121 | + store.put(getKey(row), row) |
| 122 | + } |
| 123 | + |
| 124 | + override def iterator(store: StateStore): Iterator[UnsafeRowPair] = { |
| 125 | + store.iterator() |
| 126 | + } |
| 127 | + |
| 128 | + override def values(store: StateStore): Iterator[UnsafeRow] = { |
| 129 | + store.iterator().map(_.value) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * The implementation of StreamingAggregationStateManager for state version 2. |
| 135 | + * In state version 2, the schema of key and value in state are follow: |
| 136 | + * |
| 137 | + * - key: Same as key expressions. |
| 138 | + * - value: The diff between input row attributes and key expressions. |
| 139 | + * |
| 140 | + * The schema of value is changed to optimize the memory/space usage in state, via removing |
| 141 | + * duplicated columns in key-value pair. Hence key columns are excluded from the schema of value. |
| 142 | + * |
| 143 | + * @param keyExpressions The attributes of keys. |
| 144 | + * @param inputRowAttributes The attributes of input row. |
| 145 | + */ |
| 146 | +class StreamingAggregationStateManagerImplV2( |
| 147 | + keyExpressions: Seq[Attribute], |
| 148 | + inputRowAttributes: Seq[Attribute]) |
| 149 | + extends StreamingAggregationStateManagerBaseImpl(keyExpressions, inputRowAttributes) { |
| 150 | + |
| 151 | + private val valueExpressions: Seq[Attribute] = inputRowAttributes.diff(keyExpressions) |
| 152 | + private val keyValueJoinedExpressions: Seq[Attribute] = keyExpressions ++ valueExpressions |
| 153 | + |
| 154 | + // flag to check whether the row needs to be project into input row attributes after join |
| 155 | + // e.g. if the fields in the joined row are not in the expected order |
| 156 | + private val needToProjectToRestoreValue: Boolean = |
| 157 | + keyValueJoinedExpressions != inputRowAttributes |
| 158 | + |
| 159 | + @transient private lazy val valueProjector = |
| 160 | + GenerateUnsafeProjection.generate(valueExpressions, inputRowAttributes) |
| 161 | + |
| 162 | + @transient private lazy val joiner = |
| 163 | + GenerateUnsafeRowJoiner.create(StructType.fromAttributes(keyExpressions), |
| 164 | + StructType.fromAttributes(valueExpressions)) |
| 165 | + @transient private lazy val restoreValueProjector = GenerateUnsafeProjection.generate( |
| 166 | + inputRowAttributes, keyValueJoinedExpressions) |
| 167 | + |
| 168 | + override def getStateValueSchema: StructType = valueExpressions.toStructType |
| 169 | + |
| 170 | + override def get(store: StateStore, key: UnsafeRow): UnsafeRow = { |
| 171 | + val savedState = store.get(key) |
| 172 | + if (savedState == null) { |
| 173 | + return savedState |
| 174 | + } |
| 175 | + |
| 176 | + restoreOriginalRow(key, savedState) |
| 177 | + } |
| 178 | + |
| 179 | + override def put(store: StateStore, row: UnsafeRow): Unit = { |
| 180 | + val key = keyProjector(row) |
| 181 | + val value = valueProjector(row) |
| 182 | + store.put(key, value) |
| 183 | + } |
| 184 | + |
| 185 | + override def iterator(store: StateStore): Iterator[UnsafeRowPair] = { |
| 186 | + store.iterator().map(rowPair => new UnsafeRowPair(rowPair.key, restoreOriginalRow(rowPair))) |
| 187 | + } |
| 188 | + |
| 189 | + override def values(store: StateStore): Iterator[UnsafeRow] = { |
| 190 | + store.iterator().map(rowPair => restoreOriginalRow(rowPair)) |
| 191 | + } |
| 192 | + |
| 193 | + private def restoreOriginalRow(rowPair: UnsafeRowPair): UnsafeRow = { |
| 194 | + restoreOriginalRow(rowPair.key, rowPair.value) |
| 195 | + } |
| 196 | + |
| 197 | + private def restoreOriginalRow(key: UnsafeRow, value: UnsafeRow): UnsafeRow = { |
| 198 | + val joinedRow = joiner.join(key, value) |
| 199 | + if (needToProjectToRestoreValue) { |
| 200 | + restoreValueProjector(joinedRow) |
| 201 | + } else { |
| 202 | + joinedRow |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments