juliuszsompolski commented on code in PR #41443: URL: https://github.com/apache/spark/pull/41443#discussion_r1263894841
########## connector/connect/server/src/main/scala/org/apache/spark/sql/connect/service/RequestEvents.scala: ########## @@ -0,0 +1,327 @@ +/* + * 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.service + +import com.fasterxml.jackson.annotation.JsonIgnore +import com.google.protobuf.Message + +import org.apache.spark.connect.proto +import org.apache.spark.scheduler.{LiveListenerBus, SparkListenerEvent} +import org.apache.spark.sql.catalyst.{QueryPlanningTracker, QueryPlanningTrackerCallback} +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.connect.common.ProtoUtils +import org.apache.spark.util.{Clock, Utils} + +object RequestEvents { + // TODO: Make this configurable + val MAX_STATEMENT_TEXT_SIZE = 65535 +} + +/** + * Post request Connect events to @link org.apache.spark.scheduler.LiveListenerBus. + * + * @param planHolder: + * Request for which the events are generated. + * @param clock: + * Source of time for unit tests. + */ +case class RequestEvents(planHolder: ExecutePlanHolder, clock: Clock) + extends QueryPlanningTrackerCallback { + + /** + * Post @link org.apache.spark.sql.connect.service.SparkListenerConnectOperationStarted. + */ + def postStarted(): Unit = { + val request = planHolder.request + val plan: Message = + request.getPlan.getOpTypeCase match { + case proto.Plan.OpTypeCase.COMMAND => request.getPlan.getCommand + case proto.Plan.OpTypeCase.ROOT => request.getPlan.getRoot + case _ => + throw new UnsupportedOperationException( + s"${request.getPlan.getOpTypeCase} not supported.") + } + + listenerBus.post( + SparkListenerConnectOperationStarted( + jobTag, + operationId, + clock.getTimeMillis(), + request.getSessionId, + request.getUserContext.getUserId, + request.getUserContext.getUserName, + Utils.redact( + sessionHolder.session.sessionState.conf.stringRedactionPattern, + ProtoUtils.abbreviate(plan, RequestEvents.MAX_STATEMENT_TEXT_SIZE).toString), + Some(request))) + } + + /** + * Post @link org.apache.spark.sql.connect.service.SparkListenerConnectOperationAnalyzed. + * + * @param analyzedPlan + * The analyzed plan generated by the Connect request plan. None when the request does not + * generate a plan. + */ + def postAnalyzed(analyzedPlan: Option[LogicalPlan] = None): Unit = { + val event = + SparkListenerConnectOperationAnalyzed(jobTag, operationId, clock.getTimeMillis()) + event.analyzedPlan = analyzedPlan + listenerBus.post(event) + } + + /** + * Post @link + * org.apache.spark.sql.connect.service.SparkListenerConnectOperationReadyForExecution. + */ + def postReadyForExecution(): Unit = { + listenerBus.post( + SparkListenerConnectOperationReadyForExecution(jobTag, operationId, clock.getTimeMillis())) + } + + /** + * Post @link org.apache.spark.sql.connect.service.SparkListenerConnectOperationCanceled. + */ + def postCanceled(): Unit = { + listenerBus + .post(SparkListenerConnectOperationCanceled(jobTag, operationId, clock.getTimeMillis())) + } + + /** + * Post @link org.apache.spark.sql.connect.service.SparkListenerConnectOperationFailed. + * + * @param errorMessage + * The message of the error thrown during the request. + */ + def postFailed(errorMessage: String): Unit = { + listenerBus.post( + SparkListenerConnectOperationFailed( + jobTag, + operationId, + clock.getTimeMillis(), + errorMessage)) + } + + /** + * Post @link org.apache.spark.sql.connect.service.SparkListenerConnectOperationFinished. + */ + def postFinished(): Unit = { + listenerBus + .post(SparkListenerConnectOperationFinished(jobTag, operationId, clock.getTimeMillis())) + } + + /** + * Post @link org.apache.spark.sql.connect.service.SparkListenerConnectOperationClosed. + */ + def postClosed(): Unit = { + listenerBus + .post(SparkListenerConnectOperationClosed(jobTag, operationId, clock.getTimeMillis())) + } + + def analyzed(tracker: QueryPlanningTracker, analyzedPlan: LogicalPlan): Unit = { + postAnalyzed(Some(analyzedPlan)) + } + + def readyForExecution(tracker: QueryPlanningTracker): Unit = postReadyForExecution + + private def operationId(): String = { + planHolder.operationId + } + + private def jobTag(): String = { + planHolder.jobTag + } + + private def listenerBus(): LiveListenerBus = { + sessionHolder.session.sparkContext.listenerBus + } + + private def sessionHolder(): SessionHolder = { + planHolder.sessionHolder + } Review Comment: (just my ocd concisity preference) - make them all one liners on top of the file; you don't need explicit typing for private methods in scala) -- 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]
