| 1 | package importer |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/influxdata/influxdb/cmd/influx-tools/internal/errlist" |
| 12 | "github.com/influxdata/influxdb/cmd/influx-tools/server" |
| 13 | "github.com/influxdata/influxdb/services/meta" |
| 14 | "github.com/influxdata/influxdb/tsdb" |
| 15 | "github.com/influxdata/influxdb/tsdb/engine/tsm1" |
| 16 | "go.uber.org/zap" |
| 17 | ) |
| 18 | |
| 19 | type importer struct { |
| 20 | MetaClient server.MetaClient |
| 21 | db string |
| 22 | dataDir string |
| 23 | replace bool |
| 24 | |
| 25 | rpi *meta.RetentionPolicyInfo |
| 26 | log *zap.Logger |
| 27 | skipShard bool |
| 28 | currentShard uint64 |
| 29 | sh *shardWriter |
| 30 | sfile *tsdb.SeriesFile |
| 31 | sw *seriesWriter |
| 32 | buildTsi bool |
| 33 | seriesBuf []byte |
| 34 | } |
| 35 | |
| 36 | const seriesBatchSize = 1000 |
| 37 | |
| 38 | func newImporter(server server.Interface, db string, rp string, replace bool, buildTsi bool, log *zap.Logger) *importer { |
| 39 | i := &importer{MetaClient: server.MetaClient(), db: db, dataDir: server.TSDBConfig().Dir, replace: replace, buildTsi: buildTsi, log: log, skipShard: false} |
| 40 | |
| 41 | if !buildTsi { |
| 42 | i.seriesBuf = make([]byte, 0, 2048) |
| 43 | } |
| 44 | return i |
| 45 | } |
| 46 | |
| 47 | func (i *importer) Close() error { |
| 48 | el := errlist.NewErrorList() |
| 49 | if i.sh != nil { |
| 50 | el.Add(i.CloseShardGroup()) |
| 51 | } |
| 52 | return el.Err() |
| 53 | } |
| 54 | |
| 55 | func (i *importer) CreateDatabase(rp *meta.RetentionPolicySpec) error { |
| 56 | var rpi *meta.RetentionPolicyInfo |
| 57 | dbInfo := i.MetaClient.Database(i.db) |
| 58 | if dbInfo == nil { |
| 59 | return i.createDatabaseWithRetentionPolicy(rp) |
| 60 | } |
| 61 | |
| 62 | rpi, err := i.MetaClient.RetentionPolicy(i.db, rp.Name) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | nonmatchingRp := (rpi != nil) && ((rp.Duration != nil && rpi.Duration != *rp.Duration) || |
| 68 | (rp.ReplicaN != nil && rpi.ReplicaN != *rp.ReplicaN) || |
| 69 | (rpi.ShardGroupDuration != rp.ShardGroupDuration)) |
| 70 | if nonmatchingRp { |
| 71 | return fmt.Errorf("retention policy %v already exists with different parameters", rp.Name) |
| 72 | } else { |
| 73 | if _, err := i.MetaClient.CreateRetentionPolicy(i.db, rp, false); err != nil { |
| 74 | return err |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | i.rpi, err = i.MetaClient.RetentionPolicy(i.db, rp.Name) |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | func (i *importer) createDatabaseWithRetentionPolicy(rp *meta.RetentionPolicySpec) error { |
| 83 | var err error |
| 84 | var dbInfo *meta.DatabaseInfo |
| 85 | if len(rp.Name) == 0 { |
| 86 | dbInfo, err = i.MetaClient.CreateDatabase(i.db) |
| 87 | } else { |
| 88 | dbInfo, err = i.MetaClient.CreateDatabaseWithRetentionPolicy(i.db, rp) |
| 89 | } |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | i.rpi = dbInfo.RetentionPolicy(rp.Name) |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (i *importer) StartShardGroup(start int64, end int64) error { |
| 98 | existingSg, err := i.MetaClient.ShardGroupsByTimeRange(i.db, i.rpi.Name, time.Unix(0, start), time.Unix(0, end)) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | var sgi *meta.ShardGroupInfo |
| 104 | var shardID uint64 |
| 105 | |
| 106 | shardsPath := i.shardPath(i.rpi.Name) |
| 107 | var shardPath string |
| 108 | if len(existingSg) > 0 { |
| 109 | sgi = &existingSg[0] |
| 110 | if len(sgi.Shards) > 1 { |
| 111 | return fmt.Errorf("multiple shards for the same owner %v and time range %v to %v", sgi.Shards[0].Owners, start, end) |
| 112 | } |
| 113 | |
| 114 | shardID = sgi.Shards[0].ID |
| 115 | |
| 116 | shardPath = filepath.Join(shardsPath, strconv.Itoa(int(shardID))) |
| 117 | _, err = os.Stat(shardPath) |
| 118 | if err != nil { |
| 119 | if !os.IsNotExist(err) { |
| 120 | return err |
| 121 | } |
| 122 | } else { |
| 123 | if i.replace { |
| 124 | if err := os.RemoveAll(shardPath); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | } else { |
| 128 | if i.log != nil { |
| 129 | i.log.Error(fmt.Sprintf("shard %d already exists, skipping over new shard data", sgi.ID)) |
| 130 | } |
| 131 | i.skipShard = true |
| 132 | return nil |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | sgi, err = i.MetaClient.CreateShardGroup(i.db, i.rpi.Name, time.Unix(0, start)) |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | shardID = sgi.Shards[0].ID |
| 141 | } |
| 142 | |
| 143 | shardPath = filepath.Join(shardsPath, strconv.Itoa(int(shardID))) |
| 144 | if err = os.MkdirAll(shardPath, 0777); err != nil { |
| 145 | return err |
| 146 | } |
| 147 | |
| 148 | i.skipShard = false |
| 149 | i.sh = newShardWriter(shardID, shardsPath) |
| 150 | i.currentShard = shardID |
| 151 | |
| 152 | i.startSeriesFile() |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | func (i *importer) shardPath(rp string) string { |
| 157 | return filepath.Join(i.dataDir, i.db, rp) |
| 158 | } |
| 159 | |
| 160 | func (i *importer) removeShardGroup(rp string, shardID uint64) error { |
| 161 | shardPath := i.shardPath(rp) |
| 162 | err := os.RemoveAll(filepath.Join(shardPath, strconv.Itoa(int(shardID)))) |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | func (i *importer) Write(key []byte, values tsm1.Values) error { |
| 167 | if i.skipShard { |
| 168 | return nil |
| 169 | } |
| 170 | if i.sh == nil { |
| 171 | return errors.New("importer not currently writing a shard") |
| 172 | } |
| 173 | i.sh.Write(key, values) |
| 174 | if i.sh.err != nil { |
| 175 | el := errlist.NewErrorList() |
| 176 | el.Add(i.sh.err) |
| 177 | el.Add(i.CloseShardGroup()) |
| 178 | el.Add(i.removeShardGroup(i.rpi.Name, i.currentShard)) |
| 179 | i.sh = nil |
| 180 | i.currentShard = 0 |
| 181 | return el.Err() |
| 182 | } |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | func (i *importer) CloseShardGroup() error { |
| 187 | if i.skipShard { |
| 188 | i.skipShard = false |
| 189 | return nil |
| 190 | } |
| 191 | el := errlist.NewErrorList() |
| 192 | el.Add(i.closeSeriesFile()) |
| 193 | i.sh.Close() |
| 194 | if i.sh.err != nil { |
| 195 | el.Add(i.sh.err) |
| 196 | } |
| 197 | i.sh = nil |
| 198 | return el.Err() |
| 199 | } |
| 200 | |
| 201 | func (i *importer) startSeriesFile() error { |
| 202 | dataPath := filepath.Join(i.dataDir, i.db) |
| 203 | shardPath := filepath.Join(i.dataDir, i.db, i.rpi.Name) |
| 204 | |
| 205 | i.sfile = tsdb.NewSeriesFile(filepath.Join(dataPath, tsdb.SeriesFileDirectory)) |
| 206 | if err := i.sfile.Open(); err != nil { |
| 207 | return err |
| 208 | } |
| 209 | |
| 210 | var err error |
| 211 | if i.buildTsi { |
| 212 | i.sw, err = newTSI1SeriesWriter(i.sfile, i.db, dataPath, shardPath, int(i.sh.id)) |
| 213 | } else { |
| 214 | i.sw, err = newInMemSeriesWriter(i.sfile, i.db, dataPath, shardPath, int(i.sh.id), i.seriesBuf) |
| 215 | } |
| 216 | |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | return nil |
| 221 | } |
| 222 | |
| 223 | func (i *importer) AddSeries(seriesKey []byte) error { |
| 224 | if i.skipShard { |
| 225 | return nil |
| 226 | } |
| 227 | return i.sw.AddSeries(seriesKey) |
| 228 | } |
| 229 | |
| 230 | func (i *importer) closeSeriesFile() error { |
| 231 | return i.sw.Close() |
| 232 | } |