/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ package org.opensearch.search.profile.query; import org.opensearch.search.profile.AbstractProfileBreakdown; import org.opensearch.search.profile.ContextualProfileBreakdown; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * A record of timings for the various operations that may happen during query execution. * A node's time may be composed of several internal attributes (rewriting, weighting, * scoring, etc). The class supports profiling the concurrent search over segments. * * @opensearch.internal */ public final class ConcurrentQueryProfileBreakdown extends ContextualProfileBreakdown { private final Map> contexts = new ConcurrentHashMap<>(); /** Sole constructor. */ public ConcurrentQueryProfileBreakdown() { super(QueryTimingType.class); } @Override public AbstractProfileBreakdown context(Object context) { // See please https://bugs.openjdk.java.net/browse/JDK-8161372 final AbstractProfileBreakdown profile = contexts.get(context); if (profile != null) { return profile; } return contexts.computeIfAbsent(context, ctx -> new QueryProfileBreakdown()); } @Override public Map toBreakdownMap() { final Map map = new HashMap<>(super.toBreakdownMap()); for (final AbstractProfileBreakdown context : contexts.values()) { for (final Map.Entry entry : context.toBreakdownMap().entrySet()) { map.merge(entry.getKey(), entry.getValue(), Long::sum); } } return map; } }