Cluster下的数据写入
数据写入的实现主要分析cluster/points_writer.go中的WritePoints函数的实现// WritePoints writes across multiple local and remote data nodes according the consistency level.func (w *PointsWriter) WritePoints(p *WritePointsRequest) error {
w.statMap.Add(statWriteReq, 1)
w.statMap.Add(statPointWriteReq, int64(len(p.Points))) //2.1 先获取RetentionPolicy
if p.RetentionPolicy == "" {
db, err := w.MetaClient.Database(p.Database) if err != nil { return err
} else if db == nil { return influxdb.ErrDatabaseNotFound(p.Database)
}
p.RetentionPolicy = db.DefaultRetentionPolicy
} // 2.2 生成 shardMap
shardMappings, err := w.MapShards(p) if err != nil { return err
} // Write each shard in it's own goroutine and return as soon
// as one fails.
ch := make(chan error, len(shardMappings.Points)) for shardID, points := range shardMappings.Points {
// 2.3 写入数据到Shard
go func(shard *meta.ShardInfo, database, retentionPolicy string, points []models.Point) {
ch
}(shardMappings.Shards[shardID], p.Database, p.RetentionPolicy, points)
} // Send points to subscriptions if possible.
ok := false
// We need to lock just in case the channel is about to be nil'ed
w.mu.RLock()
select { case w.subPoints
ok = true
default:
}
w.mu.RUnlock() if ok {
w.statMap.Add(statSubWriteOK, 1)
} else {
w.statMap.Add(statSubWriteDrop, 1)
} // 2.4 等待写入完成
for range shardMappings.Points {
select { case
}
}
} return nil}上面的函数实现主要分如下几个步骤
2.1 获取对应的RetentionPolicy
2.2 生成ShardMap, 将各个point对应到相应ShardGroup中的Shard中, 这步很关键
2.3 按ShardId不同,开启新的goroutine, 将points写入相应的Shard,可