Skip to content

Commit

Permalink
[Refactor](StmtExecutor)(step-1) Extract profile logic from StmtExecu…
Browse files Browse the repository at this point in the history
…tor and Coordinator (apache#19219)

Previously, we use RuntimeProfile class directly, and because there are multiple level in profile, so you can see there may be several RuntimeProfile instances be to maintain.

I created several new classes for profile:

class Profile:
	The root profile of a execution task(query or load)
	
class SummaryProfile:
	The profile that contains summary info of a execution task,
	such as start time, end time, query id. etc.
	
class ExecutionProfile:
	The profile for a single Coordinator. Each Coordinator will
	have a ExecutionProfile.
The profile structure is as following:

Profile:
	SummaryProfile:
	ExecutionProfile 1:
		Fragment 0:
			Instance 0:
			Instance 1:
			...
		Fragment 1:
		...
	ExecutionProfile 2:
		...
You can see, each Profile has a SummaryProfile and one or more ExecutionProfile.
For most kinds of job, such as query/insert, there is only one ExecutionProfile. But for broker load job, will may be more than one ExecutionProfile, corresponding to each sub task of the load job.

How to use
For query/insert, etc:

Each StmtExcutor will have a Profile instance.
Each Coordinator will have a ExecutionProfile instance.
StmtExcutor is responsible for the SummaryProfile, it will update the SummaryProfile during the execution.
Coordinator is responsible for the ExecutionProfile, it will first add ExecutionProfile to the child of Profile, and update the ExecutionProfile periodically during the execution.
For Load/Export, etc:

Each job will hava a Profile instance.
For each Coordinator of this job, add its ExecutionProfile to the children of job's Profile.
Behavior Change
The columns of show load profile/show query profile and QueryProfile Web UI has changed to:

| Profile ID | Task Type | Start Time | End Time | Total | Task State | User | Default Db| Sql Statement | Is Cached | Total Instances Num | Instances Num Per BE | Parallel Fragment Exec Instance Num | Trace ID |
The Query Id and Job Id is removed and using Profile ID instead.
For load job, the profile id is job id, for query/insert, is query id.
  • Loading branch information
morningman authored and Reminiscent committed May 15, 2023
1 parent 166086a commit ae2c4af
Show file tree
Hide file tree
Showing 28 changed files with 689 additions and 626 deletions.
2 changes: 1 addition & 1 deletion build-for-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ if [[ "${_USE_AVX2}" == "0" && "${ARCH}" == "x86_64" ]]; then
OUTPUT_BE="${OUTPUT_BE}-noavx2"
fi

echo "Pakage Name:"
echo "Package Name:"
echo "FE: ${OUTPUT_FE}"
echo "BE: ${OUTPUT_BE}"
echo "JAR: ${OUTPUT_DEPS}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
import org.apache.doris.common.profile.SummaryProfile;
import org.apache.doris.qe.ShowResultSetMetaData;

import com.google.common.base.Strings;
Expand All @@ -32,26 +33,7 @@
// show query profile "/e0f7390f5363419e-b416a2a79996083e/0/e0f7390f5363419e-b416a2a799960906" # show instance's graph
public class ShowQueryProfileStmt extends ShowStmt {
// This should be same as ProfileManager.PROFILE_HEADERS
public static final ShowResultSetMetaData META_DATA_QUERY_IDS =
ShowResultSetMetaData.builder()
.addColumn(new Column("JobId", ScalarType.createVarchar(128)))
.addColumn(new Column("QueryId", ScalarType.createVarchar(128)))
.addColumn(new Column("User", ScalarType.createVarchar(128)))
.addColumn(new Column("DefaultDb", ScalarType.createVarchar(128)))
.addColumn(new Column("SQL", ScalarType.createVarchar(65535)))
.addColumn(new Column("QueryType", ScalarType.createVarchar(128)))
.addColumn(new Column("StartTime", ScalarType.createVarchar(128)))
.addColumn(new Column("EndTime", ScalarType.createVarchar(128)))
.addColumn(new Column("TotalTime", ScalarType.createVarchar(128)))
.addColumn(new Column("QueryState", ScalarType.createVarchar(128)))
.addColumn(new Column("TraceId", ScalarType.createVarchar(128)))
.addColumn(new Column("AnalysisTime", ScalarType.createVarchar(128)))
.addColumn(new Column("PlanTime", ScalarType.createVarchar(128)))
.addColumn(new Column("ScheduleTime", ScalarType.createVarchar(128)))
.addColumn(new Column("FetchResultTime", ScalarType.createVarchar(128)))
.addColumn(new Column("WriteResultTime", ScalarType.createVarchar(128)))
.addColumn(new Column("WaitAndFetchResultTime", ScalarType.createVarchar(128)))
.build();
public static final ShowResultSetMetaData META_DATA_QUERY_IDS;

public static final ShowResultSetMetaData META_DATA_FRAGMENTS =
ShowResultSetMetaData.builder()
Expand All @@ -68,6 +50,14 @@ public class ShowQueryProfileStmt extends ShowStmt {
.addColumn(new Column("Instance", ScalarType.createVarchar(65535)))
.build();

static {
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
for (String key : SummaryProfile.SUMMARY_KEYS) {
builder.addColumn(new Column(key, ScalarType.createStringType()));
}
META_DATA_QUERY_IDS = builder.build();
}

public enum PathType {
QUERY_IDS,
FRAGMENTS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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.doris.common.profile;

import org.apache.doris.common.MarkedCountDownLatch;
import org.apache.doris.common.Status;
import org.apache.doris.common.util.DebugUtil;
import org.apache.doris.common.util.RuntimeProfile;
import org.apache.doris.common.util.TimeUtils;
import org.apache.doris.thrift.TUniqueId;
import org.apache.doris.thrift.TUnit;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/**
* ExecutionProfile is used to collect profile of a complete query plan(including query or load).
* Need to call addToProfileAsChild() to add it to the root profile.
* It has the following structure:
* Execution Profile:
* Fragment 0:
* Instance 0:
* ...
* Fragment 1:
* Instance 0:
* ...
* ...
* LoadChannels: // only for load job
*/
public class ExecutionProfile {
private static final Logger LOG = LogManager.getLogger(ExecutionProfile.class);

// The root profile of this execution task
private RuntimeProfile executionProfile;
// Profiles for each fragment. And the InstanceProfile is the child of fragment profile.
// Which will be added to fragment profile when calling Coordinator::sendFragment()
private List<RuntimeProfile> fragmentProfiles;
// Profile for load channels. Only for load job.
private RuntimeProfile loadChannelProfile;
// A countdown latch to mark the completion of each instance.
// instance id -> dummy value
private MarkedCountDownLatch<TUniqueId, Long> profileDoneSignal;

public ExecutionProfile(TUniqueId queryId, int fragmentNum) {
executionProfile = new RuntimeProfile("Execution Profile " + DebugUtil.printId(queryId));
RuntimeProfile fragmentsProfile = new RuntimeProfile("Fragments");
executionProfile.addChild(fragmentsProfile);
fragmentProfiles = Lists.newArrayList();
for (int i = 0; i < fragmentNum; i++) {
fragmentProfiles.add(new RuntimeProfile("Fragment " + i));
fragmentsProfile.addChild(fragmentProfiles.get(i));
}
loadChannelProfile = new RuntimeProfile("LoadChannels");
executionProfile.addChild(loadChannelProfile);
}

public RuntimeProfile getExecutionProfile() {
return executionProfile;
}

public RuntimeProfile getLoadChannelProfile() {
return loadChannelProfile;
}

public void addToProfileAsChild(RuntimeProfile rootProfile) {
rootProfile.addChild(executionProfile);
}

public void markInstances(Set<TUniqueId> instanceIds) {
profileDoneSignal = new MarkedCountDownLatch<>(instanceIds.size());
for (TUniqueId instanceId : instanceIds) {
profileDoneSignal.addMark(instanceId, -1L /* value is meaningless */);
}
}

public void update(long startTime, boolean isFinished) {
if (startTime > 0) {
executionProfile.getCounterTotalTime().setValue(TUnit.TIME_MS, TimeUtils.getElapsedTimeMs(startTime));
}
// Wait for all backends to finish reporting when writing profile last time.
if (isFinished && profileDoneSignal != null) {
try {
profileDoneSignal.await(2, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
LOG.warn("signal await error", e1);
}
}

for (RuntimeProfile fragmentProfile : fragmentProfiles) {
fragmentProfile.sortChildren();
}
}

public void onCancel() {
if (profileDoneSignal != null) {
// count down to zero to notify all objects waiting for this
profileDoneSignal.countDownToZero(new Status());
LOG.info("unfinished instance: {}", profileDoneSignal.getLeftMarks()
.stream().map(e -> DebugUtil.printId(e.getKey())).toArray());
}
}

public void markOneInstanceDone(TUniqueId fragmentInstanceId) {
if (profileDoneSignal != null) {
profileDoneSignal.markedCountDown(fragmentInstanceId, -1L);
}
}

public boolean awaitAllInstancesDone(long waitTimeS) throws InterruptedException {
if (profileDoneSignal == null) {
return true;
}
return profileDoneSignal.await(waitTimeS, TimeUnit.SECONDS);
}

public boolean isAllInstancesDone() {
if (profileDoneSignal == null) {
return true;
}
return profileDoneSignal.getCount() == 0;
}

public void addInstanceProfile(int instanceIdx, RuntimeProfile instanceProfile) {
Preconditions.checkArgument(instanceIdx < fragmentProfiles.size(),
instanceIdx + " vs. " + fragmentProfiles.size());
fragmentProfiles.get(instanceIdx).addChild(instanceProfile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.doris.common.profile;

import org.apache.doris.common.util.ProfileManager;
import org.apache.doris.common.util.RuntimeProfile;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;

/**
* Profile is a class to record the execution time of a query.
* It has the following structure:
* root profile:
* // summary of this profile, such as start time, end time, query id, etc.
* [SummaryProfile]
* // each execution profile is a complete execution of a query, a job may contain multiple queries.
* [List<ExecutionProfile>]
*
* SummaryProfile:
* Summary:
* Execution Summary:
*
* ExecutionProfile:
* Fragment 0:
* Fragment 1:
* ...
*/
public class Profile {
private RuntimeProfile rootProfile;
private SummaryProfile summaryProfile;
private List<ExecutionProfile> executionProfiles = Lists.newArrayList();
private boolean isFinished;

public Profile(String name, boolean isEnable) {
this.rootProfile = new RuntimeProfile(name);
this.summaryProfile = new SummaryProfile(rootProfile);
// if disabled, just set isFinished to true, so that update() will do nothing
this.isFinished = !isEnable;
}

public void addExecutionProfile(ExecutionProfile executionProfile) {
this.executionProfiles.add(executionProfile);
executionProfile.addToProfileAsChild(rootProfile);
}

public synchronized void update(long startTime, Map<String, String> summaryInfo, boolean isFinished) {
if (this.isFinished) {
return;
}
summaryProfile.update(summaryInfo);
for (ExecutionProfile executionProfile : executionProfiles) {
executionProfile.update(startTime, isFinished);
}
rootProfile.computeTimeInProfile();
ProfileManager.getInstance().pushProfile(rootProfile);
this.isFinished = isFinished;
}

public SummaryProfile getSummaryProfile() {
return summaryProfile;
}
}
Loading

0 comments on commit ae2c4af

Please sign in to comment.