use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class MembershipManagerTest method testCheckFailuresNoFailures.
@Test
public void testCheckFailuresNoFailures() throws Exception {
SchedulerManager schedulerManager = mock(SchedulerManager.class);
PulsarClient pulsarClient = mock(PulsarClient.class);
ReaderBuilder readerBuilder = mock(ReaderBuilder.class);
doReturn(readerBuilder).when(pulsarClient).newReader();
doReturn(readerBuilder).when(readerBuilder).topic(anyString());
doReturn(readerBuilder).when(readerBuilder).startMessageId(any());
doReturn(mock(Reader.class)).when(readerBuilder).create();
FunctionRuntimeManager functionRuntimeManager = spy(new FunctionRuntimeManager(workerConfig, pulsarClient, mock(Namespace.class), mock(MembershipManager.class)));
FunctionMetaDataManager functionMetaDataManager = mock(FunctionMetaDataManager.class);
MembershipManager membershipManager = spy(new MembershipManager(workerConfig, mock(PulsarClient.class)));
List<MembershipManager.WorkerInfo> workerInfoList = new LinkedList<>();
workerInfoList.add(MembershipManager.WorkerInfo.of("worker-1", "host-1", 8000));
workerInfoList.add(MembershipManager.WorkerInfo.of("worker-2", "host-2", 8001));
Mockito.doReturn(workerInfoList).when(membershipManager).getCurrentMembership();
Function.FunctionMetaData function1 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setTenant("test-tenant").setNamespace("test-namespace").setName("func-1")).build();
Function.FunctionMetaData function2 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setTenant("test-tenant").setNamespace("test-namespace").setName("func-2")).build();
List<Function.FunctionMetaData> metaDataList = new LinkedList<>();
metaDataList.add(function1);
metaDataList.add(function2);
Mockito.doReturn(metaDataList).when(functionMetaDataManager).getAllFunctionMetaData();
Function.Assignment assignment1 = Function.Assignment.newBuilder().setWorkerId("worker-1").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()).build();
Function.Assignment assignment2 = Function.Assignment.newBuilder().setWorkerId("worker-2").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build()).build();
// add existing assignments
functionRuntimeManager.setAssignment(assignment1);
functionRuntimeManager.setAssignment(assignment2);
membershipManager.checkFailures(functionMetaDataManager, functionRuntimeManager, schedulerManager);
verify(schedulerManager, times(0)).schedule();
verify(functionRuntimeManager, times(0)).removeAssignments(any());
Assert.assertEquals(membershipManager.unsignedFunctionDurations.size(), 0);
}
use of org.apache.pulsar.client.api.PulsarClient 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);
}
});
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class SampleCryptoProducer method main.
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
class RawFileKeyReader implements CryptoKeyReader {
String publicKeyFile = "";
String privateKeyFile = "";
RawFileKeyReader(String pubKeyFile, String privKeyFile) {
publicKeyFile = pubKeyFile;
privateKeyFile = privKeyFile;
}
@Override
public EncryptionKeyInfo getPublicKey(String keyName, Map<String, String> keyMeta) {
EncryptionKeyInfo keyInfo = new EncryptionKeyInfo();
try {
// Read the public key from the file
keyInfo.setKey(Files.readAllBytes(Paths.get(publicKeyFile)));
} catch (IOException e) {
log.error("Failed to read public key from file {}", publicKeyFile, e);
}
return keyInfo;
}
@Override
public EncryptionKeyInfo getPrivateKey(String keyName, Map<String, String> keyMeta) {
EncryptionKeyInfo keyInfo = new EncryptionKeyInfo();
try {
// Read the private key from the file
keyInfo.setKey(Files.readAllBytes(Paths.get(privateKeyFile)));
} catch (IOException e) {
log.error("Failed to read private key from file {}", privateKeyFile, e);
}
return keyInfo;
}
}
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl("http://127.0.0.1:8080").build();
// Setup the CryptoKeyReader with the file name where public/private key is kept
Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic").cryptoKeyReader(new RawFileKeyReader("test_ecdsa_pubkey.pem", "test_ecdsa_privkey.pem")).addEncryptionKey("myappkey").create();
for (int i = 0; i < 10; i++) {
producer.send("my-message".getBytes());
}
pulsarClient.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class SampleProducer method main.
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
PulsarClient client = PulsarClient.builder().serviceUrl("http://localhost:6650").build();
Producer<byte[]> producer = client.newProducer().topic("persistent://my-property/use/my-ns/my-topic").create();
for (int i = 0; i < 10; i++) {
producer.send("my-message".getBytes());
}
client.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class ContinuousProducer method main.
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl("http://127.0.0.1:8080").build();
Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic").create();
while (true) {
try {
producer.send("my-message".getBytes());
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
break;
}
}
pulsarClient.close();
}
Aggregations