Fix impl and test for setting default retention policy
parent
00e87488bd
commit
291360d42b
15
database.go
15
database.go
|
@ -466,6 +466,7 @@ func (db *Database) MarshalJSON() ([]byte, error) {
|
||||||
// Copy over properties to intermediate type.
|
// Copy over properties to intermediate type.
|
||||||
var o databaseJSON
|
var o databaseJSON
|
||||||
o.Name = db.name
|
o.Name = db.name
|
||||||
|
o.DefaultRetentionPolicy = db.defaultRetentionPolicy
|
||||||
o.MaxFieldID = db.maxFieldID
|
o.MaxFieldID = db.maxFieldID
|
||||||
for _, u := range db.users {
|
for _, u := range db.users {
|
||||||
o.Users = append(o.Users, u)
|
o.Users = append(o.Users, u)
|
||||||
|
@ -492,6 +493,7 @@ func (db *Database) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// Copy over properties from intermediate type.
|
// Copy over properties from intermediate type.
|
||||||
db.name = o.Name
|
db.name = o.Name
|
||||||
|
db.defaultRetentionPolicy = o.DefaultRetentionPolicy
|
||||||
db.maxFieldID = o.MaxFieldID
|
db.maxFieldID = o.MaxFieldID
|
||||||
|
|
||||||
// Copy users.
|
// Copy users.
|
||||||
|
@ -523,12 +525,13 @@ func (db *Database) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
// databaseJSON represents the JSON-serialization format for a database.
|
// databaseJSON represents the JSON-serialization format for a database.
|
||||||
type databaseJSON struct {
|
type databaseJSON struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
MaxFieldID uint64 `json:"maxFieldID,omitempty"`
|
DefaultRetentionPolicy string `json:"defaultRetentionPolicy,omitempty"`
|
||||||
Users []*DBUser `json:"users,omitempty"`
|
MaxFieldID uint64 `json:"maxFieldID,omitempty"`
|
||||||
Policies []*RetentionPolicy `json:"policies,omitempty"`
|
Users []*DBUser `json:"users,omitempty"`
|
||||||
Shards []*Shard `json:"shards,omitempty"`
|
Policies []*RetentionPolicy `json:"policies,omitempty"`
|
||||||
Series []*Series `json:"series,omitempty"`
|
Shards []*Shard `json:"shards,omitempty"`
|
||||||
|
Series []*Series `json:"series,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// databases represents a list of databases, sortable by name.
|
// databases represents a list of databases, sortable by name.
|
||||||
|
|
|
@ -226,13 +226,13 @@ func TestDatabase_CreateRetentionPolicy(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a retention policy on the database.
|
// Create a retention policy on the database.
|
||||||
ss := &influxdb.RetentionPolicy{
|
rp := &influxdb.RetentionPolicy{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Duration: time.Hour,
|
Duration: time.Hour,
|
||||||
ReplicaN: 2,
|
ReplicaN: 2,
|
||||||
SplitN: 3,
|
SplitN: 3,
|
||||||
}
|
}
|
||||||
if err := s.Database("foo").CreateRetentionPolicy(ss); err != nil {
|
if err := s.Database("foo").CreateRetentionPolicy(rp); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
s.Restart()
|
s.Restart()
|
||||||
|
@ -240,7 +240,7 @@ func TestDatabase_CreateRetentionPolicy(t *testing.T) {
|
||||||
// Verify that the policy exists.
|
// Verify that the policy exists.
|
||||||
if o := s.Database("foo").RetentionPolicy("bar"); o == nil {
|
if o := s.Database("foo").RetentionPolicy("bar"); o == nil {
|
||||||
t.Fatalf("retention policy not found")
|
t.Fatalf("retention policy not found")
|
||||||
} else if !reflect.DeepEqual(ss, o) {
|
} else if !reflect.DeepEqual(rp, o) {
|
||||||
t.Fatalf("retention policy mismatch: %#v", o)
|
t.Fatalf("retention policy mismatch: %#v", o)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -352,7 +352,8 @@ func TestDatabase_SetDefaultRetentionPolicy(t *testing.T) {
|
||||||
s.CreateDatabase("foo")
|
s.CreateDatabase("foo")
|
||||||
db := s.Database("foo")
|
db := s.Database("foo")
|
||||||
|
|
||||||
if err := db.CreateRetentionPolicy(&influxdb.RetentionPolicy{Name: "bar"}); err != nil {
|
rp := &influxdb.RetentionPolicy{Name: "bar"}
|
||||||
|
if err := db.CreateRetentionPolicy(rp); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
} else if db.RetentionPolicy("bar") == nil {
|
} else if db.RetentionPolicy("bar") == nil {
|
||||||
t.Fatal("retention policy not created")
|
t.Fatal("retention policy not created")
|
||||||
|
@ -363,20 +364,20 @@ func TestDatabase_SetDefaultRetentionPolicy(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rp := db.DefaultRetentionPolicy()
|
o := db.DefaultRetentionPolicy()
|
||||||
if rp == nil {
|
if o == nil {
|
||||||
t.Fatal("default policy not set")
|
t.Fatal("default policy not set")
|
||||||
} else if !reflect.DeepEqual(db.RetentionPolicy("bar"), rp) {
|
} else if !reflect.DeepEqual(rp, o) {
|
||||||
t.Fatalf("retention policy mismatch: %#v", rp)
|
t.Fatalf("retention policy mismatch: %#v", o)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Restart()
|
s.Restart()
|
||||||
|
|
||||||
rp = db.DefaultRetentionPolicy()
|
o = s.Database("foo").DefaultRetentionPolicy()
|
||||||
if rp == nil {
|
if o == nil {
|
||||||
t.Fatal("default policy not kept after restart")
|
t.Fatal("default policy not kept after restart")
|
||||||
} else if !reflect.DeepEqual(db.RetentionPolicy("bar"), rp) {
|
} else if !reflect.DeepEqual(rp, o) {
|
||||||
t.Fatalf("retention policy mismatch after restart: %#v", rp)
|
t.Fatalf("retention policy mismatch after restart: %#v", o)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
server.go
11
server.go
|
@ -619,7 +619,16 @@ func (s *Server) applySetDefaultRetentionPolicy(m *messaging.Message) error {
|
||||||
return ErrDatabaseNotFound
|
return ErrDatabaseNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.applySetDefaultRetentionPolicy(c.Name)
|
if err := db.applySetDefaultRetentionPolicy(c.Name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persist to metastore.
|
||||||
|
s.meta.mustUpdate(func(tx *metatx) error {
|
||||||
|
return tx.saveDatabase(db)
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type setDefaultRetentionPolicyCommand struct {
|
type setDefaultRetentionPolicyCommand struct {
|
||||||
|
|
Loading…
Reference in New Issue