-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
33 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...lugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/DirectPushRestfulApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.engineplugin.spark; | ||
|
||
import org.apache.linkis.engineplugin.spark.utils.DataFrameResponse; | ||
import org.apache.linkis.engineplugin.spark.utils.DirectPushCache; | ||
import org.apache.linkis.server.Message; | ||
|
||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import java.util.Map; | ||
|
||
import io.swagger.annotations.Api; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Api(tags = "DirectPush") | ||
@RestController | ||
@RequestMapping(path = "directpush") | ||
public class DirectPushRestfulApi { | ||
private static final Logger logger = LoggerFactory.getLogger(DirectPushRestfulApi.class); | ||
|
||
@RequestMapping(path = "pull", method = RequestMethod.POST) | ||
public Message getDirectPushResult( | ||
HttpServletRequest req, @RequestBody Map<String, Object> json) { | ||
Message message = null; | ||
try { | ||
String taskId = (String) json.getOrDefault("taskId", null); | ||
if (taskId == null) { | ||
message = Message.error("taskId is null"); | ||
return message; | ||
} | ||
int fetchSize = (int) json.getOrDefault("fetchSize", 1000); | ||
|
||
DataFrameResponse response = DirectPushCache.fetchResultSetOfDataFrame(taskId, fetchSize); | ||
if (response.dataFrame() == null) { | ||
message = Message.error("No result found for taskId: " + taskId); | ||
} else { | ||
message = | ||
Message.ok() | ||
.data("data", response.dataFrame()) | ||
.data("hasMoreData", response.hasMoreData()); | ||
} | ||
} catch (Exception e) { | ||
logger.error("Failed to get direct push result", e); | ||
message = Message.error("Failed to get direct push result: " + e.getMessage()); | ||
} | ||
return message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...ins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/utils/DirectPushCache.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.engineplugin.spark.utils | ||
|
||
import org.apache.linkis.engineconn.common.conf.{EngineConnConf, EngineConnConstant} | ||
|
||
import org.apache.spark.sql.DataFrame | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
import com.google.common.cache.{Cache, CacheBuilder} | ||
|
||
case class DataFrameResponse(dataFrame: DataFrame, hasMoreData: Boolean) | ||
|
||
object DirectPushCache { | ||
|
||
private val resultSet: Cache[String, DataFrame] = CacheBuilder | ||
.newBuilder() | ||
.expireAfterAccess(EngineConnConf.ENGINE_TASK_EXPIRE_TIME.getValue, TimeUnit.MILLISECONDS) | ||
.maximumSize(EngineConnConstant.MAX_TASK_NUM) | ||
.build() | ||
|
||
// This method is not idempotent. After fetching a result set of size fetchSize each time, the corresponding results will be removed from the cache. | ||
def fetchResultSetOfDataFrame(taskId: String, fetchSize: Int): DataFrameResponse = { | ||
val df = DirectPushCache.resultSet.getIfPresent(taskId) | ||
if (df == null) { | ||
throw new IllegalAccessException(s"Task $taskId not exists in resultSet cache.") | ||
} else { | ||
val batchDf = df.limit(fetchSize) | ||
if (batchDf.count() < fetchSize) { | ||
// All the data in df has been consumed. | ||
DirectPushCache.resultSet.invalidate(taskId) | ||
DataFrameResponse(batchDf, hasMoreData = false) | ||
} else { | ||
// Update df with consumed one. | ||
DirectPushCache.resultSet.put(taskId, df.except(batchDf)) | ||
DataFrameResponse(batchDf, hasMoreData = true) | ||
} | ||
} | ||
} | ||
|
||
def isTaskCached(taskId: String): Boolean = { | ||
DirectPushCache.resultSet.getIfPresent(taskId) != null | ||
} | ||
|
||
def submitExecuteResult(taskId: String, df: DataFrame): Unit = { | ||
DirectPushCache.resultSet.put(taskId, df) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters