-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stateless broker #1935
Stateless broker #1935
Changes from 1 commit
39ae8e6
16dbe8b
85be4e1
1bbf154
b937f06
ef8658e
9b5aeb1
713ca4b
5f5c6ca
4160d0b
27e9132
66115f9
5f6bcf5
7ab19b9
7880bc2
c7d4920
12e8939
4b9a93d
fc189cd
8e813ec
53dbec8
8cb7be4
96748cb
b045ad5
41d357a
06d8392
7dc465b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,9 +228,23 @@ func (b *Broker) closeTopics() { | |
// SetMaxIndex sets the highest index seen by the broker. | ||
// This is only used for internal log messages and topics may have a higher index. | ||
func (b *Broker) SetMaxIndex(index uint64) error { | ||
return b.meta.Update(func(tx *bolt.Tx) error { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
return b.setMaxIndex(index) | ||
} | ||
|
||
func (b *Broker) setMaxIndex(index uint64) error { | ||
// Update index in meta database. | ||
if err := b.meta.Update(func(tx *bolt.Tx) error { | ||
return tx.Bucket([]byte("meta")).Put([]byte("index"), u64tob(index)) | ||
}) | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
// Set in-memory index. | ||
b.index = index | ||
|
||
return nil | ||
} | ||
|
||
// Snapshot streams the current state of the broker and returns the index. | ||
|
@@ -335,8 +349,8 @@ func (b *Broker) Restore(r io.Reader) error { | |
defer b.mu.Unlock() | ||
|
||
// Remove and recreate broker path. | ||
if err := os.RemoveAll(b.path); err != nil && !os.IsNotExist(err) { | ||
return fmt.Errorf("remove all: %s", err) | ||
if err := b.reset(); err != nil && !os.IsNotExist(err) { | ||
return fmt.Errorf("reset: %s", err) | ||
} else if err = os.MkdirAll(b.path, 0700); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to |
||
return fmt.Errorf("mkdir: %s", err) | ||
} | ||
|
@@ -400,14 +414,43 @@ func (b *Broker) Restore(r io.Reader) error { | |
} | ||
|
||
// Set the highest seen index. | ||
if err := b.SetMaxIndex(sh.Index); err != nil { | ||
if err := b.setMaxIndex(sh.Index); err != nil { | ||
return fmt.Errorf("set max index: %s", err) | ||
} | ||
b.index = sh.Index | ||
|
||
return nil | ||
} | ||
|
||
// reset removes all files in the broker directory besides the raft directory. | ||
func (b *Broker) reset() error { | ||
// Open handle to directory. | ||
f, err := os.Open(b.path) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = f.Close() }() | ||
|
||
// Read directory items. | ||
fis, err := f.Readdir(0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Remove all files & directories besides raft. | ||
for _, fi := range fis { | ||
if fi.Name() == "raft" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We better never change the name of this directory. :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started moving it out of the |
||
continue | ||
} | ||
|
||
if err := os.RemoveAll(fi.Name()); err != nil { | ||
return fmt.Errorf("remove: %s", fi.Name()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Publish writes a message. | ||
// Returns the index of the message. Otherwise returns an error. | ||
func (b *Broker) Publish(m *Message) (uint64, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"seen" is not a Raft term. Is there a specific Raft term that you actually mean here? "Committed", "applied"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. It should have been "applied".