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

HDDS-12604. Reduce duplication in TestContainerStateMachine #8104

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

chiacyu
Copy link
Contributor

@chiacyu chiacyu commented Mar 18, 2025

What changes were proposed in this pull request?

In TestContainerStateMachine, some of the when().return() section are duplicated and can be reduced by wrapping into helper functions. Please take a look, thanks!

What is the link to the Apache JIRA

HDDS-12604

How was this patch tested?

CI

@adoroszlai
Copy link
Contributor

@peterxcli would you like to review?

Copy link
Contributor

@peterxcli peterxcli left a comment

Choose a reason for hiding this comment

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

Thanks @chiacyu for this cleanup. Left some comment inline, PTAL.

I have 2 additional suggestion that I can't directly comment then inline:

  1. We can have those helper to the bottom of this class then this patch would be more readable.
  2. I think we can also cleanup the "set value with property name" part in testWriteTimeout:

Want:

Field writeChunkWaitMaxNs = stateMachine.getClass().getDeclaredField("writeChunkWaitMaxNs");
writeChunkWaitMaxNs.setAccessible(true);
writeChunkWaitMaxNs.set(stateMachine, 1000_000_000);

to be:

@Test
public void testWriteTimout() throws Exception {
  conf.setDuration(HDDS_CONTAINER_RATIS_STATEMACHINE_WRITE_WAIT_INTERVAL, 1000_000_000, TimeUnit.NANOSECONDS)
  stateMachine = new ContainerStateMachine(null,
        RaftGroupId.randomId(), dispatcher, controller, executor, ratisServer, conf, "containerOp");


public final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context, String content,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context, String content,
private final void setUpMockRequestProtoReturn(ContainerStateMachine.Context context,

Since all content are the same, it can be moved to class Initialization

abstract class TestContainerStateMachine {
  ...
  private final ByteString CONTAINER_DATA = ByteString.copyFromUtf8("Test Data");

assertEquals(ContainerProtos.Result.CONTAINER_UNHEALTHY, sce.getResult());
}

public final void setUpMockDispatcherReturn(boolean failWithException) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
public final void setUpMockDispatcherReturn(boolean failWithException) {
private final void setUpMockDispatcherReturn(boolean failWithException) {

ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(1).setLocalID(1).build()).build())
.setContainerID(1)
.setDatanodeUuid(UUID.randomUUID().toString()).build());
setUpMockRequestProtoReturn(context, "Test data", 1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Suggested change
setUpMockRequestProtoReturn(context, "Test data", 1, 1);
setUpMockRequestProtoReturn(context, 1, 1);

assertResults(failWithException, throwable);

// Writing data to another container(containerId 2) should also fail.
setUpMockRequestProtoReturn(context, "Test Data", 2, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Suggested change
setUpMockRequestProtoReturn(context, "Test Data", 2, 1);
setUpMockRequestProtoReturn(context, 2, 1);

@@ -129,6 +129,31 @@ public void testWriteFailure(boolean failWithException) throws ExecutionExceptio
TransactionContext trx = mock(TransactionContext.class);
ContainerStateMachine.Context context = mock(ContainerStateMachine.Context.class);
when(trx.getStateMachineContext()).thenReturn(context);

setUpMockDispatcherReturn(failWithException);
setUpMockRequestProtoReturn(context, "Test Data", 1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Suggested change
setUpMockRequestProtoReturn(context, "Test Data", 1, 1);
setUpMockRequestProtoReturn(context, 1, 1);

AtomicReference<Throwable> throwable = new AtomicReference<>(null);
}

public final Function<Throwable, ? extends Message> getThrowableSetter(AtomicReference<Throwable> throwable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

another suggestion is replace getThrowableSetter with:

private static class ThrowableCatcher {
  private final AtomicReference<Throwable> caught = new AtomicReference<>(null);
  
  public Function<Throwable, ? extends Message> asSetter() {
    return t -> {
      caught.set(t);
      return null;
    };
  }

  public Throwable getReceived() {
    return caught.get();
  }

  public void reset() {
    caught.set(null);
  }
}

Then we can use:

ThrowableCatcher catcher = new ThrowableCatcher();
stateMachine.write(entry, trx).exceptionally(catcher.asSetter()).get();
assertNotNull(catcher.getReceived());

But this is up to you, this change has few benefit at this time.

@peterxcli
Copy link
Contributor

Hi @chiacyu, if you have time, it would be really helpful to include the CI run link in the PR description. Thank you!

@chiacyu
Copy link
Contributor Author

chiacyu commented Mar 19, 2025

Hi, @peterxcli
Thanks for the reviews indeed!, would apply the review comments soon. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants