openGauss存储引擎(2)

引言

在上一篇博客中我开始了对archive文件夹内容的学习,本片博客将继续进行该文件内容的学习

文件路径

opengauss-server\src\gausskernel\storage\access\archive\nas_am.cpp

本篇为archive文件夹下nas_am.cpp内容。 主要对Naswrite以及SortFileList两个函数做代码注释,以及相关知识点做延拓介绍。 FunctionName:Naswrite 这是一个名为 Naswrite的C函数。它接受fileName,,buffer,bufferLength,nas_config做为参数,并向NAS(网络附加存储)中写入文件信息。 以下为完整源码和注释内容

```
  1. /*
  2. function name: NasWrite
  3. description: This function first checks the validity of the input parameters.
  4. Then, it constructs the file path based on the archive configuration and file name
  5. fileName: The name of the file to read.
  6. bufferLength: The number of bytes to write.
  7. buffer: The buffer containing the data to write.
  8. nas_config: The configuration for the NAS return
  9. value: Returns readLength if the file is read successfully, otherwise returns zero.
  10. Note: The main process is as follows:
  11. It first checks if fileName and buffer are not NULL, and if nas_config is NULL, and retrieves the NAS configuration.
  12. It constructs the full file path based on the file name and archive prefix.If the file nme is "global_barrier_records",it hs a special handing. It checks if the obtained configuration is NULL. If it is, the
  13. function errors out
  14. It creates a file path based on the filename and the prefix in the configuration.
  15. It checks if the file path exists. If it doesn’t, it creates it.
  16. It opens the file for writing. If it can’t open the file, the function frees memory, logs an error, and returns -1.
  17. It writes the data from the buffer to the file. If it fails to write, the function logs an error, frees memory, closes
  18. the file, and returns -1.
  19. It flushes the file to ensure all data has been written. If it fails to flush, the function logs an error, closes the
  20. file, frees memory, and returns -1.
  21. It renames the backup file to the final filename. If it fails to rename, the function logs an error, closes the file,
  22. frees memory, and returns -1.
  23. Finally, it closes the file and returns the number of bytes read.
  24. date: 2023/9/17
  25. */
  26. int NasWrite(const char* fileName, const char *buffer, const int bufferLength, ArchiveConfig *nas_config)
  27. {
  28. int ret = 0;
  29. ArchiveConfig *archive_nas = NULL;
  30. char file_path[MAXPGPATH] = {0};
  31. char file_path_bak[MAXPGPATH] = {0};
  32. char *origin_file_path = NULL;
  33. char *base_path = NULL;
  34. FILE *fp = NULL;
  35. if ((fileName == NULL) || (buffer == NULL)) {
  36. ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
  37. errmsg("The parameter cannot be NULL")));
  38. }
  39. if (nas_config != NULL) {
  40. archive_nas = nas_config;
  41. } else {
  42. archive_nas = getArchiveConfig();
  43. }
  44. if (archive_nas == NULL) {
  45. ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
  46. errmsg("Cannot get archive config from replication slots")));
  47. }
  48. if (strncmp(fileName, "global_barrier_records", headerLen) != 0) {
  49. ret = snprintf_s(file_path, MAXPGPATH, MAXPGPATH - 1, "%s/%s", archive_nas->archive_prefix, fileName);
  50. securec_check_ss(ret, "\0", "\0");
  51. } else {
  52. char pathPrefix[MAXPGPATH] = {0};
  53. ret = strcpy_s(pathPrefix, MAXPGPATH, archive_nas->archive_prefix);
  54. securec_check_ss(ret, "\0", "\0");
  55. if (!IS_PGXC_COORDINATOR) {
  56. char *p = strrchr(pathPrefix, '/');
  57. if (p == NULL) {
  58. ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Obs path prefix is invalid")));
  59. }
  60. *p = '\0';
  61. }
  62. ret = snprintf_s(file_path, MAXPGPATH, MAXPGPATH - 1, "%s/%s", pathPrefix, fileName);
  63. securec_check_ss(ret, "\0", "\0");
  64. }
  65. canonicalize_path(file_path);
  66. origin_file_path = pstrdup(file_path);
  67. base_path = dirname(origin_file_path);
  68. if (!isDirExist(base_path)) {
  69. if (pg_mkdir_p(base_path, S_IRWXU) != 0) {
  70. pfree_ext(origin_file_path);
  71. ereport(LOG, (errmsg("could not create path \"%s\"", base_path)));
  72. return -1;
  73. }
  74. }
  75. ret = snprintf_s(file_path_bak, MAXPGPATH, MAXPGPATH - 1, "%s.bak", file_path);
  76. securec_check_ss(ret, "\0", "\0");
  77. fp = fopen(file_path_bak, "wb");
  78. if (fp == NULL) {
  79. pfree_ext(origin_file_path);
  80. ereport(LOG, (errmsg("could not create file \"%s\": %m", fileName)));
  81. return -1;
  82. }
  83. if (fwrite(buffer, bufferLength, 1, fp) != 1) {
  84. ereport(LOG, (errmsg("could not write file \"%s\": %m", fileName)));
  85. pfree_ext(origin_file_path);
  86. fclose(fp);
  87. return -1;
  88. }
  89. if (fflush(fp) != 0) {
  90. ereport(LOG, (errmsg("could not fflush file \"%s\": %m", fileName)));
  91. (void)fclose(fp);
  92. pfree_ext(origin_file_path);
  93. return -1;
  94. }
  95. if (rename(file_path_bak, file_path) < 0) {
  96. ereport(LOG, (errmsg("could not rename file \"%s\": %m", fileName)));
  97. (void)fclose(fp);
  98. pfree_ext(origin_file_path);
  99. return -1;
  100. }
  101. pfree_ext(origin_file_path);
  102. fclose(fp);
  103. return 0;
  104. }

FunctionName:GetNasFileList 这是一个名为 SortFileList 的C函数。它接受一个 file_list 作为参数,使用qsort对文件名数组排序后,并返回排序后的列表。 以下为完整源码和注释内容

 
  1. /*
  2. function: SortFileList
  3. file_list:a pointer to a List structure,which contains a list of lies.Each item in this list is a filename.
  4. description:this function returns stored list of files.
  5. main steps of this function:
  6. First,it check the length of the file list,and return null if the list is null.
  7. Second,it allocates memory for an array of character pointes with the same length as the file list.It fills the
  8. array with the filename from the list.
  9. Third, it use qsort to sort the array.
  10. After sorting ,if creates a new list and fills it.
  11. After all, it frees the memory and returns the sorted list
  12. Tips:This function uses some PostgreSQL-specific functions, such as palloc0, pfree_ext, lappend, and pstrdup. PostgreSQL is a feature-rich free software object-relational database management system.
  13. */
  14. static List* SortFileList(List* file_list)
  15. {
  16. int file_num;
  17. char** files;
  18. ListCell* lc = NULL;
  19. List* result = NIL;
  20. int i = 0;
  21. file_num = list_length(file_list);
  22. if (file_num < 1) {
  23. return NIL;
  24. }
  25. files = (char**)palloc0(file_num * sizeof(char*));
  26. foreach (lc, file_list) {
  27. files[i++] = (char*)lfirst(lc);
  28. }
  29. qsort(files, file_num, sizeof(char*), CompareFileNames);
  30. for (i = 0; i < file_num; i++) {
  31. result = lappend(result, pstrdup(files[i]));
  32. }
  33. pfree_ext(files);
  34. return result;
  35. }

NAS

从文件名可以看到醒目的“NAS”三个字母,那么nas是什么呢? 接下来下我们来看看NAS的简略介绍

  • NAS,全称为Network Attached Storage,中文译作“网络附加存储”,是一种专用的数据存储服务器。接地气地说,他就是连接在网络上,让大家可以透过网络来进行存储和读取资料的设备,如下图所示。

  • 它以数据为中心,将存储设备与服务器彻底分离,集中管理数据,从而释放带宽、提高性能、降低总拥有成本、保护投资。其成本远远低于使用服务器存储,而效率却远远高于后者。如下图所示。

  • NAS是一种文件级存储架构,可使存储的数据更易于网络设备访问。借助内置的安全、管理和容错功能,NAS为网络提供了单一的存储接入点。如下图所示

  • 在家庭或小型办公室环境中,NAS通常被用作个人或小组的共享存储系统。用户可以通过网络从他们的设备(如PC、笔记本电脑或智能手机)访问在NAS设备上存储的文件。 除了文件级存储架构,还有另外两种常见的存储架构,此处只做简略介绍:

  • 块级存储架构*:它将数据分割成块,并将这些块独立地存储。每个块都有一个唯一的标识符,而不是一个文件名和路径。这种架构的优点是性能好,因为数据可以在没有文件系统开销的情况下直接写入存储设备。但缺点是管理复杂,需要专门的软件或硬件设备来管理。

  • 对象级存储架构*:它将数据存储为一个个的对象,每个对象都有一个唯一的标识符。与文件级和块级存储不同,对象级存储可以包含元数据,使得数据管理更加灵活和高效。这种架构适合大规模的、分布式的环境,但可能需要特殊的API来访问。

以下是NAS(网络附加存储)的优点和缺点:

优点缺点
相对便宜性能取决于协议
支持多协议增加了局域网流量
容忍驱动器故障的存储卷可扩展性有限
自动备份到其他设备和云端额外的输入输出处理
可选择的系统和大小范围广泛文件传输速度不如DAS快
NAS协议的工作原理可以分为以下几个步骤:
 
  1. 客户端与服务器建立连接:在数据传输开始前,客户端需要与服务器建立连接。这可以通过TCP/IP协议实现。
  2. 认证和授权:一旦建立连接,客户端需要通过认证和授权来验证其身份和权限,以便访问服务器上的共享文件夹。
  3. 请求数据:客户端向服务器请求特定的数据或文件。请求可以是读取、写入、复制等操作。
  4. 服务器响应:服务器接收到客户端的请求后,会进行相应的处理,并将结果返回给客户端。例如,如果客户端请求读取一个文件,服务器将从磁盘中读取该文件的内容,并将其发送回客户端。
  5. 数据传输:一旦服务器准备好响应,它会将数据传输回客户端。这可以通过TCP连接进行,以确保数据的可靠性和完整性。
  6. 断开连接:在数据传输完成后,客户端和服务器之间的连接将被断开。

总的来说,网络附加存储(NAS)提供了一个便捷的解决方案,使得多个用户能够同时访问同一份数据。在企业环境中,NAS能够帮助IT团队简化数据存储和检索的过程,同时整合他们的服务器和存储基础设施。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值