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 NPE issue during recovery add #3620

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -1769,7 +1769,7 @@ void handleUnrecoverableErrorDuringAdd(int rc) {
if (getLedgerMetadata().getState() == LedgerMetadata.State.IN_RECOVERY) {
// we should not close ledger if ledger is recovery mode
// otherwise we may lose entry.
errorOutPendingAdds(rc);
executeOrdered(() -> errorOutPendingAdds(rc));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@horizonzy I may not have explained it clearly. Although this is changed to an async operation, this code block will never be executed, because when in IN_RECOVERY mode, the method of ReadOnlyLedgerHandle must be called.
What do you think if I roll back the changes to LedgerHandle and only keep the changes to ReadOnlyLedgerHandle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

return;
}
LOG.error("Closing ledger {} due to {}", ledgerId, BKException.codeLogger(rc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void handleBookieFailure(final Map<Integer, BookieId> failedBookies) {

@Override
void handleUnrecoverableErrorDuringAdd(int rc) {
errorOutPendingAdds(rc);
executeOrdered(() -> errorOutPendingAdds(rc));
}

void recover(GenericCallback<Void> finalCb) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.bookkeeper.client;

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.discover.RegistrationClient;
import org.apache.bookkeeper.net.BookieId;
import org.apache.bookkeeper.proto.BookieAddressResolver;
import org.apache.bookkeeper.stats.StatsLogger;

public class MockBookieWatcher extends BookieWatcherImpl {

public MockBookieWatcher(ClientConfiguration conf, EnsemblePlacementPolicy placementPolicy,
RegistrationClient registrationClient, BookieAddressResolver bookieAddressResolver,
StatsLogger statsLogger) {
super(conf, placementPolicy, registrationClient, bookieAddressResolver, statsLogger);
}

@Override
public BookieId replaceBookie(int ensembleSize, int writeQuorumSize, int ackQuorumSize,
Map<String, byte[]> customMetadata, List<BookieId> existingBookies, int bookieIdx,
Set<BookieId> excludeBookies) throws BKException.BKNotEnoughBookiesException {
throw new BKException.BKNotEnoughBookiesException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,14 @@ public class MockClientContext implements ClientContext {
private MockRegistrationClient regClient;
private ByteBufAllocator allocator;

static MockClientContext create(MockBookies mockBookies) throws Exception {
ClientConfiguration conf = new ClientConfiguration();
static MockClientContext create(MockBookies mockBookies, ClientConfiguration conf,
MockRegistrationClient regClient, EnsemblePlacementPolicy placementPolicy,
BookieWatcher bookieWatcher) throws Exception {
OrderedScheduler scheduler = OrderedScheduler.newSchedulerBuilder().name("mock-executor").numThreads(1).build();
MockRegistrationClient regClient = new MockRegistrationClient();
EnsemblePlacementPolicy placementPolicy = new DefaultEnsemblePlacementPolicy();
BookieWatcherImpl bookieWatcherImpl = new BookieWatcherImpl(conf, placementPolicy,
regClient,
new DefaultBookieAddressResolver(regClient),
NullStatsLogger.INSTANCE);
bookieWatcherImpl.initialBlockingBookieRead();

return new MockClientContext()
.setConf(ClientInternalConf.fromConfig(conf))
.setLedgerManager(new MockLedgerManager())
.setBookieWatcher(bookieWatcherImpl)
.setBookieWatcher(bookieWatcher)
.setPlacementPolicy(placementPolicy)
.setRegistrationClient(regClient)
.setBookieClient(new MockBookieClient(scheduler, mockBookies))
Expand All @@ -79,6 +72,18 @@ static MockClientContext create(MockBookies mockBookies) throws Exception {
.setIsClientClosed(() -> false);
}

static MockClientContext create(MockBookies mockBookies) throws Exception {
ClientConfiguration conf = new ClientConfiguration();
MockRegistrationClient regClient = new MockRegistrationClient();
EnsemblePlacementPolicy placementPolicy = new DefaultEnsemblePlacementPolicy();
BookieWatcherImpl bookieWatcherImpl = new BookieWatcherImpl(conf, placementPolicy,
regClient,
new DefaultBookieAddressResolver(regClient),
NullStatsLogger.INSTANCE);
bookieWatcherImpl.initialBlockingBookieRead();
return create(mockBookies, conf, regClient, placementPolicy, bookieWatcherImpl);
}

static MockClientContext create() throws Exception {
MockBookies mockBookies = new MockBookies();
return create(mockBookies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,26 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.bookkeeper.client.BKException.Code;
import org.apache.bookkeeper.client.api.LedgerMetadata;
import org.apache.bookkeeper.client.api.WriteFlag;
import org.apache.bookkeeper.common.util.OrderedExecutor;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.discover.MockRegistrationClient;
import org.apache.bookkeeper.net.BookieId;
import org.apache.bookkeeper.net.BookieSocketAddress;
import org.apache.bookkeeper.proto.BookieClient;
import org.apache.bookkeeper.proto.MockBookies;
import org.apache.bookkeeper.stats.NullStatsLogger;
import org.apache.bookkeeper.versioning.Versioned;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -60,6 +72,10 @@ public void setup() {
lh = mock(LedgerHandle.class);
when(lh.getDistributionSchedule())
.thenReturn(new RoundRobinDistributionSchedule(3, 3, 2));
when(lh.getWriteFlags())
.thenReturn(EnumSet.of(WriteFlag.DEFERRED_SYNC));
Map<Integer, BookieId> failedBookies = new HashMap<>();
failedBookies.put(1, BookieId.parse("0.0.0.0:3181"));
byte[] data = "test-pending-add-op".getBytes(UTF_8);
payload = Unpooled.wrappedBuffer(data);
payload.writerIndex(data.length);
Expand Down Expand Up @@ -87,4 +103,69 @@ public void testExecuteAfterCancelled() {
assertNull(op.lh);
}

@Test
public void testLedgerHandleWithDeferredSyncDuringRecoveryAdd() throws Exception {
final BookieId b1 = new BookieSocketAddress("b1", 3181).toBookieId();
final BookieId b2 = new BookieSocketAddress("b2", 3181).toBookieId();
final BookieId b3 = new BookieSocketAddress("b3", 3181).toBookieId();
MockClientContext clientCtx = MockClientContext.create();
Versioned<LedgerMetadata> md = ClientUtil.setupLedger(clientCtx, 0,
LedgerMetadataBuilder.create().withInRecoveryState().newEnsembleEntry(0L,
Lists.newArrayList(b1, b2, b3)));
LedgerHandle lh = new LedgerHandle(clientCtx, 0, md, BookKeeper.DigestType.CRC32C,
ClientUtil.PASSWD, EnumSet.of(WriteFlag.DEFERRED_SYNC));
lh.notifyWriteFailed(0, b1);
AtomicInteger rcHolder = new AtomicInteger(-0xdead);
PendingAddOp op = PendingAddOp.create(
lh, mockClientContext, lh.getCurrentEnsemble(),
payload, EnumSet.of(WriteFlag.DEFERRED_SYNC),
(rc, handle, entryId, qwcLatency, ctx) -> {
rcHolder.set(rc);
}, null).enableRecoveryAdd();
assertSame(lh, op.lh);
op.setEntryId(0);
lh.pendingAddOps.add(op);
lh.clientCtx.getMainWorkerPool().submitOrdered(lh.ledgerId, (Callable<Void>) () -> {
op.run();
return null;
}).get();
}

@Test
public void testReadOnlyLedgerHandleWithNotEnoughBookiesExceptionDuringRecoveryAdd() throws Exception {
final BookieId b1 = new BookieSocketAddress("b1", 3181).toBookieId();
final BookieId b2 = new BookieSocketAddress("b2", 3181).toBookieId();
final BookieId b3 = new BookieSocketAddress("b3", 3181).toBookieId();
MockBookies mockBookies = new MockBookies();
ClientConfiguration conf = new ClientConfiguration();
MockRegistrationClient regClient = new MockRegistrationClient();
EnsemblePlacementPolicy placementPolicy = new DefaultEnsemblePlacementPolicy();
BookieWatcher bookieWatcher = new MockBookieWatcher(conf, placementPolicy,
regClient,
new DefaultBookieAddressResolver(regClient),
NullStatsLogger.INSTANCE);
ClientContext clientCtx = MockClientContext.create(mockBookies, conf, regClient, placementPolicy,
bookieWatcher);
Versioned<LedgerMetadata> md = ClientUtil.setupLedger(clientCtx, 0,
LedgerMetadataBuilder.create().withInRecoveryState().newEnsembleEntry(0L,
Lists.newArrayList(b1, b2, b3)));
ReadOnlyLedgerHandle lh = new ReadOnlyLedgerHandle(clientCtx, 0, md, BookKeeper.DigestType.CRC32C,
ClientUtil.PASSWD, true);
lh.notifyWriteFailed(0, b1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the normal case, it can't happen.
lh.notifyWriteFailed(0, b1); should not before op.run(), it just for test I guess.
I'm curious about why the problem happened. Could you add a test to mock the real case? thanks a lot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to give a scenario where the problem might occur.
The client tries to write 945 and 946 to the same ledger (id=1256) successively, and writes 3 copies of each entry. Write 945 is successful but one of the bookies returns an exception, so LedgerHandle.delayedWriteFailedBookies will not be empty. Then when writing 946, if a BKNotEnoughBookiesException is thrown, an NPE issue will occur.

It is a little troublesome to construct the above scenario, so I ensure that delayedWriteFailedBookies is not empty (by lh.notifyWriteFailed(0, b1)) from the beginning of my test case, so that I can quickly verify the npe issue.

It should be noted that this is a scenario I deduced from the error log. In the log, only 946 write failures caused by BKNotEnoughBookiesException can be seen.

AtomicInteger rcHolder = new AtomicInteger(-0xdead);
PendingAddOp op = PendingAddOp.create(
lh, mockClientContext, lh.getCurrentEnsemble(),
payload, WriteFlag.NONE,
(rc, handle, entryId, qwcLatency, ctx) -> {
rcHolder.set(rc);
}, null).enableRecoveryAdd();
assertSame(lh, op.lh);
op.setEntryId(0);
lh.pendingAddOps.add(op);
lh.clientCtx.getMainWorkerPool().submitOrdered(lh.ledgerId, (Callable<Void>) () -> {
op.run();
return null;
}).get();
}

}