Search in sources :

Example 1 with ClusterData

use of org.apache.pulsar.common.policies.data.ClusterData in project incubator-pulsar by apache.

the class PulsarStandaloneStarter method start.

void start() throws Exception {
    if (config == null) {
        System.exit(1);
    }
    log.debug("--- setup PulsarStandaloneStarter ---");
    if (!onlyBroker) {
        // Start LocalBookKeeper
        bkEnsemble = new LocalBookkeeperEnsemble(numOfBk, zkPort, bkPort, zkDir, bkDir, wipeData, config.getAdvertisedAddress());
        bkEnsemble.startStandalone();
    }
    if (noBroker) {
        return;
    }
    // load aspectj-weaver agent for instrumentation
    AgentLoader.loadAgentClass(Agent.class.getName(), null);
    // initialize the functions worker
    if (!noFunctionsWorker) {
        WorkerConfig workerConfig;
        if (isBlank(fnWorkerConfigFile)) {
            workerConfig = new WorkerConfig();
        } else {
            workerConfig = WorkerConfig.load(fnWorkerConfigFile);
        }
        // worker talks to local broker
        workerConfig.setPulsarServiceUrl("pulsar://127.0.0.1:" + config.getBrokerServicePort());
        workerConfig.setPulsarWebServiceUrl("http://127.0.0.1:" + config.getWebServicePort());
        String hostname = ServiceConfigurationUtils.getDefaultOrConfiguredAddress(config.getAdvertisedAddress());
        workerConfig.setWorkerHostname(hostname);
        workerConfig.setWorkerId("c-" + config.getClusterName() + "-fw-" + hostname + "-" + workerConfig.getWorkerPort());
        fnWorkerService = new WorkerService(workerConfig);
    }
    // Start Broker
    broker = new PulsarService(config, Optional.ofNullable(fnWorkerService));
    broker.start();
    // Create a sample namespace
    URL webServiceUrl = new URL(String.format("http://%s:%d", config.getAdvertisedAddress(), config.getWebServicePort()));
    final String brokerServiceUrl = String.format("pulsar://%s:%d", config.getAdvertisedAddress(), config.getBrokerServicePort());
    admin = new PulsarAdmin(webServiceUrl, config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters());
    final String property = "sample";
    final String cluster = config.getClusterName();
    final String globalCluster = "global";
    final String namespace = property + "/" + cluster + "/ns1";
    try {
        ClusterData clusterData = new ClusterData(webServiceUrl.toString(), null, /* serviceUrlTls */
        brokerServiceUrl, null);
        if (!admin.clusters().getClusters().contains(cluster)) {
            admin.clusters().createCluster(cluster, clusterData);
        } else {
            admin.clusters().updateCluster(cluster, clusterData);
        }
        // Create marker for "global" cluster
        if (!admin.clusters().getClusters().contains(globalCluster)) {
            admin.clusters().createCluster(globalCluster, new ClusterData(null, null));
        }
        if (!admin.properties().getProperties().contains(property)) {
            admin.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList(config.getSuperUserRoles()), Sets.newHashSet(cluster)));
        }
        if (!admin.namespaces().getNamespaces(property).contains(namespace)) {
            admin.namespaces().createNamespace(namespace);
        }
    } catch (PulsarAdminException e) {
        log.info(e.getMessage());
    }
    if (null != fnWorkerService) {
        fnWorkerService.start();
    }
    log.debug("--- setup completed ---");
}
Also used : Agent(org.aspectj.weaver.loadtime.Agent) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) WorkerService(org.apache.pulsar.functions.worker.WorkerService) URL(java.net.URL)

Example 2 with ClusterData

use of org.apache.pulsar.common.policies.data.ClusterData in project incubator-pulsar by apache.

the class RawReaderTest method setup.

@BeforeMethod
@Override
public void setup() throws Exception {
    super.internalSetup();
    admin.clusters().createCluster("use", new ClusterData("http://127.0.0.1:" + BROKER_WEBSERVICE_PORT));
    admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
    admin.namespaces().createNamespace("my-property/use/my-ns");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with ClusterData

use of org.apache.pulsar.common.policies.data.ClusterData in project incubator-pulsar by apache.

the class PulsarAdminToolTest method clusters.

@Test
void clusters() throws Exception {
    PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
    Clusters mockClusters = mock(Clusters.class);
    when(admin.clusters()).thenReturn(mockClusters);
    CmdClusters clusters = new CmdClusters(admin);
    clusters.run(split("list"));
    verify(mockClusters).getClusters();
    clusters.run(split("get use"));
    verify(mockClusters).getCluster("use");
    clusters.run(split("create use --url http://my-service.url:8080"));
    verify(mockClusters).createCluster("use", new ClusterData("http://my-service.url:8080", null));
    clusters.run(split("update use --url http://my-service.url:8080"));
    verify(mockClusters).updateCluster("use", new ClusterData("http://my-service.url:8080", null));
    clusters.run(split("delete use"));
    verify(mockClusters).deleteCluster("use");
    clusters.run(split("list-failure-domains use"));
    verify(mockClusters).getFailureDomains("use");
    clusters.run(split("get-failure-domain use --domain-name domain"));
    verify(mockClusters).getFailureDomain("use", "domain");
    clusters.run(split("create-failure-domain use --domain-name domain --broker-list b1"));
    FailureDomain domain = new FailureDomain();
    domain.setBrokers(Sets.newHashSet("b1"));
    verify(mockClusters).createFailureDomain("use", "domain", domain);
    clusters.run(split("update-failure-domain use --domain-name domain --broker-list b1"));
    verify(mockClusters).updateFailureDomain("use", "domain", domain);
    clusters.run(split("delete-failure-domain use --domain-name domain"));
    verify(mockClusters).deleteFailureDomain("use", "domain");
    // Re-create CmdClusters to avoid a issue.
    // See https://github.com/cbeust/jcommander/issues/271
    clusters = new CmdClusters(admin);
    clusters.run(split("create my-cluster --url http://my-service.url:8080 --url-secure https://my-service.url:4443"));
    verify(mockClusters).createCluster("my-cluster", new ClusterData("http://my-service.url:8080", "https://my-service.url:4443"));
    clusters.run(split("update my-cluster --url http://my-service.url:8080 --url-secure https://my-service.url:4443"));
    verify(mockClusters).updateCluster("my-cluster", new ClusterData("http://my-service.url:8080", "https://my-service.url:4443"));
    clusters.run(split("delete my-cluster"));
    verify(mockClusters).deleteCluster("my-cluster");
    clusters.run(split("update-peer-clusters my-cluster --peer-clusters c1,c2"));
    verify(mockClusters).updatePeerClusterNames("my-cluster", Sets.newLinkedHashSet(Lists.newArrayList("c1", "c2")));
    clusters.run(split("get-peer-clusters my-cluster"));
    verify(mockClusters).getPeerClusterNames("my-cluster");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Clusters(org.apache.pulsar.client.admin.Clusters) FailureDomain(org.apache.pulsar.common.policies.data.FailureDomain) Test(org.testng.annotations.Test)

Example 4 with ClusterData

use of org.apache.pulsar.common.policies.data.ClusterData in project incubator-pulsar by apache.

the class BrokerService method getReplicationClient.

public PulsarClient getReplicationClient(String cluster) {
    PulsarClient client = replicationClients.get(cluster);
    if (client != null) {
        return client;
    }
    return replicationClients.computeIfAbsent(cluster, key -> {
        try {
            String path = PulsarWebResource.path("clusters", cluster);
            ClusterData data = this.pulsar.getConfigurationCache().clustersCache().get(path).orElseThrow(() -> new KeeperException.NoNodeException(path));
            ClientBuilder clientBuilder = PulsarClient.builder().enableTcpNoDelay(false).connectionsPerBroker(pulsar.getConfiguration().getReplicationConnectionsPerBroker()).statsInterval(0, TimeUnit.SECONDS);
            if (pulsar.getConfiguration().isAuthenticationEnabled()) {
                clientBuilder.authentication(pulsar.getConfiguration().getBrokerClientAuthenticationPlugin(), pulsar.getConfiguration().getBrokerClientAuthenticationParameters());
            }
            if (pulsar.getConfiguration().isReplicationTlsEnabled()) {
                clientBuilder.serviceUrl(isNotBlank(data.getBrokerServiceUrlTls()) ? data.getBrokerServiceUrlTls() : data.getServiceUrlTls()).enableTls(true).tlsTrustCertsFilePath(pulsar.getConfiguration().getBrokerClientTrustCertsFilePath()).allowTlsInsecureConnection(pulsar.getConfiguration().isTlsAllowInsecureConnection());
            } else {
                clientBuilder.serviceUrl(isNotBlank(data.getBrokerServiceUrl()) ? data.getBrokerServiceUrl() : data.getServiceUrl());
            }
            // Share all the IO threads across broker and client connections
            ClientConfigurationData conf = ((ClientBuilderImpl) clientBuilder).getClientConfigurationData();
            return new PulsarClientImpl(conf, workerGroup);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) ClientBuilderImpl(org.apache.pulsar.client.impl.ClientBuilderImpl) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) KeeperException(org.apache.zookeeper.KeeperException) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) IOException(java.io.IOException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) KeeperException(org.apache.zookeeper.KeeperException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder)

Example 5 with ClusterData

use of org.apache.pulsar.common.policies.data.ClusterData in project incubator-pulsar by apache.

the class ClustersBase method updateCluster.

@POST
@Path("/{cluster}")
@ApiOperation(value = "Update the configuration for a cluster.", notes = "This operation requires Pulsar super-user privileges.")
@ApiResponses(value = { @ApiResponse(code = 204, message = "Cluster has been updated"), @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public void updateCluster(@PathParam("cluster") String cluster, ClusterData clusterData) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    try {
        String clusterPath = path("clusters", cluster);
        Stat nodeStat = new Stat();
        byte[] content = globalZk().getData(clusterPath, null, nodeStat);
        ClusterData currentClusterData = null;
        if (content.length > 0) {
            currentClusterData = jsonMapper().readValue(content, ClusterData.class);
            // only update cluster-url-data and not overwrite other metadata such as peerClusterNames
            currentClusterData.update(clusterData);
        } else {
            currentClusterData = clusterData;
        }
        // Write back the new updated ClusterData into zookeeper
        globalZk().setData(clusterPath, jsonMapper().writeValueAsBytes(currentClusterData), nodeStat.getVersion());
        globalZkCache().invalidate(clusterPath);
        log.info("[{}] Updated cluster {}", clientAppId(), cluster);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update cluster {}: Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ClusterData (org.apache.pulsar.common.policies.data.ClusterData)53 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)30 Test (org.testng.annotations.Test)23 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)13 BeforeMethod (org.testng.annotations.BeforeMethod)13 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)11 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)10 URL (java.net.URL)9 PulsarService (org.apache.pulsar.broker.PulsarService)9 RestException (org.apache.pulsar.broker.web.RestException)8 KeeperException (org.apache.zookeeper.KeeperException)8 IOException (java.io.IOException)7 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)7 URI (java.net.URI)6 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)6 Authentication (org.apache.pulsar.client.api.Authentication)5 Field (java.lang.reflect.Field)4 ExecutionException (java.util.concurrent.ExecutionException)4 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)4 Policies (org.apache.pulsar.common.policies.data.Policies)4