|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.wlm; |
| 10 | + |
| 11 | +import org.opensearch.common.util.concurrent.ThreadContextStatePropagator; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * This class is used to propagate QueryGroup related headers to request and nodes |
| 19 | + */ |
| 20 | +public class QueryGroupThreadContextStatePropagator implements ThreadContextStatePropagator { |
| 21 | + // TODO: move this constant to QueryGroupService class once the QueryGroup monitoring framework PR is ready |
| 22 | + public static List<String> PROPAGATED_HEADERS = List.of("queryGroupId"); |
| 23 | + |
| 24 | + /** |
| 25 | + * @param source current context transient headers |
| 26 | + * @return the map of header and their values to be propagated across request threadContexts |
| 27 | + */ |
| 28 | + @Override |
| 29 | + @SuppressWarnings("removal") |
| 30 | + public Map<String, Object> transients(Map<String, Object> source) { |
| 31 | + final Map<String, Object> transientHeaders = new HashMap<>(); |
| 32 | + |
| 33 | + for (String headerName : PROPAGATED_HEADERS) { |
| 34 | + transientHeaders.compute(headerName, (k, v) -> source.get(headerName)); |
| 35 | + } |
| 36 | + return transientHeaders; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param source current context headers |
| 41 | + * @return map of header and their values to be propagated across nodes |
| 42 | + */ |
| 43 | + @Override |
| 44 | + @SuppressWarnings("removal") |
| 45 | + public Map<String, String> headers(Map<String, Object> source) { |
| 46 | + final Map<String, String> propagatedHeaders = new HashMap<>(); |
| 47 | + |
| 48 | + for (String headerName : PROPAGATED_HEADERS) { |
| 49 | + propagatedHeaders.compute(headerName, (k, v) -> (String) source.get(headerName)); |
| 50 | + } |
| 51 | + return propagatedHeaders; |
| 52 | + } |
| 53 | +} |
0 commit comments