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

[PulsarAdmin] Lookup to async #6569

Merged
merged 1 commit into from
Mar 21, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,43 @@
*/
package org.apache.pulsar.client.admin;

import java.util.concurrent.CompletableFuture;

/**
* This is an interface class to allow using command line tool to quickly lookup the broker serving the topic.
*/
public interface Lookup {

/**
* Lookup a topic
* Lookup a topic.
*
* @param topic
* @return the broker URL that serves the topic
*/
String lookupTopic(String topic) throws PulsarAdminException;

/**
* Lookup a topic asynchronously.
*
* @param topic
* @return the broker URL that serves the topic
*/
public String lookupTopic(String topic) throws PulsarAdminException;
CompletableFuture<String> lookupTopicAsync(String topic);

/**
* Get a bundle range of a topic
* Get a bundle range of a topic.
*
* @param topic
* @return
* @throws PulsarAdminException
*/
public String getBundleRange(String topic) throws PulsarAdminException;
String getBundleRange(String topic) throws PulsarAdminException;

/**
* Get a bundle range of a topic asynchronously.
*
* @param topic
* @return
*/
CompletableFuture<String> getBundleRangeAsync(String topic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.client.admin.internal;

import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;

import org.apache.pulsar.client.admin.Lookup;
Expand All @@ -26,6 +27,11 @@
import org.apache.pulsar.common.lookup.data.LookupData;
import org.apache.pulsar.common.naming.TopicName;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class LookupImpl extends BaseResource implements Lookup {

private final WebTarget v2lookup;
Expand All @@ -39,37 +45,77 @@ public LookupImpl(WebTarget web, Authentication auth, boolean useTls, long readT

@Override
public String lookupTopic(String topic) throws PulsarAdminException {
TopicName topicName = TopicName.get(topic);
String prefix = topicName.isV2() ? "/topic" : "/destination";
WebTarget target = v2lookup.path(prefix).path(topicName.getLookupName());

try {
return doTopicLookup(target);
} catch (Exception e) {
throw getApiException(e);
return lookupTopicAsync(topic).get(this.readTimeoutMs, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
throw (PulsarAdminException) e.getCause();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PulsarAdminException(e);
} catch (TimeoutException e) {
throw new PulsarAdminException.TimeoutException(e);
}
}

@Override
public String getBundleRange(String topic) throws PulsarAdminException {
public CompletableFuture<String> lookupTopicAsync(String topic) {
TopicName topicName = TopicName.get(topic);
String prefix = topicName.isV2() ? "/topic" : "/destination";
WebTarget target = v2lookup.path(prefix).path(topicName.getLookupName()).path("bundle");
WebTarget path = v2lookup.path(prefix).path(topicName.getLookupName());

final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<LookupData>() {
@Override
public void completed(LookupData lookupData) {
if (useTls) {
future.complete(lookupData.getBrokerUrlTls());
} else {
future.complete(lookupData.getBrokerUrl());
}
}

@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}

@Override
public String getBundleRange(String topic) throws PulsarAdminException {
try {
return request(target).get(String.class);
} catch (Exception e) {
throw getApiException(e);
return getBundleRangeAsync(topic).get(this.readTimeoutMs, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
throw (PulsarAdminException) e.getCause();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PulsarAdminException(e);
} catch (TimeoutException e) {
throw new PulsarAdminException.TimeoutException(e);
}
}

private String doTopicLookup(WebTarget lookupResource) throws PulsarAdminException {
LookupData lookupData = request(lookupResource).get(LookupData.class);
if (useTls) {
return lookupData.getBrokerUrlTls();
} else {
return lookupData.getBrokerUrl();
}
@Override
public CompletableFuture<String> getBundleRangeAsync(String topic) {
TopicName topicName = TopicName.get(topic);
String prefix = topicName.isV2() ? "/topic" : "/destination";
WebTarget path = v2lookup.path(prefix).path(topicName.getLookupName()).path("bundle");
final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<String>() {
@Override
public void completed(String bundleRange) {
future.complete(bundleRange);
}

@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}

}