Skip to content

Commit

Permalink
ManagedLedger should not attempt deferrable metadata operation while …
Browse files Browse the repository at this point in the history
…disconnected (apache#12101)

* ManagedLedger should not attempt deferrable metadata operation while disconnected

* Fixed compilation

* Remove unused imports

* Fixed ManagedLedgerWriter

* Fixed compilation in presto connector

* Also defer ledger trimmings
  • Loading branch information
merlimat authored Sep 21, 2021
1 parent 32401bb commit 4bc3c40
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2653,7 +2653,8 @@ public void operationFailed(MetaStoreException e) {

boolean shouldCloseLedger(LedgerHandle lh) {
long now = clock.millis();
if ((lh.getLastAddConfirmed() >= config.getMetadataMaxEntriesPerLedger()
if (ledger.factory.isMetadataServiceAvailable() &&
(lh.getLastAddConfirmed() >= config.getMetadataMaxEntriesPerLedger()
|| lastLedgerSwitchTimestamp < (now - config.getLedgerRolloverTimeout() * 1000))
&& (STATE_UPDATER.get(this) != State.Closed && STATE_UPDATER.get(this) != State.Closing)) {
// It's safe to modify the timestamp since this method will be only called from a callback, implying that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import lombok.Getter;
import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.client.BookKeeper;
import org.apache.bookkeeper.common.util.OrderedScheduler;
Expand Down Expand Up @@ -77,6 +78,8 @@
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.Stat;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.apache.pulsar.metadata.api.extended.SessionEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -106,6 +109,12 @@ public class ManagedLedgerFactoryImpl implements ManagedLedgerFactory {
//indicate whether shutdown() is called.
private volatile boolean closed;

/**
* Keep a flag to indicate whether we're currently connected to the metadata service
*/
@Getter
private boolean metadataServiceAvailable;

private static class PendingInitializeManagedLedger {

private final ManagedLedgerImpl ledger;
Expand All @@ -118,47 +127,47 @@ private static class PendingInitializeManagedLedger {

}

public ManagedLedgerFactoryImpl(MetadataStore metadataStore, ClientConfiguration bkClientConfiguration)
public ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore, ClientConfiguration bkClientConfiguration)
throws Exception {
this(metadataStore, bkClientConfiguration, new ManagedLedgerFactoryConfig());
}

@SuppressWarnings("deprecation")
public ManagedLedgerFactoryImpl(MetadataStore metadataStore, ClientConfiguration bkClientConfiguration,
public ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore, ClientConfiguration bkClientConfiguration,
ManagedLedgerFactoryConfig config)
throws Exception {
this(metadataStore, new DefaultBkFactory(bkClientConfiguration),
true /* isBookkeeperManaged */, config, NullStatsLogger.INSTANCE);
}

public ManagedLedgerFactoryImpl(MetadataStore metadataStore, BookKeeper bookKeeper)
public ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore, BookKeeper bookKeeper)
throws Exception {
this(metadataStore, bookKeeper, new ManagedLedgerFactoryConfig());
}

public ManagedLedgerFactoryImpl(MetadataStore metadataStore, BookKeeper bookKeeper,
public ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore, BookKeeper bookKeeper,
ManagedLedgerFactoryConfig config)
throws Exception {
this(metadataStore, (policyConfig) -> bookKeeper, config);
}

public ManagedLedgerFactoryImpl(MetadataStore metadataStore,
public ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore,
BookkeeperFactoryForCustomEnsemblePlacementPolicy bookKeeperGroupFactory,
ManagedLedgerFactoryConfig config)
throws Exception {
this(metadataStore, bookKeeperGroupFactory, false /* isBookkeeperManaged */,
config, NullStatsLogger.INSTANCE);
}

public ManagedLedgerFactoryImpl(MetadataStore metadataStore,
public ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore,
BookkeeperFactoryForCustomEnsemblePlacementPolicy bookKeeperGroupFactory,
ManagedLedgerFactoryConfig config, StatsLogger statsLogger)
throws Exception {
this(metadataStore, bookKeeperGroupFactory, false /* isBookkeeperManaged */,
config, statsLogger);
}

private ManagedLedgerFactoryImpl(MetadataStore metadataStore,
private ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore,
BookkeeperFactoryForCustomEnsemblePlacementPolicy bookKeeperGroupFactory,
boolean isBookkeeperManaged,
ManagedLedgerFactoryConfig config, StatsLogger statsLogger) throws Exception {
Expand All @@ -170,7 +179,7 @@ private ManagedLedgerFactoryImpl(MetadataStore metadataStore,
.build();
cacheEvictionExecutor = Executors
.newSingleThreadExecutor(new DefaultThreadFactory("bookkeeper-ml-cache-eviction"));

this.metadataServiceAvailable = true;
this.bookkeeperFactory = bookKeeperGroupFactory;
this.isBookkeeperManaged = isBookkeeperManaged;
this.metadataStore = metadataStore;
Expand All @@ -190,6 +199,8 @@ private ManagedLedgerFactoryImpl(MetadataStore metadataStore,

cacheEvictionExecutor.execute(this::cacheEvictionTask);
closed = false;

metadataStore.registerSessionListener(this::handleMetadataStoreNotification);
}

static class DefaultBkFactory implements BookkeeperFactoryForCustomEnsemblePlacementPolicy {
Expand All @@ -207,6 +218,22 @@ public BookKeeper get(EnsemblePlacementPolicyConfig policy) {
}
}

private synchronized void handleMetadataStoreNotification(SessionEvent e) {
log.info("Received MetadataStore session event: {}", e);

switch (e) {
case ConnectionLost:
case SessionLost:
metadataServiceAvailable = false;
break;

case Reconnected:
case SessionReestablished:
metadataServiceAvailable = true;
break;
}
}

private synchronized void flushCursors() {
ledgers.values().forEach(mlfuture -> {
if (mlfuture.isDone() && !mlfuture.isCompletedExceptionally()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,12 @@ void internalTrimConsumedLedgers(CompletableFuture<?> promise) {
}

void internalTrimLedgers(boolean isTruncate, CompletableFuture<?> promise) {
if (!factory.isMetadataServiceAvailable()) {
// Defer trimming of ledger if we cannot connect to metadata service
promise.complete(null);
return;
}

// Ensure only one trimming operation is active
if (!trimmerMutex.tryLock()) {
scheduleDeferredTrimming(isTruncate, promise);
Expand Down Expand Up @@ -3386,6 +3392,11 @@ public boolean isCursorActive(ManagedCursor cursor) {
}

private boolean currentLedgerIsFull() {
if (!factory.isMetadataServiceAvailable()) {
// We don't want to trigger metadata operations if we already know that we're currently disconnected
return false;
}

boolean spaceQuotaReached = (currentLedgerEntries >= config.getMaxEntriesPerLedger()
|| currentLedgerSize >= (config.getMaxSizePerLedgerMb() * MegaByte));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo;
import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
import org.apache.pulsar.metadata.api.extended.SessionEvent;
import org.apache.pulsar.metadata.impl.FaultInjectionMetadataStore;
import org.apache.pulsar.metadata.api.MetadataStoreException;
import org.apache.pulsar.metadata.api.Stat;
Expand Down Expand Up @@ -3552,5 +3553,43 @@ public void testCursorGetBacklog() throws Exception {
Assert.assertEquals(managedCursor.getNumberOfEntriesInBacklog(false), 4);
}

@Test
public void testCursorNoRolloverIfNoMetadataSession() throws Exception {
ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
managedLedgerConfig.setMaxEntriesPerLedger(2);
managedLedgerConfig.setMetadataMaxEntriesPerLedger(2);
managedLedgerConfig.setMinimumRolloverTime(0, TimeUnit.MILLISECONDS);
managedLedgerConfig.setThrottleMarkDelete(0);
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("testCursorNoRolloverIfNoMetadataSession", managedLedgerConfig);
ManagedCursorImpl cursor = (ManagedCursorImpl) ledger.openCursor("test");

List<Position> positions = new ArrayList<>();
for (int i = 0; i < 10; i++) {
positions.add(ledger.addEntry("test".getBytes(Encoding)));
}

cursor.delete(positions.get(0));

long initialLedgerId = cursor.getCursorLedger();

metadataStore.triggerSessionEvent(SessionEvent.SessionLost);

for (int i = 1; i < 10; i++) {
cursor.delete(positions.get(i));
}

assertEquals(cursor.getCursorLedger(), initialLedgerId);

// After the session gets reestablished, the rollover should restart
metadataStore.triggerSessionEvent(SessionEvent.SessionReestablished);

for (int i = 0; i < 10; i++) {
Position p = ledger.addEntry("test".getBytes(Encoding));
cursor.delete(p);
}

assertNotEquals(cursor.getCursorLedger(), initialLedgerId);
}

private static final Logger log = LoggerFactory.getLogger(ManagedCursorTest.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import io.netty.buffer.ByteBuf;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

import io.netty.buffer.ByteBuf;
import lombok.Cleanup;
import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.client.api.DigestType;
Expand All @@ -45,8 +42,8 @@
import org.apache.bookkeeper.mledger.ManagedLedgerFactory;
import org.apache.bookkeeper.mledger.Position;
import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
import org.apache.pulsar.metadata.impl.FaultInjectionMetadataStore;
import org.apache.pulsar.metadata.api.MetadataStoreException;
import org.apache.pulsar.metadata.impl.FaultInjectionMetadataStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import org.apache.bookkeeper.mledger.ReadOnlyCursor;
import org.apache.bookkeeper.mledger.proto.MLDataFormats;
import org.apache.pulsar.metadata.api.GetResult;
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.Stat;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
Expand All @@ -59,7 +59,7 @@ public void openEncounteredShutdown() throws Exception {
final long version = 0;
final long createTimeMillis = System.currentTimeMillis();

MetadataStore metadataStore = mock(MetadataStore.class);
MetadataStoreExtended metadataStore = mock(MetadataStoreExtended.class);
CountDownLatch slowZk = new CountDownLatch(1);
given(metadataStore.get(any())).willAnswer(inv -> {
String path = inv.getArgument(0, String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import org.apache.bookkeeper.mledger.util.Futures;
import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
import org.apache.pulsar.common.policies.data.OffloadPoliciesImpl;
import org.apache.pulsar.metadata.api.extended.SessionEvent;
import org.apache.pulsar.metadata.impl.FaultInjectionMetadataStore;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.mutable.MutableObject;
Expand Down Expand Up @@ -1780,6 +1781,61 @@ public void testMaximumRolloverTime() throws Exception {
assertEquals(ledger.getLedgersInfoAsList().size(), 2);
}

@Test
public void testNoRolloverIfNoMetadataSession() throws Exception {
ManagedLedgerConfig conf = new ManagedLedgerConfig();
conf.setMaxEntriesPerLedger(1);
conf.setMinimumRolloverTime(0, TimeUnit.SECONDS);
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("testNoRolloverIfNoMetadataSession", conf);
ledger.openCursor("c1");

metadataStore.triggerSessionEvent(SessionEvent.SessionLost);

for (int i = 1; i < 10; i++) {
ledger.addEntry("data".getBytes());
}

// This should not have changed
assertEquals(ledger.getLedgersInfoAsList().size(), 1);

metadataStore.triggerSessionEvent(SessionEvent.SessionReestablished);
ledger.addEntry("data".getBytes());
ledger.addEntry("data".getBytes());
ledger.addEntry("data".getBytes());

// After the re-establishment, we'll be creating new ledgers
assertEquals(ledger.getLedgersInfoAsList().size(), 3);
}

@Test
public void testNoRolloverIfNoMetadataSessionWithExistingData() throws Exception {
ManagedLedgerConfig conf = new ManagedLedgerConfig();
conf.setMaxEntriesPerLedger(2);
conf.setMinimumRolloverTime(0, TimeUnit.SECONDS);
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("testNoRolloverIfNoMetadataSession", conf);
ledger.openCursor("c1");

ledger.addEntry("data".getBytes());

assertEquals(ledger.getLedgersInfoAsList().size(), 1);

metadataStore.triggerSessionEvent(SessionEvent.SessionLost);

for (int i = 1; i < 10; i++) {
ledger.addEntry("data".getBytes());
}

// This should not have changed
assertEquals(ledger.getLedgersInfoAsList().size(), 1);

metadataStore.triggerSessionEvent(SessionEvent.SessionReestablished);
ledger.addEntry("data".getBytes());
ledger.addEntry("data".getBytes());

// After the re-establishment, we'll be creating new ledgers
assertEquals(ledger.getLedgersInfoAsList().size(), 2);
}

@Test
public void testRetention() throws Exception {
@Cleanup("shutdown")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreFactory;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.apache.pulsar.metadata.impl.FaultInjectionMetadataStore;
import org.apache.zookeeper.KeeperException;
import org.awaitility.Awaitility;
Expand Down Expand Up @@ -140,7 +141,7 @@ protected void startZKCluster() throws Exception {
protected void startZKCluster(String path) throws Exception {
zkUtil.startServer(path);
metadataStore = new FaultInjectionMetadataStore(
MetadataStoreFactory.create(zkUtil.getZooKeeperConnectString(),
MetadataStoreExtended.create(zkUtil.getZooKeeperConnectString(),
MetadataStoreConfig.builder().build()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreException;
import org.apache.pulsar.metadata.api.MetadataStoreFactory;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.apache.pulsar.metadata.impl.FaultInjectionMetadataStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -68,7 +69,7 @@ public MockedBookKeeperTestCase(int numBookies) {
public final void setUp(Method method) throws Exception {
LOG.info(">>>>>> starting {}", method);
metadataStore = new FaultInjectionMetadataStore(
MetadataStoreFactory.create("memory://local", MetadataStoreConfig.builder().build()));
MetadataStoreExtended.create("memory://local", MetadataStoreConfig.builder().build()));

try {
// start bookkeeper service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreFactory;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -97,7 +98,7 @@ public static void main(String[] args) throws Exception {
}

@Cleanup
MetadataStore metadataStore = MetadataStoreFactory.create(arguments.zookeeper,
MetadataStoreExtended metadataStore = MetadataStoreExtended.create(arguments.zookeeper,
MetadataStoreConfig.builder().sessionTimeoutMillis(arguments.zkSessionTimeoutMillis).build());

if (arguments.bkMetadataServiceUri != null) {
Expand Down
Loading

0 comments on commit 4bc3c40

Please sign in to comment.