-
Notifications
You must be signed in to change notification settings - Fork 979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DRILL-4258: Add threads, fragments, and queries system tables #479
Open
StevenMPhillips
wants to merge
1
commit into
apache:master
Choose a base branch
from
StevenMPhillips:drill-4258
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
exec/java-exec/src/main/java/org/apache/drill/exec/ops/ThreadStatCollector.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,124 @@ | ||
/** | ||
* 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 | ||
* <p/> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p/> | ||
* 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.drill.exec.ops; | ||
|
||
import com.carrotsearch.hppc.LongObjectHashMap; | ||
import com.carrotsearch.hppc.procedures.LongObjectProcedure; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.ThreadMXBean; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.Deque; | ||
import java.util.Iterator; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
|
||
public class ThreadStatCollector implements Runnable { | ||
private static final long ONE_BILLION = 1000000000; | ||
private static final long RETAIN_INTERVAL = 5 * ONE_BILLION; | ||
private static final int COLLECTION_INTERVAL = 1; | ||
|
||
private ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); | ||
private ThreadStat cpuStat = new ThreadStat(); | ||
private ThreadStat userStat = new ThreadStat(); | ||
|
||
@Override | ||
public void run() { | ||
while (true) { | ||
try { | ||
Thread.sleep(COLLECTION_INTERVAL * 1000); | ||
addCpuTime(); | ||
addUserTime(); | ||
} catch (InterruptedException e) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public Integer getCpuTrailingAverage(long id, int seconds) { | ||
return cpuStat.getTrailingAverage(id, seconds); | ||
} | ||
|
||
public Integer getUserTrailingAverage(long id, int seconds) { | ||
return userStat.getTrailingAverage(id, seconds); | ||
} | ||
|
||
private void addCpuTime() { | ||
for (long id : mxBean.getAllThreadIds()) { | ||
cpuStat.add(id, System.nanoTime(), mxBean.getThreadCpuTime(id)); | ||
} | ||
} | ||
|
||
private void addUserTime() { | ||
for (long id : mxBean.getAllThreadIds()) { | ||
userStat.add(id, System.nanoTime(), mxBean.getThreadUserTime(id)); | ||
} | ||
} | ||
|
||
private static class ThreadStat { | ||
volatile LongObjectHashMap<Deque<Entry<Long,Long>>> data = new LongObjectHashMap<>(); | ||
|
||
public void add(long id, long ts, long value) { | ||
Entry<Long,Long> entry = new SimpleEntry<>(ts, value); | ||
Deque<Entry<Long,Long>> list = data.get(id); | ||
if (list == null) { | ||
list = new ConcurrentLinkedDeque<>(); | ||
} | ||
list.add(entry); | ||
while (ts - list.peekFirst().getKey() > RETAIN_INTERVAL) { | ||
list.removeFirst(); | ||
} | ||
data.put(id, list); | ||
} | ||
|
||
public Integer getTrailingAverage(long id, int seconds) { | ||
Deque<Entry<Long,Long>> list = data.get(id); | ||
if (list == null) { | ||
return null; | ||
} | ||
return getTrailingAverage(list, seconds); | ||
} | ||
|
||
private Integer getTrailingAverage(Deque<Entry<Long, Long>> list, int seconds) { | ||
Entry<Long,Long> latest = list.peekLast(); | ||
Entry<Long,Long> old = list.peekFirst(); | ||
Iterator<Entry<Long,Long>> iter = list.descendingIterator(); | ||
while (iter.hasNext()) { | ||
Entry<Long,Long> e = iter.next(); | ||
if (e.getKey() - latest.getKey() > seconds * ONE_BILLION) { | ||
old = e; | ||
break; | ||
} | ||
} | ||
try { | ||
return (int) (100 * (old.getValue() - latest.getValue()) / (old.getKey() - latest.getKey())); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
public void print(final int window) { | ||
data.forEach(new LongObjectProcedure<Deque<Entry<Long,Long>>>() { | ||
@Override | ||
public void apply(long l, Deque<Entry<Long,Long>> entries) { | ||
System.out.println(String.format("%d %d", l, getTrailingAverage(entries, window))); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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
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
98 changes: 98 additions & 0 deletions
98
exec/java-exec/src/main/java/org/apache/drill/exec/store/sys/FragmentIterator.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,98 @@ | ||
/** | ||
* 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 | ||
* <p/> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p/> | ||
* 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.drill.exec.store.sys; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.drill.exec.ops.FragmentContext; | ||
import org.apache.drill.exec.proto.CoordinationProtos; | ||
import org.apache.drill.exec.proto.UserBitShared.MinorFragmentProfile; | ||
import org.apache.drill.exec.proto.UserBitShared.OperatorProfile; | ||
import org.apache.drill.exec.proto.UserBitShared.StreamProfile; | ||
import org.apache.drill.exec.proto.helper.QueryIdHelper; | ||
import org.apache.drill.exec.server.DrillbitContext; | ||
import org.apache.drill.exec.work.WorkManager; | ||
import org.apache.drill.exec.work.fragment.FragmentExecutor; | ||
|
||
import java.sql.Timestamp; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Iterator which returns {@link FragmentInfo} for every fragment running in this drillbit. | ||
*/ | ||
public class FragmentIterator implements Iterator<Object> { | ||
private final WorkManager workManager; | ||
private final Iterator<FragmentExecutor> iter; | ||
|
||
public FragmentIterator(FragmentContext c) { | ||
this.workManager = c.getDrillbitContext().getWorkManager(); | ||
iter = ImmutableList.copyOf(workManager.getRunningFragments()).iterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iter.hasNext(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
FragmentExecutor fragmentExecutor = iter.next(); | ||
MinorFragmentProfile profile = fragmentExecutor.getStatus().getProfile(); | ||
FragmentInfo fragmentInfo = new FragmentInfo(); | ||
fragmentInfo.hostname = workManager.getContext().getEndpoint().getAddress(); | ||
fragmentInfo.queryId = QueryIdHelper.getQueryId(fragmentExecutor.getContext().getHandle().getQueryId()); | ||
fragmentInfo.majorFragmentId = fragmentExecutor.getContext().getHandle().getMajorFragmentId(); | ||
fragmentInfo.minorFragmentId = fragmentExecutor.getContext().getHandle().getMinorFragmentId(); | ||
fragmentInfo.rowsProcessed = getRowsProcessed(profile); | ||
fragmentInfo.memoryUsage = profile.getMemoryUsed(); | ||
fragmentInfo.startTime = new Timestamp(profile.getStartTime()); | ||
return fragmentInfo; | ||
} | ||
|
||
private long getRowsProcessed(MinorFragmentProfile profile) { | ||
long maxRecords = 0; | ||
for (OperatorProfile operatorProfile : profile.getOperatorProfileList()) { | ||
long records = 0; | ||
for (StreamProfile inputProfile :operatorProfile.getInputProfileList()) { | ||
if (inputProfile.hasRecords()) { | ||
records += inputProfile.getRecords(); | ||
} | ||
} | ||
maxRecords = Math.max(maxRecords, records); | ||
} | ||
return maxRecords; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static class FragmentInfo { | ||
public String hostname; | ||
public String queryId; | ||
public int majorFragmentId; | ||
public int minorFragmentId; | ||
public Long memoryUsage; | ||
/** | ||
* The maximum number of input records across all Operators in fragment | ||
*/ | ||
public Long rowsProcessed; | ||
public Timestamp startTime; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a thread (
WorkManager.StatusThread
) that does periodic tasks (currently updates fragment statuses), how about we add this as another task?