-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This increases throughput of adding new events by 2-3x. Previously the repository would add events one by one, making two queries per event. It was done this way to handle duplicates and to ensure the EventRaws row had the same ID as the corresponding Events row The new implementation instead does three queries for the entire batch. The way this works is using AUTOINCREMENT + ON CONFLICT IGNORE. When there is a duplicate in the batch, it will be ignored by ON CONFLICT IGNORE, but because of AUTOINCREMENT the ID will still be "used up", leaving a hole in the sequence of IDs. After all the non-duplicate events have been inserted into Events, ALL events are inserted into EventRaws. Since the IDs in the Events table are used up even on duplicate events, the ID sequence of EventRaws should still line up with the Events sequence. After both inserts are performed, the "garbage" events in EventRaws are removed by deleting all EventRaws rows which do not have a corresponding Events row. This is way faster, but I am not sure it is robust in all situations and the AUTOINCREMENT + ON CONFLICT IGNORE behavior seems like something that might change in a future version of SQLite? The other drawback is that handling duplicates is now way slower, since the EventRaws need to be added and then removed now whereas before they would just try to add to the Events table and then skip the raw after getting a conflict. My thinking is that trying to add duplicates should be the exception, so speeding up the common case is worth the tradeoff. True batch can be disabled by setting "sqlite.trueBatch" to false in the configuration file.
- Loading branch information
1 parent
5b6c057
commit 82b069b
Showing
10 changed files
with
207 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ package config | |
|
||
type SqliteConfig struct { | ||
DatabaseFile string | ||
TrueBatch bool | ||
} |
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
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,89 @@ | ||
// Copyright 2020 The Logsuck Authors | ||
// | ||
// 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. | ||
|
||
package events | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jackbister/logsuck/internal/config" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func TestAddBatchTrueBatch(t *testing.T) { | ||
db, err := sql.Open("sqlite3", ":memory:") | ||
if err != nil { | ||
t.Fatalf("TestRexPipelineStep got error when creating in-memory SQLite database: %v", err) | ||
} | ||
repo, err := SqliteRepository(db, &config.SqliteConfig{ | ||
DatabaseFile: ":memory:", | ||
TrueBatch: true, | ||
}) | ||
if err != nil { | ||
t.Fatalf("TestRexPipelineStep got error when creating events repo: %v", err) | ||
} | ||
|
||
repo.AddBatch([]Event{ | ||
{ | ||
Raw: "2021-02-01 00:00:00 log event", | ||
Timestamp: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC), | ||
Host: "localhost", | ||
Source: "log.txt", | ||
Offset: 0, | ||
}, | ||
}) | ||
|
||
evts, err := repo.GetByIds([]int64{0}, SortModeNone) | ||
if err != nil { | ||
t.Fatalf("got error when retrieving event: %v", err) | ||
} | ||
if len(evts) != 1 { | ||
t.Fatalf("got unexpected number of events, expected 1 event but got %v", len(evts)) | ||
} | ||
} | ||
|
||
func TestAddBatchOneByOne(t *testing.T) { | ||
db, err := sql.Open("sqlite3", ":memory:") | ||
if err != nil { | ||
t.Fatalf("TestRexPipelineStep got error when creating in-memory SQLite database: %v", err) | ||
} | ||
repo, err := SqliteRepository(db, &config.SqliteConfig{ | ||
DatabaseFile: ":memory:", | ||
TrueBatch: false, | ||
}) | ||
if err != nil { | ||
t.Fatalf("TestRexPipelineStep got error when creating events repo: %v", err) | ||
} | ||
|
||
repo.AddBatch([]Event{ | ||
{ | ||
Raw: "2021-02-01 00:00:00 log event", | ||
Timestamp: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC), | ||
Host: "localhost", | ||
Source: "log.txt", | ||
Offset: 0, | ||
}, | ||
}) | ||
|
||
evts, err := repo.GetByIds([]int64{0}, SortModeNone) | ||
if err != nil { | ||
t.Fatalf("got error when retrieving event: %v", err) | ||
} | ||
if len(evts) != 1 { | ||
t.Fatalf("got unexpected number of events, expected 1 event but got %v", len(evts)) | ||
} | ||
} |
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