-
-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0c4e52
commit 4b0aa1d
Showing
5 changed files
with
211 additions
and
81 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,40 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
gracefully "github.com/tj/go-gracefully" | ||
"github.com/travisjeffery/jocko/server" | ||
"github.com/travisjeffery/jocko/store" | ||
kingpin "gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
var ( | ||
logDir = kingpin.Flag("logdir", "A comma separated list of directories under which to store log files").Default("/tmp/jocko").String() | ||
httpAddr = kingpin.Flag("httpaddr", "HTTP Address to listen on").String() | ||
raftDir = kingpin.Flag("raftdir", "Directory for raft to store data").String() | ||
raftAddr = kingpin.Flag("raftaddr", "Address for Raft to bind on").String() | ||
) | ||
|
||
func main() { | ||
kingpin.Parse() | ||
|
||
store := store.New(*raftDir, *raftAddr) | ||
if err := store.Open(); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error opening raft store: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
server := server.New(*httpAddr, store) | ||
if err := server.Start(); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error starting server: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
gracefully.Timeout = 10 * time.Second | ||
gracefully.Shutdown() | ||
|
||
server.Close() | ||
} |
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
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
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,136 @@ | ||
package store | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"time" | ||
|
||
msgpack "gopkg.in/vmihailenco/msgpack.v2" | ||
|
||
"github.com/hashicorp/raft" | ||
raftboltdb "github.com/hashicorp/raft-boltdb" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
timeout = 10 * time.Second | ||
) | ||
|
||
type command struct { | ||
Op []byte `msgpack:"op"` | ||
Key []byte `msgpack:"key"` | ||
Value []byte `msgpack:"value"` | ||
} | ||
|
||
type Store struct { | ||
dataDir string | ||
bindAddr string | ||
|
||
mu sync.Mutex | ||
|
||
peerStore raft.PeerStore | ||
raft *raft.Raft | ||
store *raftboltdb.BoltStore | ||
} | ||
|
||
func New(dataDir, bindAddr string) *Store { | ||
return &Store{ | ||
dataDir: dataDir, | ||
bindAddr: bindAddr, | ||
} | ||
} | ||
|
||
func (s *Store) Open() error { | ||
conf := raft.DefaultConfig() | ||
|
||
conf.EnableSingleNode = true | ||
|
||
addr, err := net.ResolveTCPAddr("tcp", s.bindAddr) | ||
if err != nil { | ||
return errors.Wrap(err, "resolve bind addr failed") | ||
} | ||
|
||
transport, err := raft.NewTCPTransport(s.bindAddr, addr, 3, timeout, os.Stderr) | ||
if err != nil { | ||
return errors.Wrap(err, "tcp transport failede") | ||
} | ||
|
||
s.peerStore = raft.NewJSONPeers(s.dataDir, transport) | ||
|
||
snapshots, err := raft.NewFileSnapshotStore(s.dataDir, 2, os.Stderr) | ||
if err != nil { | ||
return fmt.Errorf("file snapshot store: %s", err) | ||
} | ||
|
||
boltStore, err := raftboltdb.NewBoltStore(filepath.Join(s.dataDir, "store.db")) | ||
if err != nil { | ||
return errors.Wrap(err, "bolt store failed") | ||
} | ||
s.store = boltStore | ||
|
||
raft, err := raft.NewRaft(conf, s, boltStore, boltStore, snapshots, s.peerStore, transport) | ||
if err != nil { | ||
return errors.Wrap(err, "raft failed") | ||
} | ||
|
||
s.raft = raft | ||
|
||
return nil | ||
} | ||
|
||
func (s *Store) Get(key []byte) ([]byte, error) { | ||
// add cache | ||
return s.store.Get(key) | ||
} | ||
|
||
func (s *Store) Set(key, value []byte) error { | ||
c := &command{ | ||
Op: []byte("set"), | ||
Key: key, | ||
Value: value, | ||
} | ||
b, err := msgpack.Marshal(c) | ||
if err != nil { | ||
return errors.Wrap(err, "msgpack failed") | ||
} | ||
f := s.raft.Apply(b, timeout) | ||
return f.Error() | ||
} | ||
|
||
func (s *Store) Delete(key []byte) error { | ||
return nil | ||
} | ||
|
||
func (s *Store) Join(addr []byte) error { | ||
f := s.raft.AddPeer(string(addr)) | ||
return f.Error() | ||
} | ||
|
||
func (s *Store) Apply(l *raft.Log) interface{} { | ||
return nil | ||
} | ||
|
||
func (s *Store) applySet(k, v []byte) interface{} { | ||
return nil | ||
} | ||
|
||
func (s *Store) Restore(rc io.ReadCloser) error { | ||
return nil | ||
} | ||
|
||
type FSMSnapshot struct { | ||
} | ||
|
||
func (f *FSMSnapshot) Persist(sink raft.SnapshotSink) error { | ||
return nil | ||
} | ||
|
||
func (f *FSMSnapshot) Release() {} | ||
|
||
func (s *Store) Snapshot() (raft.FSMSnapshot, error) { | ||
return &FSMSnapshot{}, nil | ||
} |
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,19 @@ | ||
package store | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStoreOpen(t *testing.T) { | ||
dataDir, _ := ioutil.TempDir("", "storetest") | ||
defer os.RemoveAll(dataDir) | ||
bindAddr := "127.0.0.1:0" | ||
|
||
s := New(dataDir, bindAddr) | ||
assert.NotNil(t, s) | ||
assert.NoError(t, s.Open()) | ||
} |