-
Notifications
You must be signed in to change notification settings - Fork 214
/
TestPubSubFactory.java
93 lines (81 loc) · 3.94 KB
/
TestPubSubFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.pubsub;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.internal.utils.ddata.DistributedData;
import org.eclipse.ditto.internal.utils.pubsub.config.PubSubConfig;
import org.eclipse.ditto.internal.utils.pubsub.ddata.DDataReader;
import org.eclipse.ditto.internal.utils.pubsub.ddata.Hashes;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.internal.utils.pubsub.extractors.PubSubTopicExtractor;
import org.apache.pekko.actor.ActorContext;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.cluster.ddata.ORMultiMap;
import org.apache.pekko.cluster.ddata.Replicator;
/**
* Pub-sub factory for tests. Messages are strings. Topics of a message are its prefixes.
*/
public final class TestPubSubFactory extends AbstractPubSubFactory<Acknowledgement> implements Hashes {
private static final DDataProvider PROVIDER = DDataProvider.of("dc-default");
private static final LiteralDDataProvider ACKS_PROVIDER = LiteralDDataProvider.of("dc-default", "acks");
private final Collection<Integer> seeds;
private TestPubSubFactory(final ActorContext context,
final PubSubTopicExtractor<Acknowledgement> topicExtractor,
final AckExtractor<Acknowledgement> ackExtractor,
final DistributedAcks distributedAcks) {
super(context, context.system(), Acknowledgement.class, topicExtractor, PROVIDER, ackExtractor,
distributedAcks);
final PubSubConfig config = PubSubConfig.of(context.system().settings().config().getConfig("ditto.pubsub"));
seeds = Hashes.digestStringsToIntegers(config.getSeed(), Hashes.HASH_FAMILY_SIZE);
}
static DistributedAcks startDistributedAcks(final ActorContext context) {
return DistributedAcksImpl.create(context, context.system(), "dc-default", ACKS_PROVIDER);
}
static TestPubSubFactory of(final ActorContext context, final AckExtractor<Acknowledgement> ackExtractor,
final DistributedAcks distributedAcks) {
return new TestPubSubFactory(context, TestPubSubFactory::getPrefixes, ackExtractor,
distributedAcks);
}
/**
* @return subscribers of a topic in the distributed data.
*/
Collection<ActorRef> getSubscribers() {
final DDataReader<ActorRef, String> reader = ddata.getReader();
return IntStream.range(0, reader.getNumberOfShards())
.mapToObj(i -> ((DistributedData<ORMultiMap<ActorRef,String>>) reader)
.get(reader.getKey(i), (Replicator.ReadConsistency) Replicator.readLocal())
.toCompletableFuture()
.thenApply(future -> future.map(ORMultiMap::getEntries)
.map(Map::keySet)
.orElse(Collections.emptySet())
).join()
).flatMap(Set::stream)
.collect(Collectors.toSet());
}
@Override
public Collection<Integer> getSeeds() {
return seeds;
}
private static Collection<String> getPrefixes(final Acknowledgement acknowledgement) {
final String string = acknowledgement.getLabel().toString();
return IntStream.range(0, string.length())
.mapToObj(i -> string.substring(0, i + 1))
.toList();
}
}