Skip to content

Commit 009eb5d

Browse files
zymapsijie
authored andcommitted
Issue #1977: Migrate command 'bookieinit'
Descriptions of the changes in this PR: Migrate command `bookieinit` from shell to bkctl ### Motivation Issue #1977 ### Changes - Replace command `bookieinit` in shell Reviewers: Sijie Guo <sijie@apache.org> This closes #1978 from zymap/command-bookieinit, closes #1977
1 parent 5a34488 commit 009eb5d

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import org.apache.bookkeeper.replication.ReplicationException.UnavailableException;
111111
import org.apache.bookkeeper.stats.NullStatsLogger;
112112
import org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
113+
import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
113114
import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
114115
import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
115116
import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
@@ -498,7 +499,8 @@ String getUsage() {
498499
@Override
499500
int runCmd(CommandLine cmdLine) throws Exception {
500501
ServerConfiguration conf = new ServerConfiguration(bkConf);
501-
boolean result = BookKeeperAdmin.initBookie(conf);
502+
InitCommand initCommand = new InitCommand();
503+
boolean result = initCommand.apply(conf, new CliFlags());
502504
return (result) ? 0 : 1;
503505
}
504506
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.bookkeeper.tools.cli.commands.bookie;
19+
20+
import com.google.common.util.concurrent.UncheckedExecutionException;
21+
import org.apache.bookkeeper.client.BookKeeperAdmin;
22+
import org.apache.bookkeeper.conf.ServerConfiguration;
23+
import org.apache.bookkeeper.tools.cli.helpers.BookieCommand;
24+
import org.apache.bookkeeper.tools.framework.CliFlags;
25+
import org.apache.bookkeeper.tools.framework.CliSpec;
26+
27+
/**
28+
* A command to initialize new bookie.
29+
*/
30+
public class InitCommand extends BookieCommand<CliFlags> {
31+
32+
private static final String NAME = "init";
33+
private static final String DESC = "Initialize new bookie.";
34+
35+
public InitCommand() {
36+
super(CliSpec.newBuilder()
37+
.withName(NAME)
38+
.withDescription(DESC)
39+
.withFlags(new CliFlags())
40+
.build());
41+
}
42+
43+
@Override
44+
public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
45+
46+
boolean result = false;
47+
try {
48+
result = BookKeeperAdmin.initBookie(conf);
49+
} catch (Exception e) {
50+
throw new UncheckedExecutionException(e.getMessage(), e);
51+
}
52+
return result;
53+
}
54+
}

tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.bookkeeper.tools.cli.BKCtl;
2424
import org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
25+
import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
2526
import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
2627
import org.apache.bookkeeper.tools.common.BKFlags;
2728
import org.apache.bookkeeper.tools.framework.CliCommandGroup;
@@ -41,6 +42,7 @@ public class BookieCommandGroup extends CliCommandGroup<BKFlags> {
4142
.withParent(BKCtl.NAME)
4243
.withCategory(CATEGORY_INFRA_SERVICE)
4344
.addCommand(new LastMarkCommand())
45+
.addCommand(new InitCommand())
4446
.addCommand(new FormatCommand())
4547
.build();
4648

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.bookkeeper.tools.cli.commands.bookie;
19+
20+
import static org.junit.Assert.fail;
21+
22+
import org.apache.bookkeeper.client.BookKeeperAdmin;
23+
import org.apache.bookkeeper.conf.ServerConfiguration;
24+
import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.powermock.api.mockito.PowerMockito;
28+
import org.powermock.core.classloader.annotations.PrepareForTest;
29+
import org.powermock.modules.junit4.PowerMockRunner;
30+
31+
/**
32+
* Unit test for {@link InitCommand}.
33+
*/
34+
@RunWith(PowerMockRunner.class)
35+
@PrepareForTest({BookKeeperAdmin.class})
36+
public class InitCommandTest extends BookieCommandTestBase {
37+
38+
public InitCommandTest() {
39+
super(3, 0);
40+
}
41+
42+
public void setup() throws Exception {
43+
PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
44+
PowerMockito.mockStatic(BookKeeperAdmin.class);
45+
PowerMockito.when(BookKeeperAdmin.initBookie(conf)).thenReturn(true);
46+
}
47+
48+
@Test
49+
public void testInitCommand() {
50+
InitCommand initCommand = new InitCommand();
51+
try {
52+
initCommand.apply(bkFlags, new String[] { "" });
53+
} catch (Exception e) {
54+
fail("Should not throw any exception here.");
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)