|
| 1 | +/* |
| 2 | + * Copyright 2022-2025 Google LLC |
| 3 | + * Copyright 2013-2021 CompilerWorks |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.google.edwmigration.dumper.application.dumper.connector.cloudera.manager; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import com.google.common.io.ByteSink; |
| 21 | +import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException; |
| 22 | +import com.google.edwmigration.dumper.application.dumper.connector.cloudera.manager.ClouderaManagerHandle.ClouderaHostDTO; |
| 23 | +import com.google.edwmigration.dumper.application.dumper.task.TaskRunContext; |
| 24 | +import java.io.Writer; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.time.ZonedDateTime; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import javax.annotation.Nonnull; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * The task collects resource allocation metrics for <strong>all</strong> individual service roles |
| 35 | + * present on a given host from the Cloudera Manager <a |
| 36 | + * href="https://cldr2-aw-dl-gateway.cldr2-cd.svye-dcxb.a5.cloudera.site/static/apidocs/resource_TimeSeriesResource.html">TimeSeries |
| 37 | + * API</a>. |
| 38 | + * |
| 39 | + * <p>This class provides a comprehensive, component-by-component breakdown of resource consumption, |
| 40 | + * programmatically fetching the data that powers the stacked resource charts on a host's status |
| 41 | + * page in the Cloudera Manager UI (found under the "Charts" tab for a specific host). |
| 42 | + * |
| 43 | + * <p>Queries are written in the <a |
| 44 | + * href="https://docs.cloudera.com/documentation/enterprise/latest/topics/cm_dg_tsquery.html">tsquery</a> |
| 45 | + * language and are filtered by a specific {@code hostId}. |
| 46 | + * |
| 47 | + * <p>Key metrics collected include: |
| 48 | + * |
| 49 | + * <ul> |
| 50 | + * <li><b>Memory:</b> {@code mem_rss} |
| 51 | + * <li><b>CPU:</b> {@code cpu_user_rate} and {@code cpu_system_rate} |
| 52 | + * <li><b>Disk I/O:</b> {@code read_ios_rate} and {@code write_ios_rate} |
| 53 | + * </ul> |
| 54 | + */ |
| 55 | +public class ClouderaServiceResourceAllocationChartTask extends AbstractClouderaTimeSeriesTask { |
| 56 | + |
| 57 | + private static final Logger logger = |
| 58 | + LoggerFactory.getLogger(ClouderaServiceResourceAllocationChartTask.class); |
| 59 | + |
| 60 | + private static final String SERVICE_RESOURCE_ALLOCATION_QUERY_TEMPLATE = |
| 61 | + "select mem_rss, cpu_user_rate, cpu_system_rate, read_ios_rate, write_ios_rate where category = \"ROLE\" AND hostId = \"%s\""; |
| 62 | + |
| 63 | + public ClouderaServiceResourceAllocationChartTask( |
| 64 | + ZonedDateTime startDate, ZonedDateTime endDate, TimeSeriesAggregation tsAggregation) { |
| 65 | + super("service-resource-allocation.jsonl", startDate, endDate, tsAggregation); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void doRun( |
| 70 | + TaskRunContext context, @Nonnull ByteSink sink, @Nonnull ClouderaManagerHandle handle) |
| 71 | + throws Exception { |
| 72 | + List<ClouderaHostDTO> hosts = getHostsFromHandle(handle); |
| 73 | + |
| 74 | + try (Writer writer = sink.asCharSink(StandardCharsets.UTF_8).openBufferedStream()) { |
| 75 | + for (ClouderaHostDTO host : hosts) { |
| 76 | + String resourceAllocationPerHostQuery = |
| 77 | + String.format(SERVICE_RESOURCE_ALLOCATION_QUERY_TEMPLATE, host.getId()); |
| 78 | + logger.debug( |
| 79 | + "Execute service resource allocation charts query: [{}] for the host: [{}].", |
| 80 | + resourceAllocationPerHostQuery, |
| 81 | + host.getName()); |
| 82 | + |
| 83 | + JsonNode chartInJson = requestTimeSeriesChart(handle, resourceAllocationPerHostQuery); |
| 84 | + |
| 85 | + writer.write(chartInJson.toString()); |
| 86 | + writer.write('\n'); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private List<ClouderaHostDTO> getHostsFromHandle(@Nonnull ClouderaManagerHandle handle) { |
| 92 | + List<ClouderaHostDTO> hosts = handle.getHosts(); |
| 93 | + if (hosts == null) { |
| 94 | + throw new MetadataDumperUsageException( |
| 95 | + "Cloudera hosts must be initialized before service resource allocation charts dumping."); |
| 96 | + } |
| 97 | + List<ClouderaHostDTO> validHosts = new ArrayList<>(); |
| 98 | + for (ClouderaHostDTO host : hosts) { |
| 99 | + if (host.getId() == null) { |
| 100 | + logger.warn( |
| 101 | + "Cloudera host id is null for host [{}]. Skip resource allocation metrics for services belonging to this host.", |
| 102 | + host.getName()); |
| 103 | + } else { |
| 104 | + validHosts.add(host); |
| 105 | + } |
| 106 | + } |
| 107 | + return validHosts; |
| 108 | + } |
| 109 | +} |
0 commit comments