-
Notifications
You must be signed in to change notification settings - Fork 140
Parallelize graph writes #542
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
Open
MarkWolters
wants to merge
17
commits into
main
Choose a base branch
from
parallelize_graph_writes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a67bfcf
initial commit
MarkWolters 16aaaf6
adding async file channel usage
MarkWolters 9b4dea1
expose threadpool configuration
MarkWolters 7433443
test application added
MarkWolters f2df4da
add FADC to testing
MarkWolters b45766c
some final details
MarkWolters 09bc7d9
added read test to test code and fixed data corruption bug
MarkWolters 9bdd64c
update regression tests to use parallel writes
MarkWolters 2cfa7c9
speedup for tests
MarkWolters 4646ba5
Merge branch 'main' into parallelize_graph_writes
MarkWolters ec68bda
removed option of parallel index write to mem + sequential to disk ou…
MarkWolters a0c89a7
backpressure controls and clearer buffer ownership
MarkWolters 67edfae
api and comment cleanup, added parallel Write jmh benchmark
MarkWolters 09eb70f
added create method for ByteBufferIndexWriter and moved comments
MarkWolters 7daf69e
Update Tasks to write ranges of ordinals rather than separate task fo…
MarkWolters 53a6951
batching based on dataset size
MarkWolters f6240b6
Merge branch 'main' into parallelize_graph_writes
MarkWolters 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
190 changes: 190 additions & 0 deletions
190
jvector-base/src/main/java/io/github/jbellis/jvector/disk/ByteBufferIndexWriter.java
This file contains hidden or 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,190 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * 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 io.github.jbellis.jvector.disk; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
|
|
||
| /** | ||
| * An IndexWriter implementation backed by a ByteBuffer for in-memory record building. | ||
| * This allows existing Feature.writeInline() implementations to write to memory buffers | ||
| * that can later be bulk-written to disk. | ||
| * <p> | ||
| * Byte order is set to BIG_ENDIAN to match Java's DataOutput specification and ensure | ||
| * cross-platform compatibility. | ||
| * <p> | ||
| * Not thread-safe. Each thread should use its own instance. | ||
| */ | ||
| public class ByteBufferIndexWriter implements IndexWriter { | ||
| private final ByteBuffer buffer; | ||
| private final int initialPosition; | ||
|
|
||
| /** | ||
| * Creates a writer that writes to the given buffer starting at its current position. | ||
| * The buffer's position will be advanced as data is written. | ||
| * The buffer's byte order is set to BIG_ENDIAN to match DataOutput behavior. | ||
| */ | ||
| public ByteBufferIndexWriter(ByteBuffer buffer) { | ||
| this.buffer = buffer; | ||
| this.buffer.order(ByteOrder.BIG_ENDIAN); | ||
| this.initialPosition = buffer.position(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a writer with a new heap ByteBuffer of the given capacity. | ||
| * The buffer uses BIG_ENDIAN byte order. | ||
| */ | ||
| public static ByteBufferIndexWriter allocate(int capacity) { | ||
| ByteBuffer buffer = ByteBuffer.allocate(capacity); | ||
| buffer.order(ByteOrder.BIG_ENDIAN); | ||
| return new ByteBufferIndexWriter(buffer); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a writer with a new direct ByteBuffer of the given capacity. | ||
| * The buffer uses BIG_ENDIAN byte order. | ||
| */ | ||
| public static ByteBufferIndexWriter allocateDirect(int capacity) { | ||
| ByteBuffer buffer = ByteBuffer.allocateDirect(capacity); | ||
| buffer.order(ByteOrder.BIG_ENDIAN); | ||
| return new ByteBufferIndexWriter(buffer); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the underlying buffer. The buffer's position will be at the end of written data. | ||
| */ | ||
| public ByteBuffer getBuffer() { | ||
| return buffer; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a read-only view of the written data (from initial position to current position). | ||
| */ | ||
| public ByteBuffer getWrittenData() { | ||
| int currentPos = buffer.position(); | ||
| buffer.position(initialPosition); | ||
| ByteBuffer slice = buffer.slice(); | ||
| slice.limit(currentPos - initialPosition); | ||
| buffer.position(currentPos); | ||
| return slice.asReadOnlyBuffer(); | ||
| } | ||
|
|
||
| /** | ||
| * Resets the buffer position to the initial position, allowing reuse. | ||
| */ | ||
| public void reset() { | ||
| buffer.position(initialPosition); | ||
| } | ||
|
|
||
| @Override | ||
| public long position() { | ||
| return buffer.position() - initialPosition; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // No-op for ByteBuffer | ||
| } | ||
|
|
||
| // DataOutput methods | ||
|
|
||
| @Override | ||
| public void write(int b) { | ||
| buffer.put((byte) b); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b) { | ||
| buffer.put(b); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) { | ||
| buffer.put(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBoolean(boolean v) { | ||
| buffer.put((byte) (v ? 1 : 0)); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeByte(int v) { | ||
| buffer.put((byte) v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeShort(int v) { | ||
| buffer.putShort((short) v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeChar(int v) { | ||
| buffer.putChar((char) v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeInt(int v) { | ||
| buffer.putInt(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeLong(long v) { | ||
| buffer.putLong(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeFloat(float v) { | ||
| buffer.putFloat(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeDouble(double v) { | ||
| buffer.putDouble(v); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBytes(String s) { | ||
| int len = s.length(); | ||
| for (int i = 0; i < len; i++) { | ||
| buffer.put((byte) s.charAt(i)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeChars(String s) { | ||
| int len = s.length(); | ||
| for (int i = 0; i < len; i++) { | ||
| buffer.putChar(s.charAt(i)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeUTF(String s) throws IOException { | ||
| // Use standard DataOutputStream UTF encoding | ||
| byte[] bytes = s.getBytes("UTF-8"); | ||
| int utflen = bytes.length; | ||
|
|
||
| if (utflen > 65535) { | ||
MarkWolters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new IOException("encoded string too long: " + utflen + " bytes"); | ||
| } | ||
|
|
||
| buffer.putShort((short) utflen); | ||
| buffer.put(bytes); | ||
| } | ||
| } | ||
This file contains hidden or 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
42 changes: 42 additions & 0 deletions
42
jvector-base/src/main/java/io/github/jbellis/jvector/graph/disk/GraphIndexWriterTypes.java
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * 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 io.github.jbellis.jvector.graph.disk; | ||
|
|
||
| /** | ||
| * Enum defining the available types of graph index writers. | ||
| * <p> | ||
| * Different writer types offer different tradeoffs between performance, | ||
| * compatibility, and features. | ||
| */ | ||
| public enum GraphIndexWriterTypes { | ||
MarkWolters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Sequential on-disk writer optimized for write-once scenarios. | ||
| * Writes all data sequentially without seeking back, making it suitable | ||
| * for cloud storage or systems that optimize for sequential I/O. | ||
| * Writes header as footer. Does not support incremental updates. | ||
| * Accepts any IndexWriter. | ||
| */ | ||
| ON_DISK_SEQUENTIAL, | ||
|
|
||
| /** | ||
| * Parallel on-disk writer that uses asynchronous I/O for improved throughput. | ||
| * Builds records in parallel across multiple threads and writes them | ||
| * asynchronously using AsynchronousFileChannel. | ||
| * Requires a Path to be provided for async file channel access. | ||
| */ | ||
| ON_DISK_PARALLEL | ||
marianotepper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.