-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enhancement] logtail: make table subscription async (#16879)
A table subscription takes long time if the table has much data which need flushed, and it will block the logtail update of other tables because they work in the same goroutine. So we put the table subscription into a independent goroutine, thus the logtails of other tables could be pushed to cn servers without being blocked. And when the first phase of pulling table data finishes, put it into the same goroutine with pushing-job, and pull the data again for the second phase. At last, send the whole data that are merged together to cn servers. After the PR, the table subscription will not block pushing-jobs. There are 50 parallel pulling jobs at the same time at most by defalt to avoid OOM and other risks. Approved by: @zhangxu19830126, @triump2020, @aptend, @XuPeng-SH
- Loading branch information
1 parent
2e1afe3
commit 9504578
Showing
17 changed files
with
417 additions
and
74 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
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
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,96 @@ | ||
// Copyright 2021 - 2024 Matrix Origin | ||
// | ||
// 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 service | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/matrixorigin/matrixone/pkg/pb/api" | ||
"github.com/matrixorigin/matrixone/pkg/pb/logtail" | ||
"github.com/matrixorigin/matrixone/pkg/pb/timestamp" | ||
) | ||
|
||
const ( | ||
ckpLocationDivider = ';' | ||
) | ||
|
||
// logtailMerger is the merger tool to merge multiple | ||
// LogtailPhase instances. | ||
type logtailMerger struct { | ||
logtails []*LogtailPhase | ||
} | ||
|
||
func newLogtailMerger(l ...*LogtailPhase) *logtailMerger { | ||
var lm logtailMerger | ||
lm.logtails = append(lm.logtails, l...) | ||
return &lm | ||
} | ||
|
||
// Merge merges all instances and return one logtail.TableLogtail | ||
// and a callback function. | ||
func (m *logtailMerger) Merge() (logtail.TableLogtail, func()) { | ||
var tableID api.TableID | ||
var entryCount int | ||
var ts timestamp.Timestamp | ||
for _, t := range m.logtails { | ||
entryCount += len(t.tail.Commands) | ||
|
||
// check the table ID. | ||
if tableID.TbId == 0 { | ||
tableID = *t.tail.Table | ||
} else if tableID.TbId != t.tail.Table.TbId { | ||
panic(fmt.Sprintf("cannot merge logtails with different table: %d, %d", | ||
tableID.TbId, t.tail.Table.TbId)) | ||
} | ||
|
||
// get the max timestamp. | ||
if ts.Less(*t.tail.Ts) { | ||
ts = *t.tail.Ts | ||
} | ||
} | ||
|
||
// create the callbacks. | ||
callbacks := make([]func(), 0, entryCount) | ||
// create a new table logtail with the entry number. | ||
tail := logtail.TableLogtail{ | ||
Ts: &ts, | ||
Table: &tableID, | ||
Commands: make([]api.Entry, 0, entryCount), | ||
} | ||
for _, t := range m.logtails { | ||
ckpLocLen := len(tail.CkpLocation) | ||
if ckpLocLen > 0 && | ||
tail.CkpLocation[ckpLocLen-1] != ckpLocationDivider { | ||
tail.CkpLocation += string(ckpLocationDivider) | ||
} | ||
tail.CkpLocation += t.tail.CkpLocation | ||
tail.Commands = append(tail.Commands, t.tail.Commands...) | ||
callbacks = append(callbacks, t.closeCB) | ||
} | ||
|
||
// remove the last ';' | ||
ckpLocLen := len(tail.CkpLocation) | ||
if ckpLocLen > 0 && | ||
tail.CkpLocation[ckpLocLen-1] == ckpLocationDivider { | ||
tail.CkpLocation = tail.CkpLocation[:ckpLocLen-1] | ||
} | ||
return tail, func() { | ||
for _, cb := range callbacks { | ||
if cb != nil { | ||
cb() | ||
} | ||
} | ||
} | ||
} |
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,68 @@ | ||
// Copyright 2021 - 2024 Matrix Origin | ||
// | ||
// 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 service | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/matrixorigin/matrixone/pkg/pb/api" | ||
"github.com/matrixorigin/matrixone/pkg/pb/logtail" | ||
"github.com/matrixorigin/matrixone/pkg/pb/timestamp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogtailMerge(t *testing.T) { | ||
var cbValue int | ||
tail1 := &LogtailPhase{ | ||
tail: logtail.TableLogtail{ | ||
CkpLocation: "aaa;bbb;", | ||
Ts: ×tamp.Timestamp{ | ||
PhysicalTime: 100, | ||
}, | ||
Table: &api.TableID{ | ||
DbId: 1, | ||
TbId: 2, | ||
}, | ||
Commands: []api.Entry{{TableId: 2}}, | ||
}, | ||
closeCB: func() { | ||
cbValue++ | ||
}, | ||
} | ||
tail2 := &LogtailPhase{ | ||
tail: logtail.TableLogtail{ | ||
CkpLocation: "ccc;ddd", | ||
Ts: ×tamp.Timestamp{ | ||
PhysicalTime: 200, | ||
}, | ||
Table: &api.TableID{ | ||
DbId: 1, | ||
TbId: 2, | ||
}, | ||
Commands: []api.Entry{{TableId: 2}}, | ||
}, | ||
closeCB: func() { | ||
cbValue++ | ||
}, | ||
} | ||
lm := newLogtailMerger(tail1, tail2) | ||
assert.NotNil(t, lm) | ||
tail, cb := lm.Merge() | ||
cb() | ||
assert.Equal(t, 2, cbValue) | ||
assert.Equal(t, 2, len(tail.Commands)) | ||
assert.Equal(t, int64(200), tail.Ts.PhysicalTime) | ||
assert.Equal(t, "aaa;bbb;ccc;ddd", tail.CkpLocation) | ||
} |
Oops, something went wrong.