-
Notifications
You must be signed in to change notification settings - Fork 587
HDDS-10479. Support Ozone to run ratis local related commands #7170
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
96f4dee
HDDS-10479. Fix RaftPeerId generated by command of raftMetaConf to us…
sarvekshayr abbafe5
Fixed checkstyle errors
sarvekshayr b2820f4
Moved the files
sarvekshayr a738bf0
Fixed checkstyle errors - 2
sarvekshayr 9100b60
Support ozone to forward arguments to Ratis
sarvekshayr e04cb2d
Fixed minor issues
sarvekshayr 6ce8591
Added dependency version
sarvekshayr b571f25
Dependency issue fixe3d
sarvekshayr a6d2056
Fixed minor issues - 2
sarvekshayr 6dcecb0
Changed the package name
sarvekshayr 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
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
59 changes: 59 additions & 0 deletions
59
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/OzoneRatis.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,59 @@ | ||
| /** | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.hadoop.ozone.shell; | ||
|
|
||
| import org.apache.hadoop.hdds.cli.HddsVersionProvider; | ||
| import org.apache.hadoop.hdds.tracing.TracingUtil; | ||
| import org.apache.ratis.shell.cli.sh.RatisShell; | ||
|
|
||
| import picocli.CommandLine; | ||
|
|
||
| /** | ||
| * Ozone Ratis Command line tool. | ||
| */ | ||
| @CommandLine.Command(name = "ozone ratis", | ||
| description = "Shell for running Ratis commands", | ||
| versionProvider = HddsVersionProvider.class, | ||
| mixinStandardHelpOptions = true) | ||
| public class OzoneRatis extends Shell { | ||
|
|
||
| public OzoneRatis() { | ||
| super(OzoneRatis.class); | ||
| } | ||
|
|
||
| /** | ||
| * Main for the OzoneRatis Command handling. | ||
| * | ||
| * @param argv - System Args Strings[] | ||
| */ | ||
| public static void main(String[] argv) throws Exception { | ||
| new OzoneRatis().run(argv); | ||
| } | ||
|
|
||
| @Override | ||
| public int execute(String[] argv) { | ||
| TracingUtil.initTracing("shell", createOzoneConfiguration()); | ||
| String spanName = "ozone ratis" + String.join(" ", argv); | ||
| return TracingUtil.executeInNewSpan(spanName, () -> { | ||
| // TODO: When Ozone has RATIS-2155, update this line to use the RatisShell.Builder | ||
| // in order to setup TLS and other confs. | ||
| final RatisShell shell = new RatisShell(System.out); | ||
| return shell.run(argv); | ||
| }); | ||
| } | ||
| } |
172 changes: 172 additions & 0 deletions
172
hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneRatis.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,172 @@ | ||
| /** | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.hadoop.ozone.shell; | ||
|
|
||
| import org.apache.ratis.proto.RaftProtos; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.io.PrintStream; | ||
| import java.io.InputStream; | ||
| import java.io.UnsupportedEncodingException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Tests for OzoneRatis. | ||
| */ | ||
| public class TestOzoneRatis { | ||
| private static final String DEFAULT_ENCODING = StandardCharsets.UTF_8.name(); | ||
| private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
| private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
| private final PrintStream originalOut = System.out; | ||
| private final PrintStream originalErr = System.err; | ||
| private OzoneRatis ozoneRatis; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws UnsupportedEncodingException { | ||
| System.setOut(new PrintStream(outContent, false, DEFAULT_ENCODING)); | ||
| System.setErr(new PrintStream(errContent, false, DEFAULT_ENCODING)); | ||
| ozoneRatis = new OzoneRatis(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| System.setOut(originalOut); | ||
| System.setErr(originalErr); | ||
| } | ||
|
|
||
| /** | ||
| * Execute method to invoke the OzoneRatis class and capture output. | ||
| * | ||
| * @param args command line arguments to pass | ||
| * @return the output from OzoneRatis | ||
| */ | ||
| private String execute(String[] args) throws IOException { | ||
| ozoneRatis.execute(args); | ||
| return outContent.toString(StandardCharsets.UTF_8.name()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBasicOzoneRatisCommand() throws IOException { | ||
| String[] args = {""}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Usage: ratis sh [generic options]")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLocalRaftMetaConfSubcommand(@TempDir Path tempDir) throws IOException { | ||
| // Set up temporary directory and files | ||
| Path metadataDir = tempDir.resolve("data/metadata/ratis/test-cluster/current/"); | ||
| Files.createDirectories(metadataDir); | ||
|
|
||
| // Create a dummy raft-meta.conf file using protobuf | ||
| Path raftMetaConfFile = metadataDir.resolve("raft-meta.conf"); | ||
|
|
||
| // Create a LogEntryProto with a dummy index and peer | ||
| RaftProtos.RaftPeerProto raftPeerProto = RaftProtos.RaftPeerProto.newBuilder() | ||
| .setId(ByteString.copyFromUtf8("peer1")) | ||
| .setAddress("localhost:8000") | ||
| .setStartupRole(RaftProtos.RaftPeerRole.FOLLOWER) | ||
| .build(); | ||
|
|
||
| RaftProtos.LogEntryProto logEntryProto = RaftProtos.LogEntryProto.newBuilder() | ||
| .setConfigurationEntry(RaftProtos.RaftConfigurationProto.newBuilder() | ||
| .addPeers(raftPeerProto).build()) | ||
| .setIndex(0) | ||
| .build(); | ||
|
|
||
| // Write the logEntryProto to the raft-meta.conf file | ||
| try (OutputStream out = Files.newOutputStream(raftMetaConfFile)) { | ||
| logEntryProto.writeTo(out); | ||
| } | ||
|
|
||
|
|
||
| String[] args = {"local", "raftMetaConf", "-peers", "peer1|localhost:8080", "-path", metadataDir.toString()}; | ||
| String output = execute(args); | ||
|
|
||
| assertTrue(output.contains("Index in the original file is: 0")); | ||
| assertTrue(output.contains("Generate new LogEntryProto info is:")); | ||
|
|
||
| // Verify that the new raft-meta.conf is generated | ||
| Path newRaftMetaConfFile = metadataDir.resolve("new-raft-meta.conf"); | ||
| assertTrue(Files.exists(newRaftMetaConfFile), "New raft-meta.conf file should be created."); | ||
|
|
||
| // Verify content of the newly generated file | ||
| try (InputStream in = Files.newInputStream(newRaftMetaConfFile)) { | ||
| RaftProtos.LogEntryProto newLogEntryProto = RaftProtos.LogEntryProto.parseFrom(in); | ||
| assertEquals(1, newLogEntryProto.getIndex()); | ||
| RaftProtos.RaftPeerProto peerProto = newLogEntryProto.getConfigurationEntry().getPeers(0); | ||
| assertEquals("peer1", peerProto.getId().toStringUtf8()); | ||
| assertEquals("localhost:8080", peerProto.getAddress()); | ||
| assertEquals(RaftProtos.RaftPeerRole.FOLLOWER, peerProto.getStartupRole()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testMissingRequiredArguments() throws IOException { | ||
| String[] args = {"local", "raftMetaConf"}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Failed to parse args for raftMetaConf: Missing required options: peers, path")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMissingPeerArgument() throws IOException { | ||
| String[] args = {"local", "raftMetaConf", "-path", "/path"}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Failed to parse args for raftMetaConf: Missing required option: peers")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMissingPathArgument() throws IOException { | ||
| String[] args = {"local", "raftMetaConf", "-peers", "localhost:8080"}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Failed to parse args for raftMetaConf: Missing required option: path")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidPeersFormat() throws IOException { | ||
| String[] args = {"local", "raftMetaConf", "-peers", "localhost8080", "-path", "/path"}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Failed to parse the server address parameter \"localhost8080\".")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDuplicatePeersAddress() throws IOException { | ||
| String[] args = {"local", "raftMetaConf", "-peers", "localhost:8080,localhost:8080", "-path", "/path"}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Found duplicated address: localhost:8080.")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDuplicatePeersId() throws IOException { | ||
| String[] args = {"local", "raftMetaConf", "-peers", "peer1|localhost:8080,peer1|localhost:8081", "-path", "/path"}; | ||
| String output = execute(args); | ||
| assertTrue(output.contains("Found duplicated ID: peer1.")); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.