Skip to content

Commit 233d9f8

Browse files
committed
[appengine] 对话性能优化
1 parent cb897c6 commit 233d9f8

File tree

5 files changed

+85
-60
lines changed

5 files changed

+85
-60
lines changed

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/fitable/FlowPublishSubscriber.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import modelengine.fit.jober.aipp.constants.AippConst;
1010
import modelengine.fit.jober.aipp.domain.AppBuilderRuntimeInfo;
1111
import modelengine.fit.jober.aipp.dto.chat.AppChatRsp;
12+
import modelengine.fit.jober.aipp.entity.ChatSession;
1213
import modelengine.fit.jober.aipp.repository.AppBuilderRuntimeInfoRepository;
1314
import modelengine.fit.jober.aipp.service.AppChatSessionService;
1415
import modelengine.fit.jober.aipp.service.AppChatSseService;
@@ -88,13 +89,17 @@ public void publishNodeInfo(FlowNodePublishInfo flowNodePublishInfo) {
8889

8990
private void stageProcessedHandle(FlowNodePublishInfo flowNodePublishInfo, Map<String, Object> businessData,
9091
String aippInstId) {
92+
Optional<ChatSession<Object>> instanceSession = this.appChatSessionService.getSession(aippInstId);
93+
if (instanceSession.isPresent() && !instanceSession.get().isDebug()) {
94+
return;
95+
}
9196
FlowPublishContext context = flowNodePublishInfo.getFlowContext();
9297
String traceId = context.getTraceId();
9398
String nodeId = flowNodePublishInfo.getNodeId();
9499
String nodeType = flowNodePublishInfo.getNodeType();
95100
FlowErrorInfo errorInfo = flowNodePublishInfo.getErrorMsg();
96101
AtomicReference<Locale> locale = new AtomicReference<>(Locale.getDefault());
97-
appChatSessionService.getSession(aippInstId).ifPresent(e -> locale.set(e.getLocale()));
102+
instanceSession.ifPresent(session -> locale.set(session.getLocale()));
98103
ToolExceptionHandle.handleFitException(errorInfo);
99104
String finalErrorMsg = this.toolExceptionHandle.getFixErrorMsg(errorInfo, locale.get(), false);
100105
if (StringUtils.isBlank(finalErrorMsg)) {
@@ -103,7 +108,7 @@ private void stageProcessedHandle(FlowNodePublishInfo flowNodePublishInfo, Map<S
103108
AppBuilderRuntimeInfo runtimeInfo = AppBuilderRuntimeInfo.builder()
104109
.traceId(traceId)
105110
.flowDefinitionId(flowNodePublishInfo.getFlowDefinitionId())
106-
.instanceId(ObjectUtils.cast(businessData.get(AippConst.BS_AIPP_INST_ID_KEY)))
111+
.instanceId(aippInstId)
107112
.nodeId(nodeId)
108113
.nodeType(nodeType)
109114
.startTime(ConvertUtils.toLong(context.getCreateAt()))

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/service/impl/AippRunTimeServiceImpl.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public String createAippInstance(String aippId, String version, Map<String, Obje
246246
@Fitable("default")
247247
public String createLatestAippInstanceByAppId(String appId, boolean isDebug, Map<String, Object> initContext,
248248
OperationContext context) {
249-
Meta meta = getMetaByAppId(metaService, appId, isDebug, context);
249+
Meta meta = CacheUtils.getMetaByAppId(metaService, appId, isDebug, context);
250250
String metaInstId = this.createAippInstance(meta.getId(), meta.getVersion(), initContext, context);
251251
return metaInstanceService.list(Collections.singletonList(metaInstId), 0, 1, context)
252252
.getResults()
@@ -268,46 +268,16 @@ public String createLatestAippInstanceByAppId(String appId, boolean isDebug, Map
268268
@Override
269269
public Tuple createInstanceByApp(String appId, String question, Map<String, Object> businessData,
270270
OperationContext context, boolean isDebug) {
271-
Meta meta = getMetaByAppId(metaService, appId, isDebug, context);
271+
Meta meta = CacheUtils.getMetaByAppId(metaService, appId, isDebug, context);
272272
return this.createInstanceHandle(question, businessData, meta, context, isDebug);
273273
}
274274

275275
@Override
276276
public MetaVo queryLatestMetaVoByAppId(String appId, boolean isDebug, OperationContext context) {
277-
Meta meta = getMetaByAppId(metaService, appId, isDebug, context);
277+
Meta meta = CacheUtils.getMetaByAppId(metaService, appId, isDebug, context);
278278
return MetaVo.builder().id(meta.getId()).version(meta.getVersion()).build();
279279
}
280280

281-
/**
282-
* 根据appid查询对应meta
283-
*
284-
* @param metaService 操作meta的service
285-
* @param appId 应用appId
286-
* @param isDebug 是否为debug会话
287-
* @param context 操作上下文
288-
* @return app对应的meta信息
289-
*/
290-
public static Meta getMetaByAppId(MetaService metaService, String appId, boolean isDebug,
291-
OperationContext context) {
292-
if (isDebug) {
293-
return getLatestMetaByAppId(metaService, appId, context);
294-
}
295-
// get 一个aipp_id的缓存,然后根据aipp_id查询最新发布版的meta
296-
String aippId = CacheUtils.APP_ID_TO_AIPP_ID_CACHE.get(appId,
297-
(id) -> getLatestMetaByAppId(metaService, id, context).getId());
298-
Meta lastPublishedMeta = MetaUtils.getLastPublishedMeta(metaService, aippId, context);
299-
return Optional.ofNullable(lastPublishedMeta)
300-
.orElseThrow(() -> new AippException(AippErrCode.APP_CHAT_PUBLISHED_META_NOT_FOUND));
301-
}
302-
303-
private static Meta getLatestMetaByAppId(MetaService metaService, String appId, OperationContext context) {
304-
List<Meta> meta = MetaUtils.getAllMetasByAppId(metaService, appId, context);
305-
if (CollectionUtils.isEmpty(meta)) {
306-
throw new AippException(AippErrCode.APP_CHAT_DEBUG_META_NOT_FOUND);
307-
}
308-
return meta.get(0);
309-
}
310-
311281
@Override
312282
public Choir<Object> startFlowWithUserSelectMemory(String metaInstId, Map<String, Object> initContext,
313283
OperationContext context, boolean isDebug) {

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/service/impl/AppChatServiceImpl.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@
4141
import modelengine.fit.jober.aipp.service.AippLogService;
4242
import modelengine.fit.jober.aipp.service.AippRunTimeService;
4343
import modelengine.fit.jober.aipp.service.AppChatService;
44-
import modelengine.fit.jober.aipp.util.AippLogUtils;
45-
import modelengine.fit.jober.aipp.util.AppUtils;
46-
import modelengine.fit.jober.aipp.util.FlowUtils;
47-
import modelengine.fit.jober.aipp.util.JsonUtils;
48-
import modelengine.fit.jober.aipp.util.MetaUtils;
49-
import modelengine.fit.jober.aipp.util.UUIDUtil;
44+
import modelengine.fit.jober.aipp.util.*;
5045
import modelengine.fit.jober.common.ServerInternalException;
5146
import modelengine.fitframework.annotation.Component;
5247
import modelengine.fitframework.annotation.Value;
@@ -132,11 +127,13 @@ public Choir<Object> chat(CreateAppChatRequest body, OperationContext context, b
132127
Map<String, Object> businessData = this.convertContextToBusinessData(body, isDebug);
133128
// 这里几行代码的顺序不可以调整,必须先把对话的appId查询出来,再去创建chatId
134129
String chatAppId = this.getAppId(body);
135-
boolean hasAtOtherApp = this.hasAtOtherApp(body);
130+
boolean hasAtOtherApp = body.hasAtOtherApp();
136131
this.createChatId(body, hasAtOtherApp, businessData);
137132
// create instance —— 根据实际的那个app创建
138133
AppUtils.setAppChatInfo(body.getAppId(), isDebug);
139-
this.appService.updateFlow(chatAppId, context);
134+
if (isDebug) {
135+
this.appService.updateFlow(chatAppId, context);
136+
}
140137
LOGGER.info("[perf] [{}] chat updateFlow end, appId={}", System.currentTimeMillis(), body.getAppId());
141138
this.addUserContext(body, businessData, isDebug, context, app.getType());
142139
Tuple tuple = this.aippRunTimeService.createInstanceByApp(chatAppId,
@@ -150,8 +147,8 @@ public Choir<Object> chat(CreateAppChatRequest body, OperationContext context, b
150147
this.saveChatInfos(body,
151148
context,
152149
ObjectUtils.cast(tuple.get(0).orElseThrow(this::generalServerException)),
153-
hasAtOtherApp,
154-
chatAppId);
150+
chatAppId,
151+
isDebug);
155152
} catch (AippTaskNotFoundException e) {
156153
throw new AippException(TASK_NOT_FOUND);
157154
}
@@ -243,15 +240,15 @@ private void validateApp(String appId) {
243240
}
244241

245242
private void saveChatInfos(CreateAppChatRequest body, OperationContext context, String instId,
246-
boolean hasAtOtherApp, String chatAppId) throws AippTaskNotFoundException {
243+
String chatAppId, boolean isDebug) throws AippTaskNotFoundException {
247244
AppBuilderApp app = this.appFactory.create(body.getAppId());
248245
Map<String, String> attributes = new HashMap<>();
249-
List<Meta> metas = MetaUtils.getAllMetasByAppId(this.metaService, chatAppId, context);
250-
if (CollectionUtils.isEmpty(metas)) {
251-
LOGGER.error("metas is empty.");
246+
Meta meta = CacheUtils.getMetaByAppId(this.metaService, chatAppId, isDebug, context);
247+
if (meta == null) {
248+
LOGGER.error("Cannot find meta for chat app. [appId={}, instId={}]", chatAppId, instId);
252249
throw new AippTaskNotFoundException(TASK_NOT_FOUND);
253250
}
254-
String aippId = metas.get(0).getId();
251+
String aippId = meta.getId();
255252
attributes.put(AippConst.ATTR_CHAT_INST_ID_KEY, instId);
256253
attributes.put(AippConst.ATTR_CHAT_STATE_KEY, app.getState());
257254
attributes.put(AippConst.BS_AIPP_ID_KEY, aippId);
@@ -261,7 +258,7 @@ private void saveChatInfos(CreateAppChatRequest body, OperationContext context,
261258
String chatId = body.getChatId();
262259
this.buildAndInsertChatInfo(app, attributes, body.getQuestion(), chatId, context.getOperator());
263260
this.buildAndInsertWideRelationInfo(instId, chatId);
264-
if (hasAtOtherApp) {
261+
if (body.hasAtOtherApp()) {
265262
AppBuilderApp chatApp = this.appFactory.create(chatAppId);
266263
// 被@的应用的对话
267264
Map<String, String> originAttributes = new HashMap<>();
@@ -287,9 +284,9 @@ private Map<String, Object> convertContextToBusinessData(CreateAppChatRequest bo
287284

288285
private void addUserContext(CreateAppChatRequest body, Map<String, Object> businessData, boolean isDebug,
289286
OperationContext context, String appType) {
290-
Meta meta = getMetaByAppId(metaService, body.getAppId(), isDebug, context);
287+
Meta meta = CacheUtils.getMetaByAppId(this.metaService, body.getAppId(), isDebug, context);
291288
String flowDefinitionId = ObjectUtils.cast(meta.getAttributes().get(ATTR_FLOW_DEF_ID_KEY));
292-
List<AppInputParam> inputParams = FlowUtils.getAppInputParams(flowsService, flowDefinitionId, context);
289+
List<AppInputParam> inputParams = FlowUtils.getAppInputParams(this.flowsService, flowDefinitionId, context);
293290
if (StringUtils.equals(APP.code(), appType)) {
294291
inputParams = inputParams.stream()
295292
.filter(param -> !StringUtils.equals("Question", param.getName()))
@@ -376,11 +373,6 @@ private String getAppId(CreateAppChatRequest body) {
376373
return body.getAppId();
377374
}
378375

379-
private boolean hasAtOtherApp(CreateAppChatRequest body) {
380-
return StringUtils.isNotBlank(body.getContext().getAtChatId()) || StringUtils.isNotBlank(body.getContext()
381-
.getAtAppId());
382-
}
383-
384376
private void createChatId(CreateAppChatRequest body, boolean hasAtOtherApp, Map<String, Object> businessData) {
385377
// body里没有chatId:第一次对话
386378
if (StringUtils.isBlank(body.getChatId())) {

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/util/CacheUtils.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
import modelengine.fit.jade.waterflow.FlowsService;
1313
import modelengine.fit.jade.waterflow.dto.FlowInfo;
1414
import modelengine.fit.jane.common.entity.OperationContext;
15+
import modelengine.fit.jane.meta.multiversion.MetaService;
16+
import modelengine.fit.jane.meta.multiversion.definition.Meta;
17+
import modelengine.fit.jober.aipp.common.exception.AippErrCode;
18+
import modelengine.fit.jober.aipp.common.exception.AippException;
1519
import modelengine.fit.jober.aipp.po.AppBuilderAppPo;
1620
import modelengine.fitframework.annotation.Component;
21+
import modelengine.fitframework.util.CollectionUtils;
1722

23+
import java.util.List;
24+
import java.util.Optional;
1825
import java.util.concurrent.TimeUnit;
1926

2027
/**
@@ -50,29 +57,70 @@ public class CacheUtils {
5057
.maximumSize(1000)
5158
.build();
5259

60+
/**
61+
* 用于缓存app_id和meta的关系
62+
*/
63+
public static final Cache<String, Meta> AIPP_ID_LAST_META_CACHE = Caffeine.newBuilder()
64+
.expireAfterAccess(5, TimeUnit.SECONDS)
65+
.maximumSize(1000)
66+
.build();
67+
5368
/**
5469
* 清理缓存
5570
*/
5671
public static void clear() {
5772
APP_CACHE.invalidateAll();
5873
FLOW_CACHE.invalidateAll();
5974
APP_ID_TO_AIPP_ID_CACHE.invalidateAll();
75+
AIPP_ID_LAST_META_CACHE.invalidateAll();
6076

6177
APP_CACHE.cleanUp();
6278
FLOW_CACHE.cleanUp();
6379
APP_ID_TO_AIPP_ID_CACHE.cleanUp();
80+
AIPP_ID_LAST_META_CACHE.cleanUp();
6481
}
6582

6683
/**
6784
* 用于获取flowdefinition的缓存
6885
*
69-
* @param flowsService 操作flow的service
86+
* @param flowsService 操作flow的service
7087
* @param flowDefinitionId 缓存的flowDefinition的id
71-
* @param context 人员上下文
88+
* @param context 人员上下文
7289
* @return 缓存的FlowInfo
7390
*/
7491
public static FlowInfo getPublishedFlowWithCache(FlowsService flowsService, String flowDefinitionId,
7592
OperationContext context) {
7693
return FLOW_CACHE.get(flowDefinitionId, id -> flowsService.getFlows(id, context));
7794
}
95+
96+
/**
97+
* 根据appId查询对应meta
98+
*
99+
* @param metaService 用于查询meta的服务实例
100+
* @param appId 应用appId
101+
* @param isDebug 是否为debug会话
102+
* @param context 操作上下文
103+
* @return app对应的meta信息
104+
*/
105+
public static Meta getMetaByAppId(MetaService metaService, String appId, boolean isDebug,
106+
OperationContext context) {
107+
if (isDebug) {
108+
return getLatestMetaByAppId(metaService, appId, context);
109+
}
110+
// get一个aipp_id的缓存,然后根据aipp_id查询最新发布版的meta
111+
String aippId = APP_ID_TO_AIPP_ID_CACHE.get(appId, id -> getLatestMetaByAppId(metaService, id, context).getId());
112+
return AIPP_ID_LAST_META_CACHE.get(aippId, (ignore) -> {
113+
Meta lastPublishedMeta = MetaUtils.getLastPublishedMeta(metaService, appId, context);
114+
return Optional.ofNullable(lastPublishedMeta)
115+
.orElseThrow(() -> new AippException(AippErrCode.APP_CHAT_PUBLISHED_META_NOT_FOUND));
116+
});
117+
}
118+
119+
private static Meta getLatestMetaByAppId(MetaService metaService, String appId, OperationContext context) {
120+
List<Meta> metas = MetaUtils.getAllMetasByAppId(metaService, appId, context);
121+
if (CollectionUtils.isEmpty(metas)) {
122+
throw new AippException(AippErrCode.APP_CHAT_DEBUG_META_NOT_FOUND);
123+
}
124+
return metas.get(0);
125+
}
78126
}

app-builder/jane/services/aipp-service/src/main/java/modelengine/fit/jober/aipp/dto/chat/CreateAppChatRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import lombok.NoArgsConstructor;
1313
import modelengine.fit.jober.aipp.constants.AippConst;
1414
import modelengine.fitframework.annotation.Property;
15+
import modelengine.fitframework.util.StringUtils;
1516

1617
import java.util.Map;
1718

@@ -38,6 +39,15 @@ public class CreateAppChatRequest {
3839
@Property(description = "context", name = "context")
3940
private Context context;
4041

42+
/**
43+
* 判断是否有@应用
44+
*
45+
* @return true-有; false-没有
46+
*/
47+
public boolean hasAtOtherApp() {
48+
return StringUtils.isNotBlank(getContext().getAtChatId()) || StringUtils.isNotBlank(getContext().getAtAppId());
49+
}
50+
4151
/**
4252
* 本类表示对话的上下文
4353
*/

0 commit comments

Comments
 (0)