Skip to content

Commit

Permalink
KYLIN-2971 Fix the wrong "Realization Names" and missing "Cuboid Ids"…
Browse files Browse the repository at this point in the history
… in logQuery when hit cache

Problems:
1. The value of "Realization Names" in logQuery is wrong when query two different sqls within the same thread and second sql hits cache:
Example:
 1). query Q1 hit project P1 and cube C1;
 2). query Q2 hit project P2 and cube C2 in the same thread with Q1;
 3). Q1 comes again and hits cache, it will show project P1 and cube C2. However, it should be cube C1.

2. Missing "Cuboid Ids" in logQuery when hit cache in a new thread which does not have OLAPContext;

Solutions:
1. Call 'OLAPContext.clearThreadLocalContexts()' before a query starts;
2. Get "Cuboid Ids" from SQLResponse when "OLAPContext.getThreadLocalContexts()" is null;
  • Loading branch information
zzcclp committed May 21, 2020
1 parent 7886a24 commit 2ef96ec
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.apache.calcite.prepare.OnlyPrepareEarlyAbortException;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.sql.type.BasicSqlType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
Expand Down Expand Up @@ -325,12 +327,30 @@ public void logQuery(final String queryId, final SQLRequest request, final SQLRe
}
}

// if Realization Names is empty, get value from SQLResponse.
if (realizationNames.isEmpty()) {
if (!Strings.isNullOrEmpty(response.getCube())) {
realizationNames.addAll(Lists.newArrayList(StringUtil.splitByComma(response.getCube())));
}
}

// if Cuboid Ids is empty, get value from SQLResponse.
if (cuboidIds.isEmpty()) {
List<QueryContext.CubeSegmentStatisticsResult> cubeSegmentStatisticsList =
response.getCubeSegmentStatisticsList();
if (CollectionUtils.isNotEmpty(cubeSegmentStatisticsList)) {
cubeSegmentStatisticsList.forEach(cubeSegmentStatResult -> {
if (MapUtils.isNotEmpty(cubeSegmentStatResult.getCubeSegmentStatisticsMap())) {
cubeSegmentStatResult.getCubeSegmentStatisticsMap().values().forEach(cubeSegmentStatMap -> {
cubeSegmentStatMap.values().forEach(cubeSegmentStat -> {
cuboidIds.add(cubeSegmentStat.getTargetCuboidId());
});
});
}
});
}
}

int resultRowCount = 0;
if (!response.getIsException() && response.getResults() != null) {
resultRowCount = response.getResults().size();
Expand Down Expand Up @@ -411,6 +431,9 @@ public SQLResponse doQueryWithCache(SQLRequest sqlRequest, boolean isQueryInspec
queryContext.setProject(sqlRequest.getProject());

try (SetThreadName ignored = new SetThreadName("Query %s", queryContext.getQueryId())) {
// force clear the query context before a new query
OLAPContext.clearThreadLocalContexts();

SQLResponse sqlResponse = null;
String sql = sqlRequest.getSql();
String project = sqlRequest.getProject();
Expand Down Expand Up @@ -660,8 +683,6 @@ private SQLResponse queryWithSqlMassage(SQLRequest sqlRequest) throws Exception
parameters.put(OLAPContext.PRM_USER_AUTHEN_INFO, userInfo);
parameters.put(OLAPContext.PRM_ACCEPT_PARTIAL_RESULT, String.valueOf(sqlRequest.isAcceptPartial()));
OLAPContext.setParameters(parameters);
// force clear the query context before a new query
OLAPContext.clearThreadLocalContexts();

// special case for prepare query.
List<List<String>> results = Lists.newArrayList();
Expand Down

0 comments on commit 2ef96ec

Please sign in to comment.