com.playtika.shepherd.KafkaPullFarm Maven / Gradle / Ivy
package com.playtika.shepherd;
import com.playtika.shepherd.common.PastureListener;
import com.playtika.shepherd.common.pull.Farm;
import com.playtika.shepherd.common.pull.Herd;
import com.playtika.shepherd.common.pull.Pasture;
import com.playtika.shepherd.common.pull.Shepherd;
import com.playtika.shepherd.inernal.PastureShepherd;
import com.playtika.shepherd.inernal.PastureShepherdBuilder;
import com.playtika.shepherd.inernal.Population;
import com.playtika.shepherd.serde.SerDe;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import static com.playtika.shepherd.inernal.CheckedHerd.checked;
import static com.playtika.shepherd.serde.SerDeUtils.BYTE_BUFFER_DE_SER;
import static com.playtika.shepherd.serde.SerDeUtils.getSerDe;
public class KafkaPullFarm implements Farm {
private final String bootstrapServers;
private final Map properties;
public KafkaPullFarm(String bootstrapServers) {
this(bootstrapServers, Map.of());
}
public KafkaPullFarm(String bootstrapServers, Map properties) {
this.bootstrapServers = bootstrapServers;
this.properties = properties;
}
@Override
public Pasture addPasture(Herd herd, PastureListener pastureListener) {
return addBreedingPasture(herd, BYTE_BUFFER_DE_SER, pastureListener);
}
@Override
public Pasture addBreedingPasture(Herd herd, Class breedClass, PastureListener pastureListener) {
return addBreedingPasture(herd, getSerDe(breedClass), pastureListener);
}
private Pasture addBreedingPasture(Herd herd, SerDe serDe, PastureListener pastureListener){
PullHerd pullHerd = new PullHerd<>(herd, serDe, pastureListener);
PastureShepherd pastureShepherd = new PastureShepherdBuilder()
.setBootstrapServers(bootstrapServers)
.setGroupId(herd.getName())
.setProperties(properties)
.setRebalanceListener(pullHerd)
.setHerd(checked(pullHerd))
.build();
pullHerd.setPastureShepherd(pastureShepherd);
return pullHerd;
}
static class PullHerd implements com.playtika.shepherd.inernal.Herd, Pasture, Shepherd, PastureListener{
private final Herd herd;
private final SerDe serDe;
private final PastureListener pastureListener;
private PastureShepherd pastureShepherd;
PullHerd(Herd herd, SerDe serDe, PastureListener pastureListener) {
this.herd = herd;
this.serDe = serDe;
this.pastureListener = pastureListener;
}
@Override
public Population getPopulation() {
Herd.Population population = herd.getPopulation();
return new Population(serDe.serialize(List.of(population.population())), population.version());
}
@Override
public void reset() {
herd.reset();
}
@Override
public Shepherd getShepherd() {
return this;
}
@Override
public void start() {
pastureShepherd.start();
}
@Override
public void close(Duration timeout) {
pastureShepherd.stop(timeout.toMillis());
}
@Override
public void rebalanceHerd() {
if(pastureShepherd.isLeaderElected()){
pastureShepherd.setNeedsReconfigRebalance();
}
}
@Override
public void assigned(List population, long version, int generation, boolean isLeader) {
pastureListener.assigned(serDe.deserialize(population), version, generation, isLeader);
}
@Override
public void cleanup() {
pastureListener.cleanup();
}
public void setPastureShepherd(PastureShepherd pastureShepherd) {
this.pastureShepherd = pastureShepherd;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy