@Override protected AggregationProfileBreakdown createProfileBreakdown() { return new AggregationProfileBreakdown(); }
public ProfilingLeafBucketCollector(LeafBucketCollector delegate, AggregationProfileBreakdown profileBreakdown) { this.delegate = delegate; this.collectTimer = profileBreakdown.getTimer(AggregationTimingType.COLLECT); }
public AggregationProfiler() { super(new InternalAggregationProfileTree()); }
@Override public void preCollection() throws IOException { this.profileBreakdown = profiler.getQueryBreakdown(delegate); Timer timer = profileBreakdown.getTimer(AggregationTimingType.INITIALIZE); timer.start(); try { delegate.preCollection(); } finally { timer.stop(); } profiler.pollLastElement(); }
/** * Helper method to convert Profiler into InternalProfileShardResults, which * can be serialized to other nodes, emitted as JSON, etc. * * @param profilers * The {@link Profilers} to convert into results * @return A {@link ProfileShardResult} representing the results for this * shard */ public static ProfileShardResult buildShardResults(Profilers profilers) { List<QueryProfiler> queryProfilers = profilers.getQueryProfilers(); AggregationProfiler aggProfiler = profilers.getAggregationProfiler(); List<QueryProfileShardResult> queryResults = new ArrayList<>(queryProfilers.size()); for (QueryProfiler queryProfiler : queryProfilers) { QueryProfileShardResult result = new QueryProfileShardResult(queryProfiler.getTree(), queryProfiler.getRewriteTime(), queryProfiler.getCollector()); queryResults.add(result); } AggregationProfileShardResult aggResults = new AggregationProfileShardResult(aggProfiler.getTree()); return new ProfileShardResult(queryResults, aggResults); } }
public ProfileShardResult(StreamInput in) throws IOException { int profileSize = in.readVInt(); List<QueryProfileShardResult> queryProfileResults = new ArrayList<>(profileSize); for (int i = 0; i < profileSize; i++) { QueryProfileShardResult result = new QueryProfileShardResult(in); queryProfileResults.add(result); } this.queryProfileResults = Collections.unmodifiableList(queryProfileResults); this.aggProfileShardResult = new AggregationProfileShardResult(in); }
public Aggregator[] createTopLevelAggregators() throws IOException { // These aggregators are going to be used with a single bucket ordinal, no need to wrap the PER_BUCKET ones Aggregator[] aggregators = new Aggregator[factories.length]; for (int i = 0; i < factories.length; i++) { // top-level aggs only get called with bucket 0 final boolean collectsFromSingleBucket = true; Aggregator factory = factories[i].create(null, collectsFromSingleBucket); Profilers profilers = factory.context().getProfilers(); if (profilers != null) { factory = new ProfilingAggregator(factory, profilers.getAggregationProfiler()); } aggregators[i] = factory; } return aggregators; }
@Override public String toString() { return name().toLowerCase(Locale.ROOT); } }
@Override public LeafBucketCollector getLeafCollector(LeafReaderContext ctx) throws IOException { return new ProfilingLeafBucketCollector(delegate.getLeafCollector(ctx), profileBreakdown); }
@Override public AggregationProfileBreakdown getQueryBreakdown(Aggregator agg) { List<String> path = getAggregatorPath(agg); AggregationProfileBreakdown aggregationProfileBreakdown = profileBrakdownLookup.get(path); if (aggregationProfileBreakdown == null) { aggregationProfileBreakdown = super.getQueryBreakdown(agg); profileBrakdownLookup.put(path, aggregationProfileBreakdown); } return aggregationProfileBreakdown; }
/** Sole constructor. This {@link Profilers} instance will initially wrap one {@link QueryProfiler}. */ public Profilers(ContextIndexSearcher searcher) { this.searcher = searcher; this.queryProfilers = new ArrayList<>(); this.aggProfiler = new AggregationProfiler(); addQueryProfiler(); }
/** * Resolves the topmost aggregator pointed by this path using the given root as a point of reference. * * @param root The point of reference of this path * @return The first child aggregator of the root pointed by this path */ public Aggregator resolveTopmostAggregator(Aggregator root) { AggregationPath.PathElement token = pathElements.get(0); Aggregator aggregator = ProfilingAggregator.unwrap(root.subAggregator(token.name)); assert (aggregator instanceof SingleBucketAggregator ) || (aggregator instanceof NumericMetricsAggregator) : "this should be picked up before aggregation execution - on validate"; return aggregator; }
@Override public void writeTo(StreamOutput out) throws IOException { out.writeVInt(queryProfileResults.size()); for (QueryProfileShardResult queryShardResult : queryProfileResults) { queryShardResult.writeTo(out); } aggProfileShardResult.writeTo(out); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(PROFILE_FIELD).startArray(SHARDS_FIELD); // shardResults is a map, but we print entries in a json array, which is ordered. // we sort the keys of the map, so that toXContent always prints out the same array order TreeSet<String> sortedKeys = new TreeSet<>(shardResults.keySet()); for (String key : sortedKeys) { builder.startObject(); builder.field(ID_FIELD, key); builder.startArray(SEARCHES_FIELD); ProfileShardResult profileShardResult = shardResults.get(key); for (QueryProfileShardResult result : profileShardResult.getQueryProfileResults()) { result.toXContent(builder, params); } builder.endArray(); profileShardResult.getAggregationProfileResults().toXContent(builder, params); builder.endObject(); } builder.endArray().endObject(); return builder; }
aggProfileShardResult = AggregationProfileShardResult.fromXContent(parser); } else { parser.skipChildren();
@Override public InternalAggregation buildAggregation(long bucket) throws IOException { Timer timer = profileBreakdown.getTimer(AggregationTimingType.BUILD_AGGREGATION); timer.start(); InternalAggregation result; try { result = delegate.buildAggregation(bucket); } finally { timer.stop(); } return result; }
public static AggregationProfileShardResult fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); ensureExpectedToken(XContentParser.Token.START_ARRAY, token, parser::getTokenLocation); List<ProfileResult> aggProfileResults = new ArrayList<>(); while((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { aggProfileResults.add(ProfileResult.fromXContent(parser)); } return new AggregationProfileShardResult(aggProfileResults); } }
/** * Create all aggregators so that they can be consumed with multiple * buckets. */ public Aggregator[] createSubAggregators(Aggregator parent) throws IOException { Aggregator[] aggregators = new Aggregator[countAggregators()]; for (int i = 0; i < factories.length; ++i) { // TODO: sometimes even sub aggregations always get called with bucket 0, eg. if // you have a terms agg under a top-level filter agg. We should have a way to // propagate the fact that only bucket 0 will be collected with single-bucket // aggs final boolean collectsFromSingleBucket = false; Aggregator factory = factories[i].create(parent, collectsFromSingleBucket); Profilers profilers = factory.context().getProfilers(); if (profilers != null) { factory = new ProfilingAggregator(factory, profilers.getAggregationProfiler()); } aggregators[i] = factory; } return aggregators; }
/** * Resolves the aggregator pointed by this path using the given root as a point of reference. * * @param root The point of reference of this path * @return The aggregator pointed by this path starting from the given aggregator as a point of reference */ public Aggregator resolveAggregator(Aggregator root) { Aggregator aggregator = root; for (int i = 0; i < pathElements.size(); i++) { AggregationPath.PathElement token = pathElements.get(i); aggregator = ProfilingAggregator.unwrap(aggregator.subAggregator(token.name)); assert (aggregator instanceof SingleBucketAggregator && i <= pathElements.size() - 1) || (aggregator instanceof NumericMetricsAggregator && i == pathElements.size() - 1) : "this should be picked up before aggregation execution - on validate"; } return aggregator; }
Aggregator aggregator = root; for (int i = 0; i < pathElements.size(); i++) { aggregator = ProfilingAggregator.unwrap(aggregator.subAggregator(pathElements.get(i).name)); if (aggregator == null) { throw new AggregationExecutionException("Invalid aggregator order path [" + this + "]. Unknown aggregation ["