This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
[PaxosStateLog] Parallel reads and processing #4758
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type: improvement | ||
improvement: | ||
description: '`PaxosStateLogImpl` now uses a reentrant read write lock for synchronisation | ||
instead of a reentrant lock. This slightly improves performance and allows for | ||
higher read throughput by parallelising reads.' | ||
links: | ||
- https://github.com/palantir/atlasdb/pull/4758 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
leader-election-impl/src/main/java/com/palantir/paxos/PaxosStateLogBatchReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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 com.palantir.paxos; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.LongStream; | ||
|
||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.palantir.atlasdb.futures.AtlasFutures; | ||
import com.palantir.common.base.Throwables; | ||
import com.palantir.common.concurrent.NamedThreadFactory; | ||
import com.palantir.common.concurrent.PTExecutors; | ||
import com.palantir.common.persist.Persistable; | ||
|
||
public class PaxosStateLogBatchReader<V extends Persistable & Versionable> implements AutoCloseable { | ||
private final PaxosStateLog<V> delegate; | ||
private final Persistable.Hydrator<V> hydrator; | ||
private final ListeningExecutorService executor; | ||
|
||
public PaxosStateLogBatchReader(PaxosStateLog<V> delegate, Persistable.Hydrator<V> hydrator, int numThreads) { | ||
this.delegate = delegate; | ||
this.hydrator = hydrator; | ||
this.executor = MoreExecutors.listeningDecorator( | ||
PTExecutors.newFixedThreadPool(numThreads, new NamedThreadFactory("psl-reader", true))); | ||
} | ||
|
||
/** | ||
* Reads entries from startSequence (inclusive) to startSequence + numEntries (exclusive) from the delegate log. | ||
* | ||
* @param startSequence first sequence to read | ||
* @param numEntries number of entries to read | ||
* @return a list of paxos rounds for all the present entries in the delegate log | ||
*/ | ||
public List<PaxosRound<V>> readBatch(long startSequence, int numEntries) { | ||
return AtlasFutures.getUnchecked( | ||
Futures.allAsList( | ||
LongStream.range(startSequence, startSequence + numEntries) | ||
.mapToObj(sequence -> executor.submit(() -> singleRead(sequence))) | ||
.collect(Collectors.toList()))) | ||
.stream() | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Optional<PaxosRound<V>> singleRead(long sequence) { | ||
try { | ||
return Optional.ofNullable(delegate.readRound(sequence)) | ||
.map(bytes -> PaxosRound.of(sequence, hydrator.hydrateFromBytes(bytes))); | ||
} catch (IOException e) { | ||
throw Throwables.rewrapAndThrowUncheckedException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
executor.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
leader-election-impl/src/test/java/com/palantir/paxos/PaxosStateLogBatchReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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 com.palantir.paxos; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.LongStream; | ||
|
||
import org.junit.Test; | ||
|
||
import com.google.common.util.concurrent.Uninterruptibles; | ||
|
||
public class PaxosStateLogBatchReaderTest { | ||
private static final int START_SEQUENCE = 123; | ||
private static final int BATCH_SIZE = 250; | ||
private static final List<PaxosRound<PaxosValue>> EXPECTED_ROUNDS = LongStream | ||
.range(START_SEQUENCE, START_SEQUENCE + BATCH_SIZE) | ||
.mapToObj(PaxosStateLogBatchReaderTest::valueForRound) | ||
.map(value -> PaxosRound.of(value.seq, value)) | ||
.collect(Collectors.toList()); | ||
|
||
private PaxosStateLog<PaxosValue> mockLog = mock(PaxosStateLog.class); | ||
|
||
@Test | ||
public void readConsecutiveBatch() throws IOException { | ||
when(mockLog.readRound(anyLong())) | ||
.thenAnswer(invocation -> valueForRound((long) invocation.getArguments()[0]).persistToBytes()); | ||
|
||
try (PaxosStateLogBatchReader<PaxosValue> reader = createReader()) { | ||
assertThat(reader.readBatch(START_SEQUENCE, BATCH_SIZE)).isEqualTo(EXPECTED_ROUNDS); | ||
} | ||
} | ||
|
||
@Test | ||
public void exceptionsArePropagated() throws IOException { | ||
IOException ioException = new IOException("test"); | ||
when(mockLog.readRound(anyLong())) | ||
.thenAnswer(invocation -> { | ||
long sequence = (long) invocation.getArguments()[0]; | ||
if (sequence == 200) { | ||
throw ioException; | ||
} | ||
return valueForRound(sequence).persistToBytes(); | ||
}); | ||
|
||
try (PaxosStateLogBatchReader<PaxosValue> reader = createReader()) { | ||
assertThatThrownBy(() -> reader.readBatch(START_SEQUENCE, BATCH_SIZE)).isInstanceOf(RuntimeException.class); | ||
} | ||
} | ||
|
||
@Test | ||
public void readBatchFiltersOutNulls() throws IOException { | ||
Predicate<Long> isOdd = num -> num % 2 != 0; | ||
when(mockLog.readRound(anyLong())) | ||
.thenAnswer(invocation -> { | ||
long sequence = (long) invocation.getArguments()[0]; | ||
if (!isOdd.test(sequence)) { | ||
return null; | ||
} | ||
return valueForRound(sequence).persistToBytes(); | ||
}); | ||
|
||
try (PaxosStateLogBatchReader<PaxosValue> reader = createReader()) { | ||
assertThat(reader.readBatch(START_SEQUENCE, BATCH_SIZE)) | ||
.isEqualTo(EXPECTED_ROUNDS.stream() | ||
.filter(round -> isOdd.test(round.sequence())) | ||
.collect(Collectors.toList())); | ||
} | ||
} | ||
|
||
@Test | ||
public void noResultsReturnsEmptyList() throws IOException { | ||
when(mockLog.readRound(anyLong())).thenReturn(null); | ||
|
||
try (PaxosStateLogBatchReader<PaxosValue> reader = createReader()) { | ||
assertThat(reader.readBatch(START_SEQUENCE, BATCH_SIZE)).isEmpty(); | ||
} | ||
} | ||
|
||
@Test | ||
public void executionsGetBatched() throws IOException { | ||
when(mockLog.readRound(anyLong())).thenAnswer(invocation -> { | ||
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); | ||
return valueForRound((long) invocation.getArguments()[0]).persistToBytes(); | ||
}); | ||
|
||
try (PaxosStateLogBatchReader<PaxosValue> reader = createReader()) { | ||
Instant startInstant = Instant.now(); | ||
reader.readBatch(START_SEQUENCE, BATCH_SIZE); | ||
assertThat(Duration.between(Instant.now(), startInstant)).isLessThan(Duration.ofSeconds(1)); | ||
} | ||
} | ||
|
||
private PaxosStateLogBatchReader<PaxosValue> createReader() { | ||
return new PaxosStateLogBatchReader<>(mockLog, PaxosValue.BYTES_HYDRATOR, 100); | ||
} | ||
|
||
private static PaxosValue valueForRound(long round) { | ||
byte[] bytes = new byte[] { 1, 2, 3 }; | ||
return new PaxosValue("someLeader", round, bytes); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't #readRound have a exclusive lock in PaxosStateLogImpl? How are you going to rewire this stuff, will there be a different impl just for reading the files that doesn't log?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's something we have to discuss, but it's a solvable problem just outside the scope of this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I think we should be good to change that to a R/W lock (though be careful when reading that class to make sure it's safe!)