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

[Refactor] StreamInput, StreamOutput, Writeable to core library #7783

Closed
wants to merge 13 commits into from

Conversation

nknize
Copy link
Collaborator

@nknize nknize commented May 26, 2023

This is a big commit as it refactors the StreamInput and StreamOutput core serialization and supporting classes from the server module to the core library such that it can be reused and overridden in cloud native and serverless applications. The majority of the changes in this commit are the import statements. Noteable refactors include:

  • StreamInput
  • StreamOutput
  • Writeable
  • NamedWriteable
  • NamedWriteableRegistry
  • Index
  • ShardId
  • BytesReference
  • ByteArray
  • BigArray
  • SecureString
  • Text
  • ParsingException
  • RestStatus

NOTE: The below dependency PRs reduce the surface area of this commit. However, it will still be a large commit as reducing to incremental commits adds a substantial intermediate "generics" framework that only serves to complicate the implementation. Thus we instead opt to solve the refactor in one swooping change.

relates #5910
depends on #7646
depends on #7780
depends on #8035

@nknize nknize added enhancement Enhancement or improvement to existing feature or request skip-changelog v2.9.0 'Issues and PRs related to version v2.9.0' labels May 26, 2023
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: null ❌
  • URL: null
  • CommitID: 0336cf0
    Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green.
    Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: null ❌
  • URL: null
  • CommitID: 91be372
    Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green.
    Is the failure a flaky test unrelated to your change?

1 similar comment
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: null ❌
  • URL: null
  • CommitID: 91be372
    Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green.
    Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

github-actions bot commented Jun 7, 2023

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

github-actions bot commented Jun 7, 2023

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

github-actions bot commented Jun 7, 2023

Gradle Check (Jenkins) Run Completed with:

nknize added 13 commits June 12, 2023 11:18
OpenSearchException uses an enumerator to create a handler for each
serializable Exception implementation. The enumerator is to make it easy
to store in an immutable map. The problem with this Hack is that it
makes it difficult to unwind the tight coupling of StreamInput with the
OpenSearchException class. This commit switches from using an enumerator
to a registry in the opensearch-core library in order to support
serverless and cloud native implementations outside of the server
module.

This commit also refactors base primitive serialization readers and
writers from StreamInput and StreamOutput to BaseStreamInput and
BaseStreamOutput, respectively.

Signed-off-by: Nicholas Walter Knize <[email protected]>
This is a big commit as it refactors the StreamInput and StreamOutput
core serialization and supporting classes from the server module to the
core library such that it can be reused and overridden in cloud native
and serverless applications. The majority of the changes in this commit
are the import statements. Noteable refactors include:

* StreamInput
* StreamOutput
* Writeable
* NamedWriteable
* NamedWriteableRegistry
* Index
* ShardId
* BytesReference
* ByteArray
* BigArray
* SecureString
* Text
* ParsingException
* RestStatus

Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
Signed-off-by: Nicholas Walter Knize <[email protected]>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@@ -107,8 +107,12 @@ public static class IndexResult {
static {
PARSER.declareBoolean(optionalConstructorArg(), new ParseField("closed"));
PARSER.declareObject(optionalConstructorArg(), (p, c) -> {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, p.currentToken(), p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like the full import is there already:

Suggested change
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(
XContentParserUtils.ensureExpectedToken(

p.currentToken(),
p
);
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);

static {
registerExceptionHandle(
new BaseOpenSearchExceptionHandle(
org.opensearch.core.index.snapshots.IndexShardSnapshotFailedException.class,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why snapshots are moving core? I am not sure these functionality belong to it

*
* @return an array of all registered handle IDs
*/
static int[] ids() {
Copy link
Collaborator

@reta reta Jun 12, 2023

Choose a reason for hiding this comment

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

Why do we need to duplicate that vs calling OpenSearchExceptionHandleRegistry.ids()?

* @opensearch.internal
*/
public class StreamsUtil {
public static int readFully(InputStream reader, byte[] dest, int offset, int len) throws IOException {
Copy link
Collaborator

@reta reta Jun 12, 2023

Choose a reason for hiding this comment

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

InputStream::readNBytes does exactly that, no need for additional utils:

reader.readNBytes(dest, offset, len);

@@ -154,6 +186,10 @@ public BytesRef readBytesRef(int length) throws IOException {
return new BytesRef(bytes, 0, length);
}

public void readFully(byte[] b) throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method won't consume full stream, it will consume up to b length, I think readBytes (overloaded version) would be a better name:

public void readBytes(byte[] b)

@nknize
Copy link
Collaborator Author

nknize commented Jun 19, 2023

After merging #8035 it was easier to open a new PR #8157 instead of rebasing and dealing with merge conflicts. I will plan to close this PR once #8157 lands.

@@ -313,7 +313,7 @@ public int available() {
}

@Override
protected void ensureCanReadBytes(int bytesToRead) throws EOFException {
public void ensureCanReadBytes(int bytesToRead) throws EOFException {
Copy link
Contributor

@rishabhmaurya rishabhmaurya Jun 22, 2023

Choose a reason for hiding this comment

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

moving classes around may require changing access modifiers but comes with a security risk. This one seems harmless however we can move most the of consumers of this classes to lib:core from server. I also see its unit tests are still in server.
If we are not intending to move unit tests and other consumers of this class here, then we should create a list of TODOs, to change them back to their original access modifiers once consumers are moved to lib

Copy link
Collaborator Author

@nknize nknize Jun 22, 2023

Choose a reason for hiding this comment

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

Yeah @rishabhmaurya, I actually like the idea of refactoring these classes as well. The reason I held off is because one of those inheritors, BufferedChecksumStreamInput, is inside the o.o.index.translog namespace and we may want to consider having this in a dedicated :libs:opensearch-translog library instead of core so we don't set a precedent of having translog logic in the core library. Especially since we're talking about scenarios where we don't have a translog.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh by the way.. this PR is going to get closed in favor of #8157 so have a look at that one instead if you wouldn't mind.

@nknize
Copy link
Collaborator Author

nknize commented Jul 11, 2023

superseded by #8035

@nknize nknize closed this Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement or improvement to existing feature or request skip-changelog v2.9.0 'Issues and PRs related to version v2.9.0'
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants