Skip to content
Closed
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 @@ -128,7 +128,11 @@ public static String readOffsetFile(File storagePartitionDir, String offsetFileN

if (offsetFileRef.exists()) {
LOG.info("Found offset file in storage partition directory: {}", storePath);
offset = FileUtil.readWithChecksum(offsetFileRef);
try {
offset = FileUtil.readWithChecksum(offsetFileRef);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to add a unit test for these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test for the case of offset already exist.

} catch (Exception e) {
LOG.warn("Failed to read offset file in storage partition directory: {}", storePath, e);
}
} else {
LOG.info("No offset file found in storage partition directory: {}", storePath);
}
Expand Down
13 changes: 12 additions & 1 deletion samza-core/src/main/scala/org/apache/samza/util/FileUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.apache.samza.util

import java.io.{File, FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream}
import java.nio.file._
import java.util.zip.CRC32

import org.apache.samza.util.Util.info
Expand All @@ -35,17 +36,27 @@ object FileUtil {
* */
def writeWithChecksum(file: File, data: String): Unit = {
val checksum = getChecksum(data)
val tmpFilePath = file.getAbsolutePath + ".tmp"
val tmpFile = new File(tmpFilePath)
var oos: ObjectOutputStream = null
var fos: FileOutputStream = null
try {
fos = new FileOutputStream(file)
fos = new FileOutputStream(tmpFile)
oos = new ObjectOutputStream(fos)
oos.writeLong(checksum)
oos.writeUTF(data)
} finally {
oos.close()
fos.close()
}

//atomic swap of tmp and real offset file
try {
Files.move(tmpFile.toPath, file.toPath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING)
} catch {
case e: AtomicMoveNotSupportedException =>
Files.move(tmpFile.toPath, file.toPath, StandardCopyOption.REPLACE_EXISTING)
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions samza-core/src/test/scala/org/apache/samza/util/TestFileUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ class TestFileUtil {
fis.close()
}

@Test
def testWriteDataToFileWithExistingOffsetFile() {
// Invoke test
val file = new File(System.getProperty("java.io.tmpdir"), "test2")
// write the same file three times
FileUtil.writeWithChecksum(file, data)
FileUtil.writeWithChecksum(file, data)
FileUtil.writeWithChecksum(file, data)

// Check that file exists
assertTrue("File was not created!", file.exists())
val fis = new FileInputStream(file)
val ois = new ObjectInputStream(fis)

// Check content of the file is as expected
assertEquals(checksum, ois.readLong())
assertEquals(data, ois.readUTF())
ois.close()
fis.close()
}


@Test
def testReadDataFromFile() {
// Setup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ object RocksDbKeyValueStore extends Logging {
.toSet

(configuredMetrics ++ rocksDbMetrics)
.foreach(property => metrics.newGauge(property, () => rocksDb.getProperty(property)))
.foreach(property => metrics.newGauge(property, () =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this change do?

// Check isOwningHandle flag. The db is open iff the flag is true.
if (rocksDb.isOwningHandle) {
rocksDb.getProperty(property)
} else {
"0"
}
))

rocksDb
} catch {
Expand Down