-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.2 support compile service (#17209)
1. support CompileService to manage the runningCompile. 2. modify the function `AbortAllRunningTxn`. Approved by: @zhangxu19830126, @daviszhen, @qingxinhome, @badboynt1, @ouyuanning, @sukki37
- Loading branch information
Showing
14 changed files
with
280 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 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 compile | ||
|
||
import ( | ||
"context" | ||
"github.com/matrixorigin/matrixone/pkg/common/reuse" | ||
txnClient "github.com/matrixorigin/matrixone/pkg/txn/client" | ||
"github.com/matrixorigin/matrixone/pkg/vm/process" | ||
"sync" | ||
) | ||
|
||
// todo: Move it to a CN level structure next day. | ||
var compileService *ServiceOfCompile | ||
|
||
func init() { | ||
compileService = InitCompileService() | ||
txnClient.SetRunningPipelineManagement(compileService) | ||
} | ||
|
||
func GetCompileService() *ServiceOfCompile { | ||
return compileService | ||
} | ||
|
||
// ServiceOfCompile is used to manage the lifecycle of Compile structures, | ||
// including their creation and deletion. | ||
// | ||
// It also tracks the currently active complies within a single CN. | ||
type ServiceOfCompile struct { | ||
sync.RWMutex | ||
|
||
// ongoing compiles with additional information. | ||
aliveCompiles map[*Compile]compileAdditionalInformation | ||
} | ||
|
||
// compileAdditionalInformation holds additional information for one compile. | ||
// to help control one compile. | ||
type compileAdditionalInformation struct { | ||
// mustReturnError holds an error that must be returned if set. | ||
mustReturnError error | ||
|
||
// queryCancel is a method to cancel an ongoing query. | ||
queryCancel context.CancelFunc | ||
// queryDone is a waiter that checks if this query has been completed or not. | ||
queryDone queryDoneWaiter | ||
} | ||
|
||
// kill one query and block until it was completed. | ||
func (info *compileAdditionalInformation) kill(errResult error) { | ||
info.queryCancel() | ||
info.queryDone.checkCompleted() | ||
info.mustReturnError = errResult | ||
} | ||
|
||
type queryDoneWaiter chan bool | ||
|
||
func newQueryDoneWaiter() queryDoneWaiter { | ||
return make(chan bool, 1) | ||
} | ||
|
||
func (waiter queryDoneWaiter) noticeQueryCompleted() { | ||
waiter <- true | ||
} | ||
|
||
func (waiter queryDoneWaiter) checkCompleted() { | ||
<-waiter | ||
waiter <- true | ||
} | ||
|
||
func (waiter queryDoneWaiter) clear() { | ||
for len(waiter) > 0 { | ||
<-waiter | ||
} | ||
} | ||
|
||
func InitCompileService() *ServiceOfCompile { | ||
srv := &ServiceOfCompile{ | ||
aliveCompiles: make(map[*Compile]compileAdditionalInformation, 1024), | ||
} | ||
return srv | ||
} | ||
|
||
func (srv *ServiceOfCompile) getCompile( | ||
proc *process.Process) *Compile { | ||
// make sure the process has a cancel function. | ||
if proc.Cancel == nil { | ||
proc.Ctx, proc.Cancel = context.WithCancel(proc.Ctx) | ||
} | ||
|
||
runningCompile := reuse.Alloc[Compile](nil) | ||
runningCompile.ctx = proc.Ctx | ||
runningCompile.proc = proc | ||
|
||
if runningCompile.queryStatus == nil { | ||
runningCompile.queryStatus = newQueryDoneWaiter() | ||
} else { | ||
runningCompile.queryStatus.clear() | ||
} | ||
|
||
srv.Lock() | ||
srv.aliveCompiles[runningCompile] = compileAdditionalInformation{ | ||
mustReturnError: nil, | ||
queryCancel: proc.Cancel, | ||
queryDone: runningCompile.queryStatus, | ||
} | ||
srv.Unlock() | ||
|
||
return runningCompile | ||
} | ||
|
||
func (srv *ServiceOfCompile) putCompile(c *Compile) (mustReturnError bool, err error) { | ||
c.queryStatus.noticeQueryCompleted() | ||
|
||
srv.Lock() | ||
|
||
if item, ok := srv.aliveCompiles[c]; ok { | ||
err = item.mustReturnError | ||
} | ||
delete(srv.aliveCompiles, c) | ||
c.queryStatus.clear() | ||
srv.Unlock() | ||
|
||
reuse.Free[Compile](c, nil) | ||
|
||
return err != nil, err | ||
} | ||
|
||
func (srv *ServiceOfCompile) aliveCompile() int { | ||
srv.Lock() | ||
defer srv.Unlock() | ||
|
||
return len(srv.aliveCompiles) | ||
} | ||
|
||
func (srv *ServiceOfCompile) PauseService() { | ||
srv.Lock() | ||
} | ||
|
||
func (srv *ServiceOfCompile) ResumeService() { | ||
srv.Unlock() | ||
} | ||
|
||
func (srv *ServiceOfCompile) KillAllQueriesWithError(err error) { | ||
for _, v := range srv.aliveCompiles { | ||
v.kill(err) | ||
} | ||
} |
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,72 @@ | ||
// Copyright 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 compile | ||
|
||
import ( | ||
"context" | ||
"github.com/matrixorigin/matrixone/pkg/vm/process" | ||
"github.com/stretchr/testify/require" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
) | ||
|
||
func generateRunningProc(n int) []*process.Process { | ||
rs := make([]*process.Process, n) | ||
for i := range rs { | ||
ctx, cancel := context.WithCancel(context.TODO()) | ||
|
||
rs[i] = &process.Process{ | ||
Ctx: ctx, | ||
Cancel: cancel, | ||
} | ||
} | ||
return rs | ||
} | ||
|
||
func TestCompileService(t *testing.T) { | ||
service := InitCompileService() | ||
|
||
doneRoutine := atomic.Int32{} | ||
doneRoutine.Store(0) | ||
wg := sync.WaitGroup{} | ||
|
||
// 1. service should count running Compile in correct. | ||
inputs := generateRunningProc(10) | ||
for _, p := range inputs { | ||
wg.Add(1) | ||
|
||
c := service.getCompile(p) | ||
go func(cc *Compile) { | ||
<-cc.ctx.Done() | ||
|
||
doneRoutine.Add(1) | ||
_, _ = service.putCompile(cc) | ||
wg.Done() | ||
}(c) | ||
} | ||
require.Equal(t, 10, service.aliveCompile()) | ||
|
||
// 2. kill all running Compile. | ||
service.PauseService() | ||
service.KillAllQueriesWithError(nil) | ||
service.ResumeService() | ||
|
||
require.Equal(t, int32(10), doneRoutine.Load()) | ||
|
||
// after all, alive compile should be 0. | ||
wg.Wait() | ||
require.Equal(t, 0, service.aliveCompile()) | ||
} |
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
Oops, something went wrong.