forked from opensource4you/astraea
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
181 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
common/src/main/java/org/astraea/common/metrics/collector/MetricSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.metrics.collector; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.CompletionStage; | ||
import org.astraea.common.FutureUtils; | ||
import org.astraea.common.metrics.BeanObject; | ||
import org.astraea.common.producer.Producer; | ||
import org.astraea.common.producer.ProducerConfigs; | ||
import org.astraea.common.producer.Record; | ||
import org.astraea.common.producer.Serializer; | ||
|
||
public interface MetricSender extends AutoCloseable { | ||
|
||
static MetricSender local() { | ||
return LocalSenderReceiver.of(); | ||
} | ||
|
||
static MetricSender topic(String bootstrapServer) { | ||
var producer = | ||
Producer.builder() | ||
.bootstrapServers(bootstrapServer) | ||
.keySerializer(Serializer.INTEGER) | ||
.valueSerializer(Serializer.BEAN_OBJECT) | ||
.config(ProducerConfigs.ACKS_CONFIG, "0") | ||
.build(); | ||
String METRIC_TOPIC = "__metrics"; | ||
return new MetricSender() { | ||
@Override | ||
public CompletionStage<Void> send(int id, Collection<BeanObject> beans) { | ||
var records = | ||
beans.stream() | ||
.map(bean -> Record.builder().topic(METRIC_TOPIC).key(id).value(bean).build()) | ||
.toList(); | ||
return FutureUtils.sequence( | ||
producer.send(records).stream().map(CompletionStage::toCompletableFuture).toList()) | ||
.thenAccept(ignored -> {}); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
producer.close(); | ||
} | ||
}; | ||
} | ||
|
||
CompletionStage<Void> send(int id, Collection<BeanObject> beans); | ||
|
||
@Override | ||
default void close() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
common/src/main/java/org/astraea/common/metrics/collector/ServerMetricFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.metrics.collector; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.apache.kafka.common.metrics.KafkaMetric; | ||
import org.apache.kafka.common.metrics.MetricsReporter; | ||
import org.apache.kafka.server.telemetry.ClientTelemetry; | ||
import org.apache.kafka.server.telemetry.ClientTelemetryReceiver; | ||
import org.astraea.common.metrics.BeanQuery; | ||
import org.astraea.common.metrics.JndiClient; | ||
|
||
public class ServerMetricFetcher implements MetricsReporter, ClientTelemetry { | ||
private static final String BOOTSTRAP_SERVERS = "server.metric.fetcher.bootstrap.servers"; | ||
private String bootstrapServers; | ||
private int nodeId = -1; | ||
private final AtomicBoolean closed = new AtomicBoolean(false); | ||
private final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<>(1); | ||
|
||
@Override | ||
public void init(List<KafkaMetric> list) {} | ||
|
||
@Override | ||
public void metricChange(KafkaMetric kafkaMetric) {} | ||
|
||
@Override | ||
public void metricRemoval(KafkaMetric kafkaMetric) {} | ||
|
||
@Override | ||
public void close() { | ||
closed.set(true); | ||
queue.offer(true); | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> map) { | ||
if (!map.containsKey(BOOTSTRAP_SERVERS)) | ||
throw new RuntimeException(BOOTSTRAP_SERVERS + " is required"); | ||
if (!map.containsKey("node.id")) throw new RuntimeException("node.id is required"); | ||
this.bootstrapServers = map.get(BOOTSTRAP_SERVERS).toString(); | ||
nodeId = Integer.parseInt(map.get("node.id").toString()); | ||
CompletableFuture.runAsync( | ||
() -> { | ||
MetricSender sender = null; | ||
var lastSent = System.currentTimeMillis(); | ||
try { | ||
while (!closed.get()) { | ||
var done = queue.poll(3, TimeUnit.SECONDS); | ||
if (done == null) done = false; | ||
if (done) return; | ||
if (System.currentTimeMillis() - lastSent <= Duration.ofSeconds(3).toMillis()) | ||
continue; | ||
if (sender == null) sender = MetricSender.topic(bootstrapServers); | ||
var beans = JndiClient.local().beans(BeanQuery.all()); | ||
System.out.println("[CHIA] send " + beans.size() + " beans"); | ||
sender.send(nodeId, beans); | ||
lastSent = System.currentTimeMillis(); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
if (sender != null) sender.close(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public ClientTelemetryReceiver clientReceiver() { | ||
System.out.println("[CHIA] create ClientTelemetryReceiver"); | ||
return (__, ___) -> { | ||
queue.offer(false); | ||
System.out.println("[CHIA] receive ClientTelemetryReceiver"); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters