From 28ec2c6882c2fad0fffc7367d16e458dbd2e091c Mon Sep 17 00:00:00 2001 From: Ching-Hong Fang Date: Tue, 23 May 2023 19:40:02 +0800 Subject: [PATCH 1/2] [METRICS] Migrate topic collector to balancer, partitioiner (#1765) --- .../java/org/astraea/app/web/WebService.java | 71 ++++++++++----- .../org/astraea/app/web/TopicHandlerTest.java | 4 +- .../org/astraea/app/web/WebServiceTest.java | 86 ++++++++++++++++++- .../partitioner/StrictCostPartitioner.java | 57 ++++++++---- .../StrictCostPartitionerTest.java | 79 +++++++++++++++++ 5 files changed, 257 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/astraea/app/web/WebService.java b/app/src/main/java/org/astraea/app/web/WebService.java index c81f2a7b36..354bb95cf1 100644 --- a/app/src/main/java/org/astraea/app/web/WebService.java +++ b/app/src/main/java/org/astraea/app/web/WebService.java @@ -24,17 +24,20 @@ import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.concurrent.CompletionStage; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; import org.astraea.app.argument.DurationField; import org.astraea.app.argument.IntegerMapField; import org.astraea.app.argument.NonNegativeIntegerField; +import org.astraea.common.Configuration; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.NodeInfo; import org.astraea.common.metrics.JndiClient; import org.astraea.common.metrics.MBeanClient; @@ -42,6 +45,10 @@ import org.astraea.common.metrics.collector.MetricStore; public class WebService implements AutoCloseable { + public static final String METRIC_STORE_KEY = "metric.store"; + public static final String METRIC_STORE_LOCAL = "local"; + public static final String METRIC_STORE_TOPIC = "topic"; + public static final String BOOTSTRAP_SERVERS_KEY = "bootstrap.servers"; private final HttpServer server; private final Admin admin; @@ -51,32 +58,48 @@ public WebService( Admin admin, int port, Function brokerIdToJmxPort, - Duration beanExpiration) { + Duration beanExpiration, + Configuration config) { this.admin = admin; - Supplier>> clientSupplier = + Supplier>> sensorsSupplier = () -> - admin - .brokers() - .thenApply( - brokers -> - brokers.stream() - .collect( - Collectors.toUnmodifiableMap( - NodeInfo::id, - b -> - JndiClient.of(b.host(), brokerIdToJmxPort.apply(b.id()))))); + sensors.metricSensors().stream() + .distinct() + .collect( + Collectors.toUnmodifiableMap(Function.identity(), ignored -> (id, ee) -> {})); + + List receivers = + switch (config.string(METRIC_STORE_KEY).orElse(METRIC_STORE_LOCAL)) { + case METRIC_STORE_LOCAL -> { + Function, Map> asBeanClientMap = + brokers -> + brokers.stream() + .collect( + Collectors.toUnmodifiableMap( + NodeInfo::id, + b -> JndiClient.of(b.host(), brokerIdToJmxPort.apply(b.id())))); + yield List.of( + MetricStore.Receiver.local(() -> admin.brokers().thenApply(asBeanClientMap))); + } + case METRIC_STORE_TOPIC -> List.of( + MetricStore.Receiver.topic(config.requireString(BOOTSTRAP_SERVERS_KEY)), + MetricStore.Receiver.local( + () -> CompletableFuture.completedStage(Map.of(-1, JndiClient.local())))); + default -> throw new IllegalArgumentException( + "unknown metric store type: " + + config.string(METRIC_STORE_KEY) + + ". use " + + METRIC_STORE_LOCAL + + " or " + + METRIC_STORE_TOPIC); + }; var metricStore = MetricStore.builder() .beanExpiration(beanExpiration) - .receivers(List.of(MetricStore.Receiver.local(clientSupplier))) - .sensorsSupplier( - () -> - sensors.metricSensors().stream() - .distinct() - .collect( - Collectors.toUnmodifiableMap( - Function.identity(), ignored -> (id, ee) -> {}))) + .receivers(receivers) + .sensorsSupplier(sensorsSupplier) .build(); + server = Utils.packException(() -> HttpServer.create(new InetSocketAddress(port), 0)); server.createContext("/topics", to(new TopicHandler(admin))); server.createContext("/groups", to(new GroupHandler(admin))); @@ -109,7 +132,11 @@ public static void main(String[] args) throws Exception { throw new IllegalArgumentException("you must define either --jmx.port or --jmx.ports"); try (var service = new WebService( - Admin.of(arg.configs()), arg.port, arg::jmxPortMapping, arg.beanExpiration)) { + Admin.of(arg.configs()), + arg.port, + arg::jmxPortMapping, + arg.beanExpiration, + new Configuration(arg.configs()))) { if (arg.ttl == null) { System.out.println("enter ctrl + c to terminate web service"); TimeUnit.MILLISECONDS.sleep(Long.MAX_VALUE); diff --git a/app/src/test/java/org/astraea/app/web/TopicHandlerTest.java b/app/src/test/java/org/astraea/app/web/TopicHandlerTest.java index 972394489e..ce8569ee66 100644 --- a/app/src/test/java/org/astraea/app/web/TopicHandlerTest.java +++ b/app/src/test/java/org/astraea/app/web/TopicHandlerTest.java @@ -27,6 +27,7 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.astraea.common.Configuration; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; import org.astraea.common.admin.TopicPartition; @@ -69,7 +70,8 @@ void testWithWebService() { Admin.of(SERVICE.bootstrapServers()), 0, id -> SERVICE.jmxServiceURL().getPort(), - Duration.ofMillis(5))) { + Duration.ofMillis(5), + Configuration.EMPTY)) { Response response = HttpExecutor.builder() .build() diff --git a/app/src/test/java/org/astraea/app/web/WebServiceTest.java b/app/src/test/java/org/astraea/app/web/WebServiceTest.java index d40700a0b8..5ba689465a 100644 --- a/app/src/test/java/org/astraea/app/web/WebServiceTest.java +++ b/app/src/test/java/org/astraea/app/web/WebServiceTest.java @@ -17,13 +17,18 @@ package org.astraea.app.web; import java.time.Duration; +import java.util.Map; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; import org.astraea.app.argument.Argument; +import org.astraea.common.Configuration; import org.astraea.common.admin.Admin; +import org.astraea.common.metrics.collector.MetricStore; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.mockito.Mockito; +import org.mockito.stubbing.Answer; public class WebServiceTest { @@ -40,7 +45,9 @@ void testArgument() { @Timeout(10) @Test void testClose() { - var web = new WebService(Mockito.mock(Admin.class), 0, id -> -1, Duration.ofMillis(5)); + var web = + new WebService( + Mockito.mock(Admin.class), 0, id -> -1, Duration.ofMillis(5), Configuration.EMPTY); web.close(); } @@ -84,4 +91,81 @@ void testJmxPort() { Assertions.assertThrows( IllegalArgumentException.class, () -> noDefaultArgument.jmxPortMapping(4)); } + + @Test + void testMetricStoreConfiguration() { + try (var mockedReceiver = Mockito.mockStatic(MetricStore.Receiver.class)) { + var topicReceiverCount = new AtomicInteger(0); + var localReceiverCount = new AtomicInteger(0); + mockedReceiver + .when(() -> MetricStore.Receiver.topic(Mockito.any())) + .then( + (Answer) + invocation -> { + topicReceiverCount.incrementAndGet(); + return Mockito.mock(MetricStore.Receiver.class); + }); + mockedReceiver + .when(() -> MetricStore.Receiver.local(Mockito.any())) + .then( + (Answer) + invocation -> { + localReceiverCount.incrementAndGet(); + return Mockito.mock(MetricStore.Receiver.class); + }); + // Test default metric store configuration + try (var web = + new WebService( + Mockito.mock(Admin.class), 0, id -> -1, Duration.ofMillis(5), Configuration.EMPTY)) { + + Assertions.assertEquals(1, localReceiverCount.get()); + Assertions.assertEquals(0, topicReceiverCount.get()); + } + localReceiverCount.set(0); + topicReceiverCount.set(0); + // Test local metric store configuration + try (var web = + new WebService( + Mockito.mock(Admin.class), + 0, + id -> -1, + Duration.ofMillis(5), + new Configuration( + Map.of(WebService.METRIC_STORE_KEY, WebService.METRIC_STORE_LOCAL)))) { + + Assertions.assertEquals(1, localReceiverCount.get()); + Assertions.assertEquals(0, topicReceiverCount.get()); + } + localReceiverCount.set(0); + topicReceiverCount.set(0); + // Test topic metric store configuration + try (var web = + new WebService( + Mockito.mock(Admin.class), + 0, + id -> -1, + Duration.ofMillis(5), + new Configuration( + Map.of( + WebService.METRIC_STORE_KEY, + WebService.METRIC_STORE_TOPIC, + WebService.BOOTSTRAP_SERVERS_KEY, + "ignore")))) { + + // topic collector may create local receiver to receive local jmx metric + Assertions.assertEquals(1, topicReceiverCount.get()); + } + + // Test invalid metric store configuration + Assertions.assertThrows( + IllegalArgumentException.class, + () -> + new WebService( + Mockito.mock(Admin.class), + 0, + id -> -1, + Duration.ofMillis(5), + new Configuration(Map.of(WebService.METRIC_STORE_KEY, "unknown")))); + } + } } diff --git a/common/src/main/java/org/astraea/common/partitioner/StrictCostPartitioner.java b/common/src/main/java/org/astraea/common/partitioner/StrictCostPartitioner.java index 8b9fa570db..e06a2983fe 100644 --- a/common/src/main/java/org/astraea/common/partitioner/StrictCostPartitioner.java +++ b/common/src/main/java/org/astraea/common/partitioner/StrictCostPartitioner.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.function.Function; import java.util.function.Supplier; @@ -38,6 +39,7 @@ import org.astraea.common.metrics.JndiClient; import org.astraea.common.metrics.MBeanClient; import org.astraea.common.metrics.collector.MetricStore; +import org.astraea.common.producer.ProducerConfigs; /** * this partitioner scores the nodes by multiples cost functions. Each function evaluate the target @@ -55,6 +57,9 @@ * `org.astraea.cost.ThroughputCost=1,org.astraea.cost.broker.BrokerOutputCost=1`. */ public class StrictCostPartitioner extends Partitioner { + public static final String METRIC_STORE_KEY = "metric.store"; + public static final String METRIC_STORE_TOPIC = "topic"; + public static final String METRIC_STORE_LOCAL = "local"; static final int ROUND_ROBIN_LENGTH = 400; static final String JMX_PORT = "jmx.port"; static final String ROUND_ROBIN_LEASE_KEY = "round.robin.lease"; @@ -140,27 +145,47 @@ public void configure(Configuration config) { .string(ROUND_ROBIN_LEASE_KEY) .map(Utils::toDuration) .ifPresent(d -> this.roundRobinLease = d); - Supplier>> clientSupplier = - () -> - admin - .brokers() - .thenApply( - brokers -> { - var map = new HashMap(); - brokers.forEach( - b -> - map.put( - b.id(), JndiClient.of(b.host(), jmxPortGetter.apply(b.id())))); - // add local client to fetch consumer metrics - map.put(-1, JndiClient.local()); - return Collections.unmodifiableMap(map); - }); + List receivers = + switch (config.string(METRIC_STORE_KEY).orElse(METRIC_STORE_LOCAL)) { + case METRIC_STORE_TOPIC -> List.of( + MetricStore.Receiver.topic( + config.requireString(ProducerConfigs.BOOTSTRAP_SERVERS_CONFIG)), + MetricStore.Receiver.local( + () -> CompletableFuture.completedStage(Map.of(-1, JndiClient.local())))); + case METRIC_STORE_LOCAL -> { + Supplier>> clientSupplier = + () -> + admin + .brokers() + .thenApply( + brokers -> { + var map = new HashMap(); + brokers.forEach( + b -> + map.put( + b.id(), + JndiClient.of(b.host(), jmxPortGetter.apply(b.id())))); + // add local client to fetch consumer metrics + map.put(-1, JndiClient.local()); + return Collections.unmodifiableMap(map); + }); + yield List.of(MetricStore.Receiver.local(clientSupplier)); + } + default -> throw new IllegalArgumentException( + "unknown metric store type: " + + config.string(METRIC_STORE_KEY) + + ". Use " + + METRIC_STORE_TOPIC + + " or " + + METRIC_STORE_LOCAL); + }; metricStore = MetricStore.builder() - .receivers(List.of(MetricStore.Receiver.local(clientSupplier))) + .receivers(receivers) .sensorsSupplier(() -> Map.of(this.costFunction.metricSensor(), (integer, e) -> {})) .build(); + this.roundRobinKeeper = RoundRobinKeeper.of(ROUND_ROBIN_LENGTH, roundRobinLease); } diff --git a/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java b/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java index 4912bb8517..233be18563 100644 --- a/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java +++ b/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; import org.astraea.common.Configuration; @@ -36,9 +37,12 @@ import org.astraea.common.cost.NodeThroughputCost; import org.astraea.common.cost.ReplicaLeaderCost; import org.astraea.common.metrics.ClusterBean; +import org.astraea.common.metrics.collector.MetricStore; +import org.astraea.common.producer.ProducerConfigs; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.mockito.stubbing.Answer; public class StrictCostPartitionerTest { @@ -291,4 +295,79 @@ public BrokerCost brokerCost(ClusterInfo clusterInfo, ClusterBean clusterBean) { } } } + + /** Test if the partitioner use correct metric store */ + @Test + void testMetricStoreConfigure() { + try (var mockedReceiver = Mockito.mockStatic(MetricStore.Receiver.class)) { + var topicReceiverCount = new AtomicInteger(0); + var localReceiverCount = new AtomicInteger(0); + mockedReceiver + .when(() -> MetricStore.Receiver.topic(Mockito.any())) + .then( + (Answer) + invocation -> { + topicReceiverCount.incrementAndGet(); + return Mockito.mock(MetricStore.Receiver.class); + }); + mockedReceiver + .when(() -> MetricStore.Receiver.local(Mockito.any())) + .then( + (Answer) + invocation -> { + localReceiverCount.incrementAndGet(); + return Mockito.mock(MetricStore.Receiver.class); + }); + + try (var partitioner = new StrictCostPartitioner()) { + // Check default metric store + var config = + new Configuration(Map.of(ProducerConfigs.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092")); + partitioner.configure(config); + Assertions.assertNotEquals(0, localReceiverCount.get()); + Assertions.assertEquals(0, topicReceiverCount.get()); + + // Check topic metric store + localReceiverCount.set(0); + topicReceiverCount.set(0); + config = + new Configuration( + Map.of( + ProducerConfigs.BOOTSTRAP_SERVERS_CONFIG, + "localhost:9092", + StrictCostPartitioner.METRIC_STORE_KEY, + StrictCostPartitioner.METRIC_STORE_TOPIC)); + partitioner.configure(config); + Assertions.assertNotEquals(0, topicReceiverCount.get()); + // topic collector may use local receiver to get local jmx metric + + // Check local metric store + topicReceiverCount.set(0); + localReceiverCount.set(0); + config = + new Configuration( + Map.of( + ProducerConfigs.BOOTSTRAP_SERVERS_CONFIG, + "localhost:9092", + StrictCostPartitioner.METRIC_STORE_KEY, + StrictCostPartitioner.METRIC_STORE_LOCAL)); + partitioner.configure(config); + Assertions.assertNotEquals(0, localReceiverCount.get()); + Assertions.assertEquals(0, topicReceiverCount.get()); + + // Check unknown metric store + localReceiverCount.set(0); + topicReceiverCount.set(0); + var config2 = + new Configuration( + Map.of( + ProducerConfigs.BOOTSTRAP_SERVERS_CONFIG, + "localhost:9092", + StrictCostPartitioner.METRIC_STORE_KEY, + "unknown")); + Assertions.assertThrows( + IllegalArgumentException.class, () -> partitioner.configure(config2)); + } + } + } } From ced840f9ded6e4167ccbbc86a20f79f7ec6be0c0 Mon Sep 17 00:00:00 2001 From: Chao-Heng Lee Date: Wed, 24 May 2023 11:43:59 +0800 Subject: [PATCH 2/2] [COMMON] Replace NodeInfo with Broker (#1763) --- .../java/org/astraea/app/backup/Backup.java | 2 +- .../astraea/app/performance/Performance.java | 2 +- .../app/publisher/MetricPublisher.java | 16 +- .../org/astraea/app/web/BalancerHandler.java | 2 +- .../org/astraea/app/web/BrokerHandler.java | 10 +- .../astraea/app/web/ReassignmentHandler.java | 6 +- .../app/web/SkewedPartitionScenario.java | 4 +- .../org/astraea/app/web/ThrottleHandler.java | 16 +- .../org/astraea/app/web/TopicHandler.java | 2 +- .../java/org/astraea/app/web/WebService.java | 3 +- .../app/performance/PerformanceTest.java | 6 +- .../astraea/app/web/BalancerHandlerTest.java | 8 +- .../app/web/ReassignmentHandlerTest.java | 14 +- .../astraea/app/web/ThrottleHandlerTest.java | 10 +- .../java/org/astraea/common/ByteUtils.java | 19 +-- .../java/org/astraea/common/admin/Admin.java | 7 +- .../org/astraea/common/admin/AdminImpl.java | 51 +++--- .../java/org/astraea/common/admin/Broker.java | 149 +++++++----------- .../org/astraea/common/admin/ClusterInfo.java | 34 ++-- .../common/admin/ClusterInfoBuilder.java | 123 +++------------ .../astraea/common/admin/ConsumerGroup.java | 2 +- .../org/astraea/common/admin/NodeInfo.java | 89 ----------- .../common/admin/OptimizedClusterInfo.java | 19 +-- .../org/astraea/common/admin/Partition.java | 8 +- .../org/astraea/common/admin/Replica.java | 4 +- .../astraea/common/admin/ReplicaBuilder.java | 24 +-- .../common/balancer/BalancerConsoleImpl.java | 8 +- .../common/balancer/BalancerUtils.java | 20 +-- .../executor/StraightPlanExecutor.java | 3 +- .../balancer/tweakers/ShuffleTweaker.java | 10 +- .../common/cost/BrokerDiskSpaceCost.java | 6 +- .../org/astraea/common/cost/CostUtils.java | 6 +- .../astraea/common/cost/MigrationCost.java | 12 +- .../org/astraea/common/cost/NetworkCost.java | 10 +- .../common/cost/NetworkIngressCost.java | 2 +- .../astraea/common/cost/NodeMetricsCost.java | 6 +- .../astraea/common/cost/RecordSizeCost.java | 6 +- .../common/cost/ReplicaLeaderCost.java | 2 +- .../common/cost/ReplicaLeaderSizeCost.java | 2 +- .../common/cost/ReplicaNumberCost.java | 6 +- .../common/cost/utils/ClusterInfoSensor.java | 8 +- .../common/metrics/MetricSeriesBuilder.java | 16 +- .../common/partitioner/RoundRobinKeeper.java | 4 +- .../org/astraea/common/ByteUtilsTest.java | 46 ------ .../org/astraea/common/admin/AdminTest.java | 41 ++--- .../admin/AdminWithOfflineBrokerTest.java | 20 +-- .../common/admin/ClusterInfoBuilderTest.java | 60 +++---- .../astraea/common/admin/ClusterInfoTest.java | 8 +- .../admin/ClusterInfoWithOfflineNodeTest.java | 9 +- .../astraea/common/admin/NodeInfoTest.java | 38 ----- .../admin/SomePartitionOfflineTest.java | 4 +- .../balancer/BalancerConfigTestSuite.java | 36 ++--- .../common/balancer/BalancerConsoleTest.java | 2 +- .../astraea/common/balancer/BalancerTest.java | 6 +- .../common/balancer/BalancerUtilsTest.java | 22 +-- .../common/balancer/FakeClusterInfo.java | 78 ++------- .../executor/StraightPlanExecutorTest.java | 8 +- .../balancer/tweakers/ShuffleTweakerTest.java | 20 +-- .../common/cost/BrokerDiskSpaceCostTest.java | 73 +++++---- .../astraea/common/cost/CostUtilsTest.java | 41 +++-- .../common/cost/MigrationCostTest.java | 34 ++-- .../astraea/common/cost/NetworkCostTest.java | 4 +- .../common/cost/RecordSizeCostTest.java | 10 +- .../common/cost/ReplicaLeaderCostTest.java | 35 ++-- .../cost/ReplicaLeaderSizeCostTest.java | 12 +- .../cost/utils/ClusterInfoSensorTest.java | 4 +- .../common/partitioner/PartitionerTest.java | 2 +- .../StrictCostPartitionerPerfTest.java | 55 ++++--- .../StrictCostPartitionerTest.java | 14 +- .../main/java/org/astraea/gui/Context.java | 6 +- .../java/org/astraea/gui/tab/BrokerNode.java | 8 +- .../java/org/astraea/gui/tab/ClientNode.java | 2 +- .../java/org/astraea/gui/tab/SettingNode.java | 6 +- .../astraea/gui/tab/health/BalancerNode.java | 8 +- .../astraea/gui/tab/health/HealthNode.java | 10 +- .../astraea/gui/tab/topic/PartitionNode.java | 2 +- .../astraea/gui/tab/topic/ReplicaNode.java | 4 +- .../org/astraea/gui/tab/topic/TopicNode.java | 3 +- .../gui/tab/health/BalancerNodeTest.java | 13 +- .../gui/tab/topic/ReplicaNodeTest.java | 12 +- 80 files changed, 566 insertions(+), 947 deletions(-) delete mode 100644 common/src/main/java/org/astraea/common/admin/NodeInfo.java delete mode 100644 common/src/test/java/org/astraea/common/admin/NodeInfoTest.java diff --git a/app/src/main/java/org/astraea/app/backup/Backup.java b/app/src/main/java/org/astraea/app/backup/Backup.java index 4b92ee152c..055ea1c0b1 100644 --- a/app/src/main/java/org/astraea/app/backup/Backup.java +++ b/app/src/main/java/org/astraea/app/backup/Backup.java @@ -50,7 +50,7 @@ public void restoreDistribution(ClusterInfo clusterInfo, String bootstrapServers .sorted( Comparator.comparing( replica -> !replica.isLeader())) - .map(replica -> replica.nodeInfo().id()) + .map(replica -> replica.broker().id()) .toList()))))) .configs(topic.config().raw()) .run() diff --git a/app/src/main/java/org/astraea/app/performance/Performance.java b/app/src/main/java/org/astraea/app/performance/Performance.java index f3a8b3f6d1..875ccaf1b1 100644 --- a/app/src/main/java/org/astraea/app/performance/Performance.java +++ b/app/src/main/java/org/astraea/app/performance/Performance.java @@ -382,7 +382,7 @@ else if (specifiedByBroker) { .join() .replicaStream() .filter(Replica::isLeader) - .filter(replica -> specifyBrokers.contains(replica.nodeInfo().id())) + .filter(replica -> specifyBrokers.contains(replica.broker().id())) .map(replica -> TopicPartition.of(replica.topic(), replica.partition())) .distinct() .toList(); diff --git a/app/src/main/java/org/astraea/app/publisher/MetricPublisher.java b/app/src/main/java/org/astraea/app/publisher/MetricPublisher.java index d99c040995..e260bf42ac 100644 --- a/app/src/main/java/org/astraea/app/publisher/MetricPublisher.java +++ b/app/src/main/java/org/astraea/app/publisher/MetricPublisher.java @@ -25,7 +25,7 @@ import org.astraea.app.argument.StringMapField; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; -import org.astraea.common.admin.NodeInfo; +import org.astraea.common.admin.Broker; import org.astraea.common.metrics.JndiClient; import org.astraea.common.metrics.collector.MetricFetcher; @@ -50,17 +50,17 @@ static void execute(Arguments arguments) { .clientSupplier( () -> admin - .nodeInfos() + .brokers() .thenApply( - nodes -> - nodes.stream() + brokers -> + brokers.stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, - node -> + Broker::id, + broker -> JndiClient.of( - node.host(), - arguments.idToJmxPort().apply(node.id())))))) + broker.host(), + arguments.idToJmxPort().apply(broker.id())))))) .fetchBeanDelay(arguments.period) .fetchMetadataDelay(Duration.ofMinutes(5)) .threads(3) diff --git a/app/src/main/java/org/astraea/app/web/BalancerHandler.java b/app/src/main/java/org/astraea/app/web/BalancerHandler.java index 84889b19a3..6a382ddba4 100644 --- a/app/src/main/java/org/astraea/app/web/BalancerHandler.java +++ b/app/src/main/java/org/astraea/app/web/BalancerHandler.java @@ -255,7 +255,7 @@ static class Placement { final Optional size; Placement(Replica replica, Optional size) { - this.brokerId = replica.nodeInfo().id(); + this.brokerId = replica.broker().id(); this.directory = replica.path(); this.size = size; } diff --git a/app/src/main/java/org/astraea/app/web/BrokerHandler.java b/app/src/main/java/org/astraea/app/web/BrokerHandler.java index 849fb4d333..a59ad5536a 100644 --- a/app/src/main/java/org/astraea/app/web/BrokerHandler.java +++ b/app/src/main/java/org/astraea/app/web/BrokerHandler.java @@ -25,7 +25,7 @@ import java.util.concurrent.CompletionStage; import java.util.stream.Collectors; import org.astraea.common.admin.Admin; -import org.astraea.common.admin.NodeInfo; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.TopicPartition; class BrokerHandler implements Handler { @@ -44,8 +44,12 @@ CompletionStage> brokers(Optional target) { .orElseGet( () -> admin - .nodeInfos() - .thenApply(ns -> ns.stream().map(NodeInfo::id).collect(Collectors.toSet()))); + .brokers() + .thenApply( + ns -> + ns.stream() + .map(org.astraea.common.admin.Broker::id) + .collect(Collectors.toSet()))); } catch (NumberFormatException e) { return CompletableFuture.failedFuture( new NoSuchElementException("the broker id must be number")); diff --git a/app/src/main/java/org/astraea/app/web/ReassignmentHandler.java b/app/src/main/java/org/astraea/app/web/ReassignmentHandler.java index f798c05362..2c2245e7e3 100644 --- a/app/src/main/java/org/astraea/app/web/ReassignmentHandler.java +++ b/app/src/main/java/org/astraea/app/web/ReassignmentHandler.java @@ -31,8 +31,8 @@ import java.util.stream.Stream; import org.astraea.common.FutureUtils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.admin.TopicPartitionReplica; @@ -110,7 +110,7 @@ public CompletionStage post(Channel channel) { availableBrokers.stream() .filter( b -> b.topicPartitions().contains(tp)) - .map(NodeInfo::id) + .map(Broker::id) .toList(); if (!ids.isEmpty()) return ids; return List.of( @@ -211,7 +211,7 @@ static class AddingReplica implements Response { AddingReplica(Replica addingReplica, long leaderSize) { this.topicName = addingReplica.topic(); this.partition = addingReplica.partition(); - this.broker = addingReplica.nodeInfo().id(); + this.broker = addingReplica.broker().id(); this.dataFolder = addingReplica.path(); this.size = addingReplica.size(); this.leaderSize = leaderSize; diff --git a/app/src/main/java/org/astraea/app/web/SkewedPartitionScenario.java b/app/src/main/java/org/astraea/app/web/SkewedPartitionScenario.java index 7fa6eb074c..963dbb29b8 100644 --- a/app/src/main/java/org/astraea/app/web/SkewedPartitionScenario.java +++ b/app/src/main/java/org/astraea/app/web/SkewedPartitionScenario.java @@ -30,7 +30,7 @@ import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.util.Pair; import org.astraea.common.admin.Admin; -import org.astraea.common.admin.NodeInfo; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.TopicPartition; import org.astraea.common.admin.TopicPartitionReplica; @@ -62,7 +62,7 @@ public CompletionStage apply(Admin admin) { admin.waitPartitionLeaderSynced( Map.of(topicName, partitions), Duration.ofSeconds(4))) .thenCompose(ignored -> admin.brokers()) - .thenApply(brokers -> brokers.stream().map(NodeInfo::id).sorted().toList()) + .thenApply(brokers -> brokers.stream().map(Broker::id).sorted().toList()) .thenCompose( brokerIds -> { var distribution = diff --git a/app/src/main/java/org/astraea/app/web/ThrottleHandler.java b/app/src/main/java/org/astraea/app/web/ThrottleHandler.java index d47e14a856..67cc5843de 100644 --- a/app/src/main/java/org/astraea/app/web/ThrottleHandler.java +++ b/app/src/main/java/org/astraea/app/web/ThrottleHandler.java @@ -30,8 +30,8 @@ import org.astraea.common.EnumInfo; import org.astraea.common.FutureUtils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.BrokerConfigs; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.TopicConfigs; import org.astraea.common.admin.TopicPartitionReplica; import org.astraea.common.json.TypeRef; @@ -100,9 +100,8 @@ public CompletionStage post(Channel channel) { var topicToAppends = admin - .nodeInfos() - .thenApply( - nodeInfos -> nodeInfos.stream().map(NodeInfo::id).collect(Collectors.toSet())) + .brokers() + .thenApply(brokers -> brokers.stream().map(Broker::id).collect(Collectors.toSet())) .thenCompose(admin::topicPartitionReplicas) .thenApply( replicas -> @@ -197,9 +196,8 @@ public CompletionStage delete(Channel channel) { var topicToSubtracts = admin - .nodeInfos() - .thenApply( - nodeInfos -> nodeInfos.stream().map(NodeInfo::id).collect(Collectors.toSet())) + .brokers() + .thenApply(brokers -> brokers.stream().map(Broker::id).collect(Collectors.toSet())) .thenCompose(admin::topicPartitionReplicas) .thenApply( replicas -> { @@ -251,11 +249,11 @@ public CompletionStage delete(Channel channel) { var brokerToUnset = admin - .nodeInfos() + .brokers() .thenApply( ns -> ns.stream() - .map(NodeInfo::id) + .map(Broker::id) .filter( id -> !channel.queries().containsKey("broker") diff --git a/app/src/main/java/org/astraea/app/web/TopicHandler.java b/app/src/main/java/org/astraea/app/web/TopicHandler.java index 1e7beec0f2..28537281d1 100644 --- a/app/src/main/java/org/astraea/app/web/TopicHandler.java +++ b/app/src/main/java/org/astraea/app/web/TopicHandler.java @@ -316,7 +316,7 @@ private Replica() { Replica(org.astraea.common.admin.Replica replica) { this( - replica.nodeInfo().id(), + replica.broker().id(), replica.lag(), replica.size(), replica.isLeader(), diff --git a/app/src/main/java/org/astraea/app/web/WebService.java b/app/src/main/java/org/astraea/app/web/WebService.java index 354bb95cf1..e4e9eab6b4 100644 --- a/app/src/main/java/org/astraea/app/web/WebService.java +++ b/app/src/main/java/org/astraea/app/web/WebService.java @@ -38,7 +38,6 @@ import org.astraea.common.Utils; import org.astraea.common.admin.Admin; import org.astraea.common.admin.Broker; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.metrics.JndiClient; import org.astraea.common.metrics.MBeanClient; import org.astraea.common.metrics.collector.MetricSensor; @@ -76,7 +75,7 @@ public WebService( brokers.stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, + Broker::id, b -> JndiClient.of(b.host(), brokerIdToJmxPort.apply(b.id())))); yield List.of( MetricStore.Receiver.local(() -> admin.brokers().thenApply(asBeanClientMap))); diff --git a/app/src/test/java/org/astraea/app/performance/PerformanceTest.java b/app/src/test/java/org/astraea/app/performance/PerformanceTest.java index c1dad4673b..90f56293a5 100644 --- a/app/src/test/java/org/astraea/app/performance/PerformanceTest.java +++ b/app/src/test/java/org/astraea/app/performance/PerformanceTest.java @@ -193,7 +193,7 @@ void testPartitionSupplier() { .join() .replicaStream() .filter(Replica::isLeader) - .filter(r -> r.nodeInfo().id() == 1) + .filter(r -> r.broker().id() == 1) .map(Replica::topicPartition) .collect(Collectors.toUnmodifiableSet()); @@ -239,7 +239,7 @@ void testPartitionSupplier() { .join() .replicaStream() .filter(Replica::isLeader) - .filter(replica -> replica.nodeInfo().id() == 1) + .filter(replica -> replica.broker().id() == 1) .map(Replica::topicPartition) .collect(Collectors.toSet()); var selector2 = args.topicPartitionSelector(); @@ -273,7 +273,7 @@ void testPartitionSupplier() { .replicaStream() .findFirst() .get() - .nodeInfo() + .broker() .id(); var noPartitionBroker = (validBroker == 3) ? 1 : validBroker + 1; args = diff --git a/app/src/test/java/org/astraea/app/web/BalancerHandlerTest.java b/app/src/test/java/org/astraea/app/web/BalancerHandlerTest.java index f6727111cb..53725255ed 100644 --- a/app/src/test/java/org/astraea/app/web/BalancerHandlerTest.java +++ b/app/src/test/java/org/astraea/app/web/BalancerHandlerTest.java @@ -58,8 +58,8 @@ import org.astraea.common.DataSize; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.balancer.AlgorithmConfig; @@ -976,7 +976,7 @@ void testChangeOrder() { (short) 10, r -> Replica.builder(r) - .nodeInfo(base.node(srcIter.next())) + .broker(base.node(srcIter.next())) .isPreferredLeader(srcPrefIter.next()) .path(srcDirIter.next()) .build()) @@ -992,7 +992,7 @@ void testChangeOrder() { (short) 10, r -> Replica.builder(r) - .nodeInfo(base.node(dstIter.next())) + .broker(base.node(dstIter.next())) .isPreferredLeader(dstPrefIter.next()) .path(dstDirIter.next()) .build()) @@ -1343,7 +1343,7 @@ private MetricStore metricStore(Admin admin, Set costFunctions) { brokers.stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, + Broker::id, b -> JndiClient.of(b.host(), brokerIdToJmxPort.apply(b.id()))))); var cf = Utils.costFunctions(costFunctions, HasClusterCost.class, Configuration.EMPTY); diff --git a/app/src/test/java/org/astraea/app/web/ReassignmentHandlerTest.java b/app/src/test/java/org/astraea/app/web/ReassignmentHandlerTest.java index 83577b5616..01570fdca4 100644 --- a/app/src/test/java/org/astraea/app/web/ReassignmentHandlerTest.java +++ b/app/src/test/java/org/astraea/app/web/ReassignmentHandlerTest.java @@ -60,7 +60,7 @@ void testMigrateToAnotherBroker() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id(); var nextBroker = SERVICE.dataFolders().keySet().stream().filter(i -> i != currentBroker).findAny().get(); @@ -88,7 +88,7 @@ void testMigrateToAnotherBroker() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id()); } } @@ -111,7 +111,7 @@ void testMigrateToAnotherPath() { .findFirst() .get(); - var currentBroker = currentReplica.nodeInfo().id(); + var currentBroker = currentReplica.broker().id(); var currentPath = currentReplica.path(); var nextPath = SERVICE.dataFolders().get(currentBroker).stream() @@ -170,7 +170,7 @@ void testExcludeSpecificBroker() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id(); var body = @@ -194,7 +194,7 @@ void testExcludeSpecificBroker() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id()); Assertions.assertEquals( 0, @@ -221,7 +221,7 @@ void testExcludeSpecificBrokerTopic() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id(); var body = @@ -247,7 +247,7 @@ void testExcludeSpecificBrokerTopic() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id()); Assertions.assertNotEquals( 0, diff --git a/app/src/test/java/org/astraea/app/web/ThrottleHandlerTest.java b/app/src/test/java/org/astraea/app/web/ThrottleHandlerTest.java index 35ebc92214..b917b4dbb5 100644 --- a/app/src/test/java/org/astraea/app/web/ThrottleHandlerTest.java +++ b/app/src/test/java/org/astraea/app/web/ThrottleHandlerTest.java @@ -30,8 +30,8 @@ import org.astraea.common.DataRate; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.BrokerConfigs; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.TopicConfigs; import org.astraea.common.admin.TopicPartition; import org.astraea.common.json.JsonConverter; @@ -55,13 +55,13 @@ static void closeService() { public void cleanup() { try (var admin = Admin.of(SERVICE.bootstrapServers())) { admin - .nodeInfos() + .brokers() .thenApply( ns -> ns.stream() .collect( Collectors.toMap( - NodeInfo::id, + Broker::id, ignored -> Set.of( BrokerConfigs.LEADER_REPLICATION_THROTTLED_RATE_CONFIG, @@ -509,11 +509,11 @@ void testDelete() { Runnable setThrottle = () -> { admin - .nodeInfos() + .brokers() .thenApply( ns -> ns.stream() - .map(NodeInfo::id) + .map(Broker::id) .collect( Collectors.toMap( n -> n, diff --git a/common/src/main/java/org/astraea/common/ByteUtils.java b/common/src/main/java/org/astraea/common/ByteUtils.java index 29c304a041..405c01f3c6 100644 --- a/common/src/main/java/org/astraea/common/ByteUtils.java +++ b/common/src/main/java/org/astraea/common/ByteUtils.java @@ -27,9 +27,9 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; import org.astraea.common.admin.Config; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.Topic; import org.astraea.common.admin.TopicPartition; @@ -186,12 +186,13 @@ public static byte[] toBytes(BeanObject value) { return beanBuilder.build().toByteArray(); } + // TODO: Due to the change of NodeInfo to Broker. This and the test should be updated. /** Serialize ClusterInfo by protocol buffer. */ public static byte[] toBytes(ClusterInfo value) { return ClusterInfoOuterClass.ClusterInfo.newBuilder() .setClusterId(value.clusterId()) .addAllNodeInfo( - value.nodes().stream() + value.brokers().stream() .map( nodeInfo -> ClusterInfoOuterClass.ClusterInfo.NodeInfo.newBuilder() @@ -223,9 +224,9 @@ public static byte[] toBytes(ClusterInfo value) { .setPartition(replica.partition()) .setNodeInfo( ClusterInfoOuterClass.ClusterInfo.NodeInfo.newBuilder() - .setId(replica.nodeInfo().id()) - .setHost(replica.nodeInfo().host()) - .setPort(replica.nodeInfo().port()) + .setId(replica.broker().id()) + .setHost(replica.broker().host()) + .setPort(replica.broker().port()) .build()) .setLag(replica.lag()) .setSize(replica.size()) @@ -327,6 +328,7 @@ public static BeanObject readBeanObject(byte[] bytes) throws SerializationExcept } } + // TODO: Due to the change of NodeInfo to Broker. This and the test should be updated. /** Deserialize to ClusterInfo with protocol buffer */ public static ClusterInfo readClusterInfo(byte[] bytes) { try { @@ -334,8 +336,7 @@ public static ClusterInfo readClusterInfo(byte[] bytes) { return ClusterInfo.of( outerClusterInfo.getClusterId(), outerClusterInfo.getNodeInfoList().stream() - .map( - nodeInfo -> NodeInfo.of(nodeInfo.getId(), nodeInfo.getHost(), nodeInfo.getPort())) + .map(nodeInfo -> Broker.of(nodeInfo.getId(), nodeInfo.getHost(), nodeInfo.getPort())) .collect(Collectors.toList()), outerClusterInfo.getTopicList().stream() .map( @@ -370,8 +371,8 @@ public Set topicPartitions() { Replica.builder() .topic(replica.getTopic()) .partition(replica.getPartition()) - .nodeInfo( - NodeInfo.of( + .broker( + Broker.of( replica.getNodeInfo().getId(), replica.getNodeInfo().getHost(), replica.getNodeInfo().getPort())) diff --git a/common/src/main/java/org/astraea/common/admin/Admin.java b/common/src/main/java/org/astraea/common/admin/Admin.java index 68a39c4b96..d6c7068ae2 100644 --- a/common/src/main/java/org/astraea/common/admin/Admin.java +++ b/common/src/main/java/org/astraea/common/admin/Admin.java @@ -164,11 +164,6 @@ default CompletionStage>>> lates CompletionStage> partitions(Set topics); - /** - * @return online node information - */ - CompletionStage> nodeInfos(); - /** * @return online broker information */ @@ -187,7 +182,7 @@ default CompletionStage>> brokerFolders() { brokers.stream() .collect( Collectors.toMap( - NodeInfo::id, + Broker::id, n -> n.dataFolders().stream() .map(Broker.DataFolder::path) diff --git a/common/src/main/java/org/astraea/common/admin/AdminImpl.java b/common/src/main/java/org/astraea/common/admin/AdminImpl.java index 1f51b1ac0a..2d0c01eba5 100644 --- a/common/src/main/java/org/astraea/common/admin/AdminImpl.java +++ b/common/src/main/java/org/astraea/common/admin/AdminImpl.java @@ -405,7 +405,11 @@ public CompletionStage> partitions(Set topics) { ts -> ts.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().isInternal()))), - (earliestOffsets, latestOffsets, maxTimestamps, tpInfos, topicAndInternal) -> + brokers() + .thenApply( + brokers -> + brokers.stream().collect(Collectors.toMap(Broker::id, broker -> broker))), + (earliestOffsets, latestOffsets, maxTimestamps, tpInfos, topicAndInternal, brokers) -> tpInfos.keySet().stream() .map( tp -> { @@ -416,11 +420,15 @@ public CompletionStage> partitions(Set topics) { var leader = tpInfo.leader() == null || tpInfo.leader().isEmpty() ? null - : NodeInfo.of(tpInfo.leader()); + : tpInfo.leader().id(); var replicas = - tpInfo.replicas().stream().map(NodeInfo::of).collect(Collectors.toList()); + tpInfo.replicas().stream() + .map(node -> brokers.getOrDefault(node.id(), Broker.of(node))) + .collect(Collectors.toList()); var isr = - tpInfo.isr().stream().map(NodeInfo::of).collect(Collectors.toList()); + tpInfo.isr().stream() + .map(node -> brokers.getOrDefault(node.id(), Broker.of(node))) + .collect(Collectors.toList()); return new Partition( tp.topic(), tp.partition(), @@ -436,13 +444,6 @@ public CompletionStage> partitions(Set topics) { .collect(Collectors.toList())); } - @Override - public CompletionStage> nodeInfos() { - return to(kafkaAdmin.describeCluster().nodes()) - .thenApply( - nodes -> nodes.stream().map(NodeInfo::of).collect(Collectors.toUnmodifiableList())); - } - @Override public CompletionStage> brokers() { return clusterIdAndBrokers().thenApply(Map.Entry::getValue); @@ -483,7 +484,7 @@ private CompletionStage>> clusterIdAndBrokers() { configs.get(String.valueOf(node.id())), logDirs.get(node.id()), topics.values())) - .sorted(Comparator.comparing(NodeInfo::id)) + .sorted(Comparator.comparing(Broker::id)) .collect(Collectors.toList()))); } @@ -522,7 +523,7 @@ public CompletionStage> consumerGroups(Set consumerG groupId, consumerGroupDescriptions.get(groupId).partitionAssignor(), consumerGroupDescriptions.get(groupId).state().name(), - NodeInfo.of(consumerGroupDescriptions.get(groupId).coordinator()), + consumerGroupDescriptions.get(groupId).coordinator().id(), consumerGroupMetadata.get(groupId).entrySet().stream() .collect( Collectors.toUnmodifiableMap( @@ -622,12 +623,7 @@ public CompletionStage clusterInfo(Set topics) { var topicMap = topicList.stream().collect(Collectors.toUnmodifiableMap(Topic::name, t -> t)); return ClusterInfo.of( - clusterIdAndBrokers.getKey(), - clusterIdAndBrokers.getValue().stream() - .map(x -> (NodeInfo) x) - .collect(Collectors.toUnmodifiableList()), - topicMap, - replicas); + clusterIdAndBrokers.getKey(), clusterIdAndBrokers.getValue(), topicMap, replicas); }); } @@ -642,7 +638,11 @@ private CompletionStage> replicas(Set topics) { // supported version: 2.4.0 // https://issues.apache.org/jira/browse/KAFKA-8345 .exceptionally(exceptionHandler(UnsupportedVersionException.class, Map.of())), - (logDirs, ts, reassignmentMap) -> + brokers() + .thenApply( + brokers -> + brokers.stream().collect(Collectors.toMap(Broker::id, broker -> broker))), + (logDirs, ts, reassignmentMap, brokers) -> ts.values().stream() .flatMap(topic -> topic.partitions().stream().map(p -> Map.entry(topic.name(), p))) .flatMap( @@ -687,7 +687,9 @@ private CompletionStage> replicas(Set topics) { .internal(internal) .isAdding(isAdding) .isRemoving(isRemoving) - .nodeInfo(NodeInfo.of(node)) + .broker( + brokers.getOrDefault( + node.id(), Broker.of(node))) .lag(pathAndReplica.getValue().offsetLag()) .size(pathAndReplica.getValue().size()) .isLeader( @@ -716,7 +718,7 @@ private CompletionStage> replicas(Set topics) { .sorted( Comparator.comparing(Replica::topic) .thenComparing(Replica::partition) - .thenComparing(r -> r.nodeInfo().id())) + .thenComparing(r -> r.broker().id())) .collect(Collectors.toUnmodifiableList())); } @@ -1374,10 +1376,9 @@ public void close() { Integer, Map>>> logDirs() { - return nodeInfos() + return brokers() .thenApply( - nodeInfos -> - nodeInfos.stream().map(NodeInfo::id).collect(Collectors.toUnmodifiableSet())) + brokers -> brokers.stream().map(Broker::id).collect(Collectors.toUnmodifiableSet())) .thenCompose(ids -> to(kafkaAdmin.describeLogDirs(ids).allDescriptions())) .thenApply( ds -> diff --git a/common/src/main/java/org/astraea/common/admin/Broker.java b/common/src/main/java/org/astraea/common/admin/Broker.java index 69ecb71e97..9e99102178 100644 --- a/common/src/main/java/org/astraea/common/admin/Broker.java +++ b/common/src/main/java/org/astraea/common/admin/Broker.java @@ -23,9 +23,42 @@ import java.util.stream.Collectors; import org.apache.kafka.common.requests.DescribeLogDirsResponse; -public interface Broker extends NodeInfo { +/** + * @param id + * @param host + * @param port + * @param isController + * @param config config used by this node + * @param dataFolders the disk folder used to stored data by this node + * @param topicPartitions + * @param topicPartitionLeaders partition leaders hosted by this broker + */ +public record Broker( + int id, + String host, + int port, + boolean isController, + Config config, + List dataFolders, + Set topicPartitions, + Set topicPartitionLeaders) { + + /** + * @return true if the broker is offline. An offline node can't offer host or port information. + */ + public boolean offline() { + return host() == null || host().isEmpty() || port() < 0; + } + + public static Broker of(int id, String host, int port) { + return new Broker(id, host, port, false, Config.EMPTY, List.of(), Set.of(), Set.of()); + } + + public static Broker of(org.apache.kafka.common.Node node) { + return of(node.id(), node.host(), node.port()); + } - static Broker of( + public static Broker of( boolean isController, org.apache.kafka.common.Node nodeInfo, Map configs, @@ -60,26 +93,9 @@ static Broker of( tpAndSize -> !partitionsFromTopicDesc.contains(tpAndSize.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - return (DataFolder) - new DataFolder() { - - @Override - public String path() { - return path; - } - - @Override - public Map partitionSizes() { - return partitionSizes; - } - - @Override - public Map orphanPartitionSizes() { - return orphanPartitionSizes; - } - }; + return new DataFolder(path, partitionSizes, orphanPartitionSizes); }) - .collect(Collectors.toList()); + .toList(); var topicPartitionLeaders = topics.stream() .flatMap( @@ -88,83 +104,24 @@ public Map orphanPartitionSizes() { .filter(p -> p.leader() != null && p.leader().id() == nodeInfo.id()) .map(p -> TopicPartition.of(topic.name(), p.partition()))) .collect(Collectors.toUnmodifiableSet()); - return new Broker() { - @Override - public String host() { - return nodeInfo.host(); - } - - @Override - public int port() { - return nodeInfo.port(); - } - - @Override - public int id() { - return nodeInfo.id(); - } - - @Override - public boolean isController() { - return isController; - } - - @Override - public Config config() { - return config; - } - - @Override - public List dataFolders() { - return folders; - } - - @Override - public Set topicPartitions() { - return partitionsFromTopicDesc; - } - - @Override - public Set topicPartitionLeaders() { - return topicPartitionLeaders; - } - }; + return new Broker( + nodeInfo.id(), + nodeInfo.host(), + nodeInfo.port(), + isController, + config, + folders, + partitionsFromTopicDesc, + topicPartitionLeaders); } - boolean isController(); - - /** - * @return config used by this node - */ - Config config(); - - /** - * @return the disk folder used to stored data by this node - */ - List dataFolders(); - - Set topicPartitions(); - /** - * @return partition leaders hosted by this broker + * @param path the path on the local disk + * @param partitionSizes topic partition hosed by this node and size of files + * @param orphanPartitionSizes topic partition located by this node but not traced by cluster */ - Set topicPartitionLeaders(); - - interface DataFolder { - - /** - * @return the path on the local disk - */ - String path(); - - /** - * @return topic partition hosed by this node and size of files - */ - Map partitionSizes(); - - /** - * @return topic partition located by this node but not traced by cluster - */ - Map orphanPartitionSizes(); - } + public record DataFolder( + String path, + Map partitionSizes, + Map orphanPartitionSizes) {} } diff --git a/common/src/main/java/org/astraea/common/admin/ClusterInfo.java b/common/src/main/java/org/astraea/common/admin/ClusterInfo.java index 9411ed170c..0be6ff795a 100644 --- a/common/src/main/java/org/astraea/common/admin/ClusterInfo.java +++ b/common/src/main/java/org/astraea/common/admin/ClusterInfo.java @@ -87,14 +87,14 @@ static boolean placementMatch( .sorted( Comparator.comparing(Replica::isPreferredLeader) .reversed() - .thenComparing(r -> r.nodeInfo().id())) + .thenComparing(r -> r.broker().id())) .collect(Collectors.toUnmodifiableList()); final var targetIds = targetReplicas.stream() .sorted( Comparator.comparing(Replica::isPreferredLeader) .reversed() - .thenComparing(r -> r.nodeInfo().id())) + .thenComparing(r -> r.broker().id())) .collect(Collectors.toUnmodifiableList()); return IntStream.range(0, sourceIds.size()) .allMatch( @@ -102,7 +102,7 @@ static boolean placementMatch( final var source = sourceIds.get(index); final var target = targetIds.get(index); return source.isPreferredLeader() == target.isPreferredLeader() - && source.nodeInfo().id() == target.nodeInfo().id() + && source.broker().id() == target.broker().id() && Objects.equals(source.path(), target.path()); }); } @@ -121,7 +121,7 @@ static String toString(ClusterInfo allocation) { .forEach( log -> stringBuilder.append( - String.format("(%s, %s) ", log.nodeInfo().id(), log.path()))); + String.format("(%s, %s) ", log.broker().id(), log.path()))); stringBuilder.append(System.lineSeparator()); }); @@ -147,7 +147,7 @@ static String toString(ClusterInfo allocation) { * @return cluster info */ static ClusterInfo of( - String clusterId, List nodes, Map topics, List replicas) { + String clusterId, List nodes, Map topics, List replicas) { return new OptimizedClusterInfo(clusterId, nodes, topics, replicas); } @@ -306,8 +306,8 @@ default Set topicPartitionReplicas() { * @return the node information. It throws NoSuchElementException if specify node id is not * associated to any node */ - default NodeInfo node(int id) { - return nodes().stream() + default Broker node(int id) { + return brokers().stream() .filter(n -> n.id() == id) .findAny() .orElseThrow(() -> new NoSuchElementException(id + " is nonexistent")); @@ -321,7 +321,7 @@ default Map> brokerFolders() { return brokers().stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, + Broker::id, node -> node.dataFolders().stream() .map(Broker.DataFolder::path) @@ -332,7 +332,7 @@ default Map> brokerFolders() { // implements following methods by smart index to speed up the queries default Stream replicaStream(int broker) { - return replicaStream().filter(r -> r.nodeInfo().id() == broker); + return replicaStream().filter(r -> r.broker().id() == broker); } default Stream replicaStream(String topic) { @@ -340,8 +340,7 @@ default Stream replicaStream(String topic) { } default Stream replicaStream(BrokerTopic brokerTopic) { - return replicaStream(brokerTopic.topic()) - .filter(r -> r.nodeInfo().id() == brokerTopic.broker()); + return replicaStream(brokerTopic.topic()).filter(r -> r.broker().id() == brokerTopic.broker()); } default Stream replicaStream(TopicPartition partition) { @@ -350,22 +349,15 @@ default Stream replicaStream(TopicPartition partition) { default Stream replicaStream(TopicPartitionReplica replica) { return replicaStream(replica.topicPartition()) - .filter(r -> r.nodeInfo().id() == replica.brokerId()); + .filter(r -> r.broker().id() == replica.brokerId()); } // ---------------------[abstract methods]---------------------// /** - * @return The known nodes + * @return The known brokers */ - List nodes(); - - default List brokers() { - return nodes().stream() - .filter(n -> n instanceof Broker) - .map(n -> (Broker) n) - .collect(Collectors.toUnmodifiableList()); - } + List brokers(); /** * @return replica stream to offer effective way to operate a bunch of replicas diff --git a/common/src/main/java/org/astraea/common/admin/ClusterInfoBuilder.java b/common/src/main/java/org/astraea/common/admin/ClusterInfoBuilder.java index 1aa93e1c40..2661d46071 100644 --- a/common/src/main/java/org/astraea/common/admin/ClusterInfoBuilder.java +++ b/common/src/main/java/org/astraea/common/admin/ClusterInfoBuilder.java @@ -37,7 +37,7 @@ public class ClusterInfoBuilder { private final ClusterInfo sourceCluster; private final List< - BiFunction, List, Map.Entry, List>>> + BiFunction, List, Map.Entry, List>>> alterations; ClusterInfoBuilder(ClusterInfo source) { @@ -52,7 +52,7 @@ public class ClusterInfoBuilder { * @return this. */ public ClusterInfoBuilder applyNodes( - BiFunction, List, List> alteration) { + BiFunction, List, List> alteration) { this.alterations.add( (nodes, replicas) -> Map.entry(alteration.apply(nodes, replicas), replicas)); return this; @@ -65,7 +65,7 @@ public ClusterInfoBuilder applyNodes( * @return this. */ public ClusterInfoBuilder applyReplicas( - BiFunction, List, List> alteration) { + BiFunction, List, List> alteration) { this.alterations.add((nodes, replicas) -> Map.entry(nodes, alteration.apply(nodes, replicas))); return this; } @@ -121,14 +121,15 @@ public ClusterInfoBuilder addFolders(Map> folders) { .map( node -> { if (folders.containsKey(node.id())) - return FakeBroker.of( + return fakeBroker( node.id(), node.host(), node.port(), Stream.concat( - ((Broker) node).dataFolders().stream(), - folders.get(node.id()).stream().map(FakeDataFolder::of)) - .collect(Collectors.toUnmodifiableList())); + node.dataFolders().stream(), + folders.get(node.id()).stream() + .map(ClusterInfoBuilder::fakeDataFolder)) + .toList()); else return node; }) .collect(Collectors.toUnmodifiableList()); @@ -160,16 +161,13 @@ public ClusterInfoBuilder addTopic( String topicName, int partitionSize, short replicaFactor, Function mapper) { return applyReplicas( (nodes, replicas) -> { - if (nodes.stream().anyMatch(node -> !(node instanceof Broker))) - throw new IllegalStateException("All the nodes must include the folder info"); if (nodes.size() < replicaFactor) throw new IllegalArgumentException( "Insufficient node for this replica factor: " + nodes.size() + " < " + replicaFactor); - var nodeSelector = - Stream.generate(nodes::stream).flatMap(x -> x).map(x -> (Broker) x).iterator(); + var nodeSelector = Stream.generate(nodes::stream).flatMap(x -> x).iterator(); // simulate the actual Kafka logic of log placement var folderLogCounter = @@ -178,14 +176,13 @@ public ClusterInfoBuilder addTopic( Collectors.toUnmodifiableMap( node -> node, node -> - ((Broker) node) - .dataFolders().stream() - .collect( - Collectors.toMap( - Broker.DataFolder::path, x -> new AtomicInteger())))); + node.dataFolders().stream() + .collect( + Collectors.toMap( + Broker.DataFolder::path, x -> new AtomicInteger())))); replicas.forEach( replica -> - folderLogCounter.get(replica.nodeInfo()).get(replica.path()).incrementAndGet()); + folderLogCounter.get(replica.broker()).get(replica.path()).incrementAndGet()); folderLogCounter.forEach( (node, folders) -> { @@ -215,7 +212,7 @@ public ClusterInfoBuilder addTopic( return Replica.builder() .topic(tp.topic()) .partition(tp.partition()) - .nodeInfo(broker) + .broker(broker) .isAdding(false) .isRemoving(false) .lag(0) @@ -272,7 +269,7 @@ public ClusterInfoBuilder reassignReplica( r -> { if (r.topicPartitionReplica().equals(replica)) { matched.set(true); - return Replica.builder(r).nodeInfo(newNode).path(toDir).build(); + return Replica.builder(r).broker(newNode).path(toDir).build(); } else { return r; } @@ -322,7 +319,7 @@ public ClusterInfoBuilder setPreferredLeader(TopicPartitionReplica replica) { * ClusterInfo}. */ public ClusterInfo build() { - var nodes = sourceCluster.nodes(); + var nodes = sourceCluster.brokers(); var replicas = sourceCluster.replicas(); for (var alteration : alterations) { var e = alteration.apply(nodes, replicas); @@ -338,90 +335,14 @@ private static Broker fakeNode(int brokerId) { var port = new Random(brokerId).nextInt(65535) + 1; var folders = List.of(); - return FakeBroker.of(brokerId, host, port, folders); + return fakeBroker(brokerId, host, port, folders); } - interface FakeBroker extends Broker { - - static FakeBroker of(int id, String host, int port, List folders) { - var hashCode = Objects.hash(id, host, port); - return new FakeBroker() { - @Override - public List dataFolders() { - return folders; - } - - @Override - public String host() { - return host; - } - - @Override - public int port() { - return port; - } - - @Override - public int id() { - return id; - } - - @Override - public String toString() { - return "FakeNodeInfo{" + "host=" + host() + ", id=" + id() + ", port=" + port() + '}'; - } - - @Override - public int hashCode() { - return hashCode; - } - - @Override - public boolean equals(Object other) { - if (other instanceof NodeInfo) { - var node = (NodeInfo) other; - return id() == node.id() && port() == node.port() && host().equals(node.host()); - } - return false; - } - }; - } - - @Override - default boolean isController() { - throw new UnsupportedOperationException(); - } - - @Override - default Config config() { - throw new UnsupportedOperationException(); - } - - @Override - default Set topicPartitions() { - throw new UnsupportedOperationException(); - } - - @Override - default Set topicPartitionLeaders() { - throw new UnsupportedOperationException(); - } + static Broker fakeBroker(int Id, String host, int port, List dataFolders) { + return new Broker(Id, host, port, false, Config.EMPTY, dataFolders, Set.of(), Set.of()); } - interface FakeDataFolder extends Broker.DataFolder { - - static FakeDataFolder of(String path) { - return () -> path; - } - - @Override - default Map partitionSizes() { - throw new UnsupportedOperationException(); - } - - @Override - default Map orphanPartitionSizes() { - throw new UnsupportedOperationException(); - } + private static Broker.DataFolder fakeDataFolder(String path) { + return new Broker.DataFolder(path, Map.of(), Map.of()); } } diff --git a/common/src/main/java/org/astraea/common/admin/ConsumerGroup.java b/common/src/main/java/org/astraea/common/admin/ConsumerGroup.java index 29691e8337..ef8a696e83 100644 --- a/common/src/main/java/org/astraea/common/admin/ConsumerGroup.java +++ b/common/src/main/java/org/astraea/common/admin/ConsumerGroup.java @@ -23,6 +23,6 @@ public record ConsumerGroup( String groupId, String assignor, String state, - NodeInfo coordinator, + int coordinatorId, Map consumeProgress, Map> assignment) {} diff --git a/common/src/main/java/org/astraea/common/admin/NodeInfo.java b/common/src/main/java/org/astraea/common/admin/NodeInfo.java deleted file mode 100644 index fffd74cc35..0000000000 --- a/common/src/main/java/org/astraea/common/admin/NodeInfo.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.astraea.common.admin; - -import java.util.Objects; - -public interface NodeInfo { - - static NodeInfo of(org.apache.kafka.common.Node node) { - return of(node.id(), node.host(), node.port()); - } - - static NodeInfo of(int id, String host, int port) { - return new NodeInfo() { - @Override - public String toString() { - return "NodeInfo{" + "host=" + host + ", id=" + id + ", port=" + port + '}'; - } - - // NodeInfo is used to be key of Map commonly, so creating hash can reduce the memory pressure - private final int hashCode = Objects.hash(id, host, port); - - @Override - public String host() { - return host; - } - - @Override - public int id() { - return id; - } - - @Override - public int port() { - return port; - } - - @Override - public int hashCode() { - return hashCode; - } - - @Override - public boolean equals(Object other) { - if (other instanceof NodeInfo) { - var node = (NodeInfo) other; - return id() == node.id() && port() == node.port() && host().equals(node.host()); - } - return false; - } - }; - } - - /** - * @return The host name for this node - */ - String host(); - - /** - * @return The client (kafka data, jmx, etc.) port for this node - */ - int port(); - - /** - * @return id of broker node. it must be unique. - */ - int id(); - - /** - * @return true if the node is offline. An offline node can't offer host or port information. - */ - default boolean offline() { - return host() == null || host().isEmpty() || port() < 0; - } -} diff --git a/common/src/main/java/org/astraea/common/admin/OptimizedClusterInfo.java b/common/src/main/java/org/astraea/common/admin/OptimizedClusterInfo.java index d9448b92a6..e034b9368e 100644 --- a/common/src/main/java/org/astraea/common/admin/OptimizedClusterInfo.java +++ b/common/src/main/java/org/astraea/common/admin/OptimizedClusterInfo.java @@ -28,7 +28,7 @@ /** It optimizes all queries by pre-allocated Map collection. */ class OptimizedClusterInfo implements ClusterInfo { private final String clusterId; - private final List nodeInfos; + private final List brokers; private final List all; private final Lazy> topics; @@ -44,12 +44,9 @@ class OptimizedClusterInfo implements ClusterInfo { private final Lazy>> byReplica; OptimizedClusterInfo( - String clusterId, - List nodeInfos, - Map topics, - List replicas) { + String clusterId, List brokers, Map topics, List replicas) { this.clusterId = clusterId; - this.nodeInfos = nodeInfos; + this.brokers = brokers; this.all = replicas; this.topics = Lazy.of( @@ -104,7 +101,7 @@ && internal() == objTopic.internal() all.stream() .collect( Collectors.groupingBy( - r -> BrokerTopic.of(r.nodeInfo().id(), r.topic()), + r -> BrokerTopic.of(r.broker().id(), r.topic()), Collectors.toUnmodifiableList()))); this.byBrokerTopicForLeader = Lazy.of( @@ -114,7 +111,7 @@ && internal() == objTopic.internal() .filter(Replica::isLeader) .collect( Collectors.groupingBy( - r -> BrokerTopic.of(r.nodeInfo().id(), r.topic()), + r -> BrokerTopic.of(r.broker().id(), r.topic()), Collectors.toUnmodifiableList()))); this.byBroker = @@ -123,7 +120,7 @@ && internal() == objTopic.internal() all.stream() .collect( Collectors.groupingBy( - r -> r.nodeInfo().id(), Collectors.toUnmodifiableList()))); + r -> r.broker().id(), Collectors.toUnmodifiableList()))); this.byTopic = Lazy.of( @@ -204,8 +201,8 @@ public Set topicNames() { } @Override - public List nodes() { - return nodeInfos; + public List brokers() { + return brokers; } @Override diff --git a/common/src/main/java/org/astraea/common/admin/Partition.java b/common/src/main/java/org/astraea/common/admin/Partition.java index 1bbc40ecc6..1addd8330b 100644 --- a/common/src/main/java/org/astraea/common/admin/Partition.java +++ b/common/src/main/java/org/astraea/common/admin/Partition.java @@ -24,7 +24,7 @@ * @param latestOffset existent latest offset * @param maxTimestamp max timestamp of existent records. If the kafka servers don't support to * fetch max timestamp, this method will return empty - * @param leader null if the node gets offline. otherwise, it returns node info. + * @param leaderId null if the node gets offline. otherwise, it returns node id. * @param internal true if this topic is internal (system) topic */ public record Partition( @@ -33,9 +33,9 @@ public record Partition( long earliestOffset, long latestOffset, Optional maxTimestamp, - Optional leader, - List replicas, - List isr, + Optional leaderId, + List replicas, + List isr, boolean internal) { public TopicPartition topicPartition() { diff --git a/common/src/main/java/org/astraea/common/admin/Replica.java b/common/src/main/java/org/astraea/common/admin/Replica.java index 4bab1648c2..84a82b3c13 100644 --- a/common/src/main/java/org/astraea/common/admin/Replica.java +++ b/common/src/main/java/org/astraea/common/admin/Replica.java @@ -32,7 +32,7 @@ static ReplicaBuilder builder(Replica replica) { * @return TopicPartitionReplica */ default TopicPartitionReplica topicPartitionReplica() { - return TopicPartitionReplica.of(topic(), partition(), nodeInfo().id()); + return TopicPartitionReplica.of(topic(), partition(), broker().id()); } /** @@ -57,7 +57,7 @@ default TopicPartition topicPartition() { /** * @return information of the node hosts this replica */ - NodeInfo nodeInfo(); + Broker broker(); /** * @return true if this replica is a leader replica diff --git a/common/src/main/java/org/astraea/common/admin/ReplicaBuilder.java b/common/src/main/java/org/astraea/common/admin/ReplicaBuilder.java index a499e79e1a..b7e1929643 100644 --- a/common/src/main/java/org/astraea/common/admin/ReplicaBuilder.java +++ b/common/src/main/java/org/astraea/common/admin/ReplicaBuilder.java @@ -22,7 +22,7 @@ public class ReplicaBuilder { private String topic; private int partition; - private NodeInfo nodeInfo; + private Broker broker; private long lag; private long size; @@ -40,7 +40,7 @@ public class ReplicaBuilder { ReplicaBuilder replica(Replica replica) { this.topic = replica.topic(); this.partition = replica.partition(); - this.nodeInfo = replica.nodeInfo(); + this.broker = replica.broker(); this.lag = replica.lag(); this.size = replica.size(); this.isLeader = replica.isLeader(); @@ -63,8 +63,8 @@ public ReplicaBuilder partition(int partition) { return this; } - public ReplicaBuilder nodeInfo(NodeInfo nodeInfo) { - this.nodeInfo = nodeInfo; + public ReplicaBuilder broker(Broker broker) { + this.broker = broker; return this; } @@ -166,7 +166,7 @@ public Replica build() { private static class ReplicaImpl implements Replica { private final String topic; private final int partition; - private final NodeInfo nodeInfo; + private final Broker broker; private final long lag; private final long size; @@ -185,7 +185,7 @@ private static class ReplicaImpl implements Replica { private ReplicaImpl(ReplicaBuilder builder) { this.topic = Objects.requireNonNull(builder.topic); this.partition = builder.partition; - this.nodeInfo = Objects.requireNonNull(builder.nodeInfo); + this.broker = Objects.requireNonNull(builder.broker); this.isAdding = builder.isAdding; this.isRemoving = builder.isRemoving; this.lag = builder.lag; @@ -240,8 +240,8 @@ public int partition() { } @Override - public NodeInfo nodeInfo() { - return nodeInfo; + public Broker broker() { + return broker; } @Override @@ -286,7 +286,9 @@ public boolean equals(Object o) { && isOffline == replica.isOffline && isPreferredLeader == replica.isPreferredLeader && topic.equals(replica.topic) - && nodeInfo.equals(replica.nodeInfo) + && broker.id() == replica.broker.id() + && broker.host().equals(replica.broker.host()) + && broker.port() == replica.broker.port() && Objects.equals(path, replica.path); } @@ -299,7 +301,7 @@ public String toString() { + ", partition=" + partition() + ", broker=" - + nodeInfo() + + broker() + ", path='" + path() + '\'' @@ -313,7 +315,7 @@ public int hashCode() { return Objects.hash( topic, partition, - nodeInfo, + broker, lag, size, internal, diff --git a/common/src/main/java/org/astraea/common/balancer/BalancerConsoleImpl.java b/common/src/main/java/org/astraea/common/balancer/BalancerConsoleImpl.java index b13e679d40..aa9b5c2343 100644 --- a/common/src/main/java/org/astraea/common/balancer/BalancerConsoleImpl.java +++ b/common/src/main/java/org/astraea/common/balancer/BalancerConsoleImpl.java @@ -270,8 +270,8 @@ private CompletionStage checkPlanConsistency(Balancer.Plan plan) { .sorted( Comparator.comparing(Replica::isPreferredLeader) .reversed() - .thenComparing(x -> x.nodeInfo().id())) - .map(x -> Map.entry(x.nodeInfo().id(), x.path())) + .thenComparing(x -> x.broker().id())) + .map(x -> Map.entry(x.broker().id(), x.path())) .collect(Collectors.toUnmodifiableList()))); return admin .topicNames(false) @@ -292,8 +292,8 @@ private CompletionStage checkPlanConsistency(Balancer.Plan plan) { .sorted( Comparator.comparing(Replica::isPreferredLeader) .reversed() - .thenComparing(x -> x.nodeInfo().id())) - .map(x -> Map.entry(x.nodeInfo().id(), x.path())) + .thenComparing(x -> x.broker().id())) + .map(x -> Map.entry(x.broker().id(), x.path())) .collect(Collectors.toUnmodifiableList()))); var mismatchPartitions = before.entrySet().stream() diff --git a/common/src/main/java/org/astraea/common/balancer/BalancerUtils.java b/common/src/main/java/org/astraea/common/balancer/BalancerUtils.java index d846905f0b..c352de416f 100644 --- a/common/src/main/java/org/astraea/common/balancer/BalancerUtils.java +++ b/common/src/main/java/org/astraea/common/balancer/BalancerUtils.java @@ -26,8 +26,8 @@ import java.util.stream.IntStream; import java.util.stream.Stream; import org.astraea.common.EnumInfo; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; public final class BalancerUtils { @@ -57,7 +57,7 @@ public static Map balancingMode(ClusterInfo cluster, St (id) -> map.getOrDefault(id, map.getOrDefault("default", BalancingModes.BALANCING)); return cluster.brokers().stream() - .map(NodeInfo::id) + .map(Broker::id) .collect(Collectors.toUnmodifiableMap(Function.identity(), mode)); } @@ -82,7 +82,7 @@ public static void verifyClearBrokerValidness( var ongoingEventReplica = cluster.replicas().stream() - .filter(r -> isDemoted.test(r.nodeInfo().id())) + .filter(r -> isDemoted.test(r.broker().id())) .filter(r -> r.isAdding() || r.isRemoving() || r.isFuture()) .map(Replica::topicPartitionReplica) .collect(Collectors.toUnmodifiableSet()); @@ -102,7 +102,7 @@ public static void verifyClearBrokerValidness( public static ClusterInfo clearedCluster( ClusterInfo initial, Predicate clearBrokers, Predicate allowedBrokers) { final var allowed = - initial.nodes().stream() + initial.brokers().stream() .filter(node -> allowedBrokers.test(node.id())) .filter(node -> Predicate.not(clearBrokers).test(node.id())) .collect(Collectors.toUnmodifiableSet()); @@ -121,12 +121,12 @@ public static ClusterInfo clearedCluster( tp -> tp, tp -> initial.replicas(tp).stream() - .map(Replica::nodeInfo) + .map(Replica::broker) .collect(Collectors.toSet()))); return ClusterInfo.builder(initial) .mapLog( replica -> { - if (!clearBrokers.test(replica.nodeInfo().id())) return replica; + if (!clearBrokers.test(replica.broker().id())) return replica; var currentReplicaList = trackingReplicaList.get(replica.topicPartition()); var broker = IntStream.range(0, allowed.size()) @@ -139,10 +139,10 @@ public static ClusterInfo clearedCluster( "Unable to clear replica " + replica.topicPartitionReplica() + " for broker " - + replica.nodeInfo().id() + + replica.broker().id() + ", the allowed destination brokers are " + allowed.stream() - .map(NodeInfo::id) + .map(Broker::id) .collect(Collectors.toUnmodifiableSet()) + " but all of them already hosting a replica for this partition. " + "There is no broker can adopt this replica.")); @@ -150,10 +150,10 @@ public static ClusterInfo clearedCluster( // update the tracking list. have to do this to avoid putting two replicas from the // same tp to one broker. - currentReplicaList.remove(replica.nodeInfo()); + currentReplicaList.remove(replica.broker()); currentReplicaList.add(broker); - return Replica.builder(replica).nodeInfo(broker).path(folder).build(); + return Replica.builder(replica).broker(broker).path(folder).build(); }) .build(); } diff --git a/common/src/main/java/org/astraea/common/balancer/executor/StraightPlanExecutor.java b/common/src/main/java/org/astraea/common/balancer/executor/StraightPlanExecutor.java index d295bc66fc..d932266afd 100644 --- a/common/src/main/java/org/astraea/common/balancer/executor/StraightPlanExecutor.java +++ b/common/src/main/java/org/astraea/common/balancer/executor/StraightPlanExecutor.java @@ -79,8 +79,7 @@ public CompletionStage run(Admin admin, ClusterInfo logAllocation, Duratio .collect( Collectors.groupingBy( Replica::topicPartition, - Collectors.mapping( - r -> r.nodeInfo().id(), Collectors.toList())))) + Collectors.mapping(r -> r.broker().id(), Collectors.toList())))) .thenApply(ignored -> replicas)) // step 2: wait replicas get reassigned .thenCompose( diff --git a/common/src/main/java/org/astraea/common/balancer/tweakers/ShuffleTweaker.java b/common/src/main/java/org/astraea/common/balancer/tweakers/ShuffleTweaker.java index b75bddfafd..5584294b6f 100644 --- a/common/src/main/java/org/astraea/common/balancer/tweakers/ShuffleTweaker.java +++ b/common/src/main/java/org/astraea/common/balancer/tweakers/ShuffleTweaker.java @@ -65,13 +65,13 @@ public static Builder builder() { public Stream generate(ClusterInfo baseAllocation) { // There is no broker - if (baseAllocation.nodes().isEmpty()) return Stream.of(); + if (baseAllocation.brokers().isEmpty()) return Stream.of(); // No replica to working on. if (baseAllocation.replicas().size() == 0) return Stream.of(); // Only one broker & one folder exists, unable to do any meaningful log migration - if (baseAllocation.nodes().size() == 1 + if (baseAllocation.brokers().size() == 1 && baseAllocation.brokerFolders().values().stream().findFirst().orElseThrow().size() == 1) return Stream.of(); @@ -80,7 +80,7 @@ public Stream generate(ClusterInfo baseAllocation) { .filter(tp -> this.allowedTopics.test(tp.topic())) .filter(tp -> eligiblePartition(baseAllocation.replicas(tp))) .flatMap(baseAllocation::replicaStream) - .filter(r -> this.allowedBrokers.test(r.nodeInfo().id())) + .filter(r -> this.allowedBrokers.test(r.broker().id())) .toList(); return Stream.generate( @@ -108,7 +108,7 @@ public Stream generate(ClusterInfo baseAllocation) { // leader pair follower, follower pair leader .filter(r -> r.isFollower() != sourceReplica.isFollower()) // this follower is located at allowed broker - .filter(r -> this.allowedBrokers.test(r.nodeInfo().id())) + .filter(r -> this.allowedBrokers.test(r.broker().id())) // not forbidden .filter(r -> !forbiddenReplica.contains(r.topicPartitionReplica())) .map(r -> Map.entry(r, ThreadLocalRandom.current().nextInt())) @@ -136,7 +136,7 @@ public Stream generate(ClusterInfo baseAllocation) { baseAllocation.brokers().stream() // the candidate should not be part of the replica list .filter( - b -> replicaList.stream().noneMatch(r -> r.nodeInfo().id() == b.id())) + b -> replicaList.stream().noneMatch(r -> r.broker().id() == b.id())) // should be an allowed broker .filter(b -> this.allowedBrokers.test(b.id())) .map(b -> Map.entry(b, ThreadLocalRandom.current().nextInt())) diff --git a/common/src/main/java/org/astraea/common/cost/BrokerDiskSpaceCost.java b/common/src/main/java/org/astraea/common/cost/BrokerDiskSpaceCost.java index c9925fa301..db06eb3101 100644 --- a/common/src/main/java/org/astraea/common/cost/BrokerDiskSpaceCost.java +++ b/common/src/main/java/org/astraea/common/cost/BrokerDiskSpaceCost.java @@ -22,8 +22,8 @@ import java.util.stream.Stream; import org.astraea.common.Configuration; import org.astraea.common.DataSize; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.metrics.ClusterBean; @@ -73,8 +73,8 @@ private Map brokerMoveCostLimit(Configuration configuration) static boolean brokerDiskUsageSizeOverflow( ClusterInfo before, ClusterInfo after, Map brokerMoveCostLimit) { for (var id : - Stream.concat(before.nodes().stream(), after.nodes().stream()) - .map(NodeInfo::id) + Stream.concat(before.brokers().stream(), after.brokers().stream()) + .map(Broker::id) .parallel() .collect(Collectors.toSet())) { diff --git a/common/src/main/java/org/astraea/common/cost/CostUtils.java b/common/src/main/java/org/astraea/common/cost/CostUtils.java index b23985d657..32506b9782 100644 --- a/common/src/main/java/org/astraea/common/cost/CostUtils.java +++ b/common/src/main/java/org/astraea/common/cost/CostUtils.java @@ -19,8 +19,8 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; final class CostUtils { @@ -32,8 +32,8 @@ static boolean changedRecordSizeOverflow( var totalRemovedSize = 0L; var totalAddedSize = 0L; for (var id : - Stream.concat(before.nodes().stream(), after.nodes().stream()) - .map(NodeInfo::id) + Stream.concat(before.brokers().stream(), after.brokers().stream()) + .map(Broker::id) .parallel() .collect(Collectors.toSet())) { var removed = diff --git a/common/src/main/java/org/astraea/common/cost/MigrationCost.java b/common/src/main/java/org/astraea/common/cost/MigrationCost.java index f580d6ee71..ef673b40be 100644 --- a/common/src/main/java/org/astraea/common/cost/MigrationCost.java +++ b/common/src/main/java/org/astraea/common/cost/MigrationCost.java @@ -22,8 +22,8 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; public class MigrationCost { @@ -107,19 +107,19 @@ private static Map migratedChanged( }) .collect( Collectors.groupingBy( - r -> r.nodeInfo().id(), + r -> r.broker().id(), Collectors.mapping( Function.identity(), Collectors.summingLong(replicaFunction::apply)))); - return Stream.concat(dest.nodes().stream(), source.nodes().stream()) - .map(NodeInfo::id) + return Stream.concat(dest.brokers().stream(), source.brokers().stream()) + .map(Broker::id) .distinct() .parallel() .collect(Collectors.toMap(Function.identity(), n -> cost.getOrDefault(n, 0L))); } private static Map changedReplicaNumber(ClusterInfo before, ClusterInfo after) { - return Stream.concat(before.nodes().stream(), after.nodes().stream()) - .map(NodeInfo::id) + return Stream.concat(before.brokers().stream(), after.brokers().stream()) + .map(Broker::id) .distinct() .parallel() .collect( diff --git a/common/src/main/java/org/astraea/common/cost/NetworkCost.java b/common/src/main/java/org/astraea/common/cost/NetworkCost.java index bc12ae8e99..1619b954ee 100644 --- a/common/src/main/java/org/astraea/common/cost/NetworkCost.java +++ b/common/src/main/java/org/astraea/common/cost/NetworkCost.java @@ -28,9 +28,9 @@ import org.astraea.common.Configuration; import org.astraea.common.DataRate; import org.astraea.common.EnumInfo; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.BrokerTopic; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.cost.utils.ClusterInfoSensor; @@ -114,7 +114,7 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean) clusterInfo.brokers().stream() .collect( Collectors.toMap( - NodeInfo::id, + Broker::id, broker -> clusterInfo .replicaStream(broker.id()) @@ -128,7 +128,7 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean) clusterInfo.brokers().stream() .collect( Collectors.toMap( - NodeInfo::id, + Broker::id, broker -> clusterInfo .replicaStream(broker.id()) @@ -154,7 +154,7 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean) }) .sum())); // add the brokers having no replicas into map - clusterInfo.nodes().stream() + clusterInfo.brokers().stream() .filter(node -> !brokerIngressRate.containsKey(node.id())) .forEach( node -> { @@ -210,7 +210,7 @@ private Map> mapLeaderAllocation(ClusterInfo clusterI .replicaStream() .filter(Replica::isOnline) .filter(Replica::isLeader) - .map(r -> Map.entry(BrokerTopic.of(r.nodeInfo().id(), r.topic()), r)) + .map(r -> Map.entry(BrokerTopic.of(r.broker().id(), r.topic()), r)) .collect( Collectors.groupingBy( Map.Entry::getKey, diff --git a/common/src/main/java/org/astraea/common/cost/NetworkIngressCost.java b/common/src/main/java/org/astraea/common/cost/NetworkIngressCost.java index 02d54cff92..aa4f99055f 100644 --- a/common/src/main/java/org/astraea/common/cost/NetworkIngressCost.java +++ b/common/src/main/java/org/astraea/common/cost/NetworkIngressCost.java @@ -60,7 +60,7 @@ public PartitionCost partitionCost(ClusterInfo clusterInfo, ClusterBean clusterB .filter(Replica::isOnline) .collect( Collectors.groupingBy( - replica -> replica.nodeInfo().id(), + replica -> replica.broker().id(), Collectors.toMap( Replica::topicPartition, r -> partitionTraffic.get(r.topicPartition())))); diff --git a/common/src/main/java/org/astraea/common/cost/NodeMetricsCost.java b/common/src/main/java/org/astraea/common/cost/NodeMetricsCost.java index 3fc5431e81..f6acad9bef 100644 --- a/common/src/main/java/org/astraea/common/cost/NodeMetricsCost.java +++ b/common/src/main/java/org/astraea/common/cost/NodeMetricsCost.java @@ -21,8 +21,8 @@ import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.metrics.ClusterBean; import org.astraea.common.metrics.client.HasNodeMetrics; import org.astraea.common.metrics.client.producer.ProducerMetrics; @@ -59,8 +59,8 @@ public BrokerCost brokerCost(ClusterInfo clusterInfo, ClusterBean clusterBean) { .max() .ifPresent( max -> - clusterInfo.nodes().stream() - .map(NodeInfo::id) + clusterInfo.brokers().stream() + .map(Broker::id) .filter(id -> !result.containsKey(id)) .forEach(id -> result.put(id, max))); return () -> result; diff --git a/common/src/main/java/org/astraea/common/cost/RecordSizeCost.java b/common/src/main/java/org/astraea/common/cost/RecordSizeCost.java index 6b1b4c6cab..6a1085f792 100644 --- a/common/src/main/java/org/astraea/common/cost/RecordSizeCost.java +++ b/common/src/main/java/org/astraea/common/cost/RecordSizeCost.java @@ -22,8 +22,8 @@ import java.util.stream.Collectors; import org.astraea.common.Configuration; import org.astraea.common.DataSize; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.metrics.ClusterBean; @@ -43,10 +43,10 @@ public RecordSizeCost(Configuration config) { @Override public BrokerCost brokerCost(ClusterInfo clusterInfo, ClusterBean clusterBean) { var result = - clusterInfo.nodes().stream() + clusterInfo.brokers().stream() .collect( Collectors.toMap( - NodeInfo::id, + Broker::id, n -> clusterInfo.replicaStream(n.id()).mapToDouble(Replica::size).sum())); return () -> result; } diff --git a/common/src/main/java/org/astraea/common/cost/ReplicaLeaderCost.java b/common/src/main/java/org/astraea/common/cost/ReplicaLeaderCost.java index 7d4f4346e6..0ea33d71d2 100644 --- a/common/src/main/java/org/astraea/common/cost/ReplicaLeaderCost.java +++ b/common/src/main/java/org/astraea/common/cost/ReplicaLeaderCost.java @@ -62,7 +62,7 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean) } static Map leaderCount(ClusterInfo clusterInfo) { - return clusterInfo.nodes().stream() + return clusterInfo.brokers().stream() .map(nodeInfo -> Map.entry(nodeInfo.id(), clusterInfo.replicaLeaders(nodeInfo.id()).size())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } diff --git a/common/src/main/java/org/astraea/common/cost/ReplicaLeaderSizeCost.java b/common/src/main/java/org/astraea/common/cost/ReplicaLeaderSizeCost.java index 6d9c72451f..e722dc141d 100644 --- a/common/src/main/java/org/astraea/common/cost/ReplicaLeaderSizeCost.java +++ b/common/src/main/java/org/astraea/common/cost/ReplicaLeaderSizeCost.java @@ -67,7 +67,7 @@ public BrokerCost brokerCost(ClusterInfo clusterInfo, ClusterBean clusterBean) { clusterInfo.replicas().stream() .collect( Collectors.groupingBy( - r -> r.nodeInfo().id(), + r -> r.broker().id(), Collectors.mapping( r -> clusterInfo diff --git a/common/src/main/java/org/astraea/common/cost/ReplicaNumberCost.java b/common/src/main/java/org/astraea/common/cost/ReplicaNumberCost.java index 7307c7e998..0f84bbe705 100644 --- a/common/src/main/java/org/astraea/common/cost/ReplicaNumberCost.java +++ b/common/src/main/java/org/astraea/common/cost/ReplicaNumberCost.java @@ -21,8 +21,8 @@ import java.util.Map; import java.util.stream.Collectors; import org.astraea.common.Configuration; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.metrics.ClusterBean; /** more replicas migrate -> higher cost */ @@ -59,12 +59,12 @@ public ClusterCost clusterCost(ClusterInfo clusterInfo, ClusterBean clusterBean) var replicaPerBroker = clusterInfo .replicaStream() - .collect(Collectors.groupingBy(r -> r.nodeInfo().id(), Collectors.counting())); + .collect(Collectors.groupingBy(r -> r.broker().id(), Collectors.counting())); var summary = replicaPerBroker.values().stream().mapToLong(x -> x).summaryStatistics(); var anyBrokerEmpty = clusterInfo.brokers().stream() - .map(NodeInfo::id) + .map(Broker::id) .anyMatch(alive -> !replicaPerBroker.containsKey(alive)); var max = summary.getMax(); var min = anyBrokerEmpty ? 0 : summary.getMin(); diff --git a/common/src/main/java/org/astraea/common/cost/utils/ClusterInfoSensor.java b/common/src/main/java/org/astraea/common/cost/utils/ClusterInfoSensor.java index ac2db0446e..df3f62b795 100644 --- a/common/src/main/java/org/astraea/common/cost/utils/ClusterInfoSensor.java +++ b/common/src/main/java/org/astraea/common/cost/utils/ClusterInfoSensor.java @@ -21,8 +21,8 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.metrics.ClusterBean; import org.astraea.common.metrics.HasBeanObject; @@ -58,8 +58,8 @@ public static ClusterInfo metricViewCluster(ClusterBean clusterBean) { var nodes = clusterBean.brokerIds().stream() .filter(id -> id != -1) - .map(id -> NodeInfo.of(id, "", -1)) - .collect(Collectors.toUnmodifiableMap(NodeInfo::id, x -> x)); + .map(id -> Broker.of(id, "", -1)) + .collect(Collectors.toUnmodifiableMap(Broker::id, x -> x)); var replicaMap = clusterBean.brokerIds().stream() .collect( @@ -108,7 +108,7 @@ public static ClusterInfo metricViewCluster(ClusterBean clusterBean) { Replica.builder() .topic(tp.topic()) .partition(tp.partition()) - .nodeInfo(nodes.get(broker)) + .broker(nodes.get(broker)) .path("") .size(size.value()); var isLeader = m.value() != 0; diff --git a/common/src/main/java/org/astraea/common/metrics/MetricSeriesBuilder.java b/common/src/main/java/org/astraea/common/metrics/MetricSeriesBuilder.java index f4c00cdedc..206b7c40e7 100644 --- a/common/src/main/java/org/astraea/common/metrics/MetricSeriesBuilder.java +++ b/common/src/main/java/org/astraea/common/metrics/MetricSeriesBuilder.java @@ -27,8 +27,8 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.function.Bi3Function; @@ -109,7 +109,7 @@ public MetricSeriesBuilder seriesByBroker( start, (t) -> t.isBefore(end) || t.isEqual(end), (t) -> t.plus(interval)) .flatMap( time -> - cluster.nodes().stream() + cluster.brokers().stream() .map( node -> Map.entry(node.id(), seriesGenerator.apply(time, node.id())))) @@ -128,10 +128,10 @@ public MetricSeriesBuilder seriesByBrokerTopic( final var interval = sampleInterval; this.series.add( () -> - cluster.nodes().stream() + cluster.brokers().stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, + Broker::id, node -> Stream.iterate( start, @@ -159,10 +159,10 @@ public MetricSeriesBuilder seriesByBrokerPartition( final var interval = sampleInterval; this.series.add( () -> - cluster.nodes().stream() + cluster.brokers().stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, + Broker::id, node -> Stream.iterate( start, @@ -189,10 +189,10 @@ public MetricSeriesBuilder seriesByBrokerReplica( final var interval = sampleInterval; this.series.add( () -> - cluster.nodes().stream() + cluster.brokers().stream() .collect( Collectors.toUnmodifiableMap( - NodeInfo::id, + Broker::id, node -> Stream.iterate( start, diff --git a/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java b/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java index c0f0680a15..b93ebb3b3d 100644 --- a/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java +++ b/common/src/main/java/org/astraea/common/partitioner/RoundRobinKeeper.java @@ -22,8 +22,8 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.function.Supplier; import java.util.stream.Collectors; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; public class RoundRobinKeeper { private final AtomicInteger next = new AtomicInteger(0); @@ -46,7 +46,7 @@ void tryToUpdate(ClusterInfo clusterInfo, Supplier> costToS == now) { var roundRobin = RoundRobin.smooth(costToScore.get()); var ids = - clusterInfo.nodes().stream().map(NodeInfo::id).collect(Collectors.toUnmodifiableSet()); + clusterInfo.brokers().stream().map(Broker::id).collect(Collectors.toUnmodifiableSet()); // TODO: make ROUND_ROBIN_LENGTH configurable ??? for (var index = 0; index < this.roundRobin.length; ++index) this.roundRobin[index] = roundRobin.next(ids).orElse(-1); diff --git a/common/src/test/java/org/astraea/common/ByteUtilsTest.java b/common/src/test/java/org/astraea/common/ByteUtilsTest.java index 7cdef95b3c..616229e122 100644 --- a/common/src/test/java/org/astraea/common/ByteUtilsTest.java +++ b/common/src/test/java/org/astraea/common/ByteUtilsTest.java @@ -17,11 +17,6 @@ package org.astraea.common; import java.nio.charset.StandardCharsets; -import java.time.Duration; -import java.util.Set; -import org.astraea.common.admin.Admin; -import org.astraea.common.admin.ClusterInfo; -import org.astraea.it.Service; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -73,45 +68,4 @@ void testBoolean2Bytes() { Assertions.assertArrayEquals(new byte[] {1}, ByteUtils.toBytes(true)); Assertions.assertArrayEquals(new byte[] {0}, ByteUtils.toBytes(false)); } - - @Test - void testReadAndToBytesClusterInfo() { - var topic = Utils.randomString(); - try (var service = Service.builder().numberOfBrokers(3).build()) { - try (var admin = Admin.of(service.bootstrapServers())) { - admin - .creator() - .topic(topic) - .numberOfPartitions(1) - .numberOfReplicas((short) 3) - .run() - .toCompletableFuture() - .join(); - Utils.sleep(Duration.ofSeconds(1)); - var clusterInfo = admin.clusterInfo(Set.of(topic)).toCompletableFuture().join(); - - Assertions.assertDoesNotThrow(() -> ByteUtils.toBytes(clusterInfo)); - var bytes = ByteUtils.toBytes(clusterInfo); - Assertions.assertDoesNotThrow(() -> ByteUtils.readClusterInfo(bytes)); - var deserializedClusterInfo = ByteUtils.readClusterInfo(bytes); - - Assertions.assertEquals(clusterInfo.clusterId(), deserializedClusterInfo.clusterId()); - Assertions.assertTrue(clusterInfo.nodes().containsAll(deserializedClusterInfo.nodes())); - Assertions.assertEquals(clusterInfo.topics(), deserializedClusterInfo.topics()); - Assertions.assertEquals(clusterInfo.replicas(), deserializedClusterInfo.replicas()); - } - } - } - - @Test - void testReadAndToBytesEmptyClusterInfo() { - var clusterInfo = ClusterInfo.empty(); - var serializedInfo = ByteUtils.toBytes(clusterInfo); - var deserializedClusterInfo = ByteUtils.readClusterInfo(serializedInfo); - - Assertions.assertEquals(clusterInfo.clusterId(), deserializedClusterInfo.clusterId()); - Assertions.assertEquals(clusterInfo.nodes(), deserializedClusterInfo.nodes()); - Assertions.assertEquals(clusterInfo.topics(), deserializedClusterInfo.topics()); - Assertions.assertEquals(clusterInfo.replicas(), deserializedClusterInfo.replicas()); - } } diff --git a/common/src/test/java/org/astraea/common/admin/AdminTest.java b/common/src/test/java/org/astraea/common/admin/AdminTest.java index 42465de7a5..a0675e8184 100644 --- a/common/src/test/java/org/astraea/common/admin/AdminTest.java +++ b/common/src/test/java/org/astraea/common/admin/AdminTest.java @@ -24,6 +24,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.SortedSet; @@ -345,7 +346,7 @@ void testOrder() { .toCompletableFuture() .join()); - Assertions.assertInstanceOf(List.class, admin.nodeInfos().toCompletableFuture().join()); + Assertions.assertInstanceOf(List.class, admin.brokers().toCompletableFuture().join()); var brokers = admin.brokers().toCompletableFuture().join(); Assertions.assertEquals( @@ -358,7 +359,7 @@ void testOrder() { .sorted( Comparator.comparing(Replica::topic) .thenComparing(Replica::partition) - .thenComparing(r -> r.nodeInfo().id())) + .thenComparing(r -> r.broker().id())) .collect(Collectors.toList()), replicas); } @@ -378,10 +379,10 @@ void testMoveLeaderBroker() { var ids = List.of( SERVICE.dataFolders().keySet().stream() - .filter(i -> i != partition.leader().get().id()) + .filter(i -> !Objects.equals(i, partition.leaderId().get())) .findFirst() .get(), - partition.leader().get().id()); + partition.leaderId().get()); admin.moveToBrokers(Map.of(TopicPartition.of(topic, 0), ids)).toCompletableFuture().join(); Utils.sleep(Duration.ofSeconds(2)); @@ -390,7 +391,7 @@ void testMoveLeaderBroker() { var newPartition = newPartitions.get(0); Assertions.assertEquals(ids.size(), newPartition.replicas().size()); Assertions.assertEquals(ids.size(), newPartition.isr().size()); - Assertions.assertNotEquals(ids.get(0), newPartition.leader().get().id()); + Assertions.assertNotEquals(ids.get(0), newPartition.leaderId().get()); admin .preferredLeaderElection( @@ -400,7 +401,7 @@ void testMoveLeaderBroker() { Utils.sleep(Duration.ofSeconds(2)); Assertions.assertEquals( ids.get(0), - admin.partitions(Set.of(topic)).toCompletableFuture().join().get(0).leader().get().id()); + admin.partitions(Set.of(topic)).toCompletableFuture().join().get(0).leaderId().get()); } } @@ -416,7 +417,7 @@ void testMoveToAnotherFolder() { var replica = replicas.get(0); var idAndFolder = SERVICE.dataFolders().entrySet().stream() - .filter(e -> e.getKey() == replica.nodeInfo().id()) + .filter(e -> e.getKey() == replica.broker().id()) .map(e -> Map.of(e.getKey(), e.getValue().iterator().next())) .findFirst() .get(); @@ -433,7 +434,7 @@ void testMoveToAnotherFolder() { Assertions.assertEquals(1, newReplicas.size()); var newReplica = newReplicas.get(0); - Assertions.assertEquals(idAndFolder.get(newReplica.nodeInfo().id()), newReplica.path()); + Assertions.assertEquals(idAndFolder.get(newReplica.broker().id()), newReplica.path()); } } @@ -589,7 +590,7 @@ void testDeclarePreferredFoldersWithNoCrossBrokerMovement() { r -> Replica.builder(r) .path( - source.brokerFolders().get(r.nodeInfo().id()).stream() + source.brokerFolders().get(r.broker().id()).stream() .filter(p -> !p.equals(r.path())) .findAny() .orElseThrow()) @@ -719,7 +720,7 @@ void testMigrateToOtherFolders() { .replicaStream() .iterator() .next() - .nodeInfo() + .broker() .id(); var paths = new ArrayList<>(SERVICE.dataFolders().get(id)); @@ -854,12 +855,12 @@ void testCreateTopicWithReplicasAssignment() { Assertions.assertTrue(config.raw().containsValue("lz4")); Assertions.assertEquals( List.of(0, 2), - partitions.get(0).replicas().stream().map(NodeInfo::id).collect(Collectors.toList())); + partitions.get(0).replicas().stream().map(Broker::id).collect(Collectors.toList())); Assertions.assertEquals( List.of(2, 1), - partitions.get(1).replicas().stream().map(NodeInfo::id).collect(Collectors.toList())); - Assertions.assertEquals(0, partitions.get(0).leader().get().id()); - Assertions.assertEquals(2, partitions.get(1).leader().get().id()); + partitions.get(1).replicas().stream().map(Broker::id).collect(Collectors.toList())); + Assertions.assertEquals(0, partitions.get(0).leaderId().get()); + Assertions.assertEquals(2, partitions.get(1).leaderId().get()); } } @@ -999,7 +1000,7 @@ void testConsumerGroups() { .forEach( c -> { Assertions.assertNotNull(c.groupId()); - Assertions.assertNotNull(c.coordinator()); + Assertions.assertNotNull(c.coordinatorId()); Assertions.assertNotNull(c.assignor()); Assertions.assertNotNull(c.state()); }); @@ -1047,7 +1048,7 @@ void testMigrateSinglePartition() { var partitionReplicas = admin.clusterInfo(Set.of(topic)).toCompletableFuture().join().replicas(); return partitionReplicas.size() == 1 - && partitionReplicas.get(0).nodeInfo().id() == broker; + && partitionReplicas.get(0).broker().id() == broker; }); var currentBroker = @@ -1059,7 +1060,7 @@ void testMigrateSinglePartition() { .filter(replica -> replica.partition() == 0) .findFirst() .get() - .nodeInfo() + .broker() .id(); var allPath = admin.brokerFolders().toCompletableFuture().join(); var otherPath = @@ -1114,7 +1115,7 @@ void testIllegalMigrationArgument() { .findFirst() .get(); - var currentBroker = currentReplica.nodeInfo().id(); + var currentBroker = currentReplica.broker().id(); var notExistReplica = (currentBroker + 1) % SERVICE.dataFolders().keySet().size(); var nextDir = SERVICE.dataFolders().get(notExistReplica).iterator().next(); @@ -1144,7 +1145,7 @@ void testMigrateAllPartitions() { Utils.waitFor( () -> { var replicas = admin.clusterInfo(Set.of(topic)).toCompletableFuture().join().replicas(); - return replicas.stream().allMatch(r -> r.nodeInfo().id() == broker); + return replicas.stream().allMatch(r -> r.broker().id() == broker); }); } } @@ -1388,7 +1389,7 @@ void testReplicasPreferredLeaderFlag() { Collectors.groupingBy( replica -> TopicPartition.of(replica.topic(), replica.partition()), Collectors.mapping( - replica -> replica.nodeInfo().id(), Collectors.toList()))); + replica -> replica.broker().id(), Collectors.toList()))); IntStream.range(0, partitionCount) .forEach(p -> admin.moveToBrokers(Map.of(TopicPartition.of(topic, p), List.of(0, 1, 2)))); diff --git a/common/src/test/java/org/astraea/common/admin/AdminWithOfflineBrokerTest.java b/common/src/test/java/org/astraea/common/admin/AdminWithOfflineBrokerTest.java index 0b9daa3af5..43a9ebfc3a 100644 --- a/common/src/test/java/org/astraea/common/admin/AdminWithOfflineBrokerTest.java +++ b/common/src/test/java/org/astraea/common/admin/AdminWithOfflineBrokerTest.java @@ -80,7 +80,7 @@ static void closeOneBroker() { var allPs = admin.partitions(Set.of(TOPIC_NAME)).toCompletableFuture().join(); NUMBER_OF_ONLINE_PARTITIONS = PARTITIONS - - (int) allPs.stream().filter(p -> p.leader().get().id() == CLOSED_BROKER_ID).count(); + - (int) allPs.stream().filter(p -> p.leaderId().get() == CLOSED_BROKER_ID).count(); Assertions.assertEquals(PARTITIONS, allPs.size()); Utils.sleep(Duration.ofSeconds(2)); } @@ -106,18 +106,6 @@ void testProducerStates() { } } - @Timeout(10) - @Test - void testNodeInfos() { - try (var admin = Admin.of(SERVICE.bootstrapServers())) { - var nodeInfos = admin.nodeInfos().toCompletableFuture().join(); - Assertions.assertEquals(2, nodeInfos.size()); - var offlineNodeInfos = - nodeInfos.stream().filter(NodeInfo::offline).collect(Collectors.toList()); - Assertions.assertEquals(0, offlineNodeInfos.size()); - } - } - @Timeout(10) @Test void testBrokers() { @@ -128,7 +116,7 @@ void testBrokers() { b -> b.dataFolders() .forEach(d -> Assertions.assertEquals(0, d.orphanPartitionSizes().size()))); - var offlineBrokers = brokers.stream().filter(NodeInfo::offline).collect(Collectors.toList()); + var offlineBrokers = brokers.stream().filter(Broker::offline).collect(Collectors.toList()); Assertions.assertEquals(0, offlineBrokers.size()); } } @@ -140,7 +128,7 @@ void testPartitions() { var partitions = admin.partitions(Set.of(TOPIC_NAME)).toCompletableFuture().join(); Assertions.assertEquals(PARTITIONS, partitions.size()); var offlinePartitions = - partitions.stream().filter(p -> p.leader().isEmpty()).collect(Collectors.toList()); + partitions.stream().filter(p -> p.leaderId().isEmpty()).collect(Collectors.toList()); offlinePartitions.forEach( p -> { Assertions.assertEquals(1, p.replicas().size()); @@ -165,7 +153,7 @@ void testReplicas() { var offlineReplicas = replicas.stream().filter(Replica::isOffline).collect(Collectors.toList()); Assertions.assertNotEquals(PARTITIONS, offlineReplicas.size()); - offlineReplicas.forEach(r -> Assertions.assertTrue(r.nodeInfo().offline())); + offlineReplicas.forEach(r -> Assertions.assertTrue(r.broker().offline())); offlineReplicas.forEach(r -> Assertions.assertNull(r.path())); offlineReplicas.forEach(r -> Assertions.assertEquals(-1, r.size())); offlineReplicas.forEach(r -> Assertions.assertEquals(-1, r.lag())); diff --git a/common/src/test/java/org/astraea/common/admin/ClusterInfoBuilderTest.java b/common/src/test/java/org/astraea/common/admin/ClusterInfoBuilderTest.java index 0f2e5244d3..8bebbe37a4 100644 --- a/common/src/test/java/org/astraea/common/admin/ClusterInfoBuilderTest.java +++ b/common/src/test/java/org/astraea/common/admin/ClusterInfoBuilderTest.java @@ -31,14 +31,14 @@ class ClusterInfoBuilderTest { @Test void testBuild() { - var host1000 = NodeInfo.of(1000, "host1000", 9092); - var host2000 = NodeInfo.of(2000, "host2000", 9092); - var host3000 = NodeInfo.of(3000, "host3000", 9092); + var host1000 = Broker.of(1000, "host1000", 9092); + var host2000 = Broker.of(2000, "host2000", 9092); + var host3000 = Broker.of(3000, "host3000", 9092); var replica = Replica.builder() .topic("MyTopic") .partition(0) - .nodeInfo(host1000) + .broker(host1000) .size(1024) .isPreferredLeader(true) .isLeader(true) @@ -47,23 +47,23 @@ void testBuild() { ClusterInfo.of("fake", List.of(host1000, host2000, host3000), Map.of(), List.of(replica)); Assertions.assertEquals( - List.of(host1000, host2000, host3000), ClusterInfo.builder(cluster).build().nodes()); + List.of(host1000, host2000, host3000), ClusterInfo.builder(cluster).build().brokers()); Assertions.assertEquals(List.of(replica), ClusterInfo.builder(cluster).build().replicas()); - Assertions.assertEquals(List.of(), ClusterInfo.builder().build().nodes()); + Assertions.assertEquals(List.of(), ClusterInfo.builder().build().brokers()); Assertions.assertEquals(List.of(), ClusterInfo.builder().build().replicas()); } @Test void addNode() { - Assertions.assertEquals(List.of(), ClusterInfo.builder().addNode(Set.of()).build().nodes()); + Assertions.assertEquals(List.of(), ClusterInfo.builder().addNode(Set.of()).build().brokers()); Assertions.assertEquals( Set.of(1, 2, 3, 4, 5, 100), - ClusterInfo.builder().addNode(Set.of(1, 2, 3, 4, 5, 100)).build().nodes().stream() - .map(NodeInfo::id) + ClusterInfo.builder().addNode(Set.of(1, 2, 3, 4, 5, 100)).build().brokers().stream() + .map(Broker::id) .collect(Collectors.toSet())); Assertions.assertEquals( - ClusterInfo.builder().addNode(Set.of(1, 2, 3)).build().nodes(), - ClusterInfo.builder().addNode(Set.of(1, 2, 3)).build().nodes(), + ClusterInfo.builder().addNode(Set.of(1, 2, 3)).build().brokers(), + ClusterInfo.builder().addNode(Set.of(1, 2, 3)).build().brokers(), "The port number is generated by deterministic random"); } @@ -160,7 +160,7 @@ void addTopic() { path -> cluster .replicaStream() - .filter(r -> r.nodeInfo().id() == id) + .filter(r -> r.broker().id() == id) .filter(r -> r.path().equals(path)) .count())); var summary = folderLogs.values().stream().mapToLong(x -> x).summaryStatistics(); @@ -214,7 +214,7 @@ void reassignReplica() { (short) 2, (replica) -> Replica.builder(replica) - .nodeInfo(base.node(replica.isPreferredLeader() ? 1 : 2)) + .broker(base.node(replica.isPreferredLeader() ? 1 : 2)) .path(replica.isPreferredLeader() ? "/ssd1" : "/ssd2") .build()) .build(); @@ -236,22 +236,22 @@ void reassignReplica() { original .replicaStream() .filter(Replica::isPreferredLeader) - .allMatch(r -> r.nodeInfo().id() == 1 && r.path().equals("/ssd1"))); + .allMatch(r -> r.broker().id() == 1 && r.path().equals("/ssd1"))); Assertions.assertTrue( original .replicaStream() .filter(Predicate.not(Replica::isPreferredLeader)) - .allMatch(r -> r.nodeInfo().id() == 2 && r.path().equals("/ssd2"))); + .allMatch(r -> r.broker().id() == 2 && r.path().equals("/ssd2"))); Assertions.assertTrue( altered .replicaStream() .filter(Replica::isPreferredLeader) - .allMatch(r -> r.nodeInfo().id() == 3 && r.path().equals("/ssd3"))); + .allMatch(r -> r.broker().id() == 3 && r.path().equals("/ssd3"))); Assertions.assertTrue( altered .replicaStream() .filter(Predicate.not(Replica::isPreferredLeader)) - .allMatch(r -> r.nodeInfo().id() == 4 && r.path().equals("/ssd4"))); + .allMatch(r -> r.broker().id() == 4 && r.path().equals("/ssd4"))); Assertions.assertThrows( IllegalArgumentException.class, @@ -287,8 +287,8 @@ void setPreferredLeader() { (short) 4, (replica) -> Replica.builder(replica) - .isLeader(replica.nodeInfo().id() == 0) - .isPreferredLeader(replica.nodeInfo().id() == 0) + .isLeader(replica.broker().id() == 0) + .isPreferredLeader(replica.broker().id() == 0) .build()) .build(); var altered = @@ -303,19 +303,19 @@ void setPreferredLeader() { original .replicaStream() .filter((Replica::isPreferredLeader)) - .allMatch(r -> r.nodeInfo().id() == 0)); + .allMatch(r -> r.broker().id() == 0)); Assertions.assertTrue( - original.replicaStream().filter((Replica::isLeader)).allMatch(r -> r.nodeInfo().id() == 0)); + original.replicaStream().filter((Replica::isLeader)).allMatch(r -> r.broker().id() == 0)); Assertions.assertTrue( altered .replicaStream() .filter(Replica::isPreferredLeader) - .allMatch(r -> r.nodeInfo().id() == r.partition())); + .allMatch(r -> r.broker().id() == r.partition())); Assertions.assertTrue( altered .replicaStream() .filter(Replica::isLeader) - .allMatch(r -> r.nodeInfo().id() == r.partition())); + .allMatch(r -> r.broker().id() == r.partition())); Assertions.assertThrows( IllegalArgumentException.class, () -> @@ -332,9 +332,9 @@ void setPreferredLeader() { "300, host3, 3000", }) void testFakeBrokerInteraction(int id, String host, int port) { - var node0 = ClusterInfoBuilder.FakeBroker.of(id, host, port, List.of()); - var node1 = NodeInfo.of(id, host, port); - var node2 = NodeInfo.of(id + 1, host, port); + var node0 = ClusterInfoBuilder.fakeBroker(id, host, port, List.of()); + var node1 = Broker.of(id, host, port); + var node2 = Broker.of(id + 1, host, port); Assertions.assertEquals(node0.hashCode(), node1.hashCode()); Assertions.assertNotEquals(node0.hashCode(), node2.hashCode()); @@ -350,14 +350,14 @@ void testRemoveNodes() { ClusterInfo.builder(base) .removeNodes(x -> Set.of(4, 5, 6, 7, 8, 9).contains(x)) .build() - .nodes() + .brokers() .stream() - .map(NodeInfo::id) + .map(Broker::id) .collect(Collectors.toSet())); Assertions.assertEquals( Set.of(1, 3, 5, 7, 9), - ClusterInfo.builder(base).removeNodes(x -> x % 2 == 0).build().nodes().stream() - .map(NodeInfo::id) + ClusterInfo.builder(base).removeNodes(x -> x % 2 == 0).build().brokers().stream() + .map(Broker::id) .collect(Collectors.toSet())); } } diff --git a/common/src/test/java/org/astraea/common/admin/ClusterInfoTest.java b/common/src/test/java/org/astraea/common/admin/ClusterInfoTest.java index f965fd57ad..126b46754a 100644 --- a/common/src/test/java/org/astraea/common/admin/ClusterInfoTest.java +++ b/common/src/test/java/org/astraea/common/admin/ClusterInfoTest.java @@ -48,8 +48,8 @@ public static ClusterInfo of(List replicas) { return ClusterInfo.of( "fake", replicas.stream() - .map(Replica::nodeInfo) - .collect(Collectors.groupingBy(NodeInfo::id, Collectors.reducing((x, y) -> x))) + .map(Replica::broker) + .collect(Collectors.groupingBy(Broker::id, Collectors.reducing((x, y) -> x))) .values() .stream() .flatMap(Optional::stream) @@ -61,7 +61,7 @@ public static ClusterInfo of(List replicas) { @Test void testEmptyCluster() { var emptyCluster = ClusterInfo.empty(); - Assertions.assertEquals(0, emptyCluster.nodes().size()); + Assertions.assertEquals(0, emptyCluster.brokers().size()); Assertions.assertEquals(0, emptyCluster.replicaStream().count()); } @@ -100,7 +100,7 @@ void testReturnCollectionUnmodifiable() { Replica.builder() .topic("topic") .partition(0) - .nodeInfo(NodeInfo.of(0, "", -1)) + .broker(Broker.of(0, "", -1)) .path("f") .buildLeader(); Assertions.assertThrows(Exception.class, () -> cluster.replicas().add(replica)); diff --git a/common/src/test/java/org/astraea/common/admin/ClusterInfoWithOfflineNodeTest.java b/common/src/test/java/org/astraea/common/admin/ClusterInfoWithOfflineNodeTest.java index 4eed309d31..e5b251fd95 100644 --- a/common/src/test/java/org/astraea/common/admin/ClusterInfoWithOfflineNodeTest.java +++ b/common/src/test/java/org/astraea/common/admin/ClusterInfoWithOfflineNodeTest.java @@ -77,18 +77,17 @@ void testClusterInfoWithOfflineNode() { "One of the rest replicas should take over the leadership"); Assertions.assertTrue( after.availableReplicas(topicName).stream() - .allMatch(x -> x.nodeInfo().id() != brokerToClose)); + .allMatch(x -> x.broker().id() != brokerToClose)); Assertions.assertTrue( - after.replicaLeaders(topicName).stream() - .allMatch(x -> x.nodeInfo().id() != brokerToClose)); + after.replicaLeaders(topicName).stream().allMatch(x -> x.broker().id() != brokerToClose)); Assertions.assertTrue( after.replicas(topicName).stream() .filter(Replica::isOffline) - .allMatch(x -> x.nodeInfo().id() == brokerToClose)); + .allMatch(x -> x.broker().id() == brokerToClose)); Assertions.assertTrue( after.replicas(topicName).stream() .filter(x -> !x.isOffline()) - .allMatch(x -> x.nodeInfo().id() != brokerToClose)); + .allMatch(x -> x.broker().id() != brokerToClose)); } } } diff --git a/common/src/test/java/org/astraea/common/admin/NodeInfoTest.java b/common/src/test/java/org/astraea/common/admin/NodeInfoTest.java deleted file mode 100644 index 5c3d24d9fe..0000000000 --- a/common/src/test/java/org/astraea/common/admin/NodeInfoTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.astraea.common.admin; - -import org.apache.kafka.common.Node; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class NodeInfoTest { - - static Node node() { - return new Node(10, "host", 100); - } - - @Test - void testAllGetters() { - var kafkaNode = node(); - var node = NodeInfo.of(kafkaNode); - - Assertions.assertEquals(kafkaNode.host(), node.host()); - Assertions.assertEquals(kafkaNode.id(), node.id()); - Assertions.assertEquals(kafkaNode.port(), node.port()); - } -} diff --git a/common/src/test/java/org/astraea/common/admin/SomePartitionOfflineTest.java b/common/src/test/java/org/astraea/common/admin/SomePartitionOfflineTest.java index 812d78c3cb..624379db1e 100644 --- a/common/src/test/java/org/astraea/common/admin/SomePartitionOfflineTest.java +++ b/common/src/test/java/org/astraea/common/admin/SomePartitionOfflineTest.java @@ -63,7 +63,7 @@ void somePartitionsOffline() { .toCompletableFuture() .join() .replicaStream() - .filter(replica -> replica.nodeInfo().id() == 0) + .filter(replica -> replica.broker().id() == 0) .collect( Collectors.groupingBy( replica -> TopicPartition.of(replica.topic(), replica.partition()))); @@ -78,7 +78,7 @@ void somePartitionsOffline() { .toCompletableFuture() .join() .replicaStream() - .filter(replica -> replica.nodeInfo().id() == 0) + .filter(replica -> replica.broker().id() == 0) .collect( Collectors.groupingBy( replica -> TopicPartition.of(replica.topic(), replica.partition()))); diff --git a/common/src/test/java/org/astraea/common/balancer/BalancerConfigTestSuite.java b/common/src/test/java/org/astraea/common/balancer/BalancerConfigTestSuite.java index e3c2e66b35..22c0e0a608 100644 --- a/common/src/test/java/org/astraea/common/balancer/BalancerConfigTestSuite.java +++ b/common/src/test/java/org/astraea/common/balancer/BalancerConfigTestSuite.java @@ -220,15 +220,12 @@ public void testBalancingModeDemoted() { .build()); Assertions.assertTrue(plan.isPresent(), testName); var finalCluster = plan.get().proposal(); - Assertions.assertTrue(cluster.replicas().stream().anyMatch(x -> x.nodeInfo().id() == 0)); - Assertions.assertTrue(cluster.replicas().stream().anyMatch(x -> x.nodeInfo().id() == 1)); - Assertions.assertTrue(cluster.replicas().stream().anyMatch(x -> x.nodeInfo().id() == 2)); - Assertions.assertTrue( - finalCluster.replicas().stream().noneMatch(x -> x.nodeInfo().id() == 0)); - Assertions.assertTrue( - finalCluster.replicas().stream().noneMatch(x -> x.nodeInfo().id() == 1)); - Assertions.assertTrue( - finalCluster.replicas().stream().noneMatch(x -> x.nodeInfo().id() == 2)); + Assertions.assertTrue(cluster.replicas().stream().anyMatch(x -> x.broker().id() == 0)); + Assertions.assertTrue(cluster.replicas().stream().anyMatch(x -> x.broker().id() == 1)); + Assertions.assertTrue(cluster.replicas().stream().anyMatch(x -> x.broker().id() == 2)); + Assertions.assertTrue(finalCluster.replicas().stream().noneMatch(x -> x.broker().id() == 0)); + Assertions.assertTrue(finalCluster.replicas().stream().noneMatch(x -> x.broker().id() == 1)); + Assertions.assertTrue(finalCluster.replicas().stream().noneMatch(x -> x.broker().id() == 2)); AssertionsHelper.assertBrokerEmpty( finalCluster, (x) -> Set.of(0, 1, 2).contains(x), testName); } @@ -293,16 +290,13 @@ public void testBalancingModeDemoted() { var node123 = Stream.of(1, 2, 3).map(base::node).iterator(); var testCluster = ClusterInfo.builder(base) - .addTopic("OK", 1, (short) 1, r -> Replica.builder(r).nodeInfo(base.node(1)).build()) + .addTopic("OK", 1, (short) 1, r -> Replica.builder(r).broker(base.node(1)).build()) .addTopic( - "OK_SKIP", 2, (short) 1, r -> Replica.builder(r).nodeInfo(node12.next()).build()) + "OK_SKIP", 2, (short) 1, r -> Replica.builder(r).broker(node12.next()).build()) .addTopic( - "Replica", 1, (short) 2, r -> Replica.builder(r).nodeInfo(node13.next()).build()) + "Replica", 1, (short) 2, r -> Replica.builder(r).broker(node13.next()).build()) .addTopic( - "Partition", - 3, - (short) 1, - r -> Replica.builder(r).nodeInfo(node123.next()).build()) + "Partition", 3, (short) 1, r -> Replica.builder(r).broker(node123.next()).build()) .build(); Assertions.assertDoesNotThrow( @@ -431,15 +425,15 @@ public void testBalancingModeDemoted() { var testName = "[if replica on clear broker is adding/removing/future, raise an exception]"; var adding = ClusterInfo.builder(cluster) - .mapLog(r -> r.nodeInfo().id() != 0 ? r : Replica.builder(r).isAdding(true).build()) + .mapLog(r -> r.broker().id() != 0 ? r : Replica.builder(r).isAdding(true).build()) .build(); var removing = ClusterInfo.builder(cluster) - .mapLog(r -> r.nodeInfo().id() != 0 ? r : Replica.builder(r).isRemoving(true).build()) + .mapLog(r -> r.broker().id() != 0 ? r : Replica.builder(r).isRemoving(true).build()) .build(); var future = ClusterInfo.builder(cluster) - .mapLog(r -> r.nodeInfo().id() != 0 ? r : Replica.builder(r).isFuture(true).build()) + .mapLog(r -> r.broker().id() != 0 ? r : Replica.builder(r).isFuture(true).build()) .build(); for (var cc : List.of(adding, removing, future)) { Assertions.assertThrows( @@ -579,7 +573,7 @@ static void assertOnlyAllowedBrokerMovement( source .replicaStream() // for those replicas that are not allowed to move - .filter(r -> !allowedBroker.test(r.nodeInfo().id())) + .filter(r -> !allowedBroker.test(r.broker().id())) // they should exist as-is in the target allocation .forEach( fixedReplica -> { @@ -603,7 +597,7 @@ static void assertBrokerEmpty(ClusterInfo target, Predicate clearBroker var violated = target .replicaStream() - .filter(i -> clearBroker.test(i.nodeInfo().id())) + .filter(i -> clearBroker.test(i.broker().id())) .collect(Collectors.toUnmodifiableSet()); Assertions.assertTrue( violated.isEmpty(), diff --git a/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java b/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java index d66e1f29a4..78b25a664e 100644 --- a/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java +++ b/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java @@ -286,7 +286,7 @@ void testCheckNoOngoingMigration() { CompletableFuture.completedFuture( ClusterInfo.of( cluster.clusterId(), - cluster.nodes(), + cluster.brokers(), cluster.topics(), cluster .replicaStream() diff --git a/common/src/test/java/org/astraea/common/balancer/BalancerTest.java b/common/src/test/java/org/astraea/common/balancer/BalancerTest.java index 5fcb74dc10..9b57cfeb75 100644 --- a/common/src/test/java/org/astraea/common/balancer/BalancerTest.java +++ b/common/src/test/java/org/astraea/common/balancer/BalancerTest.java @@ -31,8 +31,8 @@ import org.astraea.common.Configuration; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.balancer.algorithms.GreedyBalancer; @@ -73,10 +73,10 @@ void testLeaderCountRebalance(Class theClass) { .clusterInfo(admin.topicNames(false).toCompletableFuture().join()) .toCompletableFuture() .join(); - return clusterInfo.nodes().stream() + return clusterInfo.brokers().stream() .collect( Collectors.toMap( - NodeInfo::id, + Broker::id, n -> clusterInfo .replicaStream(n.id()) diff --git a/common/src/test/java/org/astraea/common/balancer/BalancerUtilsTest.java b/common/src/test/java/org/astraea/common/balancer/BalancerUtilsTest.java index bf27b63426..341968b28b 100644 --- a/common/src/test/java/org/astraea/common/balancer/BalancerUtilsTest.java +++ b/common/src/test/java/org/astraea/common/balancer/BalancerUtilsTest.java @@ -101,9 +101,9 @@ void testVerifyClearBrokerValidness() { var iter = Stream.of(1, 2, 3).map(base::node).iterator(); var cluster = ClusterInfo.builder(base) - .addTopic("A", 1, (short) 1, r -> Replica.builder(r).nodeInfo(iter.next()).build()) - .addTopic("B", 1, (short) 1, r -> Replica.builder(r).nodeInfo(iter.next()).build()) - .addTopic("C", 1, (short) 1, r -> Replica.builder(r).nodeInfo(iter.next()).build()) + .addTopic("A", 1, (short) 1, r -> Replica.builder(r).broker(iter.next()).build()) + .addTopic("B", 1, (short) 1, r -> Replica.builder(r).broker(iter.next()).build()) + .addTopic("C", 1, (short) 1, r -> Replica.builder(r).broker(iter.next()).build()) .build(); Assertions.assertThrows( @@ -173,13 +173,13 @@ void testClearedCluster() { cluster, id -> id == 1 || id == 2, id -> id == 3 || id == 4)); Assertions.assertEquals( - List.of(), clearedCluster.replicas().stream().filter(x -> x.nodeInfo().id() == 1).toList()); + List.of(), clearedCluster.replicas().stream().filter(x -> x.broker().id() == 1).toList()); Assertions.assertEquals( - List.of(), clearedCluster.replicas().stream().filter(x -> x.nodeInfo().id() == 2).toList()); + List.of(), clearedCluster.replicas().stream().filter(x -> x.broker().id() == 2).toList()); Assertions.assertNotEquals( - List.of(), clearedCluster.replicas().stream().filter(x -> x.nodeInfo().id() == 3).toList()); + List.of(), clearedCluster.replicas().stream().filter(x -> x.broker().id() == 3).toList()); Assertions.assertNotEquals( - List.of(), clearedCluster.replicas().stream().filter(x -> x.nodeInfo().id() == 4).toList()); + List.of(), clearedCluster.replicas().stream().filter(x -> x.broker().id() == 4).toList()); var sameCluster = Assertions.assertDoesNotThrow( @@ -193,16 +193,16 @@ void testClearedCluster() { Assertions.assertDoesNotThrow( () -> BalancerUtils.clearedCluster(cluster, id -> id == 1, id -> id == 3)); Assertions.assertEquals( - 0, aCluster.replicas().stream().filter(r -> r.nodeInfo().id() == 1).count(), "Demoted"); + 0, aCluster.replicas().stream().filter(r -> r.broker().id() == 1).count(), "Demoted"); Assertions.assertEquals( 100, - aCluster.replicas().stream().filter(r -> r.nodeInfo().id() == 2).count(), + aCluster.replicas().stream().filter(r -> r.broker().id() == 2).count(), "Not allowed or cleared"); Assertions.assertEquals( 100, - aCluster.replicas().stream().filter(r -> r.nodeInfo().id() == 3).count(), + aCluster.replicas().stream().filter(r -> r.broker().id() == 3).count(), "Accept replicas broker demoted broker"); Assertions.assertEquals( - 0, aCluster.replicas().stream().filter(r -> r.nodeInfo().id() == 4).count(), "Not allowed"); + 0, aCluster.replicas().stream().filter(r -> r.broker().id() == 4).count(), "Not allowed"); } } diff --git a/common/src/test/java/org/astraea/common/balancer/FakeClusterInfo.java b/common/src/test/java/org/astraea/common/balancer/FakeClusterInfo.java index 3857c4d145..8331e68f5d 100644 --- a/common/src/test/java/org/astraea/common/balancer/FakeClusterInfo.java +++ b/common/src/test/java/org/astraea/common/balancer/FakeClusterInfo.java @@ -26,7 +26,6 @@ import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; import org.astraea.common.admin.Config; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; @@ -76,70 +75,21 @@ public static ClusterInfo of( .collect(Collectors.toUnmodifiableSet()); final var nodes = IntStream.range(0, nodeCount) - .mapToObj(nodeId -> NodeInfo.of(nodeId, "host" + nodeId, 9092)) + .mapToObj(nodeId -> Broker.of(nodeId, "host" + nodeId, 9092)) .map( node -> - new Broker() { - @Override - public boolean isController() { - throw new UnsupportedOperationException(); - } - - @Override - public Config config() { - throw new UnsupportedOperationException(); - } - - @Override - public List dataFolders() { - return dataDirectories.stream() - .map( - x -> - new DataFolder() { - @Override - public String path() { - return x; - } - - @Override - public Map partitionSizes() { - throw new UnsupportedOperationException(); - } - - @Override - public Map orphanPartitionSizes() { - throw new UnsupportedOperationException(); - } - }) - .collect(Collectors.toUnmodifiableList()); - } - - @Override - public Set topicPartitions() { - throw new UnsupportedOperationException(); - } - - @Override - public Set topicPartitionLeaders() { - throw new UnsupportedOperationException(); - } - - @Override - public String host() { - return node.host(); - } - - @Override - public int port() { - return node.port(); - } - - @Override - public int id() { - return node.id(); - } - }) - .collect(Collectors.toUnmodifiableList()); + new Broker( + node.id(), + node.host(), + node.port(), + false, + new Config(Map.of()), + dataDirectories.stream() + .map(path -> new Broker.DataFolder(path, Map.of(), Map.of())) + .toList(), + Set.of(), + Set.of())) + .toList(); final var dataDirectoryList = List.copyOf(dataDirectories); final var topics = topicNameGenerator.apply(topicCount); final var replicas = @@ -156,7 +106,7 @@ public int id() { Replica.builder() .topic(tp.topic()) .partition(tp.partition()) - .nodeInfo(nodes.get(r)) + .broker(nodes.get(r)) .lag(0) .size(-1) .isLeader(r == 0) diff --git a/common/src/test/java/org/astraea/common/balancer/executor/StraightPlanExecutorTest.java b/common/src/test/java/org/astraea/common/balancer/executor/StraightPlanExecutorTest.java index 511eac0e22..59afe2d50b 100644 --- a/common/src/test/java/org/astraea/common/balancer/executor/StraightPlanExecutorTest.java +++ b/common/src/test/java/org/astraea/common/balancer/executor/StraightPlanExecutorTest.java @@ -27,9 +27,9 @@ import org.astraea.common.Configuration; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; import org.astraea.common.admin.ClusterInfoTest; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.it.Service; @@ -78,7 +78,7 @@ void testAsyncRun() { Replica.builder() .topic(tp.topic()) .partition(tp.partition()) - .nodeInfo(NodeInfo.of(broker0, "", -1)) + .broker(Broker.of(broker0, "", -1)) .lag(0) .size(0) .isLeader(true) @@ -91,7 +91,7 @@ void testAsyncRun() { Replica.builder() .topic(tp.topic()) .partition(tp.partition()) - .nodeInfo(NodeInfo.of(broker1, "", -1)) + .broker(Broker.of(broker1, "", -1)) .lag(0) .size(0) .isLeader(false) @@ -163,7 +163,7 @@ void testDisableDataDirMigration() { replica -> Replica.builder(replica) .path( - source.brokerFolders().get(replica.nodeInfo().id()).stream() + source.brokerFolders().get(replica.broker().id()).stream() .filter(p -> !replica.path().equals(p)) .findAny() .orElseThrow()) diff --git a/common/src/test/java/org/astraea/common/balancer/tweakers/ShuffleTweakerTest.java b/common/src/test/java/org/astraea/common/balancer/tweakers/ShuffleTweakerTest.java index baef67f81a..3ce16cb6cc 100644 --- a/common/src/test/java/org/astraea/common/balancer/tweakers/ShuffleTweakerTest.java +++ b/common/src/test/java/org/astraea/common/balancer/tweakers/ShuffleTweakerTest.java @@ -25,9 +25,9 @@ import java.util.concurrent.atomic.LongAdder; import java.util.stream.Collectors; import org.astraea.common.Utils; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; import org.astraea.common.admin.ClusterInfoTest; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.balancer.FakeClusterInfo; @@ -160,14 +160,14 @@ void testEligiblePartition() { 0, Set.of("/a", "/b", "c"), 1, Set.of("/a", "/b", "c"), 2, Set.of("/a", "/b", "c")); - var nodeA = NodeInfo.of(0, "", -1); - var nodeB = NodeInfo.of(1, "", -1); - var nodeC = NodeInfo.of(2, "", -1); + var nodeA = Broker.of(0, "", -1); + var nodeB = Broker.of(1, "", -1); + var nodeC = Broker.of(2, "", -1); var base = Replica.builder() .topic("topic") .partition(0) - .nodeInfo(nodeA) + .broker(nodeA) .lag(0) .size(0) .isLeader(false) @@ -185,8 +185,8 @@ void testEligiblePartition() { .isLeader(true) .isPreferredLeader(true) .build(), - Replica.builder(base).topic("normal-topic").nodeInfo(nodeB).build(), - Replica.builder(base).topic("normal-topic").nodeInfo(nodeC).build(), + Replica.builder(base).topic("normal-topic").broker(nodeB).build(), + Replica.builder(base).topic("normal-topic").broker(nodeC).build(), Replica.builder(base) .topic("offline-single") .isPreferredLeader(true) @@ -195,10 +195,10 @@ void testEligiblePartition() { Replica.builder(base) .topic("no-leader") .isPreferredLeader(true) - .nodeInfo(nodeA) + .broker(nodeA) .build(), - Replica.builder(base).topic("no-leader").nodeInfo(nodeB).build(), - Replica.builder(base).topic("no-leader").nodeInfo(nodeC).build())); + Replica.builder(base).topic("no-leader").broker(nodeB).build(), + Replica.builder(base).topic("no-leader").broker(nodeC).build())); shuffleTweaker .generate(allocation) .limit(30) diff --git a/common/src/test/java/org/astraea/common/cost/BrokerDiskSpaceCostTest.java b/common/src/test/java/org/astraea/common/cost/BrokerDiskSpaceCostTest.java index f7f7dc9b82..c91f089582 100644 --- a/common/src/test/java/org/astraea/common/cost/BrokerDiskSpaceCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/BrokerDiskSpaceCostTest.java @@ -28,7 +28,6 @@ import org.astraea.common.DataSize; import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.metrics.ClusterBean; import org.junit.jupiter.api.Assertions; @@ -54,42 +53,42 @@ void testMoveCosts() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build()); @@ -98,42 +97,42 @@ void testMoveCosts() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(dataSize.bytes()) .path("/path1") .build(), Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(dataSize.bytes()) .path("/path1") .build(), Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build(), Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(dataSize.bytes()) .path("/path0") .build()); @@ -235,15 +234,15 @@ public static ClusterInfo of(List replicas) { return ClusterInfo.of( "fake", replicas.stream() - .map(Replica::nodeInfo) + .map(Replica::broker) .distinct() .map( - nodeInfo -> + broker -> Broker.of( false, - new Node(nodeInfo.id(), "", nodeInfo.port()), + new Node(broker.id(), "", broker.port()), Map.of(), - dataPath.get(nodeInfo.id()), + dataPath.get(broker.id()), List.of())) .collect(Collectors.toList()), Map.of(), @@ -282,7 +281,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(100) .isLeader(true) .path("/path0") @@ -290,7 +289,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(99) .isLeader(false) .path("/path0") @@ -298,7 +297,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(500) .isLeader(true) .path("/path0") @@ -306,7 +305,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(499) .isLeader(false) .path("/path0") @@ -314,7 +313,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(1000) .isLeader(true) .path("/path0") @@ -322,7 +321,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(1000) .isLeader(false) .path("/path0") @@ -330,15 +329,15 @@ private static ClusterInfo beforeClusterInfo() { return ClusterInfo.of( "fake", replicas.stream() - .map(Replica::nodeInfo) + .map(Replica::broker) .distinct() .map( - nodeInfo -> + broker -> Broker.of( false, - new Node(nodeInfo.id(), "", nodeInfo.port()), + new Node(broker.id(), "", broker.port()), Map.of(), - dataPath.get(nodeInfo.id()), + dataPath.get(broker.id()), List.of())) .collect(Collectors.toList()), Map.of(), @@ -363,7 +362,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(100) .isLeader(true) .path("/path1") @@ -371,7 +370,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(99) .isLeader(false) .path("/path0") @@ -379,7 +378,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(500) .isLeader(true) .path("/path0") @@ -387,7 +386,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(500) .isLeader(false) .path("/path1") @@ -395,7 +394,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(1000) .isLeader(true) .path("/path1") @@ -403,7 +402,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(1000) .isLeader(false) .path("/path0") @@ -411,15 +410,15 @@ private static ClusterInfo afterClusterInfo() { return ClusterInfo.of( "fake", replicas.stream() - .map(Replica::nodeInfo) + .map(Replica::broker) .distinct() .map( - nodeInfo -> + broker -> Broker.of( false, - new Node(nodeInfo.id(), "", nodeInfo.port()), + new Node(broker.id(), "", broker.port()), Map.of(), - dataPath.get(nodeInfo.id()), + dataPath.get(broker.id()), List.of())) .collect(Collectors.toList()), Map.of(), diff --git a/common/src/test/java/org/astraea/common/cost/CostUtilsTest.java b/common/src/test/java/org/astraea/common/cost/CostUtilsTest.java index e086242e57..db1817e725 100644 --- a/common/src/test/java/org/astraea/common/cost/CostUtilsTest.java +++ b/common/src/test/java/org/astraea/common/cost/CostUtilsTest.java @@ -27,7 +27,6 @@ import org.apache.kafka.common.requests.DescribeLogDirsResponse; import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -90,7 +89,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(100) .isLeader(true) .path("/path0") @@ -98,7 +97,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(99) .isLeader(false) .path("/path0") @@ -106,7 +105,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(500) .isLeader(true) .path("/path0") @@ -114,7 +113,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(499) .isLeader(false) .path("/path0") @@ -122,7 +121,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(1000) .isLeader(true) .path("/path0") @@ -130,7 +129,7 @@ private static ClusterInfo beforeClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(1000) .isLeader(false) .path("/path0") @@ -138,15 +137,15 @@ private static ClusterInfo beforeClusterInfo() { return ClusterInfo.of( "fake", replicas.stream() - .map(Replica::nodeInfo) + .map(Replica::broker) .distinct() .map( - nodeInfo -> + broker -> Broker.of( false, - new Node(nodeInfo.id(), "", nodeInfo.port()), + new Node(broker.id(), "", broker.port()), Map.of(), - dataPath.get(nodeInfo.id()), + dataPath.get(broker.id()), List.of())) .collect(Collectors.toList()), Map.of(), @@ -171,7 +170,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(100) .isLeader(true) .path("/path1") @@ -179,7 +178,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(99) .isLeader(false) .path("/path0") @@ -187,7 +186,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(500) .isLeader(true) .path("/path0") @@ -195,7 +194,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .size(500) .isLeader(false) .path("/path1") @@ -203,7 +202,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .size(1000) .isLeader(true) .path("/path1") @@ -211,7 +210,7 @@ private static ClusterInfo afterClusterInfo() { Replica.builder() .topic("topic1") .partition(2) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .size(1000) .isLeader(false) .path("/path0") @@ -219,15 +218,15 @@ private static ClusterInfo afterClusterInfo() { return ClusterInfo.of( "fake", replicas.stream() - .map(Replica::nodeInfo) + .map(Replica::broker) .distinct() .map( - nodeInfo -> + broker -> Broker.of( false, - new Node(nodeInfo.id(), "", nodeInfo.port()), + new Node(broker.id(), "", broker.port()), Map.of(), - dataPath.get(nodeInfo.id()), + dataPath.get(broker.id()), List.of())) .collect(Collectors.toList()), Map.of(), diff --git a/common/src/test/java/org/astraea/common/cost/MigrationCostTest.java b/common/src/test/java/org/astraea/common/cost/MigrationCostTest.java index 89ddd813bf..c7ccc33aa0 100644 --- a/common/src/test/java/org/astraea/common/cost/MigrationCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/MigrationCostTest.java @@ -21,8 +21,8 @@ import static org.astraea.common.cost.MigrationCost.replicaNumChanged; import java.util.List; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfoTest; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -36,7 +36,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -49,7 +49,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -62,7 +62,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -75,7 +75,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -90,7 +90,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -103,7 +103,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -116,7 +116,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -129,7 +129,7 @@ void testChangedReplicaLeaderNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -176,7 +176,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -189,7 +189,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -202,7 +202,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -215,7 +215,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -230,7 +230,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -243,7 +243,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(0) - .nodeInfo(NodeInfo.of(1, "broker0", 1111)) + .broker(Broker.of(1, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) @@ -256,7 +256,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(0, "broker0", 1111)) + .broker(Broker.of(0, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(true) @@ -269,7 +269,7 @@ void testChangedReplicaNumber() { Replica.builder() .topic("topic1") .partition(1) - .nodeInfo(NodeInfo.of(2, "broker0", 1111)) + .broker(Broker.of(2, "broker0", 1111)) .lag(-1) .size(-1) .isLeader(false) diff --git a/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java b/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java index e85a4b0d76..80544becc8 100644 --- a/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java @@ -184,7 +184,7 @@ void testReplicationAware() { (short) 2, (r) -> Replica.builder(r) - .nodeInfo(base.node(iter1.next())) + .broker(base.node(iter1.next())) .isPreferredLeader(iter2.next()) .isLeader(iter3.next()) .size(1) @@ -195,7 +195,7 @@ void testReplicationAware() { (short) 2, (r) -> Replica.builder(r) - .nodeInfo(base.node(iter4.next())) + .broker(base.node(iter4.next())) .isPreferredLeader(iter5.next()) .isLeader(iter6.next()) .size(1) diff --git a/common/src/test/java/org/astraea/common/cost/RecordSizeCostTest.java b/common/src/test/java/org/astraea/common/cost/RecordSizeCostTest.java index 75698f3ad6..6d099b062d 100644 --- a/common/src/test/java/org/astraea/common/cost/RecordSizeCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/RecordSizeCostTest.java @@ -18,8 +18,8 @@ import java.util.List; import java.util.Map; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.metrics.ClusterBean; @@ -34,27 +34,27 @@ public class RecordSizeCostTest { private final ClusterInfo clusterInfo = ClusterInfo.of( "fake", - List.of(NodeInfo.of(0, "aa", 22), NodeInfo.of(1, "aa", 22), NodeInfo.of(2, "aa", 22)), + List.of(Broker.of(0, "aa", 22), Broker.of(1, "aa", 22), Broker.of(2, "aa", 22)), Map.of(), List.of( Replica.builder() .topic("topic") .partition(0) - .nodeInfo(NodeInfo.of(1, "aa", 22)) + .broker(Broker.of(1, "aa", 22)) .size(100) .path("/tmp/aa") .buildLeader(), Replica.builder() .topic("topic") .partition(0) - .nodeInfo(NodeInfo.of(2, "aa", 22)) + .broker(Broker.of(2, "aa", 22)) .size(99) .path("/tmp/aa") .buildInSyncFollower(), Replica.builder() .topic("topic") .partition(1) - .nodeInfo(NodeInfo.of(1, "aa", 22)) + .broker(Broker.of(1, "aa", 22)) .size(11) .path("/tmp/aa") .buildLeader())); diff --git a/common/src/test/java/org/astraea/common/cost/ReplicaLeaderCostTest.java b/common/src/test/java/org/astraea/common/cost/ReplicaLeaderCostTest.java index 47884a01be..caa18ea1e7 100644 --- a/common/src/test/java/org/astraea/common/cost/ReplicaLeaderCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/ReplicaLeaderCostTest.java @@ -21,8 +21,8 @@ import java.util.Set; import java.util.stream.Collectors; import org.astraea.common.Configuration; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.metrics.ClusterBean; import org.junit.jupiter.api.Assertions; @@ -42,28 +42,16 @@ void testLeaderCount() { var sourceCluster = ClusterInfo.builder(baseCluster) .addTopic( - "topic1", - 3, - (short) 1, - r -> Replica.builder(r).nodeInfo(baseCluster.node(1)).build()) + "topic1", 3, (short) 1, r -> Replica.builder(r).broker(baseCluster.node(1)).build()) .addTopic( - "topic2", - 3, - (short) 1, - r -> Replica.builder(r).nodeInfo(baseCluster.node(2)).build()) + "topic2", 3, (short) 1, r -> Replica.builder(r).broker(baseCluster.node(2)).build()) .build(); var overFlowTargetCluster = ClusterInfo.builder(baseCluster) .addTopic( - "topic1", - 3, - (short) 1, - r -> Replica.builder(r).nodeInfo(baseCluster.node(2)).build()) + "topic1", 3, (short) 1, r -> Replica.builder(r).broker(baseCluster.node(2)).build()) .addTopic( - "topic2", - 3, - (short) 1, - r -> Replica.builder(r).nodeInfo(baseCluster.node(1)).build()) + "topic2", 3, (short) 1, r -> Replica.builder(r).broker(baseCluster.node(1)).build()) .build(); var overFlowMoveCost = @@ -88,30 +76,31 @@ void testNoMetrics() { .topic("topic") .partition(0) .isLeader(true) - .nodeInfo(NodeInfo.of(10, "broker0", 1111)) + .broker(Broker.of(10, "broker0", 1111)) .path("/tmp/aa") .buildLeader(), Replica.builder() .topic("topic") .partition(1) .isLeader(true) - .nodeInfo(NodeInfo.of(10, "broker0", 1111)) + .partition(0) + .broker(Broker.of(10, "broker0", 1111)) .path("/tmp/aa") .buildLeader(), Replica.builder() .topic("topic") .partition(0) - .nodeInfo(NodeInfo.of(11, "broker1", 1111)) .isLeader(true) + .broker(Broker.of(11, "broker1", 1111)) .path("/tmp/aa") .buildLeader()); var clusterInfo = ClusterInfo.of( "fake", List.of( - NodeInfo.of(10, "host1", 8080), - NodeInfo.of(11, "host1", 8080), - NodeInfo.of(12, "host1", 8080)), + Broker.of(10, "host1", 8080), + Broker.of(11, "host1", 8080), + Broker.of(12, "host1", 8080)), Map.of(), replicas); var brokerCost = ReplicaLeaderCost.leaderCount(clusterInfo); diff --git a/common/src/test/java/org/astraea/common/cost/ReplicaLeaderSizeCostTest.java b/common/src/test/java/org/astraea/common/cost/ReplicaLeaderSizeCostTest.java index 8826579dd1..d78ae5ad12 100644 --- a/common/src/test/java/org/astraea/common/cost/ReplicaLeaderSizeCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/ReplicaLeaderSizeCostTest.java @@ -18,8 +18,8 @@ import java.util.List; import java.util.Map; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.admin.TopicPartition; import org.astraea.common.metrics.ClusterBean; @@ -74,33 +74,33 @@ private ClusterInfo clusterInfo() { .topic("t") .partition(10) .isLeader(true) - .nodeInfo(NodeInfo.of(0, "", -1)) + .broker(Broker.of(0, "", -1)) .size(777) .build(), Replica.builder() .topic("t") .partition(11) .isLeader(true) - .nodeInfo(NodeInfo.of(1, "", -1)) + .broker(Broker.of(1, "", -1)) .size(700) .build(), Replica.builder() .topic("t") .partition(12) .isLeader(true) - .nodeInfo(NodeInfo.of(2, "", -1)) + .broker(Broker.of(2, "", -1)) .size(500) .build(), Replica.builder() .topic("t") .partition(12) .isLeader(false) - .nodeInfo(NodeInfo.of(0, "", -1)) + .broker(Broker.of(0, "", -1)) .size(499) .build()); return ClusterInfo.of( "fake", - List.of(NodeInfo.of(0, "", -1), NodeInfo.of(1, "", -1), NodeInfo.of(2, "", -1)), + List.of(Broker.of(0, "", -1), Broker.of(1, "", -1), Broker.of(2, "", -1)), Map.of(), replicas); } diff --git a/common/src/test/java/org/astraea/common/cost/utils/ClusterInfoSensorTest.java b/common/src/test/java/org/astraea/common/cost/utils/ClusterInfoSensorTest.java index 639d8872d1..94fbcc9b4a 100644 --- a/common/src/test/java/org/astraea/common/cost/utils/ClusterInfoSensorTest.java +++ b/common/src/test/java/org/astraea/common/cost/utils/ClusterInfoSensorTest.java @@ -101,7 +101,7 @@ void testClusterInfoSensor() { TopicPartition.of(topic, 1), TopicPartition.of(topic, 2)))); // compare broker id - Assertions.assertTrue(info.replicaStream().allMatch(r -> r.nodeInfo().id() == aBroker.id())); + Assertions.assertTrue(info.replicaStream().allMatch(r -> r.broker().id() == aBroker.id())); // compare replica size var realCluster = admin.clusterInfo(Set.of(topic)).toCompletableFuture().join(); Assertions.assertTrue( @@ -241,7 +241,7 @@ void testClusterId() { var info = ClusterInfoSensor.metricViewCluster(cb); - Assertions.assertEquals(1, info.nodes().size()); + Assertions.assertEquals(1, info.brokers().size()); Assertions.assertEquals(id, info.clusterId()); } diff --git a/common/src/test/java/org/astraea/common/partitioner/PartitionerTest.java b/common/src/test/java/org/astraea/common/partitioner/PartitionerTest.java index af5fa82063..a877df3912 100644 --- a/common/src/test/java/org/astraea/common/partitioner/PartitionerTest.java +++ b/common/src/test/java/org/astraea/common/partitioner/PartitionerTest.java @@ -79,7 +79,7 @@ public int partition(String topic, byte[] key, byte[] value, ClusterInfo cluster Map.of(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, SERVICE.bootstrapServers())); Assertions.assertNotNull(partitioner.admin); Utils.sleep(Duration.ofSeconds(3)); - Assertions.assertNotEquals(0, partitioner.clusterInfo.nodes().size()); + Assertions.assertNotEquals(0, partitioner.clusterInfo.brokers().size()); } } diff --git a/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerPerfTest.java b/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerPerfTest.java index 1fbaad6424..cef2691d5b 100644 --- a/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerPerfTest.java +++ b/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerPerfTest.java @@ -29,33 +29,34 @@ import org.astraea.common.Configuration; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; -import org.astraea.common.metrics.MBeanRegister; +import org.astraea.common.metrics.BeanObject; +import org.astraea.common.metrics.ClusterBean; +import org.astraea.common.metrics.client.HasNodeMetrics; +import org.astraea.common.metrics.collector.MetricStore; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; public class StrictCostPartitionerPerfTest { - private static AtomicLong createMetric(int brokerId) { - var latency = new AtomicLong(100); - MBeanRegister.local() - .domainName("kafka.producer") - .property("type", "producer-node-metrics") - .property("node-id", String.valueOf(brokerId)) - .property("client-id", "xxxx") - .attribute("request-latency-avg", Double.class, () -> (double) latency.get()) - .register(); - return latency; + private static BeanObject getBeanObject(int brokerId, double latency) { + return new BeanObject( + "kafka.producer", + Map.of( + "type", "producer-node-metrics", + "node-id", String.valueOf(brokerId), + "client-id", "xxxx"), + Map.of("request-latency-avg", latency)); } @Test void test() { - var node0 = NodeInfo.of(0, "node0", 2222); - var node1 = NodeInfo.of(1, "node1", 2222); - var node2 = NodeInfo.of(2, "node2", 2222); + var node0 = Broker.of(0, "node0", 2222); + var node1 = Broker.of(1, "node1", 2222); + var node2 = Broker.of(2, "node2", 2222); var clusterInfo = ClusterInfo.of( "fake", @@ -65,19 +66,19 @@ void test() { Replica.builder() .topic("topic") .partition(0) - .nodeInfo(node0) + .broker(node0) .path("/tmp/aa") .buildLeader(), Replica.builder() .topic("topic") .partition(1) - .nodeInfo(node1) + .broker(node1) .path("/tmp/aa") .buildLeader(), Replica.builder() .topic("topic") .partition(2) - .nodeInfo(node2) + .broker(node2) .path("/tmp/aa") .buildLeader())); var admin = Mockito.mock(Admin.class); @@ -86,15 +87,27 @@ void test() { Mockito.when(admin.clusterInfo(Mockito.anySet())) .thenReturn(CompletableFuture.completedStage(clusterInfo)); - var node0Latency = createMetric(0); - var node1Latency = createMetric(1); - var node2Latency = createMetric(2); + var node0Latency = new AtomicLong(100); + var node1Latency = new AtomicLong(100); + var node2Latency = new AtomicLong(100); + + var metricStore = Mockito.mock(MetricStore.class); + Mockito.when(metricStore.clusterBean()) + .thenReturn( + ClusterBean.of( + Map.of( + -1, + List.of( + (HasNodeMetrics) () -> getBeanObject(0, node0Latency.get()), + () -> getBeanObject(1, node1Latency.get()), + () -> getBeanObject(2, node2Latency.get()))))); var key = "key".getBytes(StandardCharsets.UTF_8); var value = "value".getBytes(StandardCharsets.UTF_8); try (var partitioner = new StrictCostPartitioner()) { partitioner.admin = admin; partitioner.configure(new Configuration(Map.of("round.robin.lease", "2s"))); + partitioner.metricStore = metricStore; Supplier>> resultSupplier = () -> { diff --git a/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java b/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java index 233be18563..8809be5932 100644 --- a/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java +++ b/common/src/test/java/org/astraea/common/partitioner/StrictCostPartitionerTest.java @@ -26,9 +26,9 @@ import java.util.stream.IntStream; import org.astraea.common.Configuration; import org.astraea.common.Utils; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; import org.astraea.common.admin.ClusterInfoTest; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.cost.BrokerCost; import org.astraea.common.cost.BrokerInputCost; @@ -110,13 +110,13 @@ void testNoAvailableBrokers() { @Test void testSingleBroker() { - var nodeInfo = NodeInfo.of(10, "host", 11111); + var nodeInfo = Broker.of(10, "host", 11111); var replicaInfo = Replica.builder() .topic("topic") .partition(10) .path("/tmp/aa") - .nodeInfo(nodeInfo) + .broker(nodeInfo) .buildLeader(); try (var partitioner = new StrictCostPartitioner()) { partitioner.configure(Configuration.EMPTY); @@ -142,14 +142,14 @@ void testCostFunctionWithoutSensor() { .topic("topic") .partition(0) .path("/tmp/aa") - .nodeInfo(NodeInfo.of(10, "host", 11111)) + .broker(Broker.of(10, "host", 11111)) .buildLeader(); var replicaInfo1 = Replica.builder() .topic("topic") .partition(0) .path("/tmp/aa") - .nodeInfo(NodeInfo.of(12, "host2", 11111)) + .broker(Broker.of(12, "host2", 11111)) .buildLeader(); try (var partitioner = new StrictCostPartitioner()) { partitioner.configure( @@ -196,14 +196,14 @@ void testReturnedPartition() { .topic("topic") .partition(partitionId) .path("/tmp/aa") - .nodeInfo(NodeInfo.of(brokerId, "host", 11111)) + .broker(Broker.of(brokerId, "host", 11111)) .buildLeader(); var replicaInfo1 = Replica.builder() .topic("topic") .partition(1) .path("/tmp/aa") - .nodeInfo(NodeInfo.of(1111, "host2", 11111)) + .broker(Broker.of(1111, "host2", 11111)) .buildLeader(); Assertions.assertEquals( partitionId, diff --git a/gui/src/main/java/org/astraea/gui/Context.java b/gui/src/main/java/org/astraea/gui/Context.java index ecf305b7a3..c10baec19e 100644 --- a/gui/src/main/java/org/astraea/gui/Context.java +++ b/gui/src/main/java/org/astraea/gui/Context.java @@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicReference; import javafx.stage.Stage; import org.astraea.common.admin.Admin; -import org.astraea.common.admin.NodeInfo; +import org.astraea.common.admin.Broker; import org.astraea.common.connector.ConnectorClient; import org.astraea.common.metrics.JndiClient; @@ -73,9 +73,9 @@ public void workerJmxPort(int workerJmxPort) { } @SuppressWarnings("resource") - public Map addBrokerClients(List nodeInfos) { + public Map addBrokerClients(List brokers) { if (brokerClients == null) return Map.of(); - nodeInfos.forEach( + brokers.forEach( n -> brokerClients.clients.computeIfAbsent( n.id(), ignored -> JndiClient.of(n.host(), brokerClients.jmxPort))); diff --git a/gui/src/main/java/org/astraea/gui/tab/BrokerNode.java b/gui/src/main/java/org/astraea/gui/tab/BrokerNode.java index d66ac07ae0..e9655be4aa 100644 --- a/gui/src/main/java/org/astraea/gui/tab/BrokerNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/BrokerNode.java @@ -38,7 +38,6 @@ import org.astraea.common.MapUtils; import org.astraea.common.admin.Broker; import org.astraea.common.admin.BrokerConfigs; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.TopicPartition; import org.astraea.common.metrics.JndiClient; import org.astraea.common.metrics.broker.ControllerMetrics; @@ -224,7 +223,7 @@ static Node metricsNode(Context context) { (argument, logger) -> context .admin() - .nodeInfos() + .brokers() .thenApply( nodes -> context.addBrokerClients(nodes).entrySet().stream() @@ -378,12 +377,11 @@ private static Node configNode(Context context) { var unset = brokers.stream() .collect( - Collectors.toMap( - NodeInfo::id, b -> input.emptyValueKeys())); + Collectors.toMap(Broker::id, b -> input.emptyValueKeys())); var set = brokers.stream() .collect( - Collectors.toMap(NodeInfo::id, b -> input.nonEmptyTexts())); + Collectors.toMap(Broker::id, b -> input.nonEmptyTexts())); if (unset.isEmpty() && set.isEmpty()) { logger.log("nothing to alter"); return CompletableFuture.completedStage(null); diff --git a/gui/src/main/java/org/astraea/gui/tab/ClientNode.java b/gui/src/main/java/org/astraea/gui/tab/ClientNode.java index f3378f5111..abad4cd470 100644 --- a/gui/src/main/java/org/astraea/gui/tab/ClientNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/ClientNode.java @@ -184,7 +184,7 @@ static List> consumerResult( result.put("group", cg.groupId()); result.put("assignor", cg.assignor()); result.put("state", cg.state()); - result.put("coordinator", cg.coordinator().id()); + result.put("coordinator", cg.coordinatorId()); result.put("topic", tp.topic()); result.put("partition", tp.partition()); Optional.ofNullable(cg.consumeProgress().get(tp)) diff --git a/gui/src/main/java/org/astraea/gui/tab/SettingNode.java b/gui/src/main/java/org/astraea/gui/tab/SettingNode.java index 2b03786ddd..1e0f40b006 100644 --- a/gui/src/main/java/org/astraea/gui/tab/SettingNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/SettingNode.java @@ -149,16 +149,16 @@ public static Node of(Context context) { .url(Utils.packException(() -> new URL("http://" + url))) .build()); return FutureUtils.combine( - newAdmin.nodeInfos(), + newAdmin.brokers(), client .map(ConnectorClient::activeWorkers) .orElse(CompletableFuture.completedFuture(List.of())), - (nodeInfos, workers) -> { + (brokers, workers) -> { context.replace(newAdmin); client.ifPresent(context::replace); prop.brokerJmxPort.ifPresent(context::brokerJmxPort); prop.workerJmxPort.ifPresent(context::workerJmxPort); - context.addBrokerClients(nodeInfos); + context.addBrokerClients(brokers); context.addWorkerClients( workers.stream() .map(WorkerStatus::hostname) diff --git a/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java b/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java index 2561971e49..b2ccf6415d 100644 --- a/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java @@ -123,20 +123,20 @@ static List> assignmentResult(Balancer.Plan plan) { .filter(Replica::isLeader) .findFirst() .ifPresent( - r -> result.put(PREVIOUS_LEADER_KEY, r.nodeInfo().id() + ":" + r.path())); + r -> result.put(PREVIOUS_LEADER_KEY, r.broker().id() + ":" + r.path())); newAssignments.stream() .filter(Replica::isLeader) .findFirst() - .ifPresent(r -> result.put(NEW_LEADER_KEY, r.nodeInfo().id() + ":" + r.path())); + .ifPresent(r -> result.put(NEW_LEADER_KEY, r.broker().id() + ":" + r.path())); var previousFollowers = previousAssignments.stream() .filter(r -> !r.isLeader()) - .map(r -> r.nodeInfo().id() + ":" + r.path()) + .map(r -> r.broker().id() + ":" + r.path()) .collect(Collectors.joining(",")); var newFollowers = newAssignments.stream() .filter(r -> !r.isLeader()) - .map(r -> r.nodeInfo().id() + ":" + r.path()) + .map(r -> r.broker().id() + ":" + r.path()) .collect(Collectors.joining(",")); if (!previousFollowers.isBlank()) result.put(PREVIOUS_FOLLOWER_KEY, previousFollowers); diff --git a/gui/src/main/java/org/astraea/gui/tab/health/HealthNode.java b/gui/src/main/java/org/astraea/gui/tab/health/HealthNode.java index 168161d10c..93fac415f2 100644 --- a/gui/src/main/java/org/astraea/gui/tab/health/HealthNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/health/HealthNode.java @@ -109,15 +109,13 @@ static CompletionStage>> unavailablePartitions(Admin ad .filter( p -> p.isr().size() < minInSync.getOrDefault(p.topic(), 1) - || p.leader().isEmpty()) + || p.leaderId().isEmpty()) .map( p -> { var r = new LinkedHashMap(); r.put("topic", p.topic()); r.put("partition", p.partition()); - r.put( - "leader", - p.leader().map(n -> String.valueOf(n.id())).orElse("null")); + r.put("leader", p.leaderId().map(String::valueOf).orElse("null")); r.put( "in-sync replicas", p.isr().stream() @@ -126,10 +124,10 @@ static CompletionStage>> unavailablePartitions(Admin ad r.put( TopicConfigs.MIN_IN_SYNC_REPLICAS_CONFIG, minInSync.getOrDefault(p.topic(), 1)); - r.put("readable", p.leader().isPresent()); + r.put("readable", p.leaderId().isPresent()); r.put( "writable", - p.leader().isPresent() + p.leaderId().isPresent() && p.isr().size() >= minInSync.getOrDefault(p.topic(), 1)); return (Map) r; }) diff --git a/gui/src/main/java/org/astraea/gui/tab/topic/PartitionNode.java b/gui/src/main/java/org/astraea/gui/tab/topic/PartitionNode.java index b1d6e43416..49df25c46d 100644 --- a/gui/src/main/java/org/astraea/gui/tab/topic/PartitionNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/topic/PartitionNode.java @@ -59,7 +59,7 @@ private static List> basicResult(List ps) { result.put(TOPIC_NAME_KEY, p.topic()); result.put(PARTITION_KEY, p.partition()); result.put("internal", p.internal()); - p.leader().ifPresent(l -> result.put("leader", l.id())); + p.leaderId().ifPresent(l -> result.put("leader", l)); result.put( "replicas", p.replicas().stream() diff --git a/gui/src/main/java/org/astraea/gui/tab/topic/ReplicaNode.java b/gui/src/main/java/org/astraea/gui/tab/topic/ReplicaNode.java index 3ee8168fd9..8d0e1905a5 100644 --- a/gui/src/main/java/org/astraea/gui/tab/topic/ReplicaNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/topic/ReplicaNode.java @@ -64,7 +64,7 @@ static List> allResult(ClusterInfo clusterInfo) { result.put(TOPIC_NAME_KEY, replica.topic()); result.put(PARTITION_KEY, replica.partition()); result.put("internal", replica.internal()); - result.put("broker", replica.nodeInfo().id()); + result.put("broker", replica.broker().id()); if (replica.path() != null) result.put(PATH_KEY, replica.path()); result.put("isLeader", replica.isLeader()); result.put("isPreferredLeader", replica.isPreferredLeader()); @@ -195,7 +195,7 @@ static List> allResult(ClusterInfo clusterInfo) { .allMatch( entry -> rs.replicas(entry.getKey()).stream() - .map(r -> r.nodeInfo().id()) + .map(r -> r.broker().id()) .collect(Collectors.toSet()) .containsAll(entry.getValue())), Duration.ofSeconds(10), diff --git a/gui/src/main/java/org/astraea/gui/tab/topic/TopicNode.java b/gui/src/main/java/org/astraea/gui/tab/topic/TopicNode.java index bf3ec4fa2d..7fcdf3e730 100644 --- a/gui/src/main/java/org/astraea/gui/tab/topic/TopicNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/topic/TopicNode.java @@ -38,7 +38,6 @@ import org.astraea.common.MapUtils; import org.astraea.common.admin.Broker; import org.astraea.common.admin.ConsumerGroup; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Partition; import org.astraea.common.admin.ProducerState; import org.astraea.common.admin.TopicConfigs; @@ -413,7 +412,7 @@ private static List> basicResult( "number of producer id", topicProducers.getOrDefault(topic, Set.of()).size()); ps.stream() .flatMap(p -> p.replicas().stream()) - .collect(Collectors.groupingBy(NodeInfo::id)) + .collect(Collectors.groupingBy(Broker::id)) .entrySet() .stream() .sorted(Map.Entry.comparingByKey()) diff --git a/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java b/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java index ebfe02274f..90ca3ca23b 100644 --- a/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java +++ b/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java @@ -25,8 +25,8 @@ import java.util.stream.Collectors; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.common.balancer.Balancer; import org.astraea.common.cost.ReplicaLeaderSizeCost; @@ -104,8 +104,7 @@ void testGenerator() { void testResult() { var topic = Utils.randomString(); var leaderSize = 100; - var allNodes = - List.of(NodeInfo.of(0, "aa", 0), NodeInfo.of(1, "aa", 0), NodeInfo.of(3, "aa", 0)); + var allNodes = List.of(Broker.of(0, "aa", 0), Broker.of(1, "aa", 0), Broker.of(3, "aa", 0)); var beforeReplicas = List.of( Replica.builder() @@ -113,7 +112,7 @@ void testResult() { .isPreferredLeader(false) .topic(topic) .partition(0) - .nodeInfo(allNodes.get(0)) + .broker(allNodes.get(0)) .size(leaderSize) .path("/tmp/aaa") .build(), @@ -122,7 +121,7 @@ void testResult() { .isPreferredLeader(true) .topic(topic) .partition(0) - .nodeInfo(allNodes.get(1)) + .broker(allNodes.get(1)) .size(leaderSize) .path("/tmp/bbb") .build()); @@ -133,7 +132,7 @@ void testResult() { .isPreferredLeader(false) .topic(topic) .partition(0) - .nodeInfo(allNodes.get(2)) + .broker(allNodes.get(2)) .size(leaderSize) .path("/tmp/ddd") .build(), @@ -142,7 +141,7 @@ void testResult() { .isPreferredLeader(true) .topic(topic) .partition(0) - .nodeInfo(allNodes.get(1)) + .broker(allNodes.get(1)) .size(leaderSize) .path("/tmp/bbb") .build()); diff --git a/gui/src/test/java/org/astraea/gui/tab/topic/ReplicaNodeTest.java b/gui/src/test/java/org/astraea/gui/tab/topic/ReplicaNodeTest.java index cea7020372..0530ff0b77 100644 --- a/gui/src/test/java/org/astraea/gui/tab/topic/ReplicaNodeTest.java +++ b/gui/src/test/java/org/astraea/gui/tab/topic/ReplicaNodeTest.java @@ -25,8 +25,8 @@ import java.util.stream.Collectors; import org.astraea.common.Utils; import org.astraea.common.admin.Admin; +import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; -import org.astraea.common.admin.NodeInfo; import org.astraea.common.admin.Replica; import org.astraea.gui.Context; import org.astraea.gui.pane.Argument; @@ -124,7 +124,7 @@ void testTableAction() { .join() .replicas() .get(0) - .nodeInfo() + .broker() .id()); Assertions.assertEquals( path, @@ -143,14 +143,14 @@ void testResult() { var topic = Utils.randomString(); var partition = 0; var leaderSize = 100; - var nodes = List.of(NodeInfo.of(0, "aa", 0), NodeInfo.of(1, "aa", 0), NodeInfo.of(2, "aa", 0)); + var nodes = List.of(Broker.of(0, "aa", 0), Broker.of(1, "aa", 0), Broker.of(2, "aa", 0)); var replicas = List.of( Replica.builder() .isLeader(true) .topic(topic) .partition(partition) - .nodeInfo(nodes.get(0)) + .broker(nodes.get(0)) .size(leaderSize) .path("/tmp/aaa") .build(), @@ -158,14 +158,14 @@ void testResult() { .isLeader(false) .topic(topic) .partition(partition) - .nodeInfo(nodes.get(1)) + .broker(nodes.get(1)) .size(20) .build(), Replica.builder() .isLeader(false) .topic(topic) .partition(partition) - .nodeInfo(nodes.get(2)) + .broker(nodes.get(2)) .size(30) .build()); var results = ReplicaNode.allResult(ClusterInfo.of("fake", nodes, Map.of(), replicas));