Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,7 @@ flexible messaging model and an intuitive client API.</description>
<module>pulsar-common</module>
<module>pulsar-broker-common</module>
<module>pulsar-broker</module>
<module>pulsar-cli-utils</module>
<module>pulsar-client-api</module>
<module>pulsar-client</module>
<module>pulsar-client-shaded</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,8 @@ protected void handleGetLastMessageId(CommandGetLastMessageId getLastMessageId)
(PositionImpl) markDeletePosition,
partitionIndex,
requestId,
consumer.getSubscription().getName());
consumer.getSubscription().getName(),
consumer.readCompacted());
}).exceptionally(e -> {
writeAndFlush(Commands.newError(getLastMessageId.getRequestId(),
ServerError.UnknownError, "Failed to recover Transaction Buffer."));
Expand All @@ -2095,16 +2096,33 @@ private void getLargestBatchIndexWhenPossible(
PositionImpl markDeletePosition,
int partitionIndex,
long requestId,
String subscriptionName) {

String subscriptionName,
boolean readCompacted) {
PersistentTopic persistentTopic = (PersistentTopic) topic;
ManagedLedgerImpl ml = (ManagedLedgerImpl) persistentTopic.getManagedLedger();

// If it's not pointing to a valid entry, respond messageId of the current position.
// If the compaction cursor reach the end of the topic, respond messageId from compacted ledger
Optional<Position> compactionHorizon = persistentTopic.getCompactedTopic().getCompactionHorizon();
if (lastPosition.getEntryId() == -1 || (compactionHorizon.isPresent()
&& lastPosition.compareTo((PositionImpl) compactionHorizon.get()) <= 0)) {
Optional<Position> compactionHorizon = readCompacted ?
persistentTopic.getCompactedTopic().getCompactionHorizon() : Optional.empty();
if (lastPosition.getEntryId() == -1 || !ml.ledgerExists(lastPosition.getLedgerId())) {
// there is no entry in the original topic
if (compactionHorizon != null && compactionHorizon.isPresent()) {
// if readCompacted is true, we need to read the last entry from compacted topic
handleLastMessageIdFromCompactedLedger(persistentTopic, requestId, partitionIndex,
markDeletePosition);
return;
} else {
// if readCompacted is false, we need to return MessageId.earliest
writeAndFlush(Commands.newGetLastMessageIdResponse(requestId, -1, -1, partitionIndex, -1,
markDeletePosition != null ? markDeletePosition.getLedgerId() : -1,
markDeletePosition != null ? markDeletePosition.getEntryId() : -1));
}
return;
}

if (compactionHorizon != null && compactionHorizon.isPresent()
&& lastPosition.compareTo((PositionImpl) compactionHorizon.get()) <= 0) {
handleLastMessageIdFromCompactedLedger(persistentTopic, requestId, partitionIndex,
markDeletePosition);
return;
Expand Down Expand Up @@ -2133,7 +2151,8 @@ public void readEntryFailed(ManagedLedgerException exception, Object ctx) {

batchSizeFuture.whenComplete((batchSize, e) -> {
if (e != null) {
if (e.getCause() instanceof ManagedLedgerException.NonRecoverableLedgerException) {
if (e.getCause() instanceof ManagedLedgerException.NonRecoverableLedgerException
&& readCompacted) {
handleLastMessageIdFromCompactedLedger(persistentTopic, requestId, partitionIndex,
markDeletePosition);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -32,6 +34,7 @@
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
import org.apache.pulsar.client.api.CompressionType;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.ProducerBuilder;
Expand Down Expand Up @@ -415,4 +418,28 @@ public void testGetLastMessageIdAfterCompactionAllNullMsg(boolean enabledBatch)
producer.close();
admin.topics().delete(topicName, false);
}

@Test(dataProvider = "enabledBatch")
public void testReaderStuckWithCompaction(boolean enabledBatch) throws Exception {
String topicName = "persistent://public/default/" + BrokerTestUtil.newUniqueName("tp");
String subName = "sub";
Producer<String> producer = createProducer(enabledBatch, topicName);
producer.newMessage().key("k0").value("v0").sendAsync();
producer.newMessage().key("k0").value("v1").sendAsync();
producer.flush();

triggerCompactionAndWait(topicName);
triggerLedgerSwitch(topicName);
clearAllTheLedgersOutdated(topicName);

var reader = pulsarClient.newReader(Schema.STRING)
.topic(topicName)
.subscriptionName(subName)
.startMessageId(MessageId.earliest)
.create();
while (reader.hasMessageAvailable()) {
Message<String> message = reader.readNext(5, TimeUnit.SECONDS);
assertNotEquals(message, null);
}
}
}
149 changes: 149 additions & 0 deletions pulsar-cli-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0"?>
<!--

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.

-->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar</artifactId>
<version>3.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<artifactId>pulsar-cli-utils</artifactId>
<name>Pulsar CLI Utils</name>
<description>Isolated CLI utility module</description>

<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>${pulsar.client.compiler.release}</release>
</configuration>
</plugin>

<plugin>
<groupId>org.gaul</groupId>
<artifactId>modernizer-maven-plugin</artifactId>
<configuration>
<failOnViolations>true</failOnViolations>
<javaVersion>8</javaVersion>
</configuration>
<executions>
<execution>
<id>modernizer</id>
<phase>verify</phase>
<goals>
<goal>modernizer</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<id>git-info</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>false</skip>
<useNativeGit>true</useNativeGit>
<prefix>git</prefix>
<verbose>false</verbose>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<format>properties</format>
<gitDescribe>
<skip>true</skip>
</gitDescribe>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filtering-java-templates</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${spotbugs-maven-plugin.version}</version>
<executions>
<execution>
<id>spotbugs</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeFilterFile>${basedir}/src/main/resources/findbugsExclude.xml</excludeFilterFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>checkstyle</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.cli;

import com.beust.jcommander.ParameterException;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

@UtilityClass
public class ValueValidationUtil {

public static void maxValueCheck(String paramName, long value, long maxValue) {
if (value > maxValue) {
throw new ParameterException(paramName + " cannot be bigger than <" + maxValue + ">!");
}
}

public static void positiveCheck(String paramName, long value) {
if (value <= 0) {
throw new ParameterException(paramName + " cannot be less than or equal to <0>!");
}
}

public static void positiveCheck(String paramName, int value) {
if (value <= 0) {
throw new ParameterException(paramName + " cannot be less than or equal to <0>!");
}
}

public static void emptyCheck(String paramName, String value) {
if (StringUtils.isEmpty(value)) {
throw new ParameterException("The value of " + paramName + " can't be empty");
}
}

public static void minValueCheck(String name, Long value, long min) {
if (value < min) {
throw new ParameterException(name + " cannot be less than <" + min + ">!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.cli.converters;

import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck;
import static org.apache.pulsar.cli.converters.ByteUnitUtil.validateSizeString;
import com.beust.jcommander.converters.BaseConverter;

public class ByteUnitIntegerConverter extends BaseConverter<Integer> {

public ByteUnitIntegerConverter(String optionName) {
super(optionName);
}

@Override
public Integer convert(String argStr) {
return parseBytes(argStr).intValue();
}

Long parseBytes(String argStr) {
emptyCheck(getOptionName(), argStr);
long valueInBytes = validateSizeString(argStr);
return valueInBytes;
}
}
Loading