merlimat closed pull request #1501: Rest endpoint to query compaction status
URL: https://github.com/apache/incubator-pulsar/pull/1501
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
index 86e68583d3..9d82a2fdef 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
@@ -74,6 +74,7 @@
 import org.apache.pulsar.common.api.proto.PulsarApi.KeyValue;
 import org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata;
 import 
org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.compression.CompressionCodec;
 import org.apache.pulsar.common.compression.CompressionCodecProvider;
 import org.apache.pulsar.common.naming.TopicDomain;
@@ -1086,6 +1087,12 @@ protected void internalTriggerCompaction(boolean 
authoritative) {
         }
     }
 
+    protected CompactionStatus internalCompactionStatus(boolean authoritative) 
{
+        validateAdminOperationOnTopic(authoritative);
+        PersistentTopic topic = (PersistentTopic) getTopicReference(topicName);
+        return topic.compactionStatus();
+    }
+
     public static CompletableFuture<PartitionedTopicMetadata> 
getPartitionedTopicMetadata(PulsarService pulsar,
             String clientAppId, AuthenticationDataSource authenticationData, 
TopicName topicName) {
         CompletableFuture<PartitionedTopicMetadata> metadataFuture = new 
CompletableFuture<>();
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
index f28d229e0f..b03b533524 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/PersistentTopics.java
@@ -40,6 +40,7 @@
 import org.apache.pulsar.broker.admin.impl.PersistentTopicsBase;
 import org.apache.pulsar.client.api.MessageId;
 import org.apache.pulsar.client.impl.MessageIdImpl;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
 import org.apache.pulsar.common.policies.data.AuthAction;
 import org.apache.pulsar.common.policies.data.PartitionedTopicStats;
@@ -435,4 +436,17 @@ public void compact(@PathParam("property") String 
property, @PathParam("cluster"
         internalTriggerCompaction(authoritative);
     }
 
+    @GET
+    @Path("/{property}/{cluster}/{namespace}/{topic}/compaction")
+    @ApiOperation(value = "Get the status of a compaction operation for a 
topic.")
+    @ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have 
admin permission"),
+                            @ApiResponse(code = 405, message = "Operation not 
allowed on persistent topic"),
+                            @ApiResponse(code = 404, message = "Topic does not 
exist, or compaction hasn't run") })
+    public CompactionStatus compactionStatus(
+            @PathParam("property") String property, @PathParam("cluster") 
String cluster,
+            @PathParam("namespace") String namespace, @PathParam("topic") 
@Encoded String encodedTopic,
+            @QueryParam("authoritative") @DefaultValue("false") boolean 
authoritative) {
+        validateTopicName(property, cluster, namespace, encodedTopic);
+        return internalCompactionStatus(authoritative);
+    }
 }
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
index 3ffdde8721..4b887434db 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
@@ -40,6 +40,7 @@
 import org.apache.pulsar.broker.admin.impl.PersistentTopicsBase;
 import org.apache.pulsar.client.api.MessageId;
 import org.apache.pulsar.client.impl.MessageIdImpl;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
 import org.apache.pulsar.common.policies.data.AuthAction;
 import org.apache.pulsar.common.policies.data.PartitionedTopicStats;
@@ -407,4 +408,18 @@ public void compact(@PathParam("property") String property,
         validateTopicName(property, namespace, encodedTopic);
         internalTriggerCompaction(authoritative);
     }
+
+    @GET
+    @Path("/{property}/{namespace}/{topic}/compaction")
+    @ApiOperation(value = "Get the status of a compaction operation for a 
topic.")
+    @ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have 
admin permission"),
+                            @ApiResponse(code = 405, message = "Operation not 
allowed on persistent topic"),
+                            @ApiResponse(code = 404, message = "Topic does not 
exist, or compaction hasn't run") })
+    public CompactionStatus compactionStatus(
+            @PathParam("property") String property,
+            @PathParam("namespace") String namespace, @PathParam("topic") 
@Encoded String encodedTopic,
+            @QueryParam("authoritative") @DefaultValue("false") boolean 
authoritative) {
+        validateTopicName(property, namespace, encodedTopic);
+        return internalCompactionStatus(authoritative);
+    }
 }
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
index 6f7e4fb36c..2d63fb6d11 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
@@ -29,6 +29,8 @@
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
@@ -84,6 +86,7 @@
 import org.apache.pulsar.client.impl.MessageImpl;
 import 
org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition;
 import org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.SubType;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.naming.TopicName;
 import org.apache.pulsar.common.policies.data.BacklogQuota;
 import org.apache.pulsar.common.policies.data.ConsumerStats;
@@ -1622,6 +1625,27 @@ public synchronized void triggerCompaction()
         }
     }
 
+
+    public synchronized CompactionStatus compactionStatus() {
+        final CompletableFuture<Long> current;
+        synchronized (this) {
+            current = currentCompaction;
+        }
+        if (!current.isDone()) {
+            return CompactionStatus.forStatus(CompactionStatus.Status.RUNNING);
+        } else {
+            try {
+                if (current.join() == COMPACTION_NEVER_RUN) {
+                    return 
CompactionStatus.forStatus(CompactionStatus.Status.NOT_RUN);
+                } else {
+                    return 
CompactionStatus.forStatus(CompactionStatus.Status.SUCCESS);
+                }
+            } catch (CancellationException | CompletionException e) {
+                return CompactionStatus.forError(e.getMessage());
+            }
+        }
+    }
+
     private static final Logger log = 
LoggerFactory.getLogger(PersistentTopic.class);
 
     @Override
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java
index 80fbf18cf7..c53db5b3c9 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java
@@ -70,6 +70,7 @@
 import org.apache.pulsar.client.api.Producer;
 import org.apache.pulsar.client.api.PulsarClient;
 import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.lookup.data.LookupData;
 import org.apache.pulsar.common.naming.NamespaceBundle;
 import org.apache.pulsar.common.naming.NamespaceBundleFactory;
@@ -1927,4 +1928,39 @@ public void testTriggerCompaction() throws Exception {
         verify(compactor, times(2)).compact(topicName);
     }
 
+    @Test
+    public void testCompactionStatus() throws Exception {
+        String topicName = "persistent://prop-xyz/use/ns1/topic1";
+
+        // create a topic by creating a producer
+        pulsarClient.newProducer().topic(topicName).create().close();
+        assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));
+
+        
assertEquals(admin.persistentTopics().compactionStatus(topicName).status,
+                     CompactionStatus.Status.NOT_RUN);
+
+        // mock actual compaction, we don't need to really run it
+        CompletableFuture<Long> promise = new CompletableFuture<Long>();
+        Compactor compactor = pulsar.getCompactor();
+        doReturn(promise).when(compactor).compact(topicName);
+        admin.persistentTopics().triggerCompaction(topicName);
+
+        
assertEquals(admin.persistentTopics().compactionStatus(topicName).status,
+                     CompactionStatus.Status.RUNNING);
+
+        promise.complete(1L);
+
+        
assertEquals(admin.persistentTopics().compactionStatus(topicName).status,
+                     CompactionStatus.Status.SUCCESS);
+
+        CompletableFuture<Long> errorPromise = new CompletableFuture<Long>();
+        doReturn(errorPromise).when(compactor).compact(topicName);
+        admin.persistentTopics().triggerCompaction(topicName);
+        errorPromise.completeExceptionally(new Exception("Failed at 
something"));
+
+        
assertEquals(admin.persistentTopics().compactionStatus(topicName).status,
+                     CompactionStatus.Status.ERROR);
+        assertTrue(admin.persistentTopics().compactionStatus(topicName)
+                   .lastError.contains("Failed at something"));
+    }
 }
diff --git 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PersistentTopics.java
 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PersistentTopics.java
index 93df29038f..364fec463c 100644
--- 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PersistentTopics.java
+++ 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PersistentTopics.java
@@ -30,6 +30,7 @@
 import 
org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException;
 import org.apache.pulsar.client.api.Message;
 import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
 import org.apache.pulsar.common.policies.data.AuthAction;
 import org.apache.pulsar.common.policies.data.PartitionedTopicStats;
@@ -909,4 +910,11 @@ void createSubscription(String topic, String 
subscriptionName, MessageId message
      *            The topic on which to trigger compaction
      */
     void triggerCompaction(String topic) throws PulsarAdminException;
+
+    /**
+     * Check the status of an ongoing compaction for a topic.
+     *
+     * @param topic The topic whose compaction status we wish to check
+     */
+    CompactionStatus compactionStatus(String topic) throws 
PulsarAdminException;
 }
diff --git 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PersistentTopicsImpl.java
 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PersistentTopicsImpl.java
index 6193d0889b..3121739fb9 100644
--- 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PersistentTopicsImpl.java
+++ 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PersistentTopicsImpl.java
@@ -54,6 +54,7 @@
 import org.apache.pulsar.common.api.proto.PulsarApi;
 import org.apache.pulsar.common.api.proto.PulsarApi.KeyValue;
 import org.apache.pulsar.common.api.proto.PulsarApi.SingleMessageMetadata;
+import org.apache.pulsar.common.compaction.CompactionStatus;
 import org.apache.pulsar.common.naming.TopicName;
 import org.apache.pulsar.common.naming.NamespaceName;
 import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
@@ -723,6 +724,18 @@ public void triggerCompaction(String topic)
         }
     }
 
+    @Override
+    public CompactionStatus compactionStatus(String topic)
+            throws PulsarAdminException {
+        try {
+            TopicName tn = validateTopic(topic);
+            return request(topicPath(tn, "compaction"))
+                .get(CompactionStatus.class);
+        } catch (Exception e) {
+            throw getApiException(e);
+        }
+    }
+
     private WebTarget namespacePath(NamespaceName namespace, String... parts) {
         final WebTarget base = namespace.isV2() ? adminV2PersistentTopics : 
adminPersistentTopics;
         WebTarget namespacePath = base.path(namespace.toString());
diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/compaction/CompactionStatus.java
 
b/pulsar-common/src/main/java/org/apache/pulsar/common/compaction/CompactionStatus.java
new file mode 100644
index 0000000000..9020c21331
--- /dev/null
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/compaction/CompactionStatus.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.compaction;
+
+/**
+ * Status of compaction for a topic.
+ */
+public class CompactionStatus {
+    public enum Status {
+        NOT_RUN,
+        RUNNING,
+        SUCCESS,
+        ERROR
+    };
+
+    public Status status;
+    public String lastError;
+
+    public CompactionStatus() {
+        this.status = Status.NOT_RUN;
+        this.lastError = "";
+    }
+
+    private CompactionStatus(Status status, String lastError) {
+        this.status = status;
+        this.lastError = lastError;
+    }
+
+    public static CompactionStatus forStatus(Status status) {
+        return new CompactionStatus(status, "");
+    }
+
+    public static CompactionStatus forError(String lastError) {
+        return new CompactionStatus(Status.ERROR, lastError);
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to