forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for eventlog (gomods#129)
Eventlog is append-only log. Will be used in - Olympus as a log of backed modules - Proxy as a log of backed modules - Proxy as a log of registered cache misses (clearable after read) Multiple backing storages should be supported, for now, mongo is implemented. Preparation for Olympus reader, which is remote reader of Olympus event log, is in place without implementation, this implementation is for proxies to read logs from Olympus, and Olympus instances between each other (Pull synchronization)
- Loading branch information
1 parent
cde2507
commit 7cd41ef
Showing
11 changed files
with
606 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package disposable | ||
|
||
import ( | ||
"github.com/gomods/athens/pkg/eventlog" | ||
) | ||
|
||
// Log is event log fetched from olympus server | ||
type Log struct { | ||
e eventlog.Eventlog | ||
} | ||
|
||
// NewLog creates log reader from remote log, log gets cleared after each read | ||
func NewLog(log eventlog.Eventlog) (*Log, error) { | ||
return &Log{e: log}, nil | ||
} | ||
|
||
// Read reads all events in event log. | ||
func (c *Log) Read() ([]eventlog.Event, error) { | ||
ee, err := c.e.Read() | ||
if err != nil { | ||
return ee, err | ||
} | ||
|
||
if len(ee) > 0 { | ||
last := ee[len(ee)-1] | ||
return ee, c.e.Clear(last.ID) | ||
} | ||
|
||
return ee, nil | ||
} | ||
|
||
// ReadFrom reads all events from the log starting at event with specified id (excluded). | ||
// If id is not found behaves like Read(). | ||
func (c *Log) ReadFrom(id string) ([]eventlog.Event, error) { | ||
ee, err := c.e.ReadFrom(id) | ||
if err != nil { | ||
return ee, err | ||
} | ||
|
||
if len(ee) > 0 { | ||
return ee, c.e.Clear(id) | ||
} | ||
|
||
return ee, nil | ||
} | ||
|
||
// Append appends Event to event log and returns its ID. | ||
func (c *Log) Append(event eventlog.Event) (string, error) { | ||
return c.e.Append(event) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package eventlog | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Event is entry of event log specifying demand for a module. | ||
type Event struct { | ||
// ID is identifier, also used as a pointer reference target. | ||
ID string `json:"_id" bson:"_id,omitempty"` | ||
// Time is cache-miss created/handled time. | ||
Time time.Time `json:"time_created" bson:"time_created"` | ||
// Module is module name. | ||
Module string `json:"module" bson:"module"` | ||
// Version is version of a module e.g. "1.10", "1.10-deprecated" | ||
Version string `json:"version" bson:"version"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package eventlog | ||
|
||
// Eventlog is append only log of Events. | ||
type Eventlog interface { | ||
Reader | ||
Appender | ||
Clearer | ||
} | ||
|
||
// Reader is reader of append only event log.s | ||
type Reader interface { | ||
// Read reads all events in event log. | ||
Read() ([]Event, error) | ||
|
||
// ReadFrom reads all events from the log starting at event with specified id (excluded). | ||
// If id is not found behaves like Read(). | ||
ReadFrom(id string) ([]Event, error) | ||
} | ||
|
||
// Appender is writer to append only event log. | ||
type Appender interface { | ||
// Append appends Event to event log and returns its ID. | ||
Append(event Event) (string, error) | ||
} | ||
|
||
// Clearer is interface used to clear state of event log | ||
type Clearer interface { | ||
Clear(id string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package mongo | ||
|
||
import ( | ||
"github.com/globalsign/mgo" | ||
"github.com/globalsign/mgo/bson" | ||
"github.com/gomods/athens/pkg/eventlog" | ||
) | ||
|
||
// Log is event log fetched from backing mongo database | ||
type Log struct { | ||
s *mgo.Session | ||
d string // database | ||
c string // collection | ||
url string | ||
} | ||
|
||
// NewLog creates event log from backing mongo database | ||
func NewLog(url string) (*Log, error) { | ||
return NewLogWithCollection(url, "eventlog") | ||
} | ||
|
||
// NewLogWithCollection creates event log from backing mongo database | ||
func NewLogWithCollection(url, collection string) (*Log, error) { | ||
m := &Log{ | ||
url: url, | ||
c: collection, | ||
d: "athens", | ||
} | ||
return m, m.Connect() | ||
} | ||
|
||
// Connect establishes a session to the mongo cluster. | ||
func (m *Log) Connect() error { | ||
s, err := mgo.Dial(m.url) | ||
if err != nil { | ||
return err | ||
} | ||
m.s = s | ||
|
||
return nil | ||
} | ||
|
||
// Read reads all events in event log. | ||
func (m *Log) Read() ([]eventlog.Event, error) { | ||
var events []eventlog.Event | ||
|
||
c := m.s.DB(m.d).C(m.c) | ||
err := c.Find(nil).All(&events) | ||
|
||
return events, err | ||
} | ||
|
||
// ReadFrom reads all events from the log starting at event with specified id (excluded). | ||
// If id is not found behaves like Read(). | ||
func (m *Log) ReadFrom(id string) ([]eventlog.Event, error) { | ||
var events []eventlog.Event | ||
|
||
c := m.s.DB(m.d).C(m.c) | ||
err := c.Find(bson.M{"_id": bson.M{"$gt": id}}).All(&events) | ||
|
||
return events, err | ||
} | ||
|
||
// Append appends Event to event log and returns its ID. | ||
func (m *Log) Append(event eventlog.Event) (string, error) { | ||
event.ID = bson.NewObjectId().Hex() | ||
c := m.s.DB(m.d).C(m.c) | ||
err := c.Insert(event) | ||
|
||
return event.ID, err | ||
} | ||
|
||
// Clear is a method for clearing entire state of event log | ||
func (m *Log) Clear(id string) error { | ||
c := m.s.DB(m.d).C(m.c) | ||
|
||
if id == "" { | ||
_, err := c.RemoveAll(nil) | ||
return err | ||
} | ||
|
||
_, err := c.RemoveAll(bson.M{"_id": bson.M{"$lte": id}}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package mongo | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/gomods/athens/pkg/eventlog" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type MongoTests struct { | ||
suite.Suite | ||
log *Log | ||
} | ||
|
||
func TestMongo(t *testing.T) { | ||
suite.Run(t, new(MongoTests)) | ||
} | ||
|
||
func (m *MongoTests) SetupTest() { | ||
store, err := NewLog("mongodb://127.0.0.1:27017") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
store.Connect() | ||
|
||
store.s.DB(store.d).C(store.c).RemoveAll(nil) | ||
m.log = store | ||
} | ||
|
||
func (m *MongoTests) TestRead() { | ||
r := m.Require() | ||
versions := []string{"v1.0.0", "v1.1.0", "v1.2.0"} | ||
for _, version := range versions { | ||
_, err := m.log.Append(eventlog.Event{Module: "m1", Version: version, Time: time.Now()}) | ||
r.NoError(err) | ||
} | ||
|
||
retVersions, err := m.log.Read() | ||
r.NoError(err) | ||
r.Equal(versions[0], retVersions[0].Version) | ||
r.Equal(versions[1], retVersions[1].Version) | ||
r.Equal(versions[2], retVersions[2].Version) | ||
} | ||
|
||
func (m *MongoTests) TestReadFrom() { | ||
r := m.Require() | ||
versions := []string{"v1.0.0", "v1.1.0", "v1.2.0"} | ||
pointers := make(map[string]string) | ||
for _, version := range versions { | ||
p, _ := m.log.Append(eventlog.Event{Module: "m1", Version: version, Time: time.Now()}) | ||
pointers[version] = p | ||
} | ||
|
||
retVersions, err := m.log.ReadFrom(pointers[versions[0]]) | ||
r.NoError(err) | ||
r.Equal(versions[1], retVersions[0].Version) | ||
r.Equal(versions[2], retVersions[1].Version) | ||
|
||
retVersions, err = m.log.ReadFrom(pointers[versions[1]]) | ||
r.NoError(err) | ||
r.Equal(versions[2], retVersions[0].Version) | ||
|
||
retVersions, err = m.log.ReadFrom(pointers[versions[2]]) | ||
r.NoError(err) | ||
r.Equal(0, len(retVersions)) | ||
} | ||
|
||
func (m *MongoTests) TestClear() { | ||
r := m.Require() | ||
versions := []string{"v1.0.0", "v1.1.0", "v1.2.0"} | ||
for _, version := range versions { | ||
m.log.Append(eventlog.Event{Module: "m1", Version: version, Time: time.Now()}) | ||
} | ||
|
||
retVersions, err := m.log.Read() | ||
r.NoError(err) | ||
r.Equal(3, len(retVersions)) | ||
|
||
err = m.log.Clear("") | ||
r.NoError(err) | ||
|
||
retVersions, err = m.log.Read() | ||
r.NoError(err) | ||
r.Equal(0, len(retVersions)) | ||
} | ||
|
||
func (m *MongoTests) TestClearFrom() { | ||
r := m.Require() | ||
versions := []string{"v1.0.0", "v1.1.0", "v1.2.0"} | ||
pointers := make(map[string]string) | ||
for _, version := range versions { | ||
p, _ := m.log.Append(eventlog.Event{Module: "m1", Version: version, Time: time.Now()}) | ||
pointers[version] = p | ||
} | ||
|
||
retVersions, err := m.log.Read() | ||
r.NoError(err) | ||
r.Equal(3, len(retVersions)) | ||
|
||
err = m.log.Clear(pointers[versions[1]]) | ||
r.NoError(err) | ||
|
||
retVersions, err = m.log.Read() | ||
r.NoError(err) | ||
r.Equal(1, len(retVersions)) | ||
r.Equal(versions[2], retVersions[0].Version) | ||
} |
Oops, something went wrong.