Corda技术理解之阅读源码的小知识和小问题记录

小知识
process_id在Corda启动中的意义。

process_id是Corda任何的节点启动的时候,都会默认生成这样一个文件的。

  1. Corda中源码的描述如下
    // Write out our process ID (which may or may not resemble a UNIX process id - to us it’s just a string) to a
    // file that we’ll do our best to delete on exit. But if we don’t, it’ll be overwritten next time. If it already
    // exists, we try to take the file lock first before replacing it and if that fails it means we’re being started
    // twice with the same directory: that’s a user error and we should bail out.

  2. 它为什么有这样一个文件?
    为了保证同一个路径下的节点,只能够启动一次,防止多次启动同一个节点。上述的描述更为具体。

  3. 它的实现过程?
    1,一开始的时候,Corda也不知道当前这个节点是否启动了。所以它仍然会生成属于自己的process_id字符串(数字),生成文件。

    2,对此文件执行如下操作:
    val pidFileLock = pidFileRw.channel.tryLock()

    3,如果无法对此文件进行上锁,则表明此节点已经在启动中了。目前还不知道为什么可以这样判断?

  4. Corda源码实现逻辑过程

val pidFile = (baseDirectory / "process-id").toFile()
pidFile.createNewFile()
val pidFileRw = RandomAccessFile(pidFile, "rw")
val pidFileLock = pidFileRw.channel.tryLock()
if (pidFileLock == null) {
   println("It appears there is already a node running with the specified data directory $baseDirectory")
   println("Shut that other node down and try again. It may have process ID ${pidFile.readText()}")
   System.exit(1)
}
pidFile.deleteOnExit()
// Avoid the lock being garbage collected. We don't really need to release it as the OS will do so for us
// when our process shuts down, but we try in stop() anyway just to be nice.
addShutdownHook {
   pidFileLock.release()
}
val ourProcessID: String = ManagementFactory.getRuntimeMXBean().name.split("@")[0]
pidFileRw.setLength(0)
pidFileRw.write(ourProcessID.toByteArray())

对此文件调用了一个钩子。
当退出的时候,需要删除此文件,并且释放锁。


小问题

  1. 启动的时候:如果版本太低,有bug。如下:bug描述过程
  2. Paths.get("").normalize() throws ArrayIndexOutOfBoundsException when using Oracle JDK 8u20.
    Stack trace:
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at sun.nio.fs.UnixPath.normalize(UnixPath.java:508)
    at net.corda.node.services.config.ConfigHelper.loadConfig(ConfigUtilities.kt:34)
    at net.corda.node.services.config.ConfigHelper.loadConfig$default(ConfigUtilities.kt:30)
    at net.corda.node.MainKt.main(Main.kt:74)
    Solution is to upgrade JDK above version 1.8.0_20. Suggest we catch this exception and print the solution to screen -> Upgrade JDK above version 1.8.0_20 or something.
    So Corda defines a method named canNormalizeEmptyPath that can catch exception when node starts at Oracle JDK 8u20 and print the solution to screen.

小结
  1. 记录一个Corda节点启动时候,默认生成的一个process_id文件的相关知识。
  2. 记录一个bug。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值