|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.protocol; |
| 20 | + |
| 21 | +import io.netty.channel.ChannelInitializer; |
| 22 | +import io.netty.channel.socket.SocketChannel; |
| 23 | +import java.net.InetSocketAddress; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import lombok.Cleanup; |
| 32 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 33 | +import org.apache.pulsar.broker.service.BrokerService; |
| 34 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
| 35 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
| 36 | +import org.apache.pulsar.client.api.MessageId; |
| 37 | +import org.apache.pulsar.client.api.PulsarClient; |
| 38 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 39 | +import org.apache.pulsar.client.api.Reader; |
| 40 | +import org.apache.pulsar.common.naming.TopicName; |
| 41 | +import org.apache.pulsar.common.policies.data.ClusterData; |
| 42 | +import org.apache.pulsar.common.policies.data.TenantInfo; |
| 43 | + |
| 44 | +public class PulsarClientBasedHandler implements ProtocolHandler { |
| 45 | + |
| 46 | + static final String PROTOCOL = "test"; |
| 47 | + |
| 48 | + private String topic; |
| 49 | + private int partitions; |
| 50 | + private String cluster; |
| 51 | + private PulsarClient client; |
| 52 | + private List<Reader<byte[]>> readers; |
| 53 | + private ExecutorService executor; |
| 54 | + private volatile boolean running = false; |
| 55 | + volatile long closeTimeMs; |
| 56 | + |
| 57 | + @Override |
| 58 | + public String protocolName() { |
| 59 | + return PROTOCOL; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean accept(String protocol) { |
| 64 | + return protocol.equals(PROTOCOL); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void initialize(ServiceConfiguration conf) throws Exception { |
| 69 | + final var properties = conf.getProperties(); |
| 70 | + topic = (String) properties.getOrDefault("metadata.topic", "metadata-topic"); |
| 71 | + partitions = (Integer) properties.getOrDefault("metadata.partitions", 1); |
| 72 | + cluster = conf.getClusterName(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getProtocolDataToAdvertise() { |
| 77 | + return ""; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void start(BrokerService service) { |
| 82 | + try { |
| 83 | + final var port = service.getPulsar().getListenPortHTTP().orElseThrow(); |
| 84 | + @Cleanup final var admin = PulsarAdmin.builder().serviceHttpUrl("http://localhost:" + port).build(); |
| 85 | + try { |
| 86 | + admin.clusters().createCluster(cluster, ClusterData.builder() |
| 87 | + .serviceUrl(service.getPulsar().getWebServiceAddress()) |
| 88 | + .serviceUrlTls(service.getPulsar().getWebServiceAddressTls()) |
| 89 | + .brokerServiceUrl(service.getPulsar().getBrokerServiceUrl()) |
| 90 | + .brokerServiceUrlTls(service.getPulsar().getBrokerServiceUrlTls()) |
| 91 | + .build()); |
| 92 | + } catch (PulsarAdminException ignored) { |
| 93 | + } |
| 94 | + try { |
| 95 | + admin.tenants().createTenant("public", TenantInfo.builder().allowedClusters(Set.of(cluster)).build()); |
| 96 | + } catch (PulsarAdminException ignored) { |
| 97 | + } |
| 98 | + try { |
| 99 | + admin.namespaces().createNamespace("public/default"); |
| 100 | + } catch (PulsarAdminException ignored) { |
| 101 | + } |
| 102 | + } catch (PulsarClientException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + try { |
| 106 | + final var port = service.getListenPort().orElseThrow(); |
| 107 | + client = PulsarClient.builder().serviceUrl("pulsar://localhost:" + port).build(); |
| 108 | + readers = new ArrayList<>(); |
| 109 | + for (int i = 0; i < partitions; i++) { |
| 110 | + readers.add(client.newReader().topic(topic + TopicName.PARTITIONED_TOPIC_SUFFIX + i) |
| 111 | + .startMessageId(MessageId.earliest).create()); |
| 112 | + } |
| 113 | + running = true; |
| 114 | + executor = Executors.newSingleThreadExecutor(); |
| 115 | + executor.execute(() -> { |
| 116 | + while (running) { |
| 117 | + readers.forEach(reader -> { |
| 118 | + try { |
| 119 | + reader.readNext(1, TimeUnit.MILLISECONDS); |
| 120 | + } catch (PulsarClientException ignored) { |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + }); |
| 125 | + } catch (PulsarClientException e) { |
| 126 | + throw new RuntimeException(e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Map<InetSocketAddress, ChannelInitializer<SocketChannel>> newChannelInitializers() { |
| 132 | + return Map.of(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void close() { |
| 137 | + final var start = System.currentTimeMillis(); |
| 138 | + running = false; |
| 139 | + if (client != null) { |
| 140 | + try { |
| 141 | + client.close(); |
| 142 | + } catch (PulsarClientException ignored) { |
| 143 | + } |
| 144 | + client = null; |
| 145 | + } |
| 146 | + if (executor != null) { |
| 147 | + executor.shutdown(); |
| 148 | + executor = null; |
| 149 | + } |
| 150 | + closeTimeMs = System.currentTimeMillis() - start; |
| 151 | + } |
| 152 | +} |
0 commit comments