17
17
18
18
import static com .google .common .base .Preconditions .checkState ;
19
19
20
+ import java .util .List ;
20
21
import java .util .Map ;
21
22
import java .util .Optional ;
22
23
import java .util .concurrent .CompletableFuture ;
37
38
import com .github .benmanes .caffeine .cache .Caffeine ;
38
39
import com .github .benmanes .caffeine .cache .RemovalCause ;
39
40
import com .github .benmanes .caffeine .cache .RemovalListener ;
41
+ import com .google .common .collect .Lists ;
40
42
import com .google .common .util .concurrent .MoreExecutors ;
41
43
import com .yahoo .pulsar .broker .PulsarService ;
42
44
import com .yahoo .pulsar .client .util .FutureUtil ;
@@ -141,26 +143,6 @@ public CompletableFuture<OwnedBundle> asyncLoad(String namespaceBundleZNode, Exe
141
143
}
142
144
}
143
145
144
- private class OwnedServiceUnitCacheRemovalListener implements RemovalListener <String , OwnedBundle > {
145
-
146
- @ Override
147
- public void onRemoval (String key , OwnedBundle value , RemovalCause cause ) {
148
- LOG .info ("Removing ownership for {}" , key );
149
- // Under the cache sync lock, removing the ZNode
150
- // If succeeded, we guaranteed that the cache entry is removed together w/ ZNode
151
-
152
- localZkCache .getZooKeeper ().delete (key , -1 , (rc , path , ctx ) -> {
153
- if (rc == KeeperException .Code .OK .intValue ()) {
154
- LOG .info ("Removed zk lock for service unit: {}" , key );
155
- } else {
156
- LOG .warn ("Failed to delete the namespace ephemeral node. key={}" , key ,
157
- KeeperException .Code .get (rc ));
158
- }
159
- }, null );
160
- ownershipReadOnlyCache .invalidate (key );
161
- }
162
- }
163
-
164
146
/**
165
147
* Constructor of <code>OwnershipCache</code>
166
148
*
@@ -179,7 +161,6 @@ public OwnershipCache(PulsarService pulsar, NamespaceBundleFactory bundleFactory
179
161
this .ownershipReadOnlyCache = pulsar .getLocalZkCacheService ().ownerInfoCache ();
180
162
// ownedBundlesCache contains all namespaces that are owned by the local broker
181
163
this .ownedBundlesCache = Caffeine .newBuilder ().executor (MoreExecutors .sameThreadExecutor ())
182
- .removalListener (new OwnedServiceUnitCacheRemovalListener ())
183
164
.buildAsync (new OwnedServiceUnitCacheLoader ());
184
165
}
185
166
@@ -268,8 +249,22 @@ public CompletableFuture<NamespaceEphemeralData> tryAcquiringOwnership(Namespace
268
249
* Method to remove the ownership of local broker on the <code>NamespaceBundle</code>, if owned
269
250
*
270
251
*/
271
- public void removeOwnership (NamespaceBundle bundle ) {
272
- ownedBundlesCache .synchronous ().invalidate (ServiceUnitZkUtils .path (bundle ));
252
+ public CompletableFuture <Void > removeOwnership (NamespaceBundle bundle ) {
253
+ CompletableFuture <Void > result = new CompletableFuture <>();
254
+ String key = ServiceUnitZkUtils .path (bundle );
255
+ localZkCache .getZooKeeper ().delete (key , -1 , (rc , path , ctx ) -> {
256
+ if (rc == KeeperException .Code .OK .intValue () || rc == KeeperException .Code .NONODE .intValue ()) {
257
+ LOG .info ("[{}] Removed zk lock for service unit: {}" , key , KeeperException .Code .get (rc ));
258
+ ownedBundlesCache .synchronous ().invalidate (key );
259
+ ownershipReadOnlyCache .invalidate (key );
260
+ result .complete (null );
261
+ } else {
262
+ LOG .warn ("[{}] Failed to delete the namespace ephemeral node. key={}" , key ,
263
+ KeeperException .Code .get (rc ));
264
+ result .completeExceptionally (KeeperException .create (rc ));
265
+ }
266
+ }, null );
267
+ return result ;
273
268
}
274
269
275
270
/**
@@ -278,22 +273,18 @@ public void removeOwnership(NamespaceBundle bundle) {
278
273
* @param bundles
279
274
* <code>NamespaceBundles</code> to remove from ownership cache
280
275
*/
281
- public void removeOwnership (NamespaceBundles bundles ) {
282
- boolean hasError = false ;
276
+ public CompletableFuture < Void > removeOwnership (NamespaceBundles bundles ) {
277
+ List < CompletableFuture < Void >> allFutures = Lists . newArrayList () ;
283
278
for (NamespaceBundle bundle : bundles .getBundles ()) {
284
279
if (getOwnedBundle (bundle ) == null ) {
285
280
// continue
286
281
continue ;
287
282
}
288
- try {
289
- this .removeOwnership (bundle );
290
- } catch (Exception e ) {
291
- LOG .warn (String .format ("Failed to remove ownership of a service unit: %s" , bundle ), e );
292
- hasError = true ;
293
- }
283
+ allFutures .add (this .removeOwnership (bundle ));
294
284
}
295
- checkState (! hasError , "Not able to remove all owned bundles" );
285
+ return FutureUtil . waitForAll ( allFutures );
296
286
}
287
+
297
288
298
289
/**
299
290
* Method to access the map of all <code>ServiceUnit</code> objects owned by the local broker
@@ -330,17 +321,32 @@ public OwnedBundle getOwnedBundle(NamespaceBundle bundle) {
330
321
}
331
322
}
332
323
324
+ /**
325
+ * Disable bundle in local cache and on zk
326
+ *
327
+ * @param bundle
328
+ * @throws Exception
329
+ */
333
330
public void disableOwnership (NamespaceBundle bundle ) throws Exception {
334
331
String path = ServiceUnitZkUtils .path (bundle );
335
-
332
+ updateBundleState (bundle , false );
333
+ localZkCache .getZooKeeper ().setData (path , jsonMapper .writeValueAsBytes (selfOwnerInfoDisabled ), -1 );
334
+ ownershipReadOnlyCache .invalidate (path );
335
+ }
336
+
337
+ /**
338
+ * Update bundle state in a local cache
339
+ *
340
+ * @param bundle
341
+ * @throws Exception
342
+ */
343
+ public void updateBundleState (NamespaceBundle bundle , boolean isActive ) throws Exception {
344
+ String path = ServiceUnitZkUtils .path (bundle );
336
345
// Disable owned instance in local cache
337
346
CompletableFuture <OwnedBundle > f = ownedBundlesCache .getIfPresent (path );
338
347
if (f != null && f .isDone () && !f .isCompletedExceptionally ()) {
339
- f .join ().setActive (false );
348
+ f .join ().setActive (isActive );
340
349
}
341
-
342
- localZkCache .getZooKeeper ().setData (path , jsonMapper .writeValueAsBytes (selfOwnerInfoDisabled ), -1 );
343
- ownershipReadOnlyCache .invalidate (path );
344
350
}
345
351
346
352
public NamespaceEphemeralData getSelfOwnerInfo () {
0 commit comments