Skip to content

Commit

Permalink
Pulsar Admin: reduce code duplication - part 2 (apache#12748)
Browse files Browse the repository at this point in the history
  • Loading branch information
eolivelli authored Nov 11, 2021
1 parent b533fe5 commit 538dbaa
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 1,792 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;
Expand All @@ -47,16 +44,7 @@ public BrokersImpl(WebTarget web, Authentication auth, long readTimeoutMs) {

@Override
public List<String> getActiveBrokers(String cluster) throws PulsarAdminException {
try {
return getActiveBrokersAsync(cluster).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);
}
return sync(() -> getActiveBrokersAsync(cluster));
}

@Override
Expand All @@ -80,16 +68,7 @@ public void failed(Throwable throwable) {

@Override
public BrokerInfo getLeaderBroker() throws PulsarAdminException {
try {
return getLeaderBrokerAsync().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);
}
return sync(() -> getLeaderBrokerAsync());
}

@Override
Expand All @@ -114,16 +93,7 @@ public void failed(Throwable throwable) {
@Override
public Map<String, NamespaceOwnershipStatus> getOwnedNamespaces(String cluster, String brokerUrl)
throws PulsarAdminException {
try {
return getOwnedNamespacesAsync(cluster, brokerUrl).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);
}
return sync(() -> getOwnedNamespacesAsync(cluster, brokerUrl));
}

@Override
Expand All @@ -148,17 +118,7 @@ public void failed(Throwable throwable) {

@Override
public void updateDynamicConfiguration(String configName, String configValue) throws PulsarAdminException {
try {
updateDynamicConfigurationAsync(configName, configValue).
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);
}
sync(() -> updateDynamicConfigurationAsync(configName, configValue));
}

@Override
Expand All @@ -170,16 +130,7 @@ public CompletableFuture<Void> updateDynamicConfigurationAsync(String configName

@Override
public void deleteDynamicConfiguration(String configName) throws PulsarAdminException {
try {
deleteDynamicConfigurationAsync(configName).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);
}
sync(() -> deleteDynamicConfigurationAsync(configName));
}

@Override
Expand All @@ -190,16 +141,7 @@ public CompletableFuture<Void> deleteDynamicConfigurationAsync(String configName

@Override
public Map<String, String> getAllDynamicConfigurations() throws PulsarAdminException {
try {
return getAllDynamicConfigurationsAsync().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);
}
return sync(() -> getAllDynamicConfigurationsAsync());
}

@Override
Expand All @@ -223,16 +165,7 @@ public void failed(Throwable throwable) {

@Override
public List<String> getDynamicConfigurationNames() throws PulsarAdminException {
try {
return getDynamicConfigurationNamesAsync().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);
}
return sync(() -> getDynamicConfigurationNamesAsync());
}

@Override
Expand All @@ -256,16 +189,7 @@ public void failed(Throwable throwable) {

@Override
public Map<String, String> getRuntimeConfigurations() throws PulsarAdminException {
try {
return getRuntimeConfigurationsAsync().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);
}
return sync(() -> getRuntimeConfigurationsAsync());
}

@Override
Expand All @@ -289,16 +213,7 @@ public void failed(Throwable throwable) {

@Override
public InternalConfigurationData getInternalConfigurationData() throws PulsarAdminException {
try {
return getInternalConfigurationDataAsync().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);
}
return sync(() -> getInternalConfigurationDataAsync());
}

@Override
Expand All @@ -322,16 +237,7 @@ public void failed(Throwable throwable) {

@Override
public void backlogQuotaCheck() throws PulsarAdminException {
try {
backlogQuotaCheckAsync().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);
}
sync(() -> backlogQuotaCheckAsync());
}

@Override
Expand Down Expand Up @@ -366,16 +272,7 @@ public CompletableFuture<Void> healthcheckAsync() {

@Override
public void healthcheck(TopicVersion topicVersion) throws PulsarAdminException {
try {
healthcheckAsync(topicVersion).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);
}
sync(() -> healthcheckAsync(topicVersion));
}

@Override
Expand Down Expand Up @@ -407,28 +304,24 @@ public void failed(Throwable throwable) {

@Override
public String getVersion() throws PulsarAdminException {
return sync(() -> getVersionAsync());
}

public CompletableFuture<String> getVersionAsync() {
WebTarget path = adminBrokers.path("version");
try {
final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<String>() {
@Override
public void completed(String version) {
future.complete(version);
}

@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future.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);
}

final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<String>() {
@Override
public void completed(String version) {
future.complete(version);
}

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

0 comments on commit 538dbaa

Please sign in to comment.