use of org.apache.pulsar.metadata.api.GetResult in project pulsar by apache.
the class PulsarLedgerUnderreplicationManager method getLedgerUnreplicationInfo.
@Override
public UnderreplicatedLedger getLedgerUnreplicationInfo(long ledgerId) throws ReplicationException.UnavailableException {
try {
String path = getUrLedgerPath(ledgerId);
Optional<GetResult> optRes = store.get(path).get();
if (!optRes.isPresent()) {
if (log.isDebugEnabled()) {
log.debug("Ledger: {} is not marked underreplicated", ledgerId);
}
return null;
}
byte[] data = optRes.get().getValue();
UnderreplicatedLedgerFormat.Builder builder = UnderreplicatedLedgerFormat.newBuilder();
TextFormat.merge(new String(data, UTF_8), builder);
UnderreplicatedLedgerFormat underreplicatedLedgerFormat = builder.build();
PulsarUnderreplicatedLedger underreplicatedLedger = new PulsarUnderreplicatedLedger(ledgerId);
List<String> replicaList = underreplicatedLedgerFormat.getReplicaList();
long ctime = (underreplicatedLedgerFormat.hasCtime() ? underreplicatedLedgerFormat.getCtime() : UnderreplicatedLedger.UNASSIGNED_CTIME);
underreplicatedLedger.setCtime(ctime);
underreplicatedLedger.setReplicaList(replicaList);
return underreplicatedLedger;
} catch (ExecutionException ee) {
throw new ReplicationException.UnavailableException("Error contacting with metadata store", ee);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new ReplicationException.UnavailableException("Interrupted while connecting metadata store", ie);
} catch (TextFormat.ParseException pe) {
throw new ReplicationException.UnavailableException("Error parsing proto message", pe);
}
}
use of org.apache.pulsar.metadata.api.GetResult in project pulsar by yahoo.
the class NamespaceServiceTest method testUnloadNamespaceBundleWithStuckTopic.
/**
* It verifies that unloading bundle will timeout and will not hung even if one of the topic-unloading stuck.
*
* @throws Exception
*/
@Test(timeOut = 6000)
public void testUnloadNamespaceBundleWithStuckTopic() throws Exception {
final String topicName = "persistent://my-property/use/my-ns/my-topic1";
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscriber-name").subscribe();
ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>> topics = pulsar.getBrokerService().getTopics();
Topic spyTopic = spy(topics.get(topicName).get().get());
topics.clear();
CompletableFuture<Optional<Topic>> topicFuture = CompletableFuture.completedFuture(Optional.of(spyTopic));
// add mock topic
topics.put(topicName, topicFuture);
// return uncompleted future as close-topic result.
doAnswer((Answer<CompletableFuture<Void>>) invocation -> new CompletableFuture<Void>()).when(spyTopic).close(false);
NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(TopicName.get(topicName));
// try to unload bundle whose topic will be stuck
pulsar.getNamespaceService().unloadNamespaceBundle(bundle, 1, TimeUnit.SECONDS).join();
Optional<GetResult> res = this.pulsar.getLocalMetadataStore().get(ServiceUnitUtils.path(bundle)).join();
assertFalse(res.isPresent());
consumer.close();
}
use of org.apache.pulsar.metadata.api.GetResult in project pulsar by yahoo.
the class NamespaceServiceTest method testUnloadNamespaceBundleFailure.
@Test
public void testUnloadNamespaceBundleFailure() throws Exception {
final String topicName = "persistent://my-property/use/my-ns/my-topic1";
pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscriber-name").subscribe();
ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>> topics = pulsar.getBrokerService().getTopics();
Topic spyTopic = spy(topics.get(topicName).get().get());
topics.clear();
CompletableFuture<Optional<Topic>> topicFuture = CompletableFuture.completedFuture(Optional.of(spyTopic));
// add mock topic
topics.put(topicName, topicFuture);
doAnswer((Answer<CompletableFuture<Void>>) invocation -> {
CompletableFuture<Void> result = new CompletableFuture<>();
result.completeExceptionally(new RuntimeException("first time failed"));
return result;
}).when(spyTopic).close(false);
NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(TopicName.get(topicName));
pulsar.getNamespaceService().unloadNamespaceBundle(bundle).join();
Optional<GetResult> res = this.pulsar.getLocalMetadataStore().get(ServiceUnitUtils.path(bundle)).join();
assertFalse(res.isPresent());
}
use of org.apache.pulsar.metadata.api.GetResult in project pulsar by yahoo.
the class MetadataStoreBatchingTest method testBatching.
@Test(dataProvider = "impl")
public void testBatching(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().batchingEnabled(true).batchingMaxDelayMillis(1_000).build());
String key1 = newKey();
store.put(key1, new byte[0], Optional.empty()).join();
String key2 = newKey();
CompletableFuture<Optional<GetResult>> f1 = store.get(key1);
CompletableFuture<Optional<GetResult>> f2 = store.get(key2);
Optional<GetResult> r1 = f1.join();
Optional<GetResult> r2 = f2.join();
assertTrue(r1.isPresent());
assertFalse(r2.isPresent());
}
use of org.apache.pulsar.metadata.api.GetResult in project pulsar by yahoo.
the class MetadataStoreTest method testConcurrentPutGetOneKey.
@Test(dataProvider = "impl")
public void testConcurrentPutGetOneKey(String provider, Supplier<String> urlSupplier) throws Exception {
MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
byte[] data = new byte[] { 0 };
String path = newKey();
int maxValue = 100;
store.put(path, data, Optional.of(-1L)).join();
AtomicInteger successWrites = new AtomicInteger(0);
Runnable task = new Runnable() {
@SneakyThrows
@Override
public void run() {
byte value;
while (true) {
GetResult readResult = store.get(path).get().get();
value = (byte) (readResult.getValue()[0] + 1);
if (value <= maxValue) {
CompletableFuture<Void> putResult = store.put(path, new byte[] { value }, Optional.of(readResult.getStat().getVersion())).thenRun(successWrites::incrementAndGet);
try {
putResult.get();
} catch (Exception ignore) {
}
log.info("Put value {} success:{}. ", value, !putResult.isCompletedExceptionally());
} else {
break;
}
}
}
};
CompletableFuture<Void> t1 = CompletableFuture.completedFuture(null).thenRunAsync(task);
CompletableFuture<Void> t2 = CompletableFuture.completedFuture(null).thenRunAsync(task);
task.run();
t1.join();
t2.join();
assertFalse(t1.isCompletedExceptionally());
assertFalse(t2.isCompletedExceptionally());
assertEquals(successWrites.get(), maxValue);
assertEquals(store.get(path).get().get().getValue()[0], maxValue);
}
Aggregations