-
Notifications
You must be signed in to change notification settings - Fork 961
Support markLedgerReplicated command. #4032
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
horizonzy
wants to merge
7
commits into
apache:master
Choose a base branch
from
horizonzy:support-markLedgerReplicated-command
base: master
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
70d0ef1
Support markLedgerReplicated command.
horizonzy dcbf6c9
tune doc.
horizonzy 04b572a
Code clean.
horizonzy c8af331
Fix checkstyle.
horizonzy ca9bd41
Fix checkstyle.
horizonzy 1e67cff
Fix ci.
horizonzy a84a24b
Fix ci.
horizonzy 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 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
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
135 changes: 135 additions & 0 deletions
135
...va/org/apache/bookkeeper/tools/cli/commands/autorecovery/MarkLedgerReplicatedCommand.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,135 @@ | ||
| /* | ||
| * 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.tools.cli.commands.autorecovery; | ||
|
|
||
| import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithLedgerManagerFactory; | ||
|
|
||
| import com.beust.jcommander.Parameter; | ||
| import com.google.common.util.concurrent.UncheckedExecutionException; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.concurrent.ExecutionException; | ||
| import lombok.Setter; | ||
| import lombok.experimental.Accessors; | ||
| import org.apache.bookkeeper.conf.ServerConfiguration; | ||
| import org.apache.bookkeeper.meta.LedgerUnderreplicationManager; | ||
| import org.apache.bookkeeper.meta.exceptions.MetadataException; | ||
| import org.apache.bookkeeper.replication.ReplicationException; | ||
| import org.apache.bookkeeper.tools.cli.helpers.BookieCommand; | ||
| import org.apache.bookkeeper.tools.framework.CliFlags; | ||
| import org.apache.bookkeeper.tools.framework.CliSpec; | ||
| import org.apache.bookkeeper.util.IOUtils; | ||
| import org.apache.bookkeeper.util.LedgerIdFormatter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class MarkLedgerReplicatedCommand extends BookieCommand<MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags> { | ||
|
|
||
| static final Logger LOG = LoggerFactory.getLogger(MarkLedgerReplicatedCommand.class); | ||
| private static final String NAME = "markledgerreplicated"; | ||
| private static final String DESC = "Mark a ledger replicated."; | ||
| private static final String DEFAULT = ""; | ||
| private LedgerIdFormatter ledgerIdFormatter; | ||
|
|
||
| public MarkLedgerReplicatedCommand() { | ||
| this(new MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags()); | ||
| } | ||
|
|
||
| public MarkLedgerReplicatedCommand(LedgerIdFormatter ledgerIdFormatter) { | ||
| this(new MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags()); | ||
| this.ledgerIdFormatter = ledgerIdFormatter; | ||
| } | ||
|
|
||
| private MarkLedgerReplicatedCommand(MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags flags) { | ||
| super(CliSpec.<MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags>newBuilder().withName(NAME) | ||
| .withDescription(DESC).withFlags(flags).build()); | ||
| } | ||
|
|
||
| /** | ||
| * Flags for delete ledger command. | ||
| */ | ||
| @Accessors(fluent = true) | ||
| @Setter | ||
| public static class MarkLedgerReplicatedFlags extends CliFlags { | ||
|
|
||
| @Parameter(names = {"-l", "--ledgerid"}, description = "Ledger ID", required = true) | ||
| private long ledgerId; | ||
|
|
||
| @Parameter(names = {"-f", | ||
| "--force"}, description = "Whether to force mark the Ledger replicated without prompt..?") | ||
| private boolean force; | ||
|
|
||
| @Parameter(names = {"-lf", "--ledgeridformatter"}, description = "Set ledger id formatter") | ||
| private String ledgerIdFormatter = DEFAULT; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean apply(ServerConfiguration conf, MarkLedgerReplicatedFlags cmdFlags) { | ||
| initLedgerIdFormatter(conf, cmdFlags); | ||
| try { | ||
| return markLedgerReplicated(conf, cmdFlags); | ||
| } catch (Exception e) { | ||
| throw new UncheckedExecutionException(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| private void initLedgerIdFormatter(ServerConfiguration conf, | ||
| MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags flags) { | ||
| if (null == ledgerIdFormatter && !flags.ledgerIdFormatter.equals(DEFAULT)) { | ||
| this.ledgerIdFormatter = LedgerIdFormatter.newLedgerIdFormatter(flags.ledgerIdFormatter, conf); | ||
| } else if (null == ledgerIdFormatter && flags.ledgerIdFormatter.equals(DEFAULT)) { | ||
| this.ledgerIdFormatter = LedgerIdFormatter.newLedgerIdFormatter(conf); | ||
| } | ||
| } | ||
|
|
||
| private boolean markLedgerReplicated(ServerConfiguration bkConf, | ||
| MarkLedgerReplicatedCommand.MarkLedgerReplicatedFlags flags) | ||
| throws ExecutionException, MetadataException { | ||
| return runFunctionWithLedgerManagerFactory(bkConf, mFactory -> { | ||
| LedgerUnderreplicationManager underreplicationManager; | ||
| try { | ||
| underreplicationManager = mFactory.newLedgerUnderreplicationManager(); | ||
| if (flags.ledgerId < 0) { | ||
| LOG.error("Ledger id error."); | ||
| return false; | ||
| } | ||
| boolean confirm = false; | ||
| if (!flags.force) { | ||
| confirm = IOUtils.confirmPrompt( | ||
| "Are your sure to mark Ledger:" + ledgerIdFormatter.formatLedgerId(flags.ledgerId) | ||
| + " replicated?"); | ||
| } | ||
| if (flags.force || confirm) { | ||
| underreplicationManager.markLedgerReplicated(flags.ledgerId); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } catch (ReplicationException e) { | ||
| throw new UncheckedExecutionException("Failed to new ledger underreplicated manager", e); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new UncheckedExecutionException("Interrupted on newing ledger underreplicated manager", e); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| } |
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
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
93 changes: 93 additions & 0 deletions
93
...rg/apache/bookkeeper/tools/cli/commands/autorecovery/MarkLedgerReplicatedCommandTest.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,93 @@ | ||
| /* | ||
| * 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.tools.cli.commands.autorecovery; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.powermock.api.mockito.PowerMockito.doNothing; | ||
|
|
||
| import org.apache.bookkeeper.meta.LedgerManagerFactory; | ||
| import org.apache.bookkeeper.meta.LedgerUnderreplicationManager; | ||
| import org.apache.bookkeeper.replication.ReplicationException; | ||
| import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase; | ||
| import org.apache.bookkeeper.util.IOUtils; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Unit test for {@link MarkLedgerReplicatedCommand}. | ||
| */ | ||
| public class MarkLedgerReplicatedCommandTest extends BookieCommandTestBase { | ||
|
|
||
| private LedgerManagerFactory factory; | ||
| private LedgerUnderreplicationManager underreplicationManager; | ||
|
|
||
| public MarkLedgerReplicatedCommandTest() { | ||
| super(3, 0); | ||
| } | ||
|
|
||
| @Override | ||
| public void setup() throws Exception { | ||
| super.setup(); | ||
| mockStatic(IOUtils.class); | ||
|
|
||
| factory = mock(LedgerManagerFactory.class); | ||
| mockMetadataDriversWithLedgerManagerFactory(factory); | ||
| underreplicationManager = mock(LedgerUnderreplicationManager.class); | ||
| when(factory.newLedgerUnderreplicationManager()).thenReturn(underreplicationManager); | ||
| doNothing().when(underreplicationManager).markLedgerReplicated(anyLong()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCommandWithoutForceAndConfirmNo() throws InterruptedException, ReplicationException { | ||
| getMockedStatic(IOUtils.class).when(() -> IOUtils.confirmPrompt(anyString())).thenReturn(false); | ||
|
|
||
| MarkLedgerReplicatedCommand cmd = new MarkLedgerReplicatedCommand(); | ||
| assertFalse(cmd.apply(bkFlags, new String[] { "-l", "1" })); | ||
|
|
||
| verify(factory, times(1)).newLedgerUnderreplicationManager(); | ||
| verify(underreplicationManager, times(0)).markLedgerReplicated(1L); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCommandWithoutForceAndConfirmYes() throws InterruptedException, ReplicationException { | ||
| getMockedStatic(IOUtils.class).when(() -> IOUtils.confirmPrompt(anyString())).thenReturn(true); | ||
|
|
||
| MarkLedgerReplicatedCommand cmd = new MarkLedgerReplicatedCommand(); | ||
| assertTrue(cmd.apply(bkFlags, new String[] { "-l", "1" })); | ||
|
|
||
| verify(factory, times(1)).newLedgerUnderreplicationManager(); | ||
| verify(underreplicationManager, times(1)).markLedgerReplicated(1L); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCommandWithForce() throws InterruptedException, ReplicationException { | ||
| MarkLedgerReplicatedCommand cmd = new MarkLedgerReplicatedCommand(); | ||
| assertTrue(cmd.apply(bkFlags, new String[] { "-l", "1", "-f" })); | ||
|
|
||
| verify(factory, times(1)).newLedgerUnderreplicationManager(); | ||
| verify(underreplicationManager, times(1)).markLedgerReplicated(1L); | ||
| } | ||
|
|
||
| } |
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
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
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.
It's better to provide a list or range option to allow given a group of ledger id to mark. For example, we can use
,to give a list of ledgers, "1,2,3,4,5,6". We also can use a range to specify the ledger, "1...10".This can avoid calling this command multiple times when there has many ledgers that need to be marked.
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.
Make sense.
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.
+1