@@ -26,6 +26,7 @@ import (
2626 "encoding/json"
2727 "fmt"
2828 "math"
29+ "net/http"
2930 pathUtil "path"
3031 "path/filepath"
3132 "strconv"
@@ -37,6 +38,7 @@ import (
3738 "github.com/pkg/errors"
3839 "github.com/v3io/v3io-go/pkg/dataplane"
3940 "github.com/v3io/v3io-go/pkg/dataplane/http"
41+ v3ioerrors "github.com/v3io/v3io-go/pkg/errors"
4042 "github.com/v3io/v3io-tsdb/pkg/aggregate"
4143 "github.com/v3io/v3io-tsdb/pkg/appender"
4244 "github.com/v3io/v3io-tsdb/pkg/chunkenc"
@@ -95,14 +97,24 @@ func CreateTSDB(cfg *config.V3ioConfig, schema *config.Schema, container v3io.Co
9597
9698 path := pathUtil .Join (cfg .TablePath , config .SchemaConfigFileName )
9799 // Check whether the config file already exists, and abort if it does
98- _ , err = container .GetObjectSync (& v3io.GetObjectInput {Path : path , DataPlaneInput : dataPlaneInput })
99- if err == nil {
100- return fmt .Errorf ("A TSDB table already exists at path '" + cfg .TablePath + "'." )
101- }
102-
103- err = container .PutObjectSync (& v3io.PutObjectInput {Path : path , Body : data , DataPlaneInput : dataPlaneInput })
104- if err != nil {
105- return errors .Wrapf (err , "Failed to create a TSDB schema at path '%s/%s/%s'." , cfg .WebAPIEndpoint , cfg .Container , path )
100+ for i := 0 ; i < 8 ; i ++ {
101+ _ , err = container .GetObjectSync (& v3io.GetObjectInput {Path : path , DataPlaneInput : dataPlaneInput })
102+ if err == nil {
103+ return fmt .Errorf ("A TSDB table already exists at path '" + cfg .TablePath + "'." )
104+ } else if e , hasStatusCode := err .(v3ioerrors.ErrorWithStatusCode ); hasStatusCode && e .StatusCode () != http .StatusNotFound {
105+ err = errors .Wrapf (err , "Failed to check TSDB schema at path '%s/%s/%s'." , cfg .WebAPIEndpoint , cfg .Container , path )
106+ lgr .Error (err )
107+ } else {
108+ // if no tsdb schema found
109+ err = container .PutObjectSync (& v3io.PutObjectInput {Path : path , Body : data , DataPlaneInput : dataPlaneInput })
110+ if err != nil {
111+ err = errors .Wrapf (err , "Failed to create a TSDB schema at path '%s/%s/%s'." , cfg .WebAPIEndpoint , cfg .Container , path )
112+ lgr .Error (err )
113+ } else {
114+ lgr .Info ("Successfully created TSDB schema at path '%s/%s/%s'." , cfg .WebAPIEndpoint , cfg .Container , path )
115+ break
116+ }
117+ }
106118 }
107119 return err
108120}
0 commit comments