827 lines · 27.5 KB · Java
RawDownload
     * @exception IOException If an I/O error has occurred.     */    private void deleteNewVersionFile() throws IOException {        deleteFile(fName(newVersionFile));    }     /**     * Removes the snapshot file.     *     * @param ver the version to remove     * @exception IOException If an I/O error has occurred.     */    private void deleteSnapshot(int ver) throws IOException {        if (ver == 0) return;        deleteFile(versionName(snapshotPrefix, ver));    }     /**     * Removes the log file.     *     * @param ver the version to remove     * @exception IOException If an I/O error has occurred.     */    private void deleteLogFile(int ver) throws IOException {        if (ver == 0) return;        deleteFile(versionName(logfilePrefix, ver));    }     /**     * Opens the log file in read/write mode.  If file does not exist, it is     * created.     *     * @param truncate if true and file exists, file is truncated to zero     * length     * @exception IOException If an I/O error has occurred.     */    private void openLogFile(boolean truncate) throws IOException {        try {            close();        } catch (IOException e) { /* assume this is okay */        }         logName = versionName(logfilePrefix);         try {            log = (logClassConstructor == null ?                   new LogFile(logName, "rw") :                   logClassConstructor.newInstance(logName, "rw"));        } catch (Exception e) {            throw (IOException) new IOException(                "unable to construct LogFile instance").initCause(e);        }         if (truncate) {            initializeLogFile();        }    }     /**     * Creates a new log file, truncated and initialized with the format     * version number preferred by this implementation.     * <p>Environment: inited, synchronized     * <p>Precondition: valid: log, log contains nothing useful     * <p>Postcondition: if successful, log is initialised with the format     * version number (Preferred{Major,Minor}Version), and logBytes is     * set to the resulting size of the updatelog, and logEntries is set to     * zero.  Otherwise, log is in an indeterminate state, and logBytes     * is unchanged, and logEntries is unchanged.     *     * @exception IOException If an I/O error has occurred.     */    private void initializeLogFile()        throws IOException    {        log.setLength(0);        majorFormatVersion = PreferredMajorVersion;        writeInt(log, PreferredMajorVersion);        minorFormatVersion = PreferredMinorVersion;        writeInt(log, PreferredMinorVersion);        logBytes = intBytes * 2;        logEntries = 0;    }      /**     * Writes out version number to file.     *     * @param newVersion if true, writes to a new version file     * @exception IOException If an I/O error has occurred.     */    private void writeVersionFile(boolean newVersion) throws IOException {        String name;        if (newVersion) {            name = newVersionFile;        } else {            name = versionFile;        }        DataOutputStream out =            new DataOutputStream(new FileOutputStream(fName(name)));        writeInt(out, version);        out.close();    }     /**     * Creates the initial version file     *     * @exception IOException If an I/O error has occurred.     */    private void createFirstVersion() throws IOException {        version = 0;        writeVersionFile(false);    }     /**     * Commits (atomically) the new version.     *     * @exception IOException If an I/O error has occurred.     */    private void commitToNewVersion() throws IOException {        writeVersionFile(false);        deleteNewVersionFile();    }     /**     * Reads version number from a file.     *     * @param name the name of the version file     * @return the version     * @exception IOException If an I/O error has occurred.     */    private int readVersion(String name) throws IOException {        DataInputStream in = new DataInputStream(new FileInputStream(name));        try {            return in.readInt();        } finally {            in.close();        }    }     /**     * Sets the version.  If version file does not exist, the initial     * version file is created.     *     * @exception IOException If an I/O error has occurred.     */    private void getVersion() throws IOException {        try {            version = readVersion(fName(newVersionFile));            commitToNewVersion();        } catch (IOException e) {            try {                deleteNewVersionFile();            }            catch (IOException ex) {            }             try {                version = readVersion(fName(versionFile));            }            catch (IOException ex) {                createFirstVersion();            }        }    }     /**     * Applies outstanding updates to the snapshot.     *     * @param state the most recent snapshot     * @exception IOException If serious log corruption is detected or     * if an exception occurred during a readUpdate callback or if     * other I/O error has occurred.     * @return the resulting state of the object after all updates     */    private Object recoverUpdates(Object state)        throws IOException    {        logBytes = 0;        logEntries = 0;         if (version == 0) return state;         String fname = versionName(logfilePrefix);        InputStream in =                new BufferedInputStream(new FileInputStream(fname));        DataInputStream dataIn = new DataInputStream(in);         if (Debug)            System.err.println("log.debug: reading updates from " + fname);         try {            majorFormatVersion = dataIn.readInt(); logBytes += intBytes;            minorFormatVersion = dataIn.readInt(); logBytes += intBytes;        } catch (EOFException e) {            /* This is a log which was corrupted and/or cleared (by             * fsck or equivalent).  This is not an error.             */            openLogFile(true);  // create and truncate            in = null;        }        /* A new major version number is a catastrophe (it means         * that the file format is incompatible with older         * clients, and we'll only be breaking things by trying to         * use the log).  A new minor version is no big deal for         * upward compatibility.         */        if (majorFormatVersion != PreferredMajorVersion) {            if (Debug) {                System.err.println("log.debug: major version mismatch: " +                        majorFormatVersion + "." + minorFormatVersion);            }            throw new IOException("Log file " + logName + " has a " +                                  "version " + majorFormatVersion +                                  "." + minorFormatVersion +                                  " format, and this implementation " +                                  " understands only version " +                                  PreferredMajorVersion + "." +                                  PreferredMinorVersion);        }         try {            while (in != null) {                int updateLen = 0;                 try {                    updateLen = dataIn.readInt();                } catch (EOFException e) {                    if (Debug)                        System.err.println("log.debug: log was sync'd cleanly");                    break;                }                if (updateLen <= 0) {/* crashed while writing last log entry */                    if (Debug) {                        System.err.println(                            "log.debug: last update incomplete, " +                            "updateLen = 0x" +                            Integer.toHexString(updateLen));                    }                    break;                }                 // this is a fragile use of available() which relies on the                // twin facts that BufferedInputStream correctly consults                // the underlying stream, and that FileInputStream returns                // the number of bytes remaining in the file (via FIONREAD).                if (in.available() < updateLen) {                    /* corrupted record at end of log (can happen since we                     * do only one fsync)                     */                    if (Debug)                        System.err.println("log.debug: log was truncated");                    break;                }                 if (Debug)                    System.err.println("log.debug: rdUpdate size " + updateLen);                try {                    state = handler.readUpdate(new LogInputStream(in, updateLen),                                          state);                } catch (IOException e) {                    throw e;                } catch (Exception e) {                    e.printStackTrace();                    throw new IOException("read update failed with " +                                          "exception: " + e);                }                logBytes += (intBytes + updateLen);                logEntries++;            } /* while */        } finally {            if (in != null)                in.close();        }         if (Debug)            System.err.println("log.debug: recovered updates: " + logEntries);         /* reopen log file at end */        openLogFile(false);         // avoid accessing a null log field        if (log == null) {            throw new IOException("rmid's log is inaccessible, " +                "it may have been corrupted or closed");        }         log.seek(logBytes);        log.setLength(logBytes);         return state;    }     /**     * ReliableLog's log file implementation.  This implementation     * is subclassable for testing purposes.     */    public static class LogFile extends RandomAccessFile {         private final FileDescriptor fd;         /**         * Constructs a LogFile and initializes the file descriptor.         **/        public LogFile(String name, String mode)            throws FileNotFoundException, IOException        {            super(name, mode);            this.fd = getFD();        }         /**         * Invokes sync on the file descriptor for this log file.         */        protected void sync() throws IOException {            fd.sync();        }         /**         * Returns true if writing 4 bytes starting at the specified file         * position, would span a 512 byte sector boundary; otherwise returns         * false.         **/        protected boolean checkSpansBoundary(long fp) {            return  fp % 512 > 508;        }    }}

Lines 501–827 of 827. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t