2121import static org .apache .pulsar .broker .loadbalance .LoadManager .LOADBALANCE_BROKERS_ROOT ;
2222import com .google .common .annotations .VisibleForTesting ;
2323import java .util .ArrayList ;
24+ import java .util .EnumSet ;
2425import java .util .List ;
2526import java .util .Map ;
2627import java .util .Optional ;
2728import java .util .concurrent .CompletableFuture ;
28- import java .util .concurrent .CompletionException ;
2929import java .util .concurrent .ConcurrentHashMap ;
3030import java .util .concurrent .ExecutionException ;
3131import java .util .concurrent .RejectedExecutionException ;
3939import org .apache .pulsar .broker .ServiceConfiguration ;
4040import org .apache .pulsar .broker .loadbalance .extensions .data .BrokerLookupData ;
4141import org .apache .pulsar .common .util .FutureUtil ;
42+ import org .apache .pulsar .metadata .api .MetadataCache ;
4243import org .apache .pulsar .metadata .api .MetadataStoreException ;
4344import org .apache .pulsar .metadata .api .Notification ;
4445import org .apache .pulsar .metadata .api .NotificationType ;
45- import org .apache .pulsar .metadata .api .coordination .LockManager ;
46- import org .apache .pulsar .metadata .api .coordination .ResourceLock ;
46+ import org .apache .pulsar .metadata .api .extended .CreateOption ;
4747
4848/**
4949 * The broker registry impl, base on the LockManager.
@@ -57,16 +57,14 @@ public class BrokerRegistryImpl implements BrokerRegistry {
5757
5858 private final BrokerLookupData brokerLookupData ;
5959
60- private final LockManager <BrokerLookupData > brokerLookupDataLockManager ;
60+ private final MetadataCache <BrokerLookupData > brokerLookupDataMetadataCache ;
6161
62- private final String brokerId ;
62+ private final String brokerIdKeyPath ;
6363
6464 private final ScheduledExecutorService scheduler ;
6565
6666 private final List <BiConsumer <String , NotificationType >> listeners ;
6767
68- private volatile ResourceLock <BrokerLookupData > brokerLookupDataLock ;
69-
7068 protected enum State {
7169 Init ,
7270 Started ,
@@ -79,10 +77,10 @@ protected enum State {
7977 public BrokerRegistryImpl (PulsarService pulsar ) {
8078 this .pulsar = pulsar ;
8179 this .conf = pulsar .getConfiguration ();
82- this .brokerLookupDataLockManager = pulsar .getCoordinationService ().getLockManager (BrokerLookupData .class );
80+ this .brokerLookupDataMetadataCache = pulsar .getLocalMetadataStore ().getMetadataCache (BrokerLookupData .class );
8381 this .scheduler = pulsar .getLoadManagerExecutor ();
8482 this .listeners = new ArrayList <>();
85- this .brokerId = pulsar .getBrokerId ();
83+ this .brokerIdKeyPath = keyPath ( pulsar .getBrokerId () );
8684 this .brokerLookupData = new BrokerLookupData (
8785 pulsar .getWebServiceAddress (),
8886 pulsar .getWebServiceAddressTls (),
@@ -122,7 +120,7 @@ public boolean isStarted() {
122120 public synchronized void register () throws MetadataStoreException {
123121 if (this .state == State .Started ) {
124122 try {
125- this . brokerLookupDataLock = brokerLookupDataLockManager . acquireLock ( keyPath ( brokerId ), brokerLookupData )
123+ brokerLookupDataMetadataCache . put ( brokerIdKeyPath , brokerLookupData , EnumSet . of ( CreateOption . Ephemeral ) )
126124 .get (conf .getMetadataStoreOperationTimeoutSeconds (), TimeUnit .SECONDS );
127125 this .state = State .Registered ;
128126 } catch (InterruptedException | ExecutionException | TimeoutException e ) {
@@ -135,30 +133,37 @@ public synchronized void register() throws MetadataStoreException {
135133 public synchronized void unregister () throws MetadataStoreException {
136134 if (this .state == State .Registered ) {
137135 try {
138- this . brokerLookupDataLock . release ( )
136+ brokerLookupDataMetadataCache . delete ( brokerIdKeyPath )
139137 .get (conf .getMetadataStoreOperationTimeoutSeconds (), TimeUnit .SECONDS );
140- this .state = State .Started ;
141- } catch (CompletionException | InterruptedException | ExecutionException | TimeoutException e ) {
138+ } catch (ExecutionException e ) {
139+ if (e .getCause () instanceof MetadataStoreException .NotFoundException ) {
140+ log .warn ("{} has already been unregistered" , brokerIdKeyPath );
141+ } else {
142+ throw MetadataStoreException .unwrap (e );
143+ }
144+ } catch (InterruptedException | TimeoutException e ) {
142145 throw MetadataStoreException .unwrap (e );
146+ } finally {
147+ this .state = State .Started ;
143148 }
144149 }
145150 }
146151
147152 @ Override
148153 public String getBrokerId () {
149- return this . brokerId ;
154+ return pulsar . getBrokerId () ;
150155 }
151156
152157 @ Override
153158 public CompletableFuture <List <String >> getAvailableBrokersAsync () {
154159 this .checkState ();
155- return brokerLookupDataLockManager . listLocks (LOADBALANCE_BROKERS_ROOT ). thenApply ( ArrayList :: new );
160+ return brokerLookupDataMetadataCache . getChildren (LOADBALANCE_BROKERS_ROOT );
156161 }
157162
158163 @ Override
159164 public CompletableFuture <Optional <BrokerLookupData >> lookupAsync (String broker ) {
160165 this .checkState ();
161- return brokerLookupDataLockManager . readLock (keyPath (broker ));
166+ return brokerLookupDataMetadataCache . get (keyPath (broker ));
162167 }
163168
164169 public CompletableFuture <Map <String , BrokerLookupData >> getAvailableBrokerLookupDataAsync () {
@@ -192,13 +197,8 @@ public synchronized void close() throws PulsarServerException {
192197 try {
193198 this .listeners .clear ();
194199 this .unregister ();
195- this .brokerLookupDataLockManager .close ();
196200 } catch (Exception ex ) {
197- if (ex .getCause () instanceof MetadataStoreException .NotFoundException ) {
198- throw new PulsarServerException .NotFoundException (MetadataStoreException .unwrap (ex ));
199- } else {
200- throw new PulsarServerException (MetadataStoreException .unwrap (ex ));
201- }
201+ log .error ("Unexpected error when unregistering the broker registry" , ex );
202202 } finally {
203203 this .state = State .Closed ;
204204 }
0 commit comments