Seaweedfs学习笔记_1_Store

动机

最近为了学习golang, 发现一个很好的项目 seaweedfs (github link) 。它是一个分布式小文件存储系统,项目的主页已经介绍的比较详细了,架构主要来自于 facebook 的一篇关于图片存储的论文

Volume

Seaweedfs 有master server 和volume server。volume是具体文件存储的地方。一个volume server 包含一个 reference to Store object (volume_server.go#L20)

/*
 * One VolumeServer contains one Store
 * One Store contains a disk location list
 */
type Store struct {
    Ip string
    Port int
    PublicUrl string
    Locations []*DiskLocation
    dataCenter string  //optional informaton, overwriting master setting if exists
    rack string //optional information, overwriting master setting if exists
    connected bool
    volumeSizeLimit uint64 //read from the master 
    masterNodes *MasterNodes
}

/*
 * One location contains a volume map
 */
type DiskLocation struct {
    Directory string
    MaxVolumeCount int
    volumes map[VolumeId]*Volume
}

type Volume struct {
    Id VolumeId
    dir string
    Collection    string
    dataFile      *os.File
    nm            NeedleMapper
    needleMapKind NeedleMapType
    readOnly      bool
    SuperBlock
    dataFileAccessLock sync.Mutex
    lastModifiedTime   uint64 //unix time in second
}

在文件系统上,weedfs 会创建 [volumeId].idx file 和 [volumeId].dat 文件, 从后缀名就可以看出来,前者存储的是index 信息,相当于inode, 后者存储的是数据信息。
因为在server load的时候,index data will be loaded in memory, 所以idx 文件应该要小,idx实际上是由sequenced needlevalue 组成。

type NeedleValue struct {
    Key    Key
    Offset uint32 `comment:"Volume offset"` //since aligned to 8 bytes, range is 4G*8=32G
    Size   uint32 `comment:"Size of the data portion"`
}

下面这个小程序可以用来把index 文件load进内存

import (
"encoding/binary"
"fmt"
"io"
"os"
)

// Key comment
type Key uint64

// NeedleValue comment
type NeedleValue struct {
Key Key
Offset uint32 comment:"Volume offset" //since aligned to 8 bytes, range is 4G*8=32G
Size uint32 comment:"Size of the data portion"
}

func LoadIndexFile(fileName string) ([]*NeedleValue, error) {
file, err := os.OpenFile(fileName, os.O_RDONLY, 0444)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer file.Close()

num := int64(16)
buf := make([]byte, num)
offset := int64(0)
needleValues := make([]*NeedleValue, 0, 1024)

for {
    count, e := file.ReadAt(buf, offset)
    if int64(count) < num || e == io.EOF {
        break
    }

    if e != nil {
        return nil, e
    }

    needleValues = append(needleValues, readAsNeedleValue(buf))
    offset += int64(count)
}

return needleValues, nil

}

func readAsNeedleValue(buf []byte) *NeedleValue {
if len(buf) < 16 {
return &NeedleValue{0, 0, 0}
}

key := Key(binary.BigEndian.Uint64(buf[0:8]))
offset := binary.BigEndian.Uint32(buf[8:12])
size := binary.BigEndian.Uint32(buf[12:16])

nv := &NeedleValue{Key: key, Offset: offset, Size: size}
return nv

}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,322评论 19 139
  • 一个基本的计算机系统由“硬件”和“软件”组成,一台Linux设备,主要的组成如下图所示: 一般情况下,我们所说的L...
    时待吾阅读 5,608评论 0 16
  • 1 FastDFS简介 FastDFS是一款类Google FS开源的轻量级分布式文件系统,它用纯C语言实现,支持...
    小小少年Boy阅读 11,537评论 1 7
  • 浅复制 这个复制只能处理一些的值类型的的数据, 因为引用类型的话,复制过去的只是一个指针,源数据和目标数据属性里...
    拉面的无聊时光阅读 1,898评论 0 0
  • 一分钱难倒一个好汉。眼看着周末一天天临近,陆非差的四千元钱还是没有着落。 人呀,往往总是将自己逼到绝境,才会想出办...
    吉古吉阅读 2,425评论 0 2