Skip to content

Commit

Permalink
feat: tools: generate car index and import index to mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Jun 30, 2023
1 parent bfc4fc1 commit d249207
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ badgerStAskDb/
*.puml
/droplet-client
/droplet
/index-tool
sequence_chart.md
.idea
.vscode
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ droplet-client: $(BUILD_DEPS)
rm -f droplet-client
go build -o ./droplet-client $(GOFLAGS) ./cmd/droplet-client

index: $(BUILD_DEPS)
rm -f index-tool
go build -o ./index-tool $(GOFLAGS) ./tools/index

add-debug-flag:
GOFLAGS+=-gcflags="all=-N -l"

Expand Down
11 changes: 10 additions & 1 deletion dagstore/mongo_topindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type MongoTopIndex struct {
indexCol *mongo.Collection
}

func NewMongoTopIndex(ctx context.Context, url string) (index.Inverted, error) {
func NewMongoTopIndex(ctx context.Context, url string) (*MongoTopIndex, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
Expand Down Expand Up @@ -72,3 +72,12 @@ func (mongoTopIndex *MongoTopIndex) GetShardsForMultihash(ctx context.Context, h
}
return shardKeys, nil
}

func (mongoTopIndex *MongoTopIndex) HasShard(ctx context.Context, shard shard.Key) (bool, error) {
count, err := mongoTopIndex.indexCol.CountDocuments(ctx, bson.M{"pieces": shard.String()})
if err != nil {
return false, err
}

return count > 0, nil
}
6 changes: 6 additions & 0 deletions dagstore/mongo_topindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func TestAddMultihashesForShard(t *testing.T) {
})
assert.Nil(t, err)
}

{
has, err := indexSaver.HasShard(ctx, shard.KeyFromString(entry.Name()))
assert.NoError(t, err)
assert.True(t, has)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions models/badger/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func NewShardRepo() *Shard {
return &Shard{}
}

func (s *Shard) CreateShard(ctx context.Context, shard *dagstore.PersistedShard) error {
panic("implement me")
}

func (s *Shard) SaveShard(ctx context.Context, shard *dagstore.PersistedShard) error {
panic("implement me")
}
Expand Down
4 changes: 4 additions & 0 deletions models/mysql/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func from(s *dagstore.PersistedShard) *shard {
}
}

func (s *shardRepo) CreateShard(ctx context.Context, shard *dagstore.PersistedShard) error {
return s.DB.WithContext(ctx).Create(from(shard)).Error
}

func (s *shardRepo) SaveShard(ctx context.Context, shard *dagstore.PersistedShard) error {
return s.DB.WithContext(ctx).Save(from(shard)).Error
}
Expand Down
1 change: 1 addition & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type ICidInfoRepo interface {
}

type IShardRepo interface {
CreateShard(ctx context.Context, shard *dagstore.PersistedShard) error
dagstore.ShardRepo
}

Expand Down
51 changes: 51 additions & 0 deletions tools/index/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 索引工具

主要有两个功能,一个是给未生成索引的 active 订单生成索引,另一个是迁移 top index 到 MongoDB,迁移 shard 到 MySQL。

### 编译

```
make index
```

### 生成索引

先去 droplet 获取订单状态是 active 的订单,然后去遍历 car 文件,如果被 active 订单使用且未生成索引,则为其生成索引。

* --car-dir:存储 car 文件的目录。
* --index-dir:存储索引文件的目录,`droplet` 默认在 `~/.droplet/dagstore/index`
* --mongo-url:MongoDB 的连接地址,用于存储 top index,数据库是 `market_index`,collection 是 `top_index`
* --mysql-url:MySQL 的连接地址,用于存储 shard 状态,要和 `droplet` 使用同一个数据库,表名是 `shards`
* --droplet-url:droplet 服务的 RPC 地址。
* --droplet-token:droplet 服务的 token。

```bash
./index-tool gen-index \
--car-dir=<car dir> \
--index-dir=<index dir> \
--mongo-url="mongodb://user:pass@host/?retryWrites=true&w=majority" \
--mysql-url="user:pass@(127.0.0.1:3306)/venus-market?parseTime=true&loc=Local" \
--droplet-urls="/ip4/127.0.0.1/tcp/41235" \
--droplet-token=<token>
```

> 成功生成索引会输出类似日志:`generate index success: xxxxxx`
### 迁移索引

目前 top index 和 shard 都是存储在 badger,这样多个 droplet 时不能共享,所有需要把 top index 存储到 MongoDB,shard 存储到 MySQL,方便共享数据。

* --index-dir:存储索引文件的目录,`droplet` 默认在 `~/.droplet/dagstore/index`
* --mongo-url:MongoDB 的连接地址,用于存储 top index,数据库是 `market_index``collection``top_index`
* --mysql-url:MySQL 的连接地址,用于存储 shard 状态,要和 `droplet` 使用同一个数据库,表名是 `shards`

```bash
./index-tool migrate-index \
--index-dir=<index dir> \
--mongo-url="mongodb://user:pass@host/?retryWrites=true&w=majority" \
--mysql-url="user:pass@(127.0.0.1:3306)/venus-market?parseTime=true&loc=Local" \
--droplet-urls="/ip4/127.0.0.1/tcp/41235" \
--droplet-token=<token>
```

> 成功迁移索引会输出类似日志:`migrate xxxxx success`
Loading

0 comments on commit d249207

Please sign in to comment.