diff --git a/build.sh b/build.sh index fc48ca9..2041f2e 100644 --- a/build.sh +++ b/build.sh @@ -1,4 +1,6 @@ #!/bin/bash +# +# buildpath=/tmp/gh-osc target=gh-osc diff --git a/go/binlog/binlog.go b/go/binlog/binlog.go index 6925fa1..8f7c67e 100644 --- a/go/binlog/binlog.go +++ b/go/binlog/binlog.go @@ -1,5 +1,5 @@ /* - Copyright 2015 Shlomi Noach, courtesy Booking.com + Copyright 2015 Shlomi Noach */ package binlog @@ -11,8 +11,10 @@ import ( "strings" ) +// BinlogType identifies the type of the log: relay or binary log type BinlogType int +// BinaryLog, RelayLog are binlog types const ( BinaryLog BinlogType = iota RelayLog diff --git a/go/binlog/binlog_entry.go b/go/binlog/binlog_entry.go new file mode 100644 index 0000000..bbfbecc --- /dev/null +++ b/go/binlog/binlog_entry.go @@ -0,0 +1,14 @@ +/* + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-osc/blob/master/LICENSE +*/ + +package binlog + +type BinlogEntry struct { + LogPos uint64 + EndLogPos uint64 + StatementType string // INSERT, UPDATE, DELETE + DatabaseName string + TableName string +} diff --git a/go/binlog/binlog_reader.go b/go/binlog/binlog_reader.go index a837249..3392460 100644 --- a/go/binlog/binlog_reader.go +++ b/go/binlog/binlog_reader.go @@ -1,17 +1,12 @@ /* - Copyright 2016 GitHub Inc. + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-osc/blob/master/LICENSE */ package binlog -type BinlogEntry struct { - LogPos uint64 - EndLogPos uint64 - StatementType string // INSERT, UPDATE, DELETE - DatabaseName string - TableName string -} - +// BinlogReader is a general interface whose implementations can choose their methods of reading +// a binary log file and parsing it into binlog entries type BinlogReader interface { ReadEntries(logFile string, startPos uint64, stopPos uint64) (entries [](*BinlogEntry), err error) } diff --git a/go/binlog/binlog_test.go b/go/binlog/binlog_test.go index 55fff25..75cca90 100644 --- a/go/binlog/binlog_test.go +++ b/go/binlog/binlog_test.go @@ -1,17 +1,6 @@ /* - Copyright 2014 Outbrain Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-osc/blob/master/LICENSE */ package binlog diff --git a/go/binlog/mysqlbinlog_reader.go b/go/binlog/mysqlbinlog_reader.go index 5f33284..44a60b8 100644 --- a/go/binlog/mysqlbinlog_reader.go +++ b/go/binlog/mysqlbinlog_reader.go @@ -1,5 +1,6 @@ /* - Copyright 2016 GitHub Inc. + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-osc/blob/master/LICENSE */ package binlog @@ -26,8 +27,10 @@ var ( tokenRegxp = regexp.MustCompile("### (WHERE|SET)$") ) +// BinlogEntryState is a state in the binlog parser automaton / state machine type BinlogEntryState string +// States of the state machine const ( InvalidState BinlogEntryState = "InvalidState" SearchForStartPosOrStatementState = "SearchForStartPosOrStatementState" @@ -44,6 +47,7 @@ type MySQLBinlogReader struct { MySQLBinlogBinary string } +// NewMySQLBinlogReader creates a new reader that directly parses binlog files from the filesystem func NewMySQLBinlogReader(basedir string, datadir string) (mySQLBinlogReader *MySQLBinlogReader) { mySQLBinlogReader = &MySQLBinlogReader{ Basedir: basedir, @@ -89,8 +93,8 @@ func (this *MySQLBinlogReader) ReadEntries(logFile string, startPos uint64, stop return entries, err } +// automaton step: accept wither beginning of new entry, or beginning of new statement func searchForStartPosOrStatement(scanner *bufio.Scanner, binlogEntry *BinlogEntry, previousEndLogPos uint64) (nextState BinlogEntryState, nextBinlogEntry *BinlogEntry, err error) { - onStartEntry := func(submatch []string) (BinlogEntryState, *BinlogEntry, error) { startLogPos, _ := strconv.ParseUint(submatch[1], 10, 64) @@ -135,6 +139,7 @@ func searchForStartPosOrStatement(scanner *bufio.Scanner, binlogEntry *BinlogEnt return SearchForStartPosOrStatementState, binlogEntry, nil } +// automaton step: expect an end_log_pos line` func expectEndLogPos(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState BinlogEntryState, err error) { line := scanner.Text() @@ -146,6 +151,8 @@ func expectEndLogPos(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextStat return InvalidState, fmt.Errorf("Expected to find end_log_pos following pos %+v", binlogEntry.LogPos) } +// automaton step: a not-strictly-required but good-to-have-around validation that +// we see an expected token following a statement func expectToken(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState BinlogEntryState, err error) { line := scanner.Text() if submatch := tokenRegxp.FindStringSubmatch(line); len(submatch) > 1 { @@ -154,6 +161,8 @@ func expectToken(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState Bi return InvalidState, fmt.Errorf("Expected to find token following pos %+v", binlogEntry.LogPos) } +// parseEntries will parse output of `mysqlbinlog --verbose --base64-output=DECODE-ROWS` +// It issues an automaton / state machine to do its thang. func parseEntries(scanner *bufio.Scanner) (entries [](*BinlogEntry), err error) { binlogEntry := &BinlogEntry{} var state BinlogEntryState = SearchForStartPosOrStatementState diff --git a/go/binlog/mysqlbinlog_reader_test.go b/go/binlog/mysqlbinlog_reader_test.go index 24a6e1a..0f75e82 100644 --- a/go/binlog/mysqlbinlog_reader_test.go +++ b/go/binlog/mysqlbinlog_reader_test.go @@ -1,17 +1,6 @@ /* - Copyright 2014 Outbrain Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-osc/blob/master/LICENSE */ package binlog diff --git a/go/cmd/gh-osc/main.go b/go/cmd/gh-osc/main.go index c5e9d05..4cb8a10 100644 --- a/go/cmd/gh-osc/main.go +++ b/go/cmd/gh-osc/main.go @@ -1,3 +1,8 @@ +/* + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-osc/blob/master/LICENSE +*/ + package main import ( diff --git a/go/os/process.go b/go/os/process.go index ee19b66..edcb62c 100644 --- a/go/os/process.go +++ b/go/os/process.go @@ -35,8 +35,6 @@ func execCmd(commandText string, arguments ...string) (*exec.Cmd, string, error) shellArguments = append(shellArguments, arguments...) log.Debugf("%+v", shellArguments) return exec.Command("bash", shellArguments...), tmpFile.Name(), nil - - //return exec.Command(commandText, arguments...) , "", nil } // CommandRun executes a command