| 122 | } |
| 123 | |
| 124 | Status EventsWriter::Flush() { |
| 125 | if (num_outstanding_events_ == 0) return Status::OK(); |
| 126 | CHECK(recordio_file_ != nullptr) << "Unexpected NULL file"; |
| 127 | |
| 128 | TF_RETURN_WITH_CONTEXT_IF_ERROR(recordio_writer_->Flush(), "Failed to flush ", |
| 129 | num_outstanding_events_, " events to ", |
| 130 | filename_); |
| 131 | TF_RETURN_WITH_CONTEXT_IF_ERROR(recordio_file_->Sync(), "Failed to sync ", |
| 132 | num_outstanding_events_, " events to ", |
| 133 | filename_); |
| 134 | |
| 135 | // The FileStillExists() condition is necessary because |
| 136 | // recordio_writer_->Sync() can return OK even if the underlying |
| 137 | // file has been deleted. EventWriter.FileDeletionBeforeWriting |
| 138 | // demonstrates this and will fail if the FileHasDisappeared() |
| 139 | // condition is removed. |
| 140 | // Also, we deliberately attempt to Sync() before checking for a |
| 141 | // disappearing file, in case for some file system File::Exists() is |
| 142 | // false after File::Open() but before File::Sync(). |
| 143 | TF_RETURN_WITH_CONTEXT_IF_ERROR(FileStillExists(), "Failed to flush ", |
| 144 | num_outstanding_events_, " events to ", |
| 145 | filename_); |
| 146 | VLOG(1) << "Wrote " << num_outstanding_events_ << " events to disk."; |
| 147 | num_outstanding_events_ = 0; |
| 148 | return Status::OK(); |
| 149 | } |
| 150 | |
| 151 | Status EventsWriter::Close() { |
| 152 | Status status = Flush(); |