forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
105 lines (84 loc) · 2.23 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"database/sql"
"fmt"
"time"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
)
func addJobs(jobCount int, jobChan chan struct{}) {
for i := 0; i < jobCount; i++ {
jobChan <- struct{}{}
}
close(jobChan)
}
func doInsert(table *table, db *sql.DB, count int) {
sqls, err := genRowDatas(table, count)
if err != nil {
log.Fatalf(errors.ErrorStack(err))
}
txn, err := db.Begin()
if err != nil {
log.Fatalf(errors.ErrorStack(err))
}
for _, sql := range sqls {
_, err = txn.Exec(sql)
if err != nil {
log.Fatalf(errors.ErrorStack(err))
}
}
err = txn.Commit()
if err != nil {
log.Fatalf(errors.ErrorStack(err))
}
}
func doJob(table *table, db *sql.DB, batch int, jobChan chan struct{}, doneChan chan struct{}) {
count := 0
for range jobChan {
count++
if count == batch {
doInsert(table, db, count)
count = 0
}
}
if count > 0 {
doInsert(table, db, count)
count = 0
}
doneChan <- struct{}{}
}
func doWait(doneChan chan struct{}, start time.Time, jobCount int, workerCount int) {
for i := 0; i < workerCount; i++ {
<-doneChan
}
close(doneChan)
now := time.Now()
seconds := now.Unix() - start.Unix()
tps := int64(-1)
if seconds > 0 {
tps = int64(jobCount) / seconds
}
fmt.Printf("[importer]total %d cases, cost %d seconds, tps %d, start %s, now %s\n", jobCount, seconds, tps, start, now)
}
func doProcess(table *table, dbs []*sql.DB, jobCount int, workerCount int, batch int) {
jobChan := make(chan struct{}, 16*workerCount)
doneChan := make(chan struct{}, workerCount)
start := time.Now()
go addJobs(jobCount, jobChan)
for i := 0; i < workerCount; i++ {
go doJob(table, dbs[i], batch, jobChan, doneChan)
}
doWait(doneChan, start, jobCount, workerCount)
}