HeartSaVioR commented on code in PR #56196: URL: https://github.com/apache/spark/pull/56196#discussion_r3359962224
########## core/src/main/scala/org/apache/spark/shuffle/streaming/MultiShuffleManager.scala: ########## @@ -0,0 +1,149 @@ +/* + * 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.spark.shuffle.streaming + +import java.util.Properties +import java.util.concurrent.ConcurrentHashMap + +import org.apache.spark.{ShuffleDependency, SparkConf, SparkContext, SparkException, TaskContext} +import org.apache.spark.internal.Logging +import org.apache.spark.shuffle.{ShuffleBlockResolver, ShuffleHandle, ShuffleManager, ShuffleReader, ShuffleReadMetricsReporter, ShuffleWriteMetricsReporter, ShuffleWriter} +import org.apache.spark.shuffle.sort.SortShuffleManager +import org.apache.spark.shuffle.streaming.MultiShuffleManager.isStreamingShuffleEnabled + +class MultiShuffleHandle( + val streamingShuffleHandle: ShuffleHandle, + val otherShuffleHandle: ShuffleHandle) + extends ShuffleHandle(streamingShuffleHandle.shuffleId) + +object MultiShuffleManager { + val STREAMING_SHUFFLE_ENABLED_PROPERTY = "spark.shuffle.streaming.useForCurrentQuery" Review Comment: nit: while I'm not good at naming, I'm not sure `spark.shuffle.streaming.useForCurrentQuery` is a good name. Why not simply "spark.shuffle.useStreamingShuffle" / "spark.shuffle.enableStreamingShuffle"? It would be consistent with the below function name as well. ########## core/src/test/scala/org/apache/spark/shuffle/streaming/StreamingShuffleManagerSuite.scala: ########## @@ -0,0 +1,119 @@ +/* + * 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.spark.shuffle.streaming + +import io.netty.buffer.Unpooled +import org.mockito.Mockito.when +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.mockito.MockitoSugar + +import org.apache.spark._ +import org.apache.spark.LocalSparkContext.withSpark +import org.apache.spark.internal.config.SHUFFLE_MANAGER +import org.apache.spark.network.shuffle.streaming.{DataMessage, TerminationAckMessage, TerminationControlMessage} +import org.apache.spark.shuffle.sort.SortShuffleManager +import org.apache.spark.shuffle.streaming.StreamingShuffleManager.{getQueryId, getWriterId, QUERY_ID_PROPERTY_KEY} + +class StreamingShuffleManagerSuite + extends SparkFunSuite + with LocalSparkContext + with Matchers + with MockitoSugar { + + private val SQL_EXECUTION_ID_KEY = "spark.sql.execution.id" + + // ---- getWriterId ---- + + test("getWriterId returns the writer id for a data message") { + val msg = new DataMessage(7, 3, 0, Unpooled.EMPTY_BUFFER, 0L) + getWriterId(msg) should be(7) + } + + test("getWriterId returns the writer id for a termination control message") { + getWriterId(new TerminationControlMessage(5, 2)) should be(5) + } + + test("getWriterId throws on an unexpected message type") { + val e = intercept[SparkRuntimeException] { + getWriterId(new TerminationAckMessage(1, 1)) + } + checkError( + e, + condition = "STREAMING_SHUFFLE_UNEXPECTED_MESSAGE_TYPE", + parameters = Map("messageType" -> "TERMINATION_ACK_MESSAGE")) + } + + // ---- getQueryId ---- + + test("getQueryId returns the streaming query id when set") { + val context = mock[TaskContext] + when(context.getLocalProperty(QUERY_ID_PROPERTY_KEY)).thenReturn("query-123") + getQueryId(context) should be("query-123") + } + + test("getQueryId falls back to the SQL execution id for batch queries") { + val context = mock[TaskContext] + when(context.getLocalProperty(SQL_EXECUTION_ID_KEY)).thenReturn("42") + getQueryId(context) should be("42") + } + + test("getQueryId throws when no query id property is set") { + val context = mock[TaskContext] + intercept[SparkException] { + getQueryId(context) + } + } + + // ---- registerShuffle ---- + + test("registerShuffle returns a StreamingShuffleHandle") { + withSpark(new SparkContext("local", "StreamingShuffleManagerSuite", new SparkConf())) { sc => + val rdd = sc.parallelize(1 to 4).map(x => (x, x)) + val dep = new ShuffleDependency[Int, Int, Int](rdd, new HashPartitioner(2)) + val handle = new StreamingShuffleManager().registerShuffle(0, dep) + assert(handle.isInstanceOf[StreamingShuffleHandle[_, _, _]]) + } + } + + // ---- SparkEnv tracker initialization gating ---- Review Comment: nit: same pattern - three test cases seem to be very similar except shuffle manager conf value and the expected tracker. Sounds like pretty much easy to remove redundant code. ########## core/src/test/scala/org/apache/spark/shuffle/streaming/StreamingShuffleManagerSuite.scala: ########## @@ -0,0 +1,119 @@ +/* + * 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.spark.shuffle.streaming + +import io.netty.buffer.Unpooled +import org.mockito.Mockito.when +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.mockito.MockitoSugar + +import org.apache.spark._ +import org.apache.spark.LocalSparkContext.withSpark +import org.apache.spark.internal.config.SHUFFLE_MANAGER +import org.apache.spark.network.shuffle.streaming.{DataMessage, TerminationAckMessage, TerminationControlMessage} +import org.apache.spark.shuffle.sort.SortShuffleManager +import org.apache.spark.shuffle.streaming.StreamingShuffleManager.{getQueryId, getWriterId, QUERY_ID_PROPERTY_KEY} + +class StreamingShuffleManagerSuite + extends SparkFunSuite + with LocalSparkContext + with Matchers + with MockitoSugar { + + private val SQL_EXECUTION_ID_KEY = "spark.sql.execution.id" + + // ---- getWriterId ---- + + test("getWriterId returns the writer id for a data message") { + val msg = new DataMessage(7, 3, 0, Unpooled.EMPTY_BUFFER, 0L) + getWriterId(msg) should be(7) + } + + test("getWriterId returns the writer id for a termination control message") { + getWriterId(new TerminationControlMessage(5, 2)) should be(5) + } + + test("getWriterId throws on an unexpected message type") { + val e = intercept[SparkRuntimeException] { + getWriterId(new TerminationAckMessage(1, 1)) + } + checkError( + e, + condition = "STREAMING_SHUFFLE_UNEXPECTED_MESSAGE_TYPE", + parameters = Map("messageType" -> "TERMINATION_ACK_MESSAGE")) + } + + // ---- getQueryId ---- + + test("getQueryId returns the streaming query id when set") { + val context = mock[TaskContext] + when(context.getLocalProperty(QUERY_ID_PROPERTY_KEY)).thenReturn("query-123") + getQueryId(context) should be("query-123") + } + + test("getQueryId falls back to the SQL execution id for batch queries") { + val context = mock[TaskContext] + when(context.getLocalProperty(SQL_EXECUTION_ID_KEY)).thenReturn("42") + getQueryId(context) should be("42") + } + + test("getQueryId throws when no query id property is set") { + val context = mock[TaskContext] + intercept[SparkException] { Review Comment: nit: shall we check the exception? preferably, with `checkError`. ########## core/src/main/scala/org/apache/spark/shuffle/streaming/StreamingShuffleManager.scala: ########## Review Comment: For method doc, if it's the same with superclass, we don't need to copy the content since it's automatically inherited (IIUC). If we want to be explicilt, `@inheritdoc` tag would do the thing. ########## core/src/test/scala/org/apache/spark/shuffle/streaming/MultiShuffleManagerSuite.scala: ########## @@ -0,0 +1,70 @@ +/* + * 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.spark.shuffle.streaming + +import java.util.Properties + +import org.scalatest.matchers.should.Matchers + +import org.apache.spark._ +import org.apache.spark.LocalSparkContext.withSpark +import org.apache.spark.internal.config.SHUFFLE_MANAGER +import org.apache.spark.shuffle.streaming.MultiShuffleManager.{isStreamingShuffleEnabled, STREAMING_SHUFFLE_ENABLED_PROPERTY} + +class MultiShuffleManagerSuite + extends SparkFunSuite + with LocalSparkContext + with Matchers { + + test("isStreamingShuffleEnabled reflects the per-query property") { + val props = new Properties() + isStreamingShuffleEnabled(props) should be(false) + + props.setProperty(STREAMING_SHUFFLE_ENABLED_PROPERTY, "true") + isStreamingShuffleEnabled(props) should be(true) + + props.setProperty(STREAMING_SHUFFLE_ENABLED_PROPERTY, "false") + isStreamingShuffleEnabled(props) should be(false) + } + + test("registerShuffle routes to the streaming manager when enabled for the query") { Review Comment: nit: this and below test cases are almost the same except the config value and the assertion (true/false). Sounds like pretty much easy to remove redundant code. -- 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]
