Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][broker] Fix Broker was failing to create producer with broken schema ledger #23395

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -64,6 +65,7 @@
import org.apache.pulsar.broker.service.plugin.EntryFilter;
import org.apache.pulsar.broker.service.schema.SchemaRegistryService;
import org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException;
import org.apache.pulsar.broker.service.schema.exceptions.SchemaException;
import org.apache.pulsar.broker.stats.prometheus.metrics.Summary;
import org.apache.pulsar.common.api.proto.CommandSubscribe.SubType;
import org.apache.pulsar.common.naming.NamespaceName;
Expand Down Expand Up @@ -666,9 +668,14 @@ protected String getSchemaId() {
}
@Override
public CompletableFuture<Boolean> hasSchema() {
return brokerService.pulsar()
.getSchemaRegistryService()
.getSchema(getSchemaId()).thenApply(Objects::nonNull);
return brokerService.pulsar().getSchemaRegistryService().getSchema(getSchemaId()).thenApply(Objects::nonNull)
.exceptionally(e -> {
Throwable ex = e.getCause();
if (ex instanceof SchemaException || !((SchemaException) ex).isRecoverable()) {
return false;
}
throw ex instanceof CompletionException ? (CompletionException) ex : new CompletionException(ex);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
import static org.apache.pulsar.schema.compatibility.SchemaCompatibilityCheckTest.randomName;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -177,4 +178,36 @@ public void testSchemaFailure() throws Exception {
producer.close();
consumer.close();
}

@Test
public void testAddProducerOnDeletedSchemaLedgerTopic() throws Exception {
final String tenant = PUBLIC_TENANT;
final String namespace = "test-namespace-" + randomName(16);
final String topicOne = "test-deleted-schema-ledger";
final String fqtnOne = TopicName.get(TopicDomain.persistent.value(), tenant, namespace, topicOne).toString();

//pulsar.getConfig().setManagedLedgerForceRecovery(true);
admin.namespaces().createNamespace(tenant + "/" + namespace, Sets.newHashSet("test"));

// (1) create topic with schema
Producer<Schemas.PersonTwo> producer = pulsarClient
.newProducer(Schema.AVRO(SchemaDefinition.<Schemas.PersonTwo> builder().withAlwaysAllowNull(false)
.withSupportSchemaVersioning(true).withPojo(Schemas.PersonTwo.class).build()))
.topic(fqtnOne).create();

producer.close();

String key = TopicName.get(fqtnOne).getSchemaName();
BookkeeperSchemaStorage schemaStrogate = (BookkeeperSchemaStorage) pulsar.getSchemaStorage();
long schemaLedgerId = schemaStrogate.getSchemaLedgerList(key).get(0);

// (2) break schema locator by deleting schema-ledger
schemaStrogate.getBookKeeper().deleteLedger(schemaLedgerId);

admin.topics().unload(fqtnOne);

Producer<byte[]> producerWihtoutSchema = pulsarClient.newProducer().topic(fqtnOne).create();

assertNotNull(producerWihtoutSchema);
}
}
Loading