forked from apache/linkis
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ComputationEngineConnManager.scala
194 lines (178 loc) · 7.35 KB
/
ComputationEngineConnManager.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* 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.linkis.orchestrator.ecm
import org.apache.linkis.common.ServiceInstance
import org.apache.linkis.common.exception.LinkisRetryException
import org.apache.linkis.common.utils.{Logging, Utils}
import org.apache.linkis.governance.common.conf.GovernanceCommonConf
import org.apache.linkis.manager.common.entity.node.EngineNode
import org.apache.linkis.manager.common.protocol.engine.{
EngineAskAsyncResponse,
EngineAskRequest,
EngineCreateError,
EngineCreateSuccess
}
import org.apache.linkis.manager.label.constant.LabelKeyConstant
import org.apache.linkis.manager.label.entity.entrance.BindEngineLabel
import org.apache.linkis.orchestrator.ecm.cache.EngineAsyncResponseCache
import org.apache.linkis.orchestrator.ecm.conf.ECMPluginConf
import org.apache.linkis.orchestrator.ecm.entity.{DefaultMark, Mark, MarkReq, Policy}
import org.apache.linkis.orchestrator.ecm.exception.ECMPluginErrorException
import org.apache.linkis.orchestrator.ecm.service.EngineConnExecutor
import org.apache.linkis.orchestrator.ecm.service.impl.{
ComputationConcurrentEngineConnExecutor,
ComputationEngineConnExecutor
}
import org.apache.linkis.rpc.Sender
import org.apache.commons.lang3.exception.ExceptionUtils
import java.net.{ConnectException, SocketException, SocketTimeoutException}
import java.util
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import scala.collection.JavaConverters._
import scala.concurrent.duration.Duration
/**
*/
class ComputationEngineConnManager extends AbstractEngineConnManager with Logging {
private val idCreator = new AtomicInteger()
private val cacheMap = EngineAsyncResponseCache.getCache
override def getPolicy(): Policy = Policy.Process
override def applyMark(markReq: MarkReq): Mark = {
if (null == markReq) return null
createMark(markReq)
}
override def createMark(markReq: MarkReq): Mark = {
val mark = new DefaultMark(nextMarkId(), markReq)
addMark(mark, new util.ArrayList[ServiceInstance]())
mark
}
protected def nextMarkId(): String = {
"mark_" + idCreator.getAndIncrement()
}
override protected def askEngineConnExecutor(
engineAskRequest: EngineAskRequest,
mark: Mark
): EngineConnExecutor = {
engineAskRequest.setTimeOut(getEngineConnApplyTime)
var count = getEngineConnApplyAttempts()
var retryException: LinkisRetryException = null
while (count >= 1) {
count = count - 1
val start = System.currentTimeMillis()
try {
val engineNode = getEngineNodeAskManager(engineAskRequest, mark)
if (null != engineNode) {
val engineConnExecutor =
if (
null != engineAskRequest.getLabels &&
engineAskRequest.getLabels.containsKey(LabelKeyConstant.CONCURRENT_ENGINE_KEY)
) {
new ComputationConcurrentEngineConnExecutor(engineNode, getParallelism())
} else {
new ComputationEngineConnExecutor(engineNode)
}
if (null != engineNode.getLabels) {
engineConnExecutor.setLabels(engineNode.getLabels.asScala.toList.toArray)
}
return engineConnExecutor
}
} catch {
case t: LinkisRetryException =>
val taken = System.currentTimeMillis() - start
logger.warn(
s"${mark.getMarkId()} Failed to askEngineAskRequest time taken ($taken), ${t.getMessage}"
)
retryException = t
case t: Throwable =>
val taken = System.currentTimeMillis() - start
logger.warn(s"${mark.getMarkId()} Failed to askEngineAskRequest time taken ($taken)")
throw t
}
}
if (retryException != null) {
throw retryException
} else {
throw new ECMPluginErrorException(
ECMPluginConf.ECM_ERROR_CODE,
s"${mark.getMarkId()} Failed to ask engineAskRequest $engineAskRequest by retry ${getEngineConnApplyAttempts - count} "
)
}
}
private def getEngineNodeAskManager(
engineAskRequest: EngineAskRequest,
mark: Mark
): EngineNode = {
val response = Utils.tryCatch(getManagerSender().ask(engineAskRequest)) { t: Throwable =>
val baseMsg = s"mark ${mark.getMarkId()} failed to ask linkis Manager Can be retried "
ExceptionUtils.getRootCause(t) match {
case socketTimeoutException: SocketTimeoutException =>
val msg = baseMsg + ExceptionUtils.getRootCauseMessage(t)
throw new LinkisRetryException(ECMPluginConf.ECM_ENGNE_CREATION_ERROR_CODE, msg)
case socketException: SocketException =>
val msg = baseMsg + ExceptionUtils.getRootCauseMessage(t)
throw new LinkisRetryException(ECMPluginConf.ECM_ENGNE_CREATION_ERROR_CODE, msg)
case connectException: ConnectException =>
val msg = baseMsg + ExceptionUtils.getRootCauseMessage(t)
throw new LinkisRetryException(ECMPluginConf.ECM_ENGNE_CREATION_ERROR_CODE, msg)
case _ =>
throw t
}
}
response match {
case engineNode: EngineNode =>
logger.debug(s"Succeed to get engineNode $engineNode mark ${mark.getMarkId()}")
engineNode
case EngineAskAsyncResponse(id, serviceInstance) =>
logger.info(
s"${mark.getMarkId()} received EngineAskAsyncResponse id: ${id} serviceInstance: $serviceInstance "
)
cacheMap.getAndRemove(
id,
Duration(engineAskRequest.getTimeOut + 100000, TimeUnit.MILLISECONDS)
) match {
case EngineCreateSuccess(id, engineNode) =>
logger.info(
s"${mark.getMarkId()} async id:$id success to async get EngineNode $engineNode"
)
engineNode
case EngineCreateError(id, exception, retry) =>
logger.debug(
s"${mark.getMarkId()} async id:$id Failed to async get EngineNode, $exception"
)
if (retry) {
throw new LinkisRetryException(
ECMPluginConf.ECM_ENGNE_CREATION_ERROR_CODE,
id + " Failed to async get EngineNode " + exception
)
} else {
throw new ECMPluginErrorException(
ECMPluginConf.ECM_ENGNE_CREATION_ERROR_CODE,
id + " Failed to async get EngineNode " + exception
)
}
}
case _ =>
logger.info(
s"${mark.getMarkId()} Failed to ask engineAskRequest $engineAskRequest, response is not engineNode"
)
null
}
}
private def getManagerSender(): Sender = {
Sender.getSender(GovernanceCommonConf.MANAGER_SPRING_NAME.getValue)
}
}