-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to journal functionallity to make it safer
Lots of concurrency fixes and some readme updates
- Loading branch information
1 parent
7b2aad3
commit 2a916bc
Showing
37 changed files
with
606 additions
and
328 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,78 @@ | ||
package coordinator | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"barf/internal/journal" | ||
"barf/internal/op" | ||
) | ||
|
||
var entries []*journal.JournalEntry | ||
var entriesMu sync.Mutex | ||
|
||
func addEntry(e *journal.JournalEntry) { | ||
entriesMu.Lock() | ||
defer entriesMu.Unlock() | ||
|
||
entries = append(entries, e) | ||
} | ||
|
||
func removeEntry(e *journal.JournalEntry) { | ||
entriesMu.Lock() | ||
defer entriesMu.Unlock() | ||
|
||
for i, s := range entries { | ||
if s == e { | ||
copy(entries[i:], entries[i+1:]) | ||
entries = entries[:len(entries)-1] | ||
|
||
return | ||
} | ||
} | ||
} | ||
|
||
func getEntry(operationID op.OperationID) *journal.JournalEntry { | ||
entriesMu.Lock() | ||
defer entriesMu.Unlock() | ||
|
||
for _, e := range entries { | ||
if e.Operation.ID == operationID { | ||
return e | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getEntries() []*journal.JournalEntry { | ||
entriesMu.Lock() | ||
defer entriesMu.Unlock() | ||
|
||
result := make([]*journal.JournalEntry, len(entries)) | ||
copy(result, entries) | ||
|
||
return result | ||
} | ||
|
||
func getNextIndex() (op.OperationIndex, error) { | ||
var index op.OperationIndex = 1 | ||
|
||
indexFree := func(index op.OperationIndex) bool { | ||
for _, e := range entries { | ||
if e.Operation.Index == index { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
for ; !indexFree(index); index++ { | ||
if index == 0 { | ||
return 0, errors.New("Could not get new index, overflow") | ||
} | ||
} | ||
|
||
return index, 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package journal | ||
package coordinator | ||
|
||
import "barf/internal/op" | ||
|
||
|
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,46 @@ | ||
package coordinator | ||
|
||
import ( | ||
"errors" | ||
|
||
"barf/internal/com/server" | ||
"barf/internal/journal" | ||
"barf/internal/op" | ||
) | ||
|
||
// Initialize reads active entries from disk | ||
func Initialize() error { | ||
if registerdStartHandler == nil { | ||
return errors.New("No start handler registered") | ||
} | ||
|
||
entryList, err := journal.Initialize() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
entriesMu.Lock() | ||
entries = entryList | ||
entriesMu.Unlock() | ||
|
||
server.OnOperationCreate(create) | ||
server.OnOperationAbort(abort) | ||
server.OnOperationStatus(status) | ||
server.OnListOperations(list) | ||
|
||
for _, e := range getEntries() { | ||
err = registerdStartHandler(e.Operation) | ||
|
||
if err != nil { | ||
UpdateOperationStatus(e.Operation.ID, &op.OperationStatus{ | ||
Finished: true, | ||
ExitCode: 255, | ||
Message: err.Error(), | ||
}) | ||
return err | ||
} | ||
} | ||
|
||
return 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,27 @@ | ||
package coordinator | ||
|
||
import ( | ||
"barf/internal/op" | ||
) | ||
|
||
// WriteOperationStdout writes to the operations stdout log | ||
func WriteOperationStdout(operationID op.OperationID, line string) { | ||
e := getEntry(operationID) | ||
|
||
if e == nil { | ||
return // Just drop | ||
} | ||
|
||
e.WriteStdout(line) | ||
} | ||
|
||
// WriteOperationStderr writes to the operations stderr log | ||
func WriteOperationStderr(operationID op.OperationID, line string) { | ||
e := getEntry(operationID) | ||
|
||
if e == nil { | ||
return // Just drop | ||
} | ||
|
||
e.WriteStderr(line) | ||
} |
20 changes: 8 additions & 12 deletions
20
internal/journal/update.go → internal/coordinator/update.go
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
Oops, something went wrong.