return Journal.JournalEntry.parseFrom(new ByteArrayInputStream(buffer, 0, size));
JournalEntry entry = JournalEntry.parseFrom(new ByteArrayInputStream(mBuffer, 0, size)); return entry;
@Override public JournalEntry read() throws IOException { int firstByte = inputStream.read(); if (firstByte == -1) { return null; } // All journal entries start with their size in bytes written as a varint. int size = ProtoUtils.readRawVarint32(firstByte, inputStream); byte[] buffer = size <= mBuffer.length ? mBuffer : new byte[size]; // Total bytes read so far for journal entry. int totalBytesRead = 0; while (totalBytesRead < size) { // Bytes read in last read request. int latestBytesRead = inputStream.read(buffer, totalBytesRead, size - totalBytesRead); if (latestBytesRead < 0) { break; } totalBytesRead += latestBytesRead; } if (totalBytesRead < size) { LOG.warn("Journal entry was truncated. Expected to read " + size + " bytes but only got " + totalBytesRead); return null; } JournalEntry entry = JournalEntry.parseFrom(new ByteArrayInputStream(buffer, 0, size)); if (entry != null) { mLatestSequenceNumber = entry.getSequenceNumber(); } return entry; }
JournalEntry entry = JournalEntry.parseFrom(new ByteArrayInputStream(mBuffer, 0, size)); return entry;
return Journal.JournalEntry.parseFrom(new ByteArrayInputStream(buffer, 0, size));
@Override public JournalEntry read() throws IOException { int firstByte = inputStream.read(); if (firstByte == -1) { return null; } // All journal entries start with their size in bytes written as a varint. int size = ProtoUtils.readRawVarint32(firstByte, inputStream); byte[] buffer = size <= mBuffer.length ? mBuffer : new byte[size]; // Total bytes read so far for journal entry. int totalBytesRead = 0; while (totalBytesRead < size) { // Bytes read in last read request. int latestBytesRead = inputStream.read(buffer, totalBytesRead, size - totalBytesRead); if (latestBytesRead < 0) { break; } totalBytesRead += latestBytesRead; } if (totalBytesRead < size) { LOG.warn("Journal entry was truncated. Expected to read " + size + " bytes but only got " + totalBytesRead); return null; } JournalEntry entry = JournalEntry.parseFrom(new ByteArrayInputStream(buffer, 0, size)); if (entry != null) { mLatestSequenceNumber = entry.getSequenceNumber(); } return entry; }
/** * Applies a journal entry commit to the state machine. * * This method is automatically discovered by the Copycat framework. * * @param commit the commit */ public synchronized void applyJournalEntryCommand(Commit<JournalEntryCommand> commit) { JournalEntry entry; try { entry = JournalEntry.parseFrom(commit.command().getSerializedJournalEntry()); } catch (Exception e) { ProcessUtils.fatalError(LOG, e, "Encountered invalid journal entry in commit: {}.", commit); System.exit(-1); throw new IllegalStateException(e); // We should never reach here. } try { applyEntry(entry); } finally { Preconditions.checkState(commit.index() > mLastAppliedCommitIndex); mLastAppliedCommitIndex = commit.index(); commit.close(); } }
/** * Applies a journal entry commit to the state machine. * * This method is automatically discovered by the Copycat framework. * * @param commit the commit */ public void applyJournalEntryCommand(Commit<JournalEntryCommand> commit) { JournalEntry entry; try { entry = JournalEntry.parseFrom(commit.command().getSerializedJournalEntry()); } catch (Throwable t) { ProcessUtils.fatalError(LOG, t, "Encountered invalid journal entry in commit: {}.", commit); throw new IllegalStateException(); } applyEntry(entry); }
private void readLog() { Log log = Storage.builder().withDirectory(mDir).build().openLog("copycat"); for (long i = log.firstIndex(); i < log.lastIndex(); i++) { Entry entry = log.get(i); if (entry instanceof CommandEntry) { Command command = ((CommandEntry) entry).getCommand(); if (command instanceof JournalEntryCommand) { byte[] entryBytes = ((JournalEntryCommand) command).getSerializedJournalEntry(); try { System.out.println("Entry " + i + ": " + JournalEntry.parseFrom(entryBytes)); } catch (Exception e) { throw new RuntimeException(e); } } } } }