@@ -1913,7 +1913,7 @@ public void cleanUnloadedTopicFromCache(NamespaceBundle serviceUnit) {
19131913 TopicName topicName = TopicName .get (topic );
19141914 if (serviceUnit .includes (topicName ) && getTopicReference (topic ).isPresent ()) {
19151915 log .info ("[{}][{}] Clean unloaded topic from cache." , serviceUnit .toString (), topic );
1916- pulsar .getBrokerService ().removeTopicFromCache (topicName .toString (), serviceUnit );
1916+ pulsar .getBrokerService ().removeTopicFromCache (topicName .toString (), serviceUnit , null );
19171917 }
19181918 }
19191919 }
@@ -1922,15 +1922,56 @@ public AuthorizationService getAuthorizationService() {
19221922 return authorizationService ;
19231923 }
19241924
1925- public CompletableFuture <Void > removeTopicFromCache (String topic ) {
1925+ public CompletableFuture <Void > removeTopicFromCache (String topicName ) {
1926+ return removeTopicFutureFromCache (topicName , null );
1927+ }
1928+
1929+ public CompletableFuture <Void > removeTopicFromCache (Topic topic ) {
1930+ Optional <CompletableFuture <Optional <Topic >>> createTopicFuture = findTopicFutureInCache (topic );
1931+ if (!createTopicFuture .isPresent ()){
1932+ return CompletableFuture .completedFuture (null );
1933+ }
1934+ return removeTopicFutureFromCache (topic .getName (), createTopicFuture .get ());
1935+ }
1936+
1937+ private Optional <CompletableFuture <Optional <Topic >>> findTopicFutureInCache (Topic topic ){
1938+ if (topic == null ){
1939+ return Optional .empty ();
1940+ }
1941+ final CompletableFuture <Optional <Topic >> createTopicFuture = topics .get (topic .getName ());
1942+ // If not exists in cache, do nothing.
1943+ if (createTopicFuture == null ){
1944+ return Optional .empty ();
1945+ }
1946+ // If the future in cache is not yet complete, the topic instance in the cache is not the same with the topic.
1947+ if (!createTopicFuture .isDone ()){
1948+ return Optional .empty ();
1949+ }
1950+ // If the future in cache has exception complete,
1951+ // the topic instance in the cache is not the same with the topic.
1952+ if (createTopicFuture .isCompletedExceptionally ()){
1953+ return Optional .empty ();
1954+ }
1955+ Optional <Topic > optionalTopic = createTopicFuture .join ();
1956+ Topic topicInCache = optionalTopic .orElse (null );
1957+ if (topicInCache == null || topicInCache != topic ){
1958+ return Optional .empty ();
1959+ } else {
1960+ return Optional .of (createTopicFuture );
1961+ }
1962+ }
1963+
1964+ private CompletableFuture <Void > removeTopicFutureFromCache (String topic ,
1965+ CompletableFuture <Optional <Topic >> createTopicFuture ) {
19261966 TopicName topicName = TopicName .get (topic );
19271967 return pulsar .getNamespaceService ().getBundleAsync (topicName )
19281968 .thenAccept (namespaceBundle -> {
1929- removeTopicFromCache (topic , namespaceBundle );
1969+ removeTopicFromCache (topic , namespaceBundle , createTopicFuture );
19301970 });
19311971 }
19321972
1933- public void removeTopicFromCache (String topic , NamespaceBundle namespaceBundle ) {
1973+ private void removeTopicFromCache (String topic , NamespaceBundle namespaceBundle ,
1974+ CompletableFuture <Optional <Topic >> createTopicFuture ) {
19341975 String bundleName = namespaceBundle .toString ();
19351976 String namespaceName = TopicName .get (topic ).getNamespaceObject ().toString ();
19361977
@@ -1957,7 +1998,12 @@ public void removeTopicFromCache(String topic, NamespaceBundle namespaceBundle)
19571998 }
19581999 }
19592000 }
1960- topics .remove (topic );
2001+
2002+ if (createTopicFuture == null ) {
2003+ topics .remove (topic );
2004+ } else {
2005+ topics .remove (topic , createTopicFuture );
2006+ }
19612007
19622008 Compactor compactor = pulsar .getNullableCompactor ();
19632009 if (compactor != null ) {
0 commit comments