forked from apache/linkis
-
Notifications
You must be signed in to change notification settings - Fork 5
/
EngineConnManager.scala
258 lines (211 loc) · 8.57 KB
/
EngineConnManager.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* 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.utils.{Logging, Utils}
import org.apache.linkis.manager.common.protocol.engine.EngineAskRequest
import org.apache.linkis.manager.label.constant.LabelKeyConstant
import org.apache.linkis.orchestrator.ecm.conf.ECMPluginConf
import org.apache.linkis.orchestrator.ecm.entity.{Mark, MarkReq, Policy}
import org.apache.linkis.orchestrator.ecm.exception.ECMPluginErrorException
import org.apache.linkis.orchestrator.ecm.service.EngineConnExecutor
import org.apache.commons.collections.CollectionUtils
import java.util
import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer
/**
*/
trait EngineConnManager {
/**
* Apply for a Mark
* 1. If there is no corresponding Mark, generate a new one 2. Return Mark
*
* @param markReq
* @return
*/
def applyMark(markReq: MarkReq): Mark
/**
* 1. Create a new Mark 2. There will be a process of requesting the engine when generating a
* new Mark. If the request is received, it will be stored in the Map: Mark is Key,
* EngineConnExecutor is Value
*/
def createMark(markReq: MarkReq): Mark
/**
* Obtain an available EngineConnExecutor from the cache through Mark
*
* @param mark
* @return
*/
def getAvailableEngineConnExecutor(mark: Mark): EngineConnExecutor
/**
* Remove the engineConn related to the Mark Release lock and other information
*
* @param mark
*/
def releaseMark(mark: Mark): Unit
def releaseEngineConnExecutor(engineConnExecutor: EngineConnExecutor, mark: Mark): Unit
def getEngineConnExecutorCache(): util.Map[ServiceInstance, EngineConnExecutor]
protected def getMarkCache(): util.Map[Mark, util.List[ServiceInstance]]
def getPolicy(): Policy
def setParallelism(parallelism: Int): Unit
def getParallelism(): Int
def setEngineConnApplyTime(applyTime: Long): Unit
def getEngineConnApplyTime: Long
def setEngineConnApplyAttempts(attemptNumber: Int): Unit
def getEngineConnApplyAttempts(): Int
}
abstract class AbstractEngineConnManager extends EngineConnManager with Logging {
private var parallelism: Int = _
private var timeOut: Long = _
private var attemptNumber: Int = _
private val engineConnExecutorCache = new util.HashMap[ServiceInstance, EngineConnExecutor]()
private val markCache: util.Map[Mark, util.List[ServiceInstance]] =
new util.concurrent.ConcurrentHashMap[Mark, util.List[ServiceInstance]]()
override def setEngineConnApplyAttempts(attemptNumber: Int): Unit = this.attemptNumber =
attemptNumber
override def getEngineConnApplyAttempts(): Int = this.attemptNumber
override def getParallelism(): Int = this.parallelism
override def setParallelism(parallelism: Int): Unit = this.parallelism = parallelism
override def getEngineConnApplyTime: Long = this.timeOut
override def setEngineConnApplyTime(applyTime: Long): Unit = this.timeOut = applyTime
override def getEngineConnExecutorCache(): util.Map[ServiceInstance, EngineConnExecutor] =
engineConnExecutorCache
override def getMarkCache(): util.Map[Mark, util.List[ServiceInstance]] = markCache
override def getAvailableEngineConnExecutor(mark: Mark): EngineConnExecutor = {
logger.info(s"mark ${mark.getMarkId()} start to getAvailableEngineConnExecutor")
if (null != mark) {
tryReuseEngineConnExecutor(mark) match {
case Some(engineConnExecutor) => return engineConnExecutor
case None =>
}
val engineConnExecutor =
askEngineConnExecutor(mark.getMarkReq.createEngineConnAskReq(), mark)
engineConnExecutor.useEngineConn
saveToMarkCache(mark, engineConnExecutor)
logger.debug(
s"mark ${mark.getMarkId()} Finished to getAvailableEngineConnExecutor by create"
)
engineConnExecutor
} else {
throw new ECMPluginErrorException(
ECMPluginConf.ECM_ERROR_CODE,
s" mark ${mark.getMarkId()} cannot null"
)
}
}
protected def tryReuseEngineConnExecutor(mark: Mark): Option[EngineConnExecutor] = {
val instances = getInstances(mark)
if (null != instances) {
val executors = Utils.tryAndWarn {
instances.asScala.map(getEngineConnExecutorCache().get(_)).filter(null != _).sortBy {
executor =>
if (executor.getRunningTaskCount < 0) {
0
} else {
executor.getRunningTaskCount
}
}
}
if (null != executors && executors.nonEmpty) {
if (mark.getMarkReq.getLabels.containsKey(LabelKeyConstant.BIND_ENGINE_KEY)) {
// must use the existed engine
return Some(executors.headOption.get)
}
for (executor <- executors) {
if (executor.useEngineConn) {
logger.info(
s"mark ${mark.getMarkId()} Finished to getAvailableEngineConnExecutor by reuse"
)
return Some(executor)
}
}
}
}
None
}
protected def saveToMarkCache(mark: Mark, engineConnExecutor: EngineConnExecutor) = {
getEngineConnExecutorCache().put(engineConnExecutor.getServiceInstance, engineConnExecutor)
if (null == getInstances(mark)) {
addMark(mark, new util.ArrayList[ServiceInstance]())
}
val markedInstances = getMarkCache().get(mark)
if (markedInstances.isEmpty) {
markedInstances.add(engineConnExecutor.getServiceInstance)
} else if (!mark.getMarkReq.getLabels.containsKey(LabelKeyConstant.BIND_ENGINE_KEY)) {
markedInstances.add(engineConnExecutor.getServiceInstance)
}
}
protected def addMark(mark: Mark, instances: util.List[ServiceInstance]): Unit =
if (null != mark && !getMarkCache().containsKey(mark)) {
logger.info(s"add mark ${mark.getMarkId()}")
getMarkCache().put(mark, instances)
}
protected def getInstances(mark: Mark): util.List[ServiceInstance] = if (null != mark) {
getMarkCache().get(mark)
} else {
null
}
protected def getMarksByInstance(serviceInstance: ServiceInstance): Array[Mark] = {
val markAndInstance = getMarkCache().entrySet().iterator()
val buffer = new ArrayBuffer[Mark]()
while (markAndInstance.hasNext) {
val next = markAndInstance.next()
if (next.getValue != null && next.getValue.indexOf(serviceInstance) >= 0) {
buffer.append(next.getKey)
}
}
buffer.toArray
}
override def releaseEngineConnExecutor(
engineConnExecutor: EngineConnExecutor,
mark: Mark
): Unit = {
if (null != engineConnExecutor && null != mark && getMarkCache().containsKey(mark)) {
logger.info(
s"Start to release EngineConnExecutor mark id ${mark.getMarkId()} engineConnExecutor ${engineConnExecutor.getServiceInstance}"
)
getEngineConnExecutorCache().remove(engineConnExecutor.getServiceInstance)
engineConnExecutor.close()
val instances = getInstances(mark)
if (null != instances) {
instances.remove(engineConnExecutor.getServiceInstance)
if (instances.isEmpty) releaseMark(mark)
}
}
}
protected def askEngineConnExecutor(
engineAskRequest: EngineAskRequest,
mark: Mark
): EngineConnExecutor
override def releaseMark(mark: Mark): Unit = {
if (null != mark && getMarkCache().containsKey(mark)) {
logger.debug(s"Start to release mark ${mark.getMarkId()}")
val executors = getMarkCache().get(mark).asScala.map(getEngineConnExecutorCache().get(_))
Utils.tryAndError(executors.foreach { executor =>
getEngineConnExecutorCache().remove(executor.getServiceInstance)
executor.close()
})
removeMark(mark)
logger.info(s"Finished to release mark ${mark.getMarkId()}")
}
}
protected def removeMark(mark: Mark): Unit = {
if (null != mark) {
getMarkCache().remove(mark)
}
}
}