ChengJie1053 commented on code in PR #2989: URL: https://github.com/apache/incubator-streampark/pull/2989#discussion_r1315821528
########## streampark-console/streampark-console-service/src/main/scala/org/apache/streampark/console/core/task/FlinkK8sChangeListener.scala: ########## @@ -0,0 +1,222 @@ +/* + * 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.streampark.console.core.task + +import org.apache.streampark.common.enums.ExecutionMode +import org.apache.streampark.common.util.ThreadUtils +import org.apache.streampark.common.zio.ZIOContainerSubscription.{ConcurrentMapExtension, RefMapExtension} +import org.apache.streampark.common.zio.ZIOExt.IOOps +import org.apache.streampark.console.core.entity.{Application, FlinkCluster} +import org.apache.streampark.console.core.enums.{FlinkAppState, OptionState} +import org.apache.streampark.console.core.service.{ApplicationService, FlinkClusterService} +import org.apache.streampark.console.core.service.alert.AlertService +import org.apache.streampark.console.core.utils.FlinkAppStateConverter +import org.apache.streampark.flink.kubernetes.v2.model._ +import org.apache.streampark.flink.kubernetes.v2.model.EvalJobState.EvalJobState +import org.apache.streampark.flink.kubernetes.v2.observer.{FlinkK8sObserver, Name, Namespace} +import org.apache.streampark.flink.kubernetes.v2.operator.OprError.TrackKeyNotFound + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.annotation.Lazy +import org.springframework.stereotype.Component +import zio.{UIO, ZIO} + +import java.util.Date +import java.util.concurrent.{ExecutorService, LinkedBlockingQueue, ThreadPoolExecutor, TimeUnit} + +@Component +class FlinkK8sChangeListener { + @Lazy @Autowired + private var applicationService: ApplicationService = _ + + @Lazy @Autowired + private var flinkClusterService: FlinkClusterService = _ + @Lazy @Autowired + private var alertService: AlertService = _ + + private val executor: ExecutorService = new ThreadPoolExecutor( + Runtime.getRuntime.availableProcessors * 5, + Runtime.getRuntime.availableProcessors * 10, + 20L, + TimeUnit.SECONDS, + new LinkedBlockingQueue[Runnable](1024), + ThreadUtils.threadFactory("streampark-notify-executor"), + new ThreadPoolExecutor.AbortPolicy) + + subscribeJobStatusChange.forkDaemon.runIO + subscribeMetricsChange.forkDaemon.runIO + subscribeRestSvcEndpointChange.forkDaemon.runIO + + def subscribeJobStatusChange: UIO[Unit] = { + FlinkK8sObserver.evaluatedJobSnaps + .flatSubscribeValues() + .foreach { + jobSnap => + ZIO + .attempt { + // update application recrod + val appId = jobSnap.appId + // get pre application record + val app: Application = applicationService.getById(appId) + if (app == null || jobSnap.evalState != null) return ZIO.unit + applicationService.persistMetrics(app) + // update application record + setByJobStatusCV(app, jobSnap) + + // send alert + val state: FlinkAppState = FlinkAppState.of(app.getState) + if ( + FlinkAppState.FAILED == state || FlinkAppState.LOST == state || FlinkAppState.RESTARTING == state || FlinkAppState.FINISHED == state + ) { + executor.execute(() => alertService.alert(app, state)) + } + + } + .retryN(3) + .ignore + } + } + + def subscribeMetricsChange: UIO[Unit] = { + FlinkK8sObserver.clusterMetricsSnaps + .flatSubscribe() + .foreach { + metricsSnap => + ZIO + .attempt { + val namespaceAndName: (Namespace, Name) = metricsSnap._1 + val trackKey: ZIO[Any, TrackKeyNotFound, TrackKey] = FlinkK8sObserver.trackedKeys + .find( + trackedKey => + trackedKey.clusterNamespace == namespaceAndName._1 && trackedKey.clusterName == namespaceAndName._2) + .someOrFail(TrackKeyNotFound(namespaceAndName._1, namespaceAndName._2)) + + val app: Application = applicationService.getById(trackKey.map(_.id)) + + // discard session mode change + if (app == null || ExecutionMode.isKubernetesSessionMode(app.getExecutionMode)) + return ZIO.unit + + val clusterMetrics: ClusterMetrics = metricsSnap._2 + app.setJmMemory(clusterMetrics.totalJmMemory) + app.setTmMemory(clusterMetrics.totalTmMemory) + app.setTotalTM(clusterMetrics.totalTm) + app.setTotalSlot(clusterMetrics.totalSlot) + app.setAvailableSlot(clusterMetrics.availableSlot) + applicationService.persistMetrics(app) + } + .retryN(3) + .ignore + } + } + + def subscribeRestSvcEndpointChange: UIO[Unit] = { + FlinkK8sObserver.restSvcEndpointSnaps + .flatSubscribe() + .foreach { + restSvcEndpointSnap => + ZIO + .attempt { + + val namespaceAndName: (Namespace, Name) = restSvcEndpointSnap._1 + val trackKey: ZIO[Any, TrackKeyNotFound, TrackKey] = FlinkK8sObserver.trackedKeys + .find( + trackedKey => + trackedKey.clusterNamespace == namespaceAndName._1 && trackedKey.clusterName == namespaceAndName._2) + .someOrFail(TrackKeyNotFound(namespaceAndName._1, namespaceAndName._2)) + val restSvcEndpoint: RestSvcEndpoint = restSvcEndpointSnap._2 + + val app: Application = applicationService.getById(trackKey.map(_.id)) + + val flinkCluster: FlinkCluster = flinkClusterService.getById(app.getFlinkClusterId) + + if (restSvcEndpoint == null || restSvcEndpoint.chooseRest == null) return ZIO.unit + val url = restSvcEndpoint.chooseRest Review Comment: Ok, thanks for reviewing the 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]
