Skip to content

Commit e3e042f

Browse files
committed
Expose /v1/integrations/trinoGateway/metrics for metrics used by trino gateway
1 parent 77af5ad commit e3e042f

File tree

9 files changed

+90
-3
lines changed

9 files changed

+90
-3
lines changed

core/trino-main/src/main/java/io/trino/memory/LocalMemoryManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ private void validateHeapHeadroom(NodeMemoryConfig config, long availableMemory)
6161

6262
public MemoryInfo getInfo()
6363
{
64-
return new MemoryInfo(OPERATING_SYSTEM_MX_BEAN.getAvailableProcessors(), memoryPool.getInfo());
64+
double systemCpuLoad = 0.0;
65+
if (OPERATING_SYSTEM_MX_BEAN instanceof com.sun.management.OperatingSystemMXBean osBean) {
66+
systemCpuLoad = osBean.getCpuLoad();
67+
}
68+
return new MemoryInfo(OPERATING_SYSTEM_MX_BEAN.getAvailableProcessors(), systemCpuLoad, memoryPool.getInfo());
6569
}
6670

6771
public MemoryPool getMemoryPool()

core/trino-main/src/main/java/io/trino/memory/MemoryInfo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
public class MemoryInfo
2424
{
2525
private final int availableProcessors;
26+
private final double systemCpuLoad;
2627
private final MemoryPoolInfo pool;
2728

2829
@JsonCreator
2930
public MemoryInfo(
3031
@JsonProperty("availableProcessors") int availableProcessors,
32+
@JsonProperty("systemCpuLoad") double systemCpuLoad,
3133
@JsonProperty("pool") MemoryPoolInfo pool)
3234
{
3335
this.availableProcessors = availableProcessors;
36+
this.systemCpuLoad = systemCpuLoad;
3437
this.pool = requireNonNull(pool, "pool is null");
3538
}
3639

@@ -40,6 +43,12 @@ public int getAvailableProcessors()
4043
return availableProcessors;
4144
}
4245

46+
@JsonProperty
47+
public double getSystemCpuLoad()
48+
{
49+
return systemCpuLoad;
50+
}
51+
4352
@JsonProperty
4453
public MemoryPoolInfo getPool()
4554
{
@@ -51,6 +60,7 @@ public String toString()
5160
{
5261
return toStringHelper(this)
5362
.add("availableProcessors", availableProcessors)
63+
.add("systemCpuLoad", systemCpuLoad)
5464
.add("pool", pool)
5565
.toString();
5666
}

core/trino-main/src/main/java/io/trino/node/DnsNodeInventoryModule.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ protected void setup(Binder binder)
3030
boolean coordinator = buildConfigObject(ServerConfig.class).isCoordinator();
3131
if (coordinator) {
3232
jaxrsBinder(binder).bind(AnnounceNodeResource.class);
33-
3433
configBinder(binder).bindConfig(DnsNodeInventoryConfig.class);
3534
binder.bind(DnsNodeInventory.class).in(Scopes.SINGLETON);
3635
binder.bind(NodeInventory.class).to(DnsNodeInventory.class).in(Scopes.SINGLETON);

core/trino-main/src/main/java/io/trino/server/CoordinatorModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ protected void setup(Binder binder)
243243

244244
newExporter(binder).export(ClusterMemoryManager.class).withGeneratedName();
245245

246+
// metrics used by Trino Gateway
247+
jaxrsBinder(binder).bind(TrinoGatewayResource.class);
248+
246249
// node partitioning manager
247250
binder.bind(NodePartitioningManager.class).in(Scopes.SINGLETON);
248251

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.server;
15+
16+
import com.google.inject.Inject;
17+
import io.trino.memory.ClusterMemoryManager;
18+
import io.trino.memory.MemoryInfo;
19+
import io.trino.server.security.ResourceSecurity;
20+
import io.trino.spi.memory.MemoryPoolInfo;
21+
import jakarta.ws.rs.GET;
22+
import jakarta.ws.rs.Path;
23+
24+
import java.util.Optional;
25+
26+
import static io.trino.server.security.ResourceSecurity.AccessType.MANAGEMENT_READ;
27+
28+
@Path("/v1/integrations/trinoGateway")
29+
public class TrinoGatewayResource
30+
{
31+
private final ClusterMemoryManager clusterMemoryManager;
32+
33+
@Inject
34+
public TrinoGatewayResource(ClusterMemoryManager clusterMemoryManager)
35+
{
36+
this.clusterMemoryManager = clusterMemoryManager;
37+
}
38+
39+
@GET
40+
@Path("clusterMetrics")
41+
@ResourceSecurity(MANAGEMENT_READ)
42+
public ClusterMetrics getClusterMetrics()
43+
{
44+
Map<String, Optional<MemoryInfo>> memoryInfo = clusterMemoryManager.getAllNodesMemoryInfo();
45+
long totalFreeBytes = memoryInfo
46+
.values()
47+
.stream()
48+
.flatMap(Optional::stream)
49+
.map(MemoryInfo::getPool)
50+
.mapToLong(MemoryPoolInfo::getFreeBytes)
51+
.sum();
52+
double aggregatedSystemLoad = memoryInfo
53+
.values()
54+
.stream()
55+
.flatMap(Optional::stream)
56+
.mapToDouble(MemoryInfo::getSystemCpuLoad)
57+
.sum();
58+
return new ClusterMetrics(totalFreeBytes, aggregatedSystemLoad);
59+
}
60+
61+
/**
62+
* Returns the aggregated metrics from each node in Trino cluster
63+
* @param totalFreeBytes the sum of free memory from each node in the cluster
64+
* @param aggregatedSystemLoad the sum of system load from each node in the cluster
65+
*/
66+
public record ClusterMetrics(long totalFreeBytes, double aggregatedSystemLoad)
67+
{}
68+
}

core/trino-main/src/test/java/io/trino/execution/scheduler/faulttolerant/BenchmarkBinPackingNodeAllocator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ private MemoryInfo buildWorkerMemoryInfo(DataSize usedMemory, Map<TaskId, DataSi
179179
{
180180
return new MemoryInfo(
181181
4,
182+
0,
182183
new MemoryPoolInfo(
183184
DataSize.of(64, GIGABYTE).toBytes(),
184185
usedMemory.toBytes(),

core/trino-main/src/test/java/io/trino/execution/scheduler/faulttolerant/TestBinPackingNodeAllocator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private MemoryInfo buildWorkerMemoryInfo(DataSize usedMemory, Map<TaskId, DataSi
131131
{
132132
return new MemoryInfo(
133133
4,
134+
0,
134135
new MemoryPoolInfo(
135136
DataSize.of(64, GIGABYTE).toBytes(),
136137
usedMemory.toBytes(),

core/trino-main/src/test/java/io/trino/execution/scheduler/faulttolerant/TestExponentialGrowthPartitionMemoryEstimator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ private MemoryInfo buildWorkerMemoryInfo(DataSize usedMemory)
279279
{
280280
return new MemoryInfo(
281281
4,
282+
0,
282283
new MemoryPoolInfo(
283284
DataSize.of(64, GIGABYTE).toBytes(),
284285
usedMemory.toBytes(),

core/trino-main/src/test/java/io/trino/memory/LowMemoryKillerTestingUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static List<MemoryInfo> toNodeMemoryInfoList(long memoryPoolMaxBytes, Map<String
7373
ImmutableMap.of(),
7474
tasksMemoryInfoForNode(entry.getKey().getNodeIdentifier(), tasks),
7575
ImmutableMap.of());
76-
result.add(new MemoryInfo(7, memoryPoolInfo));
76+
result.add(new MemoryInfo(7, 0, memoryPoolInfo));
7777
}
7878
return result.build();
7979
}

0 commit comments

Comments
 (0)