juliuszsompolski commented on code in PR #41315: URL: https://github.com/apache/spark/pull/41315#discussion_r1260958887
########## connector/connect/server/src/main/scala/org/apache/spark/sql/connect/execution/ExecutePlanResponseObserver.scala: ########## @@ -0,0 +1,120 @@ +/* + * 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.sql.connect.execution + +import scala.collection.mutable.ListBuffer + +import io.grpc.stub.StreamObserver + +import org.apache.spark.connect.proto.ExecutePlanResponse +import org.apache.spark.internal.Logging + +/** + * Container for ExecutePlanResponses responses. + * + * This StreamObserver is running on the execution thread and saves the responses, + * it notifies the ExecutePlanResponseSender about available responses. + * + * @param responseObserver + */ +private[connect] class ExecutePlanResponseObserver() + extends StreamObserver[ExecutePlanResponse] + with Logging { + + // Cached stream state. + private val responses = new ListBuffer[CachedExecutePlanResponse]() + private var error: Option[Throwable] = None + private var lastIndex: Option[Long] = None // index of last response before completed. + private var index: Long = 0 // first response will have index 1 + + // sender to notify of available responses. + private var responseSender: Option[ExecutePlanResponseSender] = None + + def onNext(r: ExecutePlanResponse): Unit = synchronized { + if (lastIndex.nonEmpty) { + throw new IllegalStateException("Stream onNext can't be called after stream completed") + } + index += 1 + responses += CachedExecutePlanResponse(r, index) + logDebug(s"Saved response with index=$index") + notifyAll() + } + + def onError(t: Throwable): Unit = synchronized { + if (lastIndex.nonEmpty) { + throw new IllegalStateException("Stream onError can't be called after stream completed") + } + error = Some(t) + lastIndex = Some(index) // no responses to be send after error. + logDebug(s"Error. Last stream index is $index.") + notifyAll() + } + + def onCompleted(): Unit = synchronized { + if (lastIndex.nonEmpty) { + throw new IllegalStateException("Stream onCompleted can't be called after stream completed") + } + lastIndex = Some(index) + logDebug(s"Completed. Last stream index is $index.") + notifyAll() + } + + /** Set a new response sender. */ + def setExecutePlanResponseSender(newSender: ExecutePlanResponseSender): Unit = synchronized { + // detach the current sender before attaching new one + // this.synchronized() needs to be held while detaching a sender, and the detached sender + // needs to be notified with notifyAll() afterwards. + responseSender.foreach(_.detach()) + responseSender = Some(newSender) + notifyAll() + } + + /** Remove cached responses until index */ + def removeUntilIndex(index: Long): Unit = synchronized { Review Comment: My intention was for the consumer to decide that, but it's open to tweaking whether it should be the producer deciding it... it may indeed be easier and cleaner to let the producer do this, though then the producer must know of the type of the consumer - e.g. for a non-reattachable consumer it can clear responses immediately after giving them, while for a reattachable consumer it needs to keep some back buffer in case some response got lost and reattach needs to go back. But again, indeed seems that that policy may also be cleaner to be on producer side than on the consumer side... -- 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]
