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

Ignore fsimage entries outside of the INodeSection when parsing XML. #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -52,6 +53,13 @@ class XMLParser {
* @return {@code BlockInfo}s for any blocks found.
*/
List<BlockInfo> parseLine(String line) throws IOException {
if (currentState == State.DEFAULT) {
if (line.contains("<INodeSection>")) {
transitionTo(State.INODE_SECTION);
} else {
return Collections.emptyList();
}
}
if (line.contains("<inode>")) {
transitionTo(State.INODE);
}
Expand All @@ -78,6 +86,9 @@ List<BlockInfo> parseLine(String line) throws IOException {
blockInfos.add(new BlockInfo(id, gs, size, currentReplication));
}
if (line.contains("</inode>")) {
transitionTo(State.INODE_SECTION);
}
if (line.contains("</INodeSection>")) {
transitionTo(State.DEFAULT);
}
return blockInfos;
Expand Down Expand Up @@ -113,16 +124,18 @@ private static List<String> valuesFromXMLString(String xml, String field) {

private enum State {
DEFAULT,
INODE_SECTION,
INODE,
FILE,
FILE_WITH_REPLICATION;

private final Set<State> allowedTransitions = new HashSet<>();
static {
DEFAULT.addTransitions(DEFAULT, INODE);
INODE.addTransitions(DEFAULT, FILE);
FILE.addTransitions(DEFAULT, FILE_WITH_REPLICATION);
FILE_WITH_REPLICATION.addTransitions(DEFAULT);
DEFAULT.addTransitions(DEFAULT, INODE_SECTION);
INODE_SECTION.addTransitions(DEFAULT, INODE);
INODE.addTransitions(INODE_SECTION, FILE);
FILE.addTransitions(INODE_SECTION, FILE_WITH_REPLICATION);
FILE_WITH_REPLICATION.addTransitions(INODE_SECTION);
}

private void addTransitions(State... nextState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void testBlocksFromLine() throws Exception {
"<inode><type>FILE</type>",
"<replication>12</replication>",
"<blocks><block><id>13</id><genstamp>14</genstamp><numBytes>15</numBytes></block>",
"</inode>"
"</inode>",
"</INodeSection>"
};

Map<BlockInfo, Short> expectedBlockCount = new HashMap<>();
Expand All @@ -53,4 +54,23 @@ public void testBlocksFromLine() throws Exception {
assertEquals(expect.getValue(), actualBlockCount.get(expect.getKey()));
}
}

@Test
public void testNonInodeSectionIgnored() throws Exception {
String[] lines = {
"<INodeSection>",
"</INodeSection>",
"<OtherSection>",
"<inode><id>1</id><type>FILE</type><name>fake-file</name><replication>1</replication>",
"<blocks><block><id>2</id><genstamp>1</genstamp><numBytes>1</numBytes></block>",
"</inode>",
"<replication>3</replication>",
"</OtherSection>"
};

XMLParser parser = new XMLParser();
for (String line : lines) {
assertTrue((parser.parseLine(line).isEmpty()));
}
}
}