com.github.brandtg.switchboard.HdfsLogParser.java Source code

Java tutorial

Introduction

Here is the source code for com.github.brandtg.switchboard.HdfsLogParser.java

Source

/**
 * Copyright (C) 2015 Greg Brandt (brandt.greg@gmail.com)
 *
 * Licensed 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 com.github.brandtg.switchboard;

import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
import org.apache.hadoop.hdfs.protocol.LayoutFlags;
import org.apache.hadoop.hdfs.protocol.LayoutVersion;
import org.apache.hadoop.hdfs.server.namenode.EditLogInputStream;
import org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader;
import org.apache.hadoop.hdfs.server.namenode.FSEditLogOp;
import org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes;
import org.apache.hadoop.hdfs.server.namenode.NameNodeLayoutVersion;
import org.apache.log4j.Logger;

import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;

public class HdfsLogParser extends EditLogInputStream {
    private static final Logger LOG = Logger.getLogger(HdfsLogParser.class);

    static {
        System.setProperty("hadoop.home.dir", "/tmp");
    }

    private enum State {
        UNINIT, OPEN, CLOSED
    }

    private final InputStream inputStream;

    private State state = State.UNINIT;
    private FSEditLogOp.Reader reader = null;
    private FSEditLogLoader.PositionTrackingInputStream tracker = null;
    private DataInputStream dataInputStream = null;
    private int logVersion;
    private int maxOpSize = DFSConfigKeys.DFS_NAMENODE_MAX_OP_SIZE_DEFAULT;

    public HdfsLogParser(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void resetReader() {
        reader = new FSEditLogOp.Reader(dataInputStream, tracker, logVersion);
        reader.setMaxOpSize(maxOpSize);
    }

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    @Override
    public long getFirstTxId() {
        return HdfsConstants.INVALID_TXID;
    }

    @Override
    public long getLastTxId() {
        return HdfsConstants.INVALID_TXID;
    }

    @Override
    public void close() throws IOException {
        inputStream.close();
    }

    @Override
    protected FSEditLogOp nextOp() throws IOException {
        switch (state) {
        case UNINIT:
            try {
                tracker = new FSEditLogLoader.PositionTrackingInputStream(inputStream);
                dataInputStream = new DataInputStream(tracker);
                logVersion = dataInputStream.readInt();
                if (NameNodeLayoutVersion.supports(LayoutVersion.Feature.ADD_LAYOUT_FLAGS, logVersion)
                        || logVersion < NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION) {
                    LayoutFlags.read(dataInputStream);
                }
                reader = new FSEditLogOp.Reader(dataInputStream, tracker, logVersion);
                reader.setMaxOpSize(maxOpSize);
                state = State.OPEN;
            } finally {
                if (reader == null) {
                    dataInputStream.close();
                    tracker.close();
                    state = State.CLOSED;
                }
            }
            return nextOp();
        case OPEN:
            FSEditLogOp op = reader.readOp(false);
            if (op.opCode == FSEditLogOpCodes.OP_END_LOG_SEGMENT) {
                state = State.UNINIT;
            }
            return op;
        case CLOSED:
        default:
            return null;
        }
    }

    @Override
    public int getVersion(boolean verifyVersion) throws IOException {
        return logVersion;
    }

    @Override
    public long getPosition() {
        if (state == State.OPEN) {
            return tracker.getPos();
        } else {
            return 0;
        }
    }

    @Override
    public long length() throws IOException {
        return -1;
    }

    @Override
    public boolean isInProgress() {
        return true;
    }

    @Override
    public void setMaxOpSize(int maxOpSize) {
        this.maxOpSize = maxOpSize;
        if (reader != null) {
            reader.setMaxOpSize(maxOpSize);
        }
    }

    @Override
    public boolean isLocalLog() {
        return false;
    }
}