|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. 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 org.astraea.app.checker; |
| 18 | + |
| 19 | +import com.beust.jcommander.Parameter; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URL; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.function.Function; |
| 26 | +import org.apache.kafka.clients.admin.Admin; |
| 27 | +import org.apache.kafka.common.Node; |
| 28 | +import org.astraea.app.argument.IntegerMapField; |
| 29 | +import org.astraea.app.argument.NonEmptyStringField; |
| 30 | +import org.astraea.app.argument.NonNegativeIntegerField; |
| 31 | +import org.astraea.common.json.JsonConverter; |
| 32 | +import org.astraea.common.json.TypeRef; |
| 33 | +import org.astraea.common.metrics.JndiClient; |
| 34 | +import org.astraea.common.metrics.MBeanClient; |
| 35 | + |
| 36 | +public class Checker { |
| 37 | + |
| 38 | + private static final List<Guard> GUARDS = List.of(new ProduceRpcGuard()); |
| 39 | + |
| 40 | + public static void main(String[] args) throws Exception { |
| 41 | + execute(Argument.parse(new Argument(), args)); |
| 42 | + } |
| 43 | + |
| 44 | + public static void execute(final Argument param) throws Exception { |
| 45 | + try (var admin = Admin.create(Map.of("bootstrap.servers", param.bootstrapServers()))) { |
| 46 | + for (var guard : GUARDS) { |
| 47 | + var result = guard.run(admin, param.mBeanClientFunction(), param.readChangelog()); |
| 48 | + System.out.println(result); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static class Argument extends org.astraea.app.argument.Argument { |
| 54 | + @Parameter( |
| 55 | + names = {"--changelog"}, |
| 56 | + description = "String: url of changelog file", |
| 57 | + validateWith = NonEmptyStringField.class) |
| 58 | + String changelog = |
| 59 | + "https://raw.githubusercontent.com/opensource4you/astraea/refs/heads/main/config/kafka_changelog.json"; |
| 60 | + |
| 61 | + Changelog readChangelog() throws IOException { |
| 62 | + try (var in = new URL(changelog).openStream()) { |
| 63 | + return JsonConverter.defaultConverter() |
| 64 | + .fromJson( |
| 65 | + new String(in.readAllBytes(), StandardCharsets.UTF_8), TypeRef.of(Changelog.class)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Parameter( |
| 70 | + names = {"--jmx.port"}, |
| 71 | + description = "Integer: the port to query JMX for each server", |
| 72 | + validateWith = NonNegativeIntegerField.class, |
| 73 | + converter = NonNegativeIntegerField.class) |
| 74 | + int jmxPort = -1; |
| 75 | + |
| 76 | + @Parameter( |
| 77 | + names = {"--jmx.ports"}, |
| 78 | + description = |
| 79 | + "Map: the JMX port for each broker. For example: 1024=19999 means for the broker with id 1024, its JMX port located at 19999 port", |
| 80 | + validateWith = IntegerMapField.class, |
| 81 | + converter = IntegerMapField.class) |
| 82 | + Map<Integer, Integer> jmxPorts = Map.of(); |
| 83 | + |
| 84 | + Function<Node, MBeanClient> mBeanClientFunction() { |
| 85 | + return node -> { |
| 86 | + int port = jmxPorts.getOrDefault(node.id(), jmxPort); |
| 87 | + if (port < 0) |
| 88 | + throw new IllegalArgumentException("Failed to get jmx port for broker: " + node); |
| 89 | + return JndiClient.of(node.host(), port); |
| 90 | + }; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments