use of org.apache.pulsar.common.policies.data.ClusterDataImpl in project pulsar by apache.
the class PulsarWebResource method checkLocalOrGetPeerReplicationCluster.
public static CompletableFuture<ClusterDataImpl> checkLocalOrGetPeerReplicationCluster(PulsarService pulsarService, NamespaceName namespace) {
if (!namespace.isGlobal()) {
return CompletableFuture.completedFuture(null);
}
NamespaceName heartbeatNamespace = pulsarService.getHeartbeatNamespaceV2();
if (namespace.equals(heartbeatNamespace)) {
return CompletableFuture.completedFuture(null);
}
final CompletableFuture<ClusterDataImpl> validationFuture = new CompletableFuture<>();
final String localCluster = pulsarService.getConfiguration().getClusterName();
pulsarService.getPulsarResources().getNamespaceResources().getPoliciesAsync(namespace).thenAccept(policiesResult -> {
if (policiesResult.isPresent()) {
Policies policies = policiesResult.get();
if (policies.replication_clusters.isEmpty()) {
String msg = String.format("Namespace does not have any clusters configured : local_cluster=%s ns=%s", localCluster, namespace.toString());
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
} else if (!policies.replication_clusters.contains(localCluster)) {
ClusterDataImpl ownerPeerCluster = getOwnerFromPeerClusterList(pulsarService, policies.replication_clusters);
if (ownerPeerCluster != null) {
// found a peer that own this namespace
validationFuture.complete(ownerPeerCluster);
return;
}
String msg = String.format("Namespace missing local cluster name in clusters list: local_cluster=%s ns=%s clusters=%s", localCluster, namespace.toString(), policies.replication_clusters);
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
} else {
validationFuture.complete(null);
}
} else {
String msg = String.format("Namespace %s not found", namespace.toString());
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.NOT_FOUND, "Namespace not found"));
}
}).exceptionally(ex -> {
String msg = String.format("Failed to validate global cluster configuration : cluster=%s ns=%s emsg=%s", localCluster, namespace, ex.getMessage());
log.error(msg);
validationFuture.completeExceptionally(new RestException(ex));
return null;
});
return validationFuture;
}
use of org.apache.pulsar.common.policies.data.ClusterDataImpl in project pulsar by apache.
the class TopicsTest method setup.
@Override
@BeforeMethod
protected void setup() throws Exception {
super.internalSetup();
topics = spy(new Topics());
topics.setPulsar(pulsar);
doReturn(TopicDomain.persistent.value()).when(topics).domain();
doReturn("test-app").when(topics).clientAppId();
doReturn(mock(AuthenticationDataHttps.class)).when(topics).clientAuthData();
admin.clusters().createCluster(testLocalCluster, new ClusterDataImpl());
admin.tenants().createTenant(testTenant, new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet(testLocalCluster)));
admin.namespaces().createNamespace(testTenant + "/" + testNamespace, Sets.newHashSet(testLocalCluster));
}
use of org.apache.pulsar.common.policies.data.ClusterDataImpl in project pulsar by apache.
the class TopicsAuthTest method setup.
@Override
@BeforeMethod
protected void setup() throws Exception {
// enable auth&auth and use JWT at broker
conf.setAuthenticationEnabled(true);
conf.setAuthorizationEnabled(true);
conf.getProperties().setProperty("tokenSecretKey", "data:;base64," + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
Set<String> superUserRoles = new HashSet<>();
superUserRoles.add("admin");
conf.setSuperUserRoles(superUserRoles);
Set<String> providers = new HashSet<>();
providers.add(AuthenticationProviderToken.class.getName());
conf.setAuthenticationProviders(providers);
super.internalSetup();
PulsarAdminBuilder pulsarAdminBuilder = PulsarAdmin.builder().serviceHttpUrl(brokerUrl != null ? brokerUrl.toString() : brokerUrlTls.toString()).authentication(AuthenticationToken.class.getName(), ADMIN_TOKEN);
admin = Mockito.spy(pulsarAdminBuilder.build());
admin.clusters().createCluster(testLocalCluster, new ClusterDataImpl());
admin.tenants().createTenant(testTenant, new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet(testLocalCluster)));
admin.namespaces().createNamespace(testTenant + "/" + testNamespace, Sets.newHashSet(testLocalCluster));
admin.namespaces().grantPermissionOnNamespace(testTenant + "/" + testNamespace, "producer", EnumSet.of(AuthAction.produce));
admin.namespaces().grantPermissionOnNamespace(testTenant + "/" + testNamespace, "consumer", EnumSet.of(AuthAction.consume));
}
use of org.apache.pulsar.common.policies.data.ClusterDataImpl in project pulsar by apache.
the class PulsarWorkerService method initInBroker.
@Override
public void initInBroker(ServiceConfiguration brokerConfig, WorkerConfig workerConfig, PulsarResources pulsarResources, InternalConfigurationData internalConf) throws Exception {
String namespace = workerConfig.getPulsarFunctionsNamespace();
String[] a = workerConfig.getPulsarFunctionsNamespace().split("/");
String tenant = a[0];
String cluster = workerConfig.getPulsarFunctionsCluster();
// create tenant for function worker service
try {
NamedEntity.checkName(tenant);
pulsarResources.getTenantResources().createTenant(tenant, new TenantInfoImpl(Sets.newHashSet(workerConfig.getSuperUserRoles()), Sets.newHashSet(cluster)));
LOG.info("Created tenant {} for function worker", tenant);
} catch (AlreadyExistsException e) {
LOG.debug("Failed to create already existing property {} for function worker service", cluster, e);
} catch (IllegalArgumentException e) {
LOG.error("Failed to create property with invalid name {} for function worker service", cluster, e);
throw e;
} catch (Exception e) {
LOG.error("Failed to create property {} for function worker", cluster, e);
throw e;
}
// create cluster for function worker service
try {
NamedEntity.checkName(cluster);
ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(workerConfig.getPulsarWebServiceUrl()).brokerServiceUrl(workerConfig.getPulsarServiceUrl()).build();
pulsarResources.getClusterResources().createCluster(cluster, clusterData);
LOG.info("Created cluster {} for function worker", cluster);
} catch (AlreadyExistsException e) {
LOG.debug("Failed to create already existing cluster {} for function worker service", cluster, e);
} catch (IllegalArgumentException e) {
LOG.error("Failed to create cluster with invalid name {} for function worker service", cluster, e);
throw e;
} catch (Exception e) {
LOG.error("Failed to create cluster {} for function worker service", cluster, e);
throw e;
}
// create namespace for function worker service
try {
Policies policies = createFunctionsNamespacePolicies(workerConfig.getPulsarFunctionsCluster());
policies.bundles = getBundles(brokerConfig.getDefaultNumberOfNamespaceBundles());
pulsarResources.getNamespaceResources().createPolicies(NamespaceName.get(namespace), policies);
LOG.info("Created namespace {} for function worker service", namespace);
} catch (AlreadyExistsException e) {
LOG.debug("Failed to create already existing namespace {} for function worker service", namespace);
} catch (Exception e) {
LOG.error("Failed to create namespace {}", namespace, e);
throw e;
}
URI dlogURI;
try {
// initializing dlog namespace for function worker
if (workerConfig.isInitializedDlogMetadata()) {
String metadataStoreUrl = removeIdentifierFromMetadataURL(internalConf.getMetadataStoreUrl());
dlogURI = WorkerUtils.newDlogNamespaceURI(metadataStoreUrl);
} else {
dlogURI = WorkerUtils.initializeDlogNamespace(internalConf);
}
} catch (IOException ioe) {
LOG.error("Failed to initialize dlog namespace with zookeeper {} at at metadata service uri {} for " + "storing function packages", internalConf.getMetadataStoreUrl(), internalConf.getBookkeeperMetadataServiceUri(), ioe);
throw ioe;
}
init(workerConfig, dlogURI, false);
LOG.info("Function worker service setup completed");
}
use of org.apache.pulsar.common.policies.data.ClusterDataImpl in project pulsar by yahoo.
the class PulsarWorkerService method initInBroker.
@Override
public void initInBroker(ServiceConfiguration brokerConfig, WorkerConfig workerConfig, PulsarResources pulsarResources, InternalConfigurationData internalConf) throws Exception {
String namespace = workerConfig.getPulsarFunctionsNamespace();
String[] a = workerConfig.getPulsarFunctionsNamespace().split("/");
String tenant = a[0];
String cluster = workerConfig.getPulsarFunctionsCluster();
// create tenant for function worker service
try {
NamedEntity.checkName(tenant);
pulsarResources.getTenantResources().createTenant(tenant, new TenantInfoImpl(Sets.newHashSet(workerConfig.getSuperUserRoles()), Sets.newHashSet(cluster)));
LOG.info("Created tenant {} for function worker", tenant);
} catch (AlreadyExistsException e) {
LOG.debug("Failed to create already existing property {} for function worker service", cluster, e);
} catch (IllegalArgumentException e) {
LOG.error("Failed to create property with invalid name {} for function worker service", cluster, e);
throw e;
} catch (Exception e) {
LOG.error("Failed to create property {} for function worker", cluster, e);
throw e;
}
// create cluster for function worker service
try {
NamedEntity.checkName(cluster);
ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(workerConfig.getPulsarWebServiceUrl()).brokerServiceUrl(workerConfig.getPulsarServiceUrl()).build();
pulsarResources.getClusterResources().createCluster(cluster, clusterData);
LOG.info("Created cluster {} for function worker", cluster);
} catch (AlreadyExistsException e) {
LOG.debug("Failed to create already existing cluster {} for function worker service", cluster, e);
} catch (IllegalArgumentException e) {
LOG.error("Failed to create cluster with invalid name {} for function worker service", cluster, e);
throw e;
} catch (Exception e) {
LOG.error("Failed to create cluster {} for function worker service", cluster, e);
throw e;
}
// create namespace for function worker service
try {
Policies policies = createFunctionsNamespacePolicies(workerConfig.getPulsarFunctionsCluster());
policies.bundles = getBundles(brokerConfig.getDefaultNumberOfNamespaceBundles());
pulsarResources.getNamespaceResources().createPolicies(NamespaceName.get(namespace), policies);
LOG.info("Created namespace {} for function worker service", namespace);
} catch (AlreadyExistsException e) {
LOG.debug("Failed to create already existing namespace {} for function worker service", namespace);
} catch (Exception e) {
LOG.error("Failed to create namespace {}", namespace, e);
throw e;
}
URI dlogURI = null;
if (brokerConfig.isMetadataStoreBackedByZookeeper()) {
try {
// initializing dlog namespace for function worker
if (workerConfig.isInitializedDlogMetadata()) {
String metadataStoreUrl = removeIdentifierFromMetadataURL(internalConf.getMetadataStoreUrl());
dlogURI = WorkerUtils.newDlogNamespaceURI(metadataStoreUrl);
} else {
dlogURI = WorkerUtils.initializeDlogNamespace(internalConf);
}
} catch (IOException ioe) {
LOG.error("Failed to initialize dlog namespace with zookeeper {} at at metadata service uri {} for " + "storing function packages", internalConf.getMetadataStoreUrl(), internalConf.getBookkeeperMetadataServiceUri(), ioe);
throw ioe;
}
}
init(workerConfig, dlogURI, false);
LOG.info("Function worker service setup completed");
}
Aggregations