-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-10822. Tool to omit raft log in OM #8154
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 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f217f12
HDDS-10822. Tool to omit raft log in OM
Tejaskriya f69fd69
Checkstyle fix
Tejaskriya ac03b2b
Take backup and replace original with repaired file
Tejaskriya e4e11a7
Improve error messages
Tejaskriya 935147d
support passing ratis directory
Tejaskriya 14b4a2d
backup path sanity check
Tejaskriya 726b8a5
findBugs fixes
Tejaskriya 85ccd6a
make options mutually exclusive, take backup dir, other small improve…
Tejaskriya 4881c7c
clean up dry run
Tejaskriya 3ddf02c
Use logSegment.readSegmentFile from ratis
Tejaskriya 06dbf8a
Checkstyle and findbugs issues
Tejaskriya 4840750
Merge remote-tracking branch 'origin/master' into ratisLogOmit
adoroszlai c8e7aff
fix pmd
adoroszlai 4248e84
Fix description and refactor processLogEntry
Tejaskriya 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
190 changes: 190 additions & 0 deletions
190
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/om/OMRatisLogRepair.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,190 @@ | ||
| /* | ||
| * 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.hadoop.ozone.repair.om; | ||
|
|
||
| import static org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type.EchoRPC; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.nio.ByteBuffer; | ||
| import org.apache.hadoop.hdds.cli.HddsVersionProvider; | ||
| import org.apache.hadoop.ozone.om.helpers.OMRatisHelper; | ||
| import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos; | ||
| import org.apache.hadoop.ozone.repair.RepairTool; | ||
| import org.apache.ratis.proto.RaftProtos; | ||
| import org.apache.ratis.server.metrics.SegmentedRaftLogMetrics; | ||
| import org.apache.ratis.server.raftlog.segmented.LogSegmentPath; | ||
| import org.apache.ratis.server.raftlog.segmented.LogSegmentStartEnd; | ||
| import org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogInputStream; | ||
| import org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogOutputStream; | ||
| import org.apache.ratis.util.Preconditions; | ||
| import org.apache.ratis.util.SizeInBytes; | ||
| import picocli.CommandLine; | ||
|
|
||
|
|
||
| /** | ||
| * Tool to omit a raft log in a ratis segment file. | ||
| */ | ||
| @CommandLine.Command( | ||
| name = "ratislogrepair", | ||
| description = "CLI to omit a raft log in a ratis segment file. " + | ||
| "Requires admin privileges.", | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mixinStandardHelpOptions = true, | ||
| versionProvider = HddsVersionProvider.class | ||
| ) | ||
| public class OMRatisLogRepair extends RepairTool { | ||
|
|
||
| @CommandLine.Option(names = {"-s", "--segment-path", "--segmentPath"}, | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| required = true, | ||
| description = "Path of the input segment file") | ||
| private File segmentFile; | ||
| @CommandLine.Option(names = {"-o", "--output-path", "--outputPath"}, | ||
| required = true, | ||
| description = "Path of the repaired segment file") | ||
| private String outputPath; | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @CommandLine.Option(names = {"--index"}, | ||
| required = true, | ||
| description = "Path of the segment file") | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private long index; | ||
Tejaskriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void execute() throws Exception { | ||
|
|
||
| LogSegmentPath pi = LogSegmentPath.matchLogSegment(this.segmentFile.toPath()); | ||
| if (pi == null) { | ||
| error("Invalid segment file"); | ||
| throw new RuntimeException("Invalid Segment File"); | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
aryangupta1998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| info("Processing Raft Log file: " + this.segmentFile.getAbsolutePath() + " size:" + this.segmentFile.length()); | ||
| SegmentedRaftLogOutputStream outputStream = null; | ||
| SegmentedRaftLogInputStream logInputStream = null; | ||
|
|
||
| try { | ||
| logInputStream = getInputStream(pi); | ||
| File outputFile = createOutputFile(outputPath); | ||
| outputStream = new SegmentedRaftLogOutputStream(outputFile, false, | ||
| 1024, 1024, ByteBuffer.allocateDirect(10 * 1024)); | ||
|
|
||
| RaftProtos.LogEntryProto next; | ||
| for (RaftProtos.LogEntryProto prev = null; (next = logInputStream.nextEntry()) != null; prev = next) { | ||
| if (prev != null) { | ||
| Preconditions.assertTrue(next.getIndex() == prev.getIndex() + 1L, | ||
| "gap between entry %s and entry %s", prev, next); | ||
| } | ||
|
|
||
| if (next.getIndex() != index) { | ||
| // all other logs will be written as it is | ||
| outputStream.write(next); | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| // replace the transaction with a dummy OmEcho operation | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| OzoneManagerProtocolProtos.OMRequest oldRequest = OMRatisHelper | ||
| .convertByteStringToOMRequest(next.getStateMachineLogEntry().getLogData()); | ||
| OzoneManagerProtocolProtos.OMRequest.Builder newRequest = OzoneManagerProtocolProtos.OMRequest.newBuilder() | ||
| .setCmdType(EchoRPC) | ||
| .setClientId(oldRequest.getClientId()) | ||
| .setEchoRPCRequest(OzoneManagerProtocolProtos.EchoRPCRequest.newBuilder().build()); | ||
|
|
||
| if (oldRequest.hasUserInfo()) { | ||
| newRequest.setUserInfo(oldRequest.getUserInfo()); | ||
| } | ||
| if (oldRequest.hasTraceID()) { | ||
| newRequest.setTraceID(oldRequest.getTraceID()); | ||
| } | ||
| if (oldRequest.hasLayoutVersion()) { | ||
| newRequest.setLayoutVersion(oldRequest.getLayoutVersion()); | ||
| } | ||
| if (oldRequest.hasVersion()) { | ||
| newRequest.setVersion(oldRequest.getVersion()); | ||
| } | ||
|
||
|
|
||
| RaftProtos.StateMachineLogEntryProto oldEntry = next.getStateMachineLogEntry(); | ||
| RaftProtos.StateMachineLogEntryProto.Builder newEntry = | ||
| RaftProtos.StateMachineLogEntryProto.newBuilder() | ||
| .setCallId(oldEntry.getCallId()) | ||
| .setClientId(oldEntry.getClientId()) | ||
| .setType(oldEntry.getType()) | ||
| .setLogData(OMRatisHelper.convertRequestToByteString(newRequest.build())); | ||
| if (oldEntry.hasStateMachineEntry()) { | ||
| newEntry.setStateMachineEntry(oldEntry.getStateMachineEntry()); | ||
| } | ||
|
|
||
| RaftProtos.LogEntryProto newLogEntry = RaftProtos.LogEntryProto.newBuilder() | ||
| .setTerm(next.getTerm()) | ||
| .setIndex(next.getIndex()) | ||
| .setStateMachineLogEntry(newEntry) | ||
| .build(); | ||
| outputStream.write(newLogEntry); | ||
| } | ||
| } | ||
| } catch (Exception ex) { | ||
| error("Exception: " + ex); | ||
| } finally { | ||
| if (logInputStream != null) { | ||
| logInputStream.close(); | ||
| } | ||
| if (outputStream != null) { | ||
| outputStream.flush(); | ||
| outputStream.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private File createOutputFile(String name) throws IOException { | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| File temp = new File(name); | ||
| try { | ||
| if (temp.exists()) { | ||
| throw new IOException("Error: File already exists - " + temp.getAbsolutePath()); | ||
| } else { | ||
| if (temp.createNewFile()) { | ||
| System.out.println("File created successfully: " + temp.getAbsolutePath()); | ||
| } else { | ||
| throw new IOException("Error: Failed to create file - " + temp.getAbsolutePath()); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| error("Exception while trying to open output file: " + e.getMessage()); | ||
| throw e; | ||
| } | ||
| return temp; | ||
| } | ||
|
|
||
| private SegmentedRaftLogInputStream getInputStream(LogSegmentPath pi) { | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| Class<?> logInputStreamClass = | ||
| Class.forName("org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogInputStream"); | ||
| Constructor<?> constructor = logInputStreamClass.getDeclaredConstructor(File.class, LogSegmentStartEnd.class, | ||
| SizeInBytes.class, SegmentedRaftLogMetrics.class); | ||
| constructor.setAccessible(true); | ||
| SegmentedRaftLogInputStream inputStream = | ||
| (SegmentedRaftLogInputStream) constructor.newInstance(segmentFile, pi.getStartEnd(), | ||
| SizeInBytes.valueOf("32MB"), null); | ||
| if (inputStream == null) { | ||
| throw new RuntimeException("logInputStream is null. Constructor might have failed."); | ||
| } | ||
| return inputStream; | ||
|
|
||
| } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | | ||
| InvocationTargetException | InstantiationException | IllegalAccessException ex) { | ||
| error("Exception while trying to get input stream for segment file : " + ex); | ||
| throw new RuntimeException(ex); | ||
Tejaskriya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.