Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to get replica size #83

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/java/org/astraea/producer/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ public interface Sender<Key, Value> {

Sender<Key, Value> topic(String topic);

/**
* define the data route if you don't want to partitioner to decide the target.
*
* @param partition target partition
* @return this sender
*/
Sender<Key, Value> partition(int partition);

Sender<Key, Value> timestamp(long timestamp);

Sender<Key, Value> headers(Collection<Header> headers);

/**
* send data to servers. This operation is running in background. You have to call {@link
* CompletionStage#toCompletableFuture()} to wait response of servers.
*
* @return an async operation stage.
*/
CompletionStage<Metadata> run();
}
52 changes: 33 additions & 19 deletions app/src/main/java/org/astraea/topic/TopicAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ public Map<TopicPartition, List<Replica>> replicas(Set<String> topics) {
replicaInfo == null
? -1
: replicaInfo.offsetLag(),
replicaInfo == null
? -1
: replicaInfo.size(),
topicPartitionInfo.leader().id()
== node.id(),
topicPartitionInfo.isr().contains(node),
Expand Down Expand Up @@ -441,28 +444,57 @@ public String toString() {
class Replica {
public final int broker;
public final long lag;
public final long size;
public final boolean leader;
public final boolean inSync;
public final boolean isFuture;
public final String path;

public Replica(
int broker, long lag, boolean leader, boolean inSync, boolean isFuture, String path) {
int broker,
long lag,
long size,
boolean leader,
boolean inSync,
boolean isFuture,
String path) {
this.broker = broker;
this.lag = lag;
this.size = size;
this.leader = leader;
this.inSync = inSync;
this.isFuture = isFuture;
this.path = path;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Replica replica = (Replica) o;
return broker == replica.broker
&& lag == replica.lag
&& size == replica.size
&& leader == replica.leader
&& inSync == replica.inSync
&& isFuture == replica.isFuture
&& path.equals(replica.path);
}

@Override
public int hashCode() {
return Objects.hash(broker, lag, size, leader, inSync, isFuture, path);
}

@Override
public String toString() {
return "Replica{"
+ "broker="
+ broker
+ ", lag="
+ lag
+ ", size="
+ size
+ ", leader="
+ leader
+ ", inSync="
Expand All @@ -474,23 +506,5 @@ public String toString() {
+ '\''
+ '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Replica replica = (Replica) o;
return broker == replica.broker
&& lag == replica.lag
&& leader == replica.leader
&& inSync == replica.inSync
&& isFuture == replica.isFuture
&& Objects.equals(path, replica.path);
}

@Override
public int hashCode() {
return Objects.hash(broker, lag, leader, inSync, isFuture, path);
}
}
}
34 changes: 34 additions & 0 deletions app/src/test/java/org/astraea/topic/TopicAdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.astraea.Utils;
import org.astraea.producer.Producer;
import org.astraea.service.RequireBrokerCluster;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -86,4 +88,36 @@ void testReassign() throws IOException, InterruptedException {
});
}
}

@Test
void testReplicaSize() throws IOException, ExecutionException, InterruptedException {
var topicName = "testReplicaSize";
try (var topicAdmin = TopicAdmin.of(bootstrapServers());
var producer = Producer.builder().brokers(bootstrapServers()).build()) {
producer.sender().topic(topicName).key(new byte[100]).run().toCompletableFuture().get();
var originSize =
topicAdmin
.replicas(Set.of(topicName))
.entrySet()
.iterator()
.next()
.getValue()
.get(0)
.size;

// add data again
producer.sender().topic(topicName).key(new byte[100]).run().toCompletableFuture().get();

var newSize =
topicAdmin
.replicas(Set.of(topicName))
.entrySet()
.iterator()
.next()
.getValue()
.get(0)
.size;
Assertions.assertTrue(newSize > originSize);
}
}
}